Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import os
|
| 2 |
import uuid
|
| 3 |
import threading
|
| 4 |
import time
|
|
@@ -97,8 +97,8 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 97 |
jobs[job_id]['result'] = {"error": "Roboflow model not available."}
|
| 98 |
return
|
| 99 |
|
| 100 |
-
# Upscale the image if it
|
| 101 |
-
scale_factor = 1
|
| 102 |
if img_width < 1000 or img_height < 1000:
|
| 103 |
scale_factor = 2
|
| 104 |
|
|
@@ -141,21 +141,21 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 141 |
except Exception as e:
|
| 142 |
continue
|
| 143 |
|
| 144 |
-
# Use a higher NMS threshold to
|
| 145 |
box_detections = custom_nms(processed_preds, iou_threshold=0.7)
|
| 146 |
jobs[job_id]['progress'] = 60
|
| 147 |
|
| 148 |
# --- ArUco Marker Detection for Measurement ---
|
| 149 |
-
# We assume the marker is
|
| 150 |
marker_real_width_cm = 5.0
|
| 151 |
try:
|
| 152 |
-
#
|
| 153 |
aruco_image = image.copy()
|
| 154 |
gray = cv2.cvtColor(aruco_image, cv2.COLOR_BGR2GRAY)
|
| 155 |
-
# Use DICT_6X6_250
|
| 156 |
aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)
|
| 157 |
-
# Create and adjust detector parameters for small markers
|
| 158 |
aruco_params = cv2.aruco.DetectorParameters_create()
|
|
|
|
| 159 |
aruco_params.adaptiveThreshWinSizeMin = 3
|
| 160 |
aruco_params.adaptiveThreshWinSizeMax = 23
|
| 161 |
aruco_params.adaptiveThreshWinSizeStep = 10
|
|
@@ -163,23 +163,34 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 163 |
aruco_params.cornerRefinementMethod = cv2.aruco.CORNER_REFINE_SUBPIX
|
| 164 |
|
| 165 |
corners, ids, rejected = cv2.aruco.detectMarkers(gray, aruco_dict, parameters=aruco_params)
|
| 166 |
-
print("DEBUG: Detected
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
for i in
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
else:
|
| 179 |
-
print("DEBUG: No
|
| 180 |
-
conversion_factor = None
|
| 181 |
except Exception as e:
|
| 182 |
-
print("DEBUG: Exception
|
| 183 |
conversion_factor = None
|
| 184 |
|
| 185 |
for pred in box_detections:
|
|
|
|
| 1 |
+
import os
|
| 2 |
import uuid
|
| 3 |
import threading
|
| 4 |
import time
|
|
|
|
| 97 |
jobs[job_id]['result'] = {"error": "Roboflow model not available."}
|
| 98 |
return
|
| 99 |
|
| 100 |
+
# Upscale the image if it's small to help detect small boxes.
|
| 101 |
+
scale_factor = 1 # default: no upscaling
|
| 102 |
if img_width < 1000 or img_height < 1000:
|
| 103 |
scale_factor = 2
|
| 104 |
|
|
|
|
| 141 |
except Exception as e:
|
| 142 |
continue
|
| 143 |
|
| 144 |
+
# Use a higher NMS IoU threshold to avoid merging valid nearby detections.
|
| 145 |
box_detections = custom_nms(processed_preds, iou_threshold=0.7)
|
| 146 |
jobs[job_id]['progress'] = 60
|
| 147 |
|
| 148 |
# --- ArUco Marker Detection for Measurement ---
|
| 149 |
+
# We assume the printed marker is 5cm x 5cm.
|
| 150 |
marker_real_width_cm = 5.0
|
| 151 |
try:
|
| 152 |
+
# Use a fresh copy for marker detection.
|
| 153 |
aruco_image = image.copy()
|
| 154 |
gray = cv2.cvtColor(aruco_image, cv2.COLOR_BGR2GRAY)
|
| 155 |
+
# Use the DICT_6X6_250 dictionary.
|
| 156 |
aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)
|
|
|
|
| 157 |
aruco_params = cv2.aruco.DetectorParameters_create()
|
| 158 |
+
# Adjust parameters to help detect small markers.
|
| 159 |
aruco_params.adaptiveThreshWinSizeMin = 3
|
| 160 |
aruco_params.adaptiveThreshWinSizeMax = 23
|
| 161 |
aruco_params.adaptiveThreshWinSizeStep = 10
|
|
|
|
| 163 |
aruco_params.cornerRefinementMethod = cv2.aruco.CORNER_REFINE_SUBPIX
|
| 164 |
|
| 165 |
corners, ids, rejected = cv2.aruco.detectMarkers(gray, aruco_dict, parameters=aruco_params)
|
| 166 |
+
print("DEBUG: Detected candidate IDs:", ids)
|
| 167 |
+
conversion_factor = None
|
| 168 |
+
if ids is not None and len(ids) > 0:
|
| 169 |
+
# Look for marker with id 42.
|
| 170 |
+
selected_index = None
|
| 171 |
+
for i, marker_id in enumerate(ids):
|
| 172 |
+
if marker_id[0] == 42:
|
| 173 |
+
selected_index = i
|
| 174 |
+
break
|
| 175 |
+
if selected_index is None:
|
| 176 |
+
print("DEBUG: Marker with ID 42 not found among detected markers.")
|
| 177 |
+
else:
|
| 178 |
+
marker_corners = corners[selected_index].reshape((4, 2))
|
| 179 |
+
# Draw only the selected marker
|
| 180 |
+
cv2.aruco.drawDetectedMarkers(image, [corners[selected_index]], [ids[selected_index]])
|
| 181 |
+
side_lengths = []
|
| 182 |
+
for i in range(4):
|
| 183 |
+
pt1 = marker_corners[i]
|
| 184 |
+
pt2 = marker_corners[(i + 1) % 4]
|
| 185 |
+
side_lengths.append(np.linalg.norm(pt1 - pt2))
|
| 186 |
+
avg_side_length = np.mean(side_lengths)
|
| 187 |
+
conversion_factor = marker_real_width_cm / avg_side_length
|
| 188 |
+
print("DEBUG: Detected marker ID 42, avg side length (px):", avg_side_length,
|
| 189 |
+
"conversion factor:", conversion_factor)
|
| 190 |
else:
|
| 191 |
+
print("DEBUG: No markers detected in ArUco detection.")
|
|
|
|
| 192 |
except Exception as e:
|
| 193 |
+
print("DEBUG: Exception during ArUco detection:", e)
|
| 194 |
conversion_factor = None
|
| 195 |
|
| 196 |
for pred in box_detections:
|