Spaces:
Running
on
Zero
Running
on
Zero
Martin Tomov
commited on
annotate_with_transparency
Browse files
app.py
CHANGED
@@ -57,17 +57,17 @@ def annotate_with_transparency(image: Union[Image.Image, np.ndarray], detection_
|
|
57 |
mask = detection.mask
|
58 |
color = np.random.randint(0, 256, size=3).tolist()
|
59 |
|
60 |
-
# Overlay with transparent background
|
61 |
-
overlay = np.zeros_like(image_cv2, dtype=np.uint8)
|
62 |
-
|
63 |
if mask is not None:
|
64 |
mask_uint8 = (mask * 255).astype(np.uint8)
|
65 |
contours, _ = cv2.findContours(mask_uint8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
|
|
|
66 |
cv2.drawContours(overlay, contours, -1, color, -1) # Draw filled contours on the overlay
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
|
72 |
cv2.putText(image_cv2, f'{label}: {score:.2f}', (box.xmin, box.ymin - 10),
|
73 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
|
|
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.5 # 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)
|