-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbird_bounding_box_generation.py
More file actions
65 lines (46 loc) · 1.97 KB
/
bird_bounding_box_generation.py
File metadata and controls
65 lines (46 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import argparse
import warnings
warnings.filterwarnings('ignore')
from PIL import Image
from lang_sam import LangSAM
import numpy as np
from tqdm import tqdm
import jsonlines
input_dir = '/raid/maruf/Bird/images/'
output_file = 'output/bounding_box_annotation.jsonl'
img_name_list = os.listdir(input_dir)
img_name_list = [img_name for img_name in img_name_list if img_name.split('.')[-1]=='jpg'][:5000]
TRAIT_LIST=["beak", "eye", "wings", "head", "tail"]
PROMPT_LIST=["beak of bird", "eye of bird", "wings of bird", "head of bird", "tail of bird"]
# LangSAM model : GroundingDINO + SAM
model = LangSAM()
writer = jsonlines.open(output_file, mode='w')
for img_name in tqdm(img_name_list):
img = Image.open(os.path.join(input_dir, f'{img_name}'))
image_pil = img.convert("RGB")
for trait, prompt_trait in zip(TRAIT_LIST, PROMPT_LIST):
text_prompt, BOX_THRESHOLD = prompt_trait, 0.10
masks, boxes, phrases, logits = model.predict(image_pil, text_prompt, box_threshold=BOX_THRESHOLD)
if len(boxes) == 0:
continue
output_dict = dict()
output_dict['image-name'] = img_name
output_dict['trait'] = trait
# Draw bounding boxes on the image
number_of_option = 0
for box, phrase, logit in zip(boxes, phrases, logits):
if trait not in phrase:
continue
else:
output_dict[f'Option-{number_of_option}-box'] = f"{box.detach().cpu().numpy()}"
output_dict[f'Option-{number_of_option}-phrase'] = phrase
output_dict[f'Option-{number_of_option}-logit'] = logit.item()
number_of_option += 1
output_dict['number_of_options'] = number_of_option
if number_of_option > 6:
break
writer.write(output_dict)
writer.close()
writer = jsonlines.open(output_file, mode='a')
writer.close()