jackyccl commited on
Commit
f80c1b9
·
1 Parent(s): 7b58807
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -137,15 +137,20 @@ def show_mask(mask, ax, random_color=False):
137
  def show_box(box, ax, label):
138
  x0, y0 = box[0], box[1]
139
  w, h = box[2] - box[0], box[3] - box[1]
140
- ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0,0,0,0), lw=2))
141
- ax.text(x0, y0, label, fontdict={'fontsize': 7})
142
 
143
  def get_grounding_box(image_tensor, grounding_caption, box_threshold, text_threshold):
144
  # run grounding
145
  boxes, logits, phrases = predict(groundingDino_model, image_tensor, grounding_caption, box_threshold, text_threshold, device=groundingdino_device)
 
 
 
 
 
146
  # annotated_frame = annotate(image_source=np.asarray(image_pil), boxes=boxes, logits=logits, phrases=phrases)
147
  # image_with_box = Image.fromarray(cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB))
148
- return boxes, phrases
149
 
150
  def grounding_sam(input_image, text_prompt, task_type, box_threshold, text_threshold, iou_threshold):
151
  text_prompt = text_prompt.strip()
@@ -210,7 +215,7 @@ def grounding_sam(input_image, text_prompt, task_type, box_threshold, text_thres
210
  )
211
 
212
  # draw output image
213
- plt.figure(figsize=(10, 10))
214
  plt.imshow(image)
215
  for mask in masks:
216
  show_mask(mask.cpu().numpy(), plt.gca(), random_color=True)
@@ -246,7 +251,7 @@ if __name__ == "__main__":
246
  input_image = gr.Image(source='upload', elem_id="image_upload", tool='sketch', type='pil', label="Upload")
247
  task_type = gr.Radio(["segment"], value="segment",
248
  label='Task type',interactive=True, visible=True)
249
- text_prompt = gr.Textbox(label="Detection Prompt, seperating each name with ',', i.e.: cat,dog,chair ]", \
250
  placeholder="Cannot be empty")
251
 
252
  run_button = gr.Button(label="Run")
 
137
  def show_box(box, ax, label):
138
  x0, y0 = box[0], box[1]
139
  w, h = box[2] - box[0], box[3] - box[1]
140
+ ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='red', facecolor=(0,0,0,0), lw=2))
141
+ ax.text(x0, y0+20, label, fontdict={'fontsize': 10}, color="white")
142
 
143
  def get_grounding_box(image_tensor, grounding_caption, box_threshold, text_threshold):
144
  # run grounding
145
  boxes, logits, phrases = predict(groundingDino_model, image_tensor, grounding_caption, box_threshold, text_threshold, device=groundingdino_device)
146
+ labels = [
147
+ f"{phrase} ({logit:.2f})"
148
+ for phrase, logit
149
+ in zip(phrases, logits)
150
+ ]
151
  # annotated_frame = annotate(image_source=np.asarray(image_pil), boxes=boxes, logits=logits, phrases=phrases)
152
  # image_with_box = Image.fromarray(cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB))
153
+ return boxes, labels
154
 
155
  def grounding_sam(input_image, text_prompt, task_type, box_threshold, text_threshold, iou_threshold):
156
  text_prompt = text_prompt.strip()
 
215
  )
216
 
217
  # draw output image
218
+ plt.figure()
219
  plt.imshow(image)
220
  for mask in masks:
221
  show_mask(mask.cpu().numpy(), plt.gca(), random_color=True)
 
251
  input_image = gr.Image(source='upload', elem_id="image_upload", tool='sketch', type='pil', label="Upload")
252
  task_type = gr.Radio(["segment"], value="segment",
253
  label='Task type',interactive=True, visible=True)
254
+ text_prompt = gr.Textbox(label="Detection Prompt, seperating each name with dot '.', i.e.: cat.dog.chair ]", \
255
  placeholder="Cannot be empty")
256
 
257
  run_button = gr.Button(label="Run")