Update app.py
Browse files
app.py
CHANGED
|
@@ -62,7 +62,7 @@ def compute_iou(boxA, boxB):
|
|
| 62 |
return 0
|
| 63 |
return interArea / float(boxAArea + boxBArea - interArea)
|
| 64 |
|
| 65 |
-
def custom_nms(preds, iou_threshold=0.
|
| 66 |
preds = sorted(preds, key=lambda x: x["confidence"], reverse=True)
|
| 67 |
filtered_preds = []
|
| 68 |
for pred in preds:
|
|
@@ -75,8 +75,11 @@ def custom_nms(preds, iou_threshold=0.3):
|
|
| 75 |
filtered_preds.append(pred)
|
| 76 |
return filtered_preds
|
| 77 |
|
| 78 |
-
# The process_image function
|
| 79 |
-
# with the ArUco marker detection
|
|
|
|
|
|
|
|
|
|
| 80 |
def process_image(job_id, image_path, object_type, multiplier):
|
| 81 |
try:
|
| 82 |
jobs[job_id]['progress'] = 10
|
|
@@ -148,7 +151,7 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 148 |
box_detections = custom_nms(processed_preds, iou_threshold=0.7)
|
| 149 |
jobs[job_id]['progress'] = 60
|
| 150 |
|
| 151 |
-
# --- ARUCO MARKER DETECTION (from
|
| 152 |
marker_real_width_cm = 5.0
|
| 153 |
try:
|
| 154 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
@@ -174,14 +177,25 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 174 |
conversion_factor = None
|
| 175 |
|
| 176 |
# --- Draw boxes and compute measurements ---
|
|
|
|
|
|
|
|
|
|
| 177 |
for pred in box_detections:
|
| 178 |
x1, y1, x2, y2 = pred["box"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
label = pred["class"]
|
| 180 |
confidence = pred["confidence"]
|
| 181 |
-
cv2.rectangle(image, (
|
| 182 |
if conversion_factor is not None:
|
| 183 |
-
box_width_pixels =
|
| 184 |
-
box_height_pixels =
|
| 185 |
box_width_cm = box_width_pixels * conversion_factor
|
| 186 |
box_height_cm = box_height_pixels * conversion_factor
|
| 187 |
detection_info.append({
|
|
@@ -199,8 +213,8 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 199 |
})
|
| 200 |
text = f"{label} ({confidence:.2f})"
|
| 201 |
(text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
|
| 202 |
-
cv2.rectangle(image, (
|
| 203 |
-
cv2.putText(image, text, (
|
| 204 |
|
| 205 |
elif object_type in {"person", "car"}:
|
| 206 |
if yolov5_model is None:
|
|
@@ -235,7 +249,7 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 235 |
jobs[job_id]['result'] = {"error": "Error during YOLOv5 inference."}
|
| 236 |
return
|
| 237 |
|
| 238 |
-
# Build summary text drawn on
|
| 239 |
detection_counts = Counter(det["class"] for det in detection_info)
|
| 240 |
if detection_counts:
|
| 241 |
top_text = ", ".join(f"{cls}: {count}" for cls, count in detection_counts.items())
|
|
|
|
| 62 |
return 0
|
| 63 |
return interArea / float(boxAArea + boxBArea - interArea)
|
| 64 |
|
| 65 |
+
def custom_nms(preds, iou_threshold=0.7):
|
| 66 |
preds = sorted(preds, key=lambda x: x["confidence"], reverse=True)
|
| 67 |
filtered_preds = []
|
| 68 |
for pred in preds:
|
|
|
|
| 75 |
filtered_preds.append(pred)
|
| 76 |
return filtered_preds
|
| 77 |
|
| 78 |
+
# The process_image function merges the box detection code (from your 1st prompt)
|
| 79 |
+
# with the ArUco marker detection and measurement (from the code you provided now).
|
| 80 |
+
# The only change is an added step to slightly expand each detected box (by 5%)
|
| 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
|
|
|
|
| 151 |
box_detections = custom_nms(processed_preds, iou_threshold=0.7)
|
| 152 |
jobs[job_id]['progress'] = 60
|
| 153 |
|
| 154 |
+
# --- ARUCO MARKER DETECTION (unchanged from your current prompt) ---
|
| 155 |
marker_real_width_cm = 5.0
|
| 156 |
try:
|
| 157 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 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, (x1_exp, y1_exp), (x2_exp, y2_exp), (0, 255, 0), thickness)
|
| 196 |
if conversion_factor is not None:
|
| 197 |
+
box_width_pixels = x2_exp - x1_exp
|
| 198 |
+
box_height_pixels = y2_exp - y1_exp
|
| 199 |
box_width_cm = box_width_pixels * conversion_factor
|
| 200 |
box_height_cm = box_height_pixels * conversion_factor
|
| 201 |
detection_info.append({
|
|
|
|
| 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, (x1_exp, y1_exp - text_height - baseline - 5), (x1_exp + text_width, y1_exp - 5), (0, 255, 0), -1)
|
| 217 |
+
cv2.putText(image, text, (x1_exp, y1_exp - 5 - baseline), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
|
| 218 |
|
| 219 |
elif object_type in {"person", "car"}:
|
| 220 |
if yolov5_model is None:
|
|
|
|
| 249 |
jobs[job_id]['result'] = {"error": "Error during YOLOv5 inference."}
|
| 250 |
return
|
| 251 |
|
| 252 |
+
# Build summary text (drawn on image)
|
| 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())
|