Martin Tomov commited on
Commit
ea51d02
β€’
1 Parent(s): 9bdce8e

revert to cv2.cvtColor fix

Browse files
Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -46,7 +46,7 @@ class DetectionResult:
46
  )
47
  )
48
 
49
- def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[DetectionResult], color=(0, 255, 255)) -> np.ndarray:
50
  image_cv2 = np.array(image) if isinstance(image, Image.Image) else image
51
  image_cv2 = cv2.cvtColor(image_cv2, cv2.COLOR_RGB2BGR)
52
 
@@ -55,6 +55,7 @@ def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[Dete
55
  score = detection.score
56
  box = detection.box
57
  mask = detection.mask
 
58
 
59
  cv2.rectangle(image_cv2, (box.xmin, box.ymin), (box.xmax, box.ymax), color, 2)
60
  cv2.putText(image_cv2, f'{label}: {score:.2f}', (box.xmin, box.ymin - 10),
@@ -154,9 +155,9 @@ def create_yellow_background_with_insects(image: np.ndarray, detections: List[De
154
  for detection in detections:
155
  if detection.mask is not None:
156
  extract_and_paste_insect(image, detection, yellow_background)
157
- # Draw bounding boxes and labels on yellow background
158
- annotated_background = annotate(yellow_background, detections, color=(0, 255, 255))
159
- return annotated_background
160
 
161
  def run_length_encoding(mask):
162
  pixels = mask.flatten()
@@ -215,4 +216,4 @@ gr.Interface(
215
  outputs=[gr.Image(type="numpy"), gr.Textbox()],
216
  title="InsectSAM 🐞",
217
  examples=examples
218
- ).launch()
 
46
  )
47
  )
48
 
49
+ def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[DetectionResult]) -> np.ndarray:
50
  image_cv2 = np.array(image) if isinstance(image, Image.Image) else image
51
  image_cv2 = cv2.cvtColor(image_cv2, cv2.COLOR_RGB2BGR)
52
 
 
55
  score = detection.score
56
  box = detection.box
57
  mask = detection.mask
58
+ color = np.random.randint(0, 256, size=3).tolist()
59
 
60
  cv2.rectangle(image_cv2, (box.xmin, box.ymin), (box.xmax, box.ymax), color, 2)
61
  cv2.putText(image_cv2, f'{label}: {score:.2f}', (box.xmin, box.ymin - 10),
 
155
  for detection in detections:
156
  if detection.mask is not None:
157
  extract_and_paste_insect(image, detection, yellow_background)
158
+ # Convert back to RGB to match Gradio's expected input format
159
+ yellow_background = cv2.cvtColor(yellow_background, cv2.COLOR_BGR2RGB)
160
+ return yellow_background
161
 
162
  def run_length_encoding(mask):
163
  pixels = mask.flatten()
 
216
  outputs=[gr.Image(type="numpy"), gr.Textbox()],
217
  title="InsectSAM 🐞",
218
  examples=examples
219
+ ).launch()