Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ app = Flask(__name__)
|
|
| 14 |
app.secret_key = 'your_secret_key' # Replace with a secure secret key
|
| 15 |
|
| 16 |
# Global dictionary to hold job progress and results
|
| 17 |
-
jobs = {} # jobs[job_id] = {"progress": int, "result": {
|
| 18 |
|
| 19 |
#########################################
|
| 20 |
# 1. Initialize the Models
|
|
@@ -90,6 +90,7 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 90 |
# Compute dynamic thickness based on image size and multiplier.
|
| 91 |
thickness = max(2, int(min(img_width, img_height) / 300)) * multiplier
|
| 92 |
detection_info = []
|
|
|
|
| 93 |
|
| 94 |
if object_type == "box":
|
| 95 |
if box_model is None:
|
|
@@ -97,8 +98,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,7 +142,7 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 141 |
except Exception as e:
|
| 142 |
continue
|
| 143 |
|
| 144 |
-
#
|
| 145 |
box_detections = custom_nms(processed_preds, iou_threshold=0.7)
|
| 146 |
jobs[job_id]['progress'] = 60
|
| 147 |
|
|
@@ -149,12 +150,12 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 149 |
# We assume the marker is printed at 5cm x 5cm.
|
| 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
|
| 158 |
aruco_params = cv2.aruco.DetectorParameters_create()
|
| 159 |
aruco_params.adaptiveThreshWinSizeMin = 3
|
| 160 |
aruco_params.adaptiveThreshWinSizeMax = 23
|
|
@@ -167,6 +168,12 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 167 |
if ids is not None and len(corners) > 0:
|
| 168 |
marker_corners = corners[0].reshape((4, 2))
|
| 169 |
cv2.aruco.drawDetectedMarkers(image, corners, ids)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
side_lengths = []
|
| 171 |
for i in range(4):
|
| 172 |
pt1 = marker_corners[i]
|
|
@@ -182,8 +189,14 @@ def process_image(job_id, image_path, object_type, multiplier):
|
|
| 182 |
print("DEBUG: Exception in ArUco detection:", e)
|
| 183 |
conversion_factor = None
|
| 184 |
|
|
|
|
| 185 |
for pred in box_detections:
|
| 186 |
x1, y1, x2, y2 = pred["box"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
label = pred["class"]
|
| 188 |
confidence = pred["confidence"]
|
| 189 |
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), thickness)
|
|
|
|
| 14 |
app.secret_key = 'your_secret_key' # Replace with a secure secret key
|
| 15 |
|
| 16 |
# Global dictionary to hold job progress and results
|
| 17 |
+
jobs = {} # jobs[job_id] = {"progress": int, "result": {...}}
|
| 18 |
|
| 19 |
#########################################
|
| 20 |
# 1. Initialize the Models
|
|
|
|
| 90 |
# Compute dynamic thickness based on image size and multiplier.
|
| 91 |
thickness = max(2, int(min(img_width, img_height) / 300)) * multiplier
|
| 92 |
detection_info = []
|
| 93 |
+
marker_bbox = None # To store marker bounding box if detected
|
| 94 |
|
| 95 |
if object_type == "box":
|
| 96 |
if box_model is None:
|
|
|
|
| 98 |
jobs[job_id]['result'] = {"error": "Roboflow model not available."}
|
| 99 |
return
|
| 100 |
|
| 101 |
+
# Upscale the image if it's small so that small boxes become more prominent.
|
| 102 |
+
scale_factor = 1 # default: no upscaling
|
| 103 |
if img_width < 1000 or img_height < 1000:
|
| 104 |
scale_factor = 2
|
| 105 |
|
|
|
|
| 142 |
except Exception as e:
|
| 143 |
continue
|
| 144 |
|
| 145 |
+
# Increase NMS IoU threshold to 0.7 so that nearby valid boxes are not merged.
|
| 146 |
box_detections = custom_nms(processed_preds, iou_threshold=0.7)
|
| 147 |
jobs[job_id]['progress'] = 60
|
| 148 |
|
|
|
|
| 150 |
# We assume the marker is printed at 5cm x 5cm.
|
| 151 |
marker_real_width_cm = 5.0
|
| 152 |
try:
|
| 153 |
+
# Use a fresh copy for detection to avoid interference from drawn overlays.
|
| 154 |
aruco_image = image.copy()
|
| 155 |
gray = cv2.cvtColor(aruco_image, cv2.COLOR_BGR2GRAY)
|
| 156 |
+
# Use DICT_6X6_250 (ensure your printed marker is generated with this dictionary)
|
| 157 |
aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)
|
| 158 |
+
# Create detector parameters and adjust for small markers
|
| 159 |
aruco_params = cv2.aruco.DetectorParameters_create()
|
| 160 |
aruco_params.adaptiveThreshWinSizeMin = 3
|
| 161 |
aruco_params.adaptiveThreshWinSizeMax = 23
|
|
|
|
| 168 |
if ids is not None and len(corners) > 0:
|
| 169 |
marker_corners = corners[0].reshape((4, 2))
|
| 170 |
cv2.aruco.drawDetectedMarkers(image, corners, ids)
|
| 171 |
+
# Compute bounding box of the marker
|
| 172 |
+
mx1 = int(min(marker_corners[:, 0]))
|
| 173 |
+
my1 = int(min(marker_corners[:, 1]))
|
| 174 |
+
mx2 = int(max(marker_corners[:, 0]))
|
| 175 |
+
my2 = int(max(marker_corners[:, 1]))
|
| 176 |
+
marker_bbox = (mx1, my1, mx2, my2)
|
| 177 |
side_lengths = []
|
| 178 |
for i in range(4):
|
| 179 |
pt1 = marker_corners[i]
|
|
|
|
| 189 |
print("DEBUG: Exception in ArUco detection:", e)
|
| 190 |
conversion_factor = None
|
| 191 |
|
| 192 |
+
# Process each detected box
|
| 193 |
for pred in box_detections:
|
| 194 |
x1, y1, x2, y2 = pred["box"]
|
| 195 |
+
# If a marker bounding box was detected, skip boxes that significantly overlap it.
|
| 196 |
+
if marker_bbox is not None:
|
| 197 |
+
iou_with_marker = compute_iou((x1, y1, x2, y2), marker_bbox)
|
| 198 |
+
if iou_with_marker > 0.5:
|
| 199 |
+
continue
|
| 200 |
label = pred["class"]
|
| 201 |
confidence = pred["confidence"]
|
| 202 |
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), thickness)
|