Update app.py
Browse files
app.py
CHANGED
|
@@ -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 |
|
|
@@ -149,27 +149,25 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 149 |
# We assume the printed marker is 5 cm x 5 cm.
|
| 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 |
-
#
|
| 156 |
gray = cv2.equalizeHist(gray)
|
| 157 |
-
# Use DICT_6X6_250
|
| 158 |
aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)
|
| 159 |
aruco_params = cv2.aruco.DetectorParameters_create()
|
| 160 |
-
#
|
| 161 |
-
aruco_params.
|
| 162 |
-
aruco_params.
|
| 163 |
-
aruco_params.
|
| 164 |
-
# Lower the minimum marker perimeter rate to help detect small markers.
|
| 165 |
-
aruco_params.minMarkerPerimeterRate = 0.02
|
| 166 |
aruco_params.cornerRefinementMethod = cv2.aruco.CORNER_REFINE_SUBPIX
|
| 167 |
|
| 168 |
corners, ids, rejected = cv2.aruco.detectMarkers(gray, aruco_dict, parameters=aruco_params)
|
| 169 |
print("DEBUG: Detected candidate IDs:", ids)
|
| 170 |
conversion_factor = None
|
| 171 |
if ids is not None and len(ids) > 0:
|
| 172 |
-
# Look for marker with ID 42.
|
| 173 |
selected_index = None
|
| 174 |
for i, marker_id in enumerate(ids):
|
| 175 |
if marker_id[0] == 42:
|
|
@@ -179,6 +177,7 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 179 |
print("DEBUG: Marker with ID 42 not found among detected markers.")
|
| 180 |
else:
|
| 181 |
marker_corners = corners[selected_index].reshape((4, 2))
|
|
|
|
| 182 |
cv2.aruco.drawDetectedMarkers(image, [corners[selected_index]], [ids[selected_index]])
|
| 183 |
side_lengths = []
|
| 184 |
for i in range(4):
|
|
|
|
| 97 |
jobs[job_id]['result'] = {"error": "Roboflow model not available."}
|
| 98 |
return
|
| 99 |
|
| 100 |
+
# Upscale the image if it is small to help detect small boxes.
|
| 101 |
+
scale_factor = 1
|
| 102 |
if img_width < 1000 or img_height < 1000:
|
| 103 |
scale_factor = 2
|
| 104 |
|
|
|
|
| 149 |
# We assume the printed marker is 5 cm x 5 cm.
|
| 150 |
marker_real_width_cm = 5.0
|
| 151 |
try:
|
| 152 |
+
# Use a fresh copy for marker detection to avoid any drawn overlays.
|
| 153 |
aruco_image = image.copy()
|
| 154 |
gray = cv2.cvtColor(aruco_image, cv2.COLOR_BGR2GRAY)
|
| 155 |
+
# Apply histogram equalization to improve contrast.
|
| 156 |
gray = cv2.equalizeHist(gray)
|
| 157 |
+
# Use the DICT_6X6_250 dictionary. (Make sure your printed marker was generated accordingly.)
|
| 158 |
aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)
|
| 159 |
aruco_params = cv2.aruco.DetectorParameters_create()
|
| 160 |
+
# Adjust parameters for better detection of small markers:
|
| 161 |
+
aruco_params.adaptiveThreshWinSizeMax = 400
|
| 162 |
+
aruco_params.minDistanceToBorder = 0
|
| 163 |
+
aruco_params.minMarkerPerimeterRate = 0.02 # Lowered from 0.03
|
|
|
|
|
|
|
| 164 |
aruco_params.cornerRefinementMethod = cv2.aruco.CORNER_REFINE_SUBPIX
|
| 165 |
|
| 166 |
corners, ids, rejected = cv2.aruco.detectMarkers(gray, aruco_dict, parameters=aruco_params)
|
| 167 |
print("DEBUG: Detected candidate IDs:", ids)
|
| 168 |
conversion_factor = None
|
| 169 |
if ids is not None and len(ids) > 0:
|
| 170 |
+
# Look for marker with ID 42 explicitly.
|
| 171 |
selected_index = None
|
| 172 |
for i, marker_id in enumerate(ids):
|
| 173 |
if marker_id[0] == 42:
|
|
|
|
| 177 |
print("DEBUG: Marker with ID 42 not found among detected markers.")
|
| 178 |
else:
|
| 179 |
marker_corners = corners[selected_index].reshape((4, 2))
|
| 180 |
+
# Draw only the selected marker.
|
| 181 |
cv2.aruco.drawDetectedMarkers(image, [corners[selected_index]], [ids[selected_index]])
|
| 182 |
side_lengths = []
|
| 183 |
for i in range(4):
|