Update app.py
Browse files
app.py
CHANGED
|
@@ -75,11 +75,9 @@ def custom_nms(preds, iou_threshold=0.7):
|
|
| 75 |
filtered_preds.append(pred)
|
| 76 |
return filtered_preds
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
#
|
| 80 |
-
#
|
| 81 |
-
# before drawing and computing its size. This extra "padding" has been found to
|
| 82 |
-
# improve the accuracy of the measured dimensions.
|
| 83 |
def process_image(job_id, image_path, object_type, multiplier):
|
| 84 |
try:
|
| 85 |
jobs[job_id]['progress'] = 10
|
|
@@ -102,8 +100,7 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 102 |
jobs[job_id]['result'] = {"error": "Roboflow model not available."}
|
| 103 |
return
|
| 104 |
|
| 105 |
-
# --- BOX DETECTION (
|
| 106 |
-
# Upscale the image if it is small to help detect small boxes.
|
| 107 |
scale_factor = 1
|
| 108 |
if img_width < 1000 or img_height < 1000:
|
| 109 |
scale_factor = 2
|
|
@@ -147,7 +144,6 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 147 |
except Exception as e:
|
| 148 |
continue
|
| 149 |
|
| 150 |
-
# Use a higher NMS IoU threshold to avoid merging valid nearby detections.
|
| 151 |
box_detections = custom_nms(processed_preds, iou_threshold=0.7)
|
| 152 |
jobs[job_id]['progress'] = 60
|
| 153 |
|
|
@@ -176,26 +172,15 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 176 |
except Exception as e:
|
| 177 |
conversion_factor = None
|
| 178 |
|
| 179 |
-
# --- Draw boxes and compute measurements ---
|
| 180 |
-
# Here we add a slight expansion (5%) to each detected bounding box
|
| 181 |
-
# before drawing it and computing its dimensions.
|
| 182 |
-
expansion_factor = 0.05
|
| 183 |
for pred in box_detections:
|
| 184 |
x1, y1, x2, y2 = pred["box"]
|
| 185 |
-
# Expand the box by 5% on each side
|
| 186 |
-
box_w = x2 - x1
|
| 187 |
-
box_h = y2 - y1
|
| 188 |
-
x1_exp = int(max(0, x1 - box_w * expansion_factor))
|
| 189 |
-
y1_exp = int(max(0, y1 - box_h * expansion_factor))
|
| 190 |
-
x2_exp = int(min(img_width - 1, x2 + box_w * expansion_factor))
|
| 191 |
-
y2_exp = int(min(img_height - 1, y2 + box_h * expansion_factor))
|
| 192 |
-
|
| 193 |
label = pred["class"]
|
| 194 |
confidence = pred["confidence"]
|
| 195 |
-
cv2.rectangle(image, (
|
| 196 |
if conversion_factor is not None:
|
| 197 |
-
box_width_pixels =
|
| 198 |
-
box_height_pixels =
|
| 199 |
box_width_cm = box_width_pixels * conversion_factor
|
| 200 |
box_height_cm = box_height_pixels * conversion_factor
|
| 201 |
detection_info.append({
|
|
@@ -213,8 +198,8 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 213 |
})
|
| 214 |
text = f"{label} ({confidence:.2f})"
|
| 215 |
(text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
|
| 216 |
-
cv2.rectangle(image, (
|
| 217 |
-
cv2.putText(image, text, (
|
| 218 |
|
| 219 |
elif object_type in {"person", "car"}:
|
| 220 |
if yolov5_model is None:
|
|
@@ -249,7 +234,7 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 249 |
jobs[job_id]['result'] = {"error": "Error during YOLOv5 inference."}
|
| 250 |
return
|
| 251 |
|
| 252 |
-
# Build summary text
|
| 253 |
detection_counts = Counter(det["class"] for det in detection_info)
|
| 254 |
if detection_counts:
|
| 255 |
top_text = ", ".join(f"{cls}: {count}" for cls, count in detection_counts.items())
|
|
|
|
| 75 |
filtered_preds.append(pred)
|
| 76 |
return filtered_preds
|
| 77 |
|
| 78 |
+
# This function merges the robust box-detection from your first prompt
|
| 79 |
+
# (including upscaling if needed, prediction processing, and custom NMS)
|
| 80 |
+
# with the ArUco marker detection & measurement exactly as in your current code.
|
|
|
|
|
|
|
| 81 |
def process_image(job_id, image_path, object_type, multiplier):
|
| 82 |
try:
|
| 83 |
jobs[job_id]['progress'] = 10
|
|
|
|
| 100 |
jobs[job_id]['result'] = {"error": "Roboflow model not available."}
|
| 101 |
return
|
| 102 |
|
| 103 |
+
# --- BOX DETECTION (exactly as in your first prompt) ---
|
|
|
|
| 104 |
scale_factor = 1
|
| 105 |
if img_width < 1000 or img_height < 1000:
|
| 106 |
scale_factor = 2
|
|
|
|
| 144 |
except Exception as e:
|
| 145 |
continue
|
| 146 |
|
|
|
|
| 147 |
box_detections = custom_nms(processed_preds, iou_threshold=0.7)
|
| 148 |
jobs[job_id]['progress'] = 60
|
| 149 |
|
|
|
|
| 172 |
except Exception as e:
|
| 173 |
conversion_factor = None
|
| 174 |
|
| 175 |
+
# --- Draw boxes and compute measurements exactly as in your first prompt ---
|
|
|
|
|
|
|
|
|
|
| 176 |
for pred in box_detections:
|
| 177 |
x1, y1, x2, y2 = pred["box"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
label = pred["class"]
|
| 179 |
confidence = pred["confidence"]
|
| 180 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), thickness)
|
| 181 |
if conversion_factor is not None:
|
| 182 |
+
box_width_pixels = x2 - x1
|
| 183 |
+
box_height_pixels = y2 - y1
|
| 184 |
box_width_cm = box_width_pixels * conversion_factor
|
| 185 |
box_height_cm = box_height_pixels * conversion_factor
|
| 186 |
detection_info.append({
|
|
|
|
| 198 |
})
|
| 199 |
text = f"{label} ({confidence:.2f})"
|
| 200 |
(text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
|
| 201 |
+
cv2.rectangle(image, (x1, y1 - text_height - baseline - 5), (x1 + text_width, y1 - 5), (0, 255, 0), -1)
|
| 202 |
+
cv2.putText(image, text, (x1, y1 - 5 - baseline), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
|
| 203 |
|
| 204 |
elif object_type in {"person", "car"}:
|
| 205 |
if yolov5_model is None:
|
|
|
|
| 234 |
jobs[job_id]['result'] = {"error": "Error during YOLOv5 inference."}
|
| 235 |
return
|
| 236 |
|
| 237 |
+
# Build summary text drawn on the image
|
| 238 |
detection_counts = Counter(det["class"] for det in detection_info)
|
| 239 |
if detection_counts:
|
| 240 |
top_text = ", ".join(f"{cls}: {count}" for cls, count in detection_counts.items())
|