noni27 commited on
Commit
c08c82f
·
verified ·
1 Parent(s): 4fcb5c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -114,10 +114,11 @@ def build_groundingdino():
114
  return groundingdino
115
 
116
 
117
-
 
118
 
119
  '''Predictions'''
120
- def predict_dino(image_pil, text_prompt, box_threshold, text_threshold, model_groundingdino):
121
  image_trans = transform_image(image_pil)
122
  boxes, logits, phrases = predict(model = model_groundingdino,
123
  image = image_trans,
@@ -130,7 +131,7 @@ def predict_dino(image_pil, text_prompt, box_threshold, text_threshold, model_gr
130
  return boxes, logits, phrases
131
 
132
 
133
- def predict_sam(image_pil, boxes, model_sam):
134
  image_array = np.asarray(image_pil)
135
  model_sam.set_image(image_array)
136
  transformed_boxes = model_sam.transform.apply_boxes_torch(boxes, image_array.shape[:2])
@@ -143,11 +144,11 @@ def predict_sam(image_pil, boxes, model_sam):
143
  return masks.cpu()
144
 
145
 
146
- def mask_predict(image_pil, text_prompt, box_threshold=0.3, text_threshold=0.25, models = [model_groundingdino , model_sam]):
147
- boxes, logits, phrases = predict_dino(image_pil, text_prompt, box_threshold, text_threshold, models[0])
148
  masks = torch.tensor([])
149
  if len(boxes) > 0:
150
- masks = predict_sam(image_pil, boxes, models[1])
151
  masks = masks.squeeze(1)
152
  return masks, boxes, phrases, logits
153
 
@@ -189,9 +190,7 @@ def visualize_results(img1, img2, task):
189
  # text_prompt = 'wooden stool'
190
  # image_path = '/kaggle/input/avataar/stool.jpeg'
191
  # output_image_path = '/kaggle/working'
192
-
193
- model_sam = build_sam()
194
- model_groundingdino = build_groundingdino()
195
 
196
  def main_fun(image_pil, x_units, y_units, text_prompt):
197
  # x_units = 200
@@ -199,7 +198,7 @@ def main_fun(image_pil, x_units, y_units, text_prompt):
199
  # text_prompt = 'wooden stool'
200
 
201
  # image_pil = load_image(image_path)
202
- masks, boxes, phrases, logits = mask_predict(image_pil, text_prompt=text_prompt, box_threshold=0.23, text_threshold=0.25,models = [model_groundingdino , model_sam])
203
  output = draw_image(image_pil, masks, boxes, alpha=0.4)
204
 
205
 
 
114
  return groundingdino
115
 
116
 
117
+ model_sam = build_sam()
118
+ model_groundingdino = build_groundingdino()
119
 
120
  '''Predictions'''
121
+ def predict_dino(image_pil, text_prompt, box_threshold, text_threshold):
122
  image_trans = transform_image(image_pil)
123
  boxes, logits, phrases = predict(model = model_groundingdino,
124
  image = image_trans,
 
131
  return boxes, logits, phrases
132
 
133
 
134
+ def predict_sam(image_pil, boxes):
135
  image_array = np.asarray(image_pil)
136
  model_sam.set_image(image_array)
137
  transformed_boxes = model_sam.transform.apply_boxes_torch(boxes, image_array.shape[:2])
 
144
  return masks.cpu()
145
 
146
 
147
+ def mask_predict(image_pil, text_prompt, box_threshold=0.3, text_threshold=0.25):
148
+ boxes, logits, phrases = predict_dino(image_pil, text_prompt, box_threshold, text_threshold)
149
  masks = torch.tensor([])
150
  if len(boxes) > 0:
151
+ masks = predict_sam(image_pil, boxes)
152
  masks = masks.squeeze(1)
153
  return masks, boxes, phrases, logits
154
 
 
190
  # text_prompt = 'wooden stool'
191
  # image_path = '/kaggle/input/avataar/stool.jpeg'
192
  # output_image_path = '/kaggle/working'
193
+
 
 
194
 
195
  def main_fun(image_pil, x_units, y_units, text_prompt):
196
  # x_units = 200
 
198
  # text_prompt = 'wooden stool'
199
 
200
  # image_pil = load_image(image_path)
201
+ masks, boxes, phrases, logits = mask_predict(image_pil, text_prompt=text_prompt, box_threshold=0.23, text_threshold=0.25)
202
  output = draw_image(image_pil, masks, boxes, alpha=0.4)
203
 
204