Spaces:
Sleeping
Sleeping
correct segm
Browse files
app.py
CHANGED
|
@@ -149,6 +149,8 @@ def run_detection(model, image_input, original_image, confidence_threshold):
|
|
| 149 |
|
| 150 |
return annotated, analytics_text, {"count": len(boxes), "objects": class_counts}
|
| 151 |
|
|
|
|
|
|
|
| 152 |
def run_segmentation(model, image_input, original_image):
|
| 153 |
mask_tensor = model.predict(image_input)
|
| 154 |
mask_np = mask_tensor.cpu().numpy().astype(np.uint8)
|
|
@@ -195,6 +197,38 @@ def run_segmentation(model, image_input, original_image):
|
|
| 195 |
|
| 196 |
|
| 197 |
return Image.fromarray(blended), analytics_text, {"classes_found": list(found_classes)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
# --- GRADIO UI ---
|
| 200 |
|
|
|
|
| 149 |
|
| 150 |
return annotated, analytics_text, {"count": len(boxes), "objects": class_counts}
|
| 151 |
|
| 152 |
+
|
| 153 |
+
'''
|
| 154 |
def run_segmentation(model, image_input, original_image):
|
| 155 |
mask_tensor = model.predict(image_input)
|
| 156 |
mask_np = mask_tensor.cpu().numpy().astype(np.uint8)
|
|
|
|
| 197 |
|
| 198 |
|
| 199 |
return Image.fromarray(blended), analytics_text, {"classes_found": list(found_classes)}
|
| 200 |
+
'''
|
| 201 |
+
def run_segmentation(model, image):
|
| 202 |
+
"""
|
| 203 |
+
Handles Segmentation: Returns Tensor of shape (H, W) with class IDs.
|
| 204 |
+
"""
|
| 205 |
+
# 1. Run Prediction
|
| 206 |
+
mask_tensor = model.predict(image)
|
| 207 |
+
|
| 208 |
+
# 2. Convert to Numpy
|
| 209 |
+
mask_np = mask_tensor.cpu().numpy().astype(np.uint8)
|
| 210 |
+
|
| 211 |
+
# 3. Create a Colored Mask
|
| 212 |
+
h, w = mask_np.shape
|
| 213 |
+
colored_mask = np.zeros((h, w, 3), dtype=np.uint8)
|
| 214 |
+
|
| 215 |
+
unique_classes = np.unique(mask_np)
|
| 216 |
+
|
| 217 |
+
for cls_id in unique_classes:
|
| 218 |
+
if cls_id == -1: continue
|
| 219 |
+
|
| 220 |
+
np.random.seed(int(cls_id))
|
| 221 |
+
color = np.random.randint(50, 255, size=3)
|
| 222 |
+
colored_mask[mask_np == cls_id] = color
|
| 223 |
+
|
| 224 |
+
# 4. Blend with Original Image
|
| 225 |
+
image_np = np.array(image)
|
| 226 |
+
if image_np.shape[:2] != colored_mask.shape[:2]:
|
| 227 |
+
colored_mask = cv2.resize(colored_mask, (image_np.shape[1], image_np.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 228 |
+
|
| 229 |
+
blended = cv2.addWeighted(image_np, 0.6, colored_mask, 0.4, 0)
|
| 230 |
+
return Image.fromarray(blended)
|
| 231 |
+
|
| 232 |
|
| 233 |
# --- GRADIO UI ---
|
| 234 |
|