dragonSwing commited on
Commit
cfa05ff
·
1 Parent(s): f1213ed

Using phrases for plotting

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -15,9 +15,9 @@ from segment_anything import SamPredictor
15
  from supervision.detection.utils import mask_to_polygons
16
  from supervision.detection.utils import xywh_to_xyxy
17
 
18
- if os.environ.get('IS_MY_DEBUG') is None:
19
- result = subprocess.run(['pip', 'install', '-e', 'GroundingDINO'], check=True)
20
- print(f'pip install GroundingDINO = {result}')
21
 
22
  sys.path.append("tag2text")
23
  sys.path.append("GroundingDINO")
@@ -80,7 +80,9 @@ sam_predictor = SamPredictor(sam)
80
  sam_automask_generator = SamAutomaticMaskGenerator(sam)
81
 
82
  grounding_dino_model = DinoModel(
83
- model_config_path=dino_config_file, model_checkpoint_path=dino_checkpoint, device=device
 
 
84
  )
85
 
86
 
@@ -104,7 +106,7 @@ def process(image_path, task, prompt, box_threshold, text_threshold, iou_thresho
104
  metadata["image"]["height"] = h
105
 
106
  # Generate tags
107
- if task in ["auto", "detection"] and prompt == "":
108
  tags, caption = generate_tags(tag2text_model, image_pil, "None", device)
109
  prompt = " . ".join(tags)
110
  print(f"Caption: {caption}")
@@ -129,12 +131,13 @@ def process(image_path, task, prompt, box_threshold, text_threshold, iou_thresho
129
  iou_threshold=iou_threshold,
130
  post_process=True,
131
  )
 
132
 
133
  # Draw boxes
134
  box_annotator = sv.BoxAnnotator()
135
  labels = [
136
- f"{classes[class_id] if class_id else 'Unkown'} {confidence:0.2f}"
137
- for _, _, confidence, class_id, _ in detections
138
  ]
139
  image = box_annotator.annotate(
140
  scene=image, detections=detections, labels=labels
@@ -179,24 +182,24 @@ def process(image_path, task, prompt, box_threshold, text_threshold, iou_thresho
179
 
180
  # ToDo: Extract metadata
181
  if detections:
182
- id = 1
183
- for (xyxy, mask, confidence, class_id, _), area, box_area in zip(
184
  detections, detections.area, detections.box_area
185
  ):
186
  annotation = {
187
- "id": id,
188
  "bbox": [int(x) for x in xyxy],
189
  "box_area": float(box_area),
190
  }
191
- if class_id:
192
- annotation["box_confidence"] = float(confidence)
193
- annotation["label"] = classes[class_id] if class_id else "Unkown"
194
  if mask is not None:
195
  # annotation["segmentation"] = mask_to_polygons(mask)
196
  annotation["area"] = int(area)
197
- annotation["predicted_iou"] = float(scores[id - 1])
198
  metadata["annotations"].append(annotation)
199
- id += 1
200
 
201
  meta_file = tempfile.NamedTemporaryFile(delete=False, suffix=".json")
202
  meta_file_path = meta_file.name
 
15
  from supervision.detection.utils import mask_to_polygons
16
  from supervision.detection.utils import xywh_to_xyxy
17
 
18
+ if os.environ.get("IS_MY_DEBUG") is None:
19
+ result = subprocess.run(["pip", "install", "-e", "GroundingDINO"], check=True)
20
+ print(f"pip install GroundingDINO = {result}")
21
 
22
  sys.path.append("tag2text")
23
  sys.path.append("GroundingDINO")
 
80
  sam_automask_generator = SamAutomaticMaskGenerator(sam)
81
 
82
  grounding_dino_model = DinoModel(
83
+ model_config_path=dino_config_file,
84
+ model_checkpoint_path=dino_checkpoint,
85
+ device=device,
86
  )
87
 
88
 
 
106
  metadata["image"]["height"] = h
107
 
108
  # Generate tags
109
+ if task in ["auto", "detect"] and prompt == "":
110
  tags, caption = generate_tags(tag2text_model, image_pil, "None", device)
111
  prompt = " . ".join(tags)
112
  print(f"Caption: {caption}")
 
131
  iou_threshold=iou_threshold,
132
  post_process=True,
133
  )
134
+ print(phrases)
135
 
136
  # Draw boxes
137
  box_annotator = sv.BoxAnnotator()
138
  labels = [
139
+ f"{phrases[i]} {detections.confidence[i]:0.2f}"
140
+ for i in range(len(phrases))
141
  ]
142
  image = box_annotator.annotate(
143
  scene=image, detections=detections, labels=labels
 
182
 
183
  # ToDo: Extract metadata
184
  if detections:
185
+ i = 0
186
+ for (xyxy, mask, confidence, _, _), area, box_area in zip(
187
  detections, detections.area, detections.box_area
188
  ):
189
  annotation = {
190
+ "id": i + 1,
191
  "bbox": [int(x) for x in xyxy],
192
  "box_area": float(box_area),
193
  }
194
+ if confidence:
195
+ annotation["confidence"] = float(confidence)
196
+ annotation["label"] = phrases[i]
197
  if mask is not None:
198
  # annotation["segmentation"] = mask_to_polygons(mask)
199
  annotation["area"] = int(area)
200
+ annotation["predicted_iou"] = float(scores[i])
201
  metadata["annotations"].append(annotation)
202
+ i += 1
203
 
204
  meta_file = tempfile.NamedTemporaryFile(delete=False, suffix=".json")
205
  meta_file_path = meta_file.name