Spaces:
Running
on
Zero
Running
on
Zero
Martin Tomov
commited on
bbox experiment
Browse files
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]) -> 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 |
|
@@ -57,9 +57,10 @@ def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[Dete
|
|
57 |
mask = detection.mask
|
58 |
color = np.random.randint(0, 256, size=3).tolist()
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
63 |
|
64 |
if mask is not None:
|
65 |
mask_uint8 = (mask * 255).astype(np.uint8)
|
@@ -68,8 +69,8 @@ def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[Dete
|
|
68 |
|
69 |
return cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB)
|
70 |
|
71 |
-
def plot_detections(image: Union[Image.Image, np.ndarray], detections: List[DetectionResult]) -> np.ndarray:
|
72 |
-
annotated_image = annotate(image, detections)
|
73 |
return annotated_image
|
74 |
|
75 |
def load_image(image: Union[str, Image.Image]) -> Image.Image:
|
@@ -196,18 +197,16 @@ def detections_to_json(detections):
|
|
196 |
def process_image(image, include_json, include_bboxes):
|
197 |
labels = ["insect"]
|
198 |
original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
|
199 |
-
if not include_bboxes:
|
200 |
-
for detection in detections:
|
201 |
-
detection.box = None
|
202 |
yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
|
|
|
203 |
if include_json:
|
204 |
detections_json = detections_to_json(detections)
|
205 |
json_output_path = "insect_detections.json"
|
206 |
with open(json_output_path, 'w') as json_file:
|
207 |
json.dump(detections_json, json_file, indent=4)
|
208 |
-
return
|
209 |
else:
|
210 |
-
return
|
211 |
|
212 |
examples = [
|
213 |
["flower-night.jpg"]
|
|
|
46 |
)
|
47 |
)
|
48 |
|
49 |
+
def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[DetectionResult], include_bboxes: bool = True) -> 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 |
|
|
|
57 |
mask = detection.mask
|
58 |
color = np.random.randint(0, 256, size=3).tolist()
|
59 |
|
60 |
+
if include_bboxes:
|
61 |
+
cv2.rectangle(image_cv2, (box.xmin, box.ymin), (box.xmax, box.ymax), color, 2)
|
62 |
+
cv2.putText(image_cv2, f'{label}: {score:.2f}', (box.xmin, box.ymin - 10),
|
63 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
64 |
|
65 |
if mask is not None:
|
66 |
mask_uint8 = (mask * 255).astype(np.uint8)
|
|
|
69 |
|
70 |
return cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB)
|
71 |
|
72 |
+
def plot_detections(image: Union[Image.Image, np.ndarray], detections: List[DetectionResult], include_bboxes: bool = True) -> np.ndarray:
|
73 |
+
annotated_image = annotate(image, detections, include_bboxes)
|
74 |
return annotated_image
|
75 |
|
76 |
def load_image(image: Union[str, Image.Image]) -> Image.Image:
|
|
|
197 |
def process_image(image, include_json, include_bboxes):
|
198 |
labels = ["insect"]
|
199 |
original_image, detections = grounded_segmentation(image, labels, threshold=0.3, polygon_refinement=True)
|
|
|
|
|
|
|
200 |
yellow_background_with_insects = create_yellow_background_with_insects(np.array(original_image), detections)
|
201 |
+
annotated_image = plot_detections(yellow_background_with_insects, detections, include_bboxes)
|
202 |
if include_json:
|
203 |
detections_json = detections_to_json(detections)
|
204 |
json_output_path = "insect_detections.json"
|
205 |
with open(json_output_path, 'w') as json_file:
|
206 |
json.dump(detections_json, json_file, indent=4)
|
207 |
+
return annotated_image, json.dumps(detections_json, separators=(',', ':'))
|
208 |
else:
|
209 |
+
return annotated_image, None
|
210 |
|
211 |
examples = [
|
212 |
["flower-night.jpg"]
|