Martin Tomov commited on
Commit
1f08afb
·
verified ·
1 Parent(s): 065911d

bbox dino test

Browse files
Files changed (1) hide show
  1. app.py +11 -14
app.py CHANGED
@@ -46,7 +46,7 @@ class DetectionResult:
46
  )
47
  )
48
 
49
- def annotate_with_transparency(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,27 +55,24 @@ def annotate_with_transparency(image: Union[Image.Image, np.ndarray], detection_
55
  score = detection.score
56
  box = detection.box
57
  mask = detection.mask
58
- color = np.random.randint(0, 256, size=3).tolist()
59
 
60
- if mask is not None:
61
- mask_uint8 = (mask * 255).astype(np.uint8)
62
- contours, _ = cv2.findContours(mask_uint8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
63
-
64
- # Create an overlay with transparent background
65
- overlay = image_cv2.copy()
66
- cv2.drawContours(overlay, contours, -1, color, -1) # Draw filled contours on the overlay
67
-
68
- # Blend the overlay with the original image
69
- alpha = 0.1 # Adjust the alpha value to control transparency
70
- image_cv2 = cv2.addWeighted(overlay, alpha, image_cv2, 1 - alpha, 0)
71
 
72
  cv2.putText(image_cv2, f'{label}: {score:.2f}', (box.xmin, box.ymin - 10),
73
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
74
 
 
 
 
 
 
75
  return cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB)
76
 
77
  def plot_detections(image: Union[Image.Image, np.ndarray], detections: List[DetectionResult]) -> np.ndarray:
78
- annotated_image = annotate_with_transparency(image, detections)
79
  return annotated_image
80
 
81
  def load_image(image: Union[str, Image.Image]) -> Image.Image:
 
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 = (255, 255, 255) # White color for bounding boxes and text
59
 
60
+ # Draw the bounding box with a white border and filling
61
+ cv2.rectangle(image_cv2, (box.xmin, box.ymin), (box.xmax, box.ymax), color, -1) # Filled white rectangle
62
+ cv2.rectangle(image_cv2, (box.xmin, box.ymin), (box.xmax, box.ymax), color, 2) # Border
 
 
 
 
 
 
 
 
63
 
64
  cv2.putText(image_cv2, f'{label}: {score:.2f}', (box.xmin, box.ymin - 10),
65
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
66
 
67
+ if mask is not None:
68
+ mask_uint8 = (mask * 255).astype(np.uint8)
69
+ contours, _ = cv2.findContours(mask_uint8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
70
+ cv2.drawContours(image_cv2, contours, -1, color, 2)
71
+
72
  return cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB)
73
 
74
  def plot_detections(image: Union[Image.Image, np.ndarray], detections: List[DetectionResult]) -> np.ndarray:
75
+ annotated_image = annotate(image, detections)
76
  return annotated_image
77
 
78
  def load_image(image: Union[str, Image.Image]) -> Image.Image: