Manvikk commited on
Commit
aea5ab8
·
verified ·
1 Parent(s): b7a8219

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -78,7 +78,7 @@ def custom_nms(preds, iou_threshold=0.3):
78
  def process_image(job_id, image_path, object_type, multiplier):
79
  try:
80
  jobs[job_id]['progress'] = 10
81
- # Load the original image freshly
82
  image = cv2.imread(image_path)
83
  if image is None:
84
  jobs[job_id]['progress'] = 100
@@ -87,7 +87,7 @@ def process_image(job_id, image_path, object_type, multiplier):
87
 
88
  jobs[job_id]['progress'] = 20
89
  img_height, img_width = image.shape[:2]
90
- # Compute a dynamic thickness based on image size and multiplier.
91
  thickness = max(2, int(min(img_width, img_height) / 300)) * multiplier
92
  detection_info = []
93
 
@@ -97,7 +97,7 @@ 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's small (to help detect small boxes)
101
  scale_factor = 1
102
  if img_width < 1000 or img_height < 1000:
103
  scale_factor = 2
@@ -106,11 +106,10 @@ def process_image(job_id, image_path, object_type, multiplier):
106
  upscaled_image = cv2.resize(image, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
107
  temp_path = "upscaled.jpg"
108
  cv2.imwrite(temp_path, upscaled_image)
109
- # Lower the confidence threshold to 30 and overlap to 20 for better sensitivity
110
  results = box_model.predict(temp_path, confidence=30, overlap=20).json()
111
  else:
112
  results = box_model.predict(image_path, confidence=30, overlap=20).json()
113
-
114
  predictions = results.get("predictions", [])
115
  processed_preds = []
116
  for prediction in predictions:
@@ -142,24 +141,30 @@ def process_image(job_id, image_path, object_type, multiplier):
142
  except Exception as e:
143
  continue
144
 
145
- # Increase NMS IoU threshold to 0.7 to prevent suppression of valid, close boxes.
146
  box_detections = custom_nms(processed_preds, iou_threshold=0.7)
147
  jobs[job_id]['progress'] = 60
148
 
149
- # ArUco Marker Detection for measurement:
150
- # Here we assume the marker is printed at 5cm x 5cm.
151
- marker_real_width_cm = 5.0
152
  try:
153
- # Use a fresh copy of the image (without drawn overlays) for detection.
154
  aruco_image = image.copy()
155
  gray = cv2.cvtColor(aruco_image, cv2.COLOR_BGR2GRAY)
156
- # Use the DICT_6X6_250 dictionary. (You can change to DICT_4X4_50 if desired.)
157
  aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)
 
158
  aruco_params = cv2.aruco.DetectorParameters_create()
159
- # Optionally tweak parameters, e.g., aruco_params.minMarkerPerimeterRate = 0.03
160
- corners, ids, _ = cv2.aruco.detectMarkers(gray, aruco_dict, parameters=aruco_params)
 
 
 
 
 
 
161
  if ids is not None and len(corners) > 0:
162
- # Use the first detected marker as reference.
163
  marker_corners = corners[0].reshape((4, 2))
164
  cv2.aruco.drawDetectedMarkers(image, corners, ids)
165
  side_lengths = []
@@ -169,9 +174,12 @@ def process_image(job_id, image_path, object_type, multiplier):
169
  side_lengths.append(np.linalg.norm(pt1 - pt2))
170
  avg_side_length = np.mean(side_lengths)
171
  conversion_factor = marker_real_width_cm / avg_side_length
 
172
  else:
 
173
  conversion_factor = None
174
  except Exception as e:
 
175
  conversion_factor = None
176
 
177
  for pred in box_detections:
 
78
  def process_image(job_id, image_path, object_type, multiplier):
79
  try:
80
  jobs[job_id]['progress'] = 10
81
+ # Load a fresh copy of the original image
82
  image = cv2.imread(image_path)
83
  if image is None:
84
  jobs[job_id]['progress'] = 100
 
87
 
88
  jobs[job_id]['progress'] = 20
89
  img_height, img_width = image.shape[:2]
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
 
 
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
 
106
  upscaled_image = cv2.resize(image, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
107
  temp_path = "upscaled.jpg"
108
  cv2.imwrite(temp_path, upscaled_image)
 
109
  results = box_model.predict(temp_path, confidence=30, overlap=20).json()
110
  else:
111
  results = box_model.predict(image_path, confidence=30, overlap=20).json()
112
+
113
  predictions = results.get("predictions", [])
114
  processed_preds = []
115
  for prediction in predictions:
 
141
  except Exception as e:
142
  continue
143
 
144
+ # Use a higher NMS threshold to prevent 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 marker is printed at 5cm x 5cm.
150
+ marker_real_width_cm = 5.0
151
  try:
152
+ # Create a fresh copy for ArUco detection to avoid drawn overlays
153
  aruco_image = image.copy()
154
  gray = cv2.cvtColor(aruco_image, cv2.COLOR_BGR2GRAY)
155
+ # Use DICT_6X6_250 – ensure your printed marker is generated with this dictionary.
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
162
+ aruco_params.minMarkerPerimeterRate = 0.03
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 ArUco ids:", ids)
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 = []
 
174
  side_lengths.append(np.linalg.norm(pt1 - pt2))
175
  avg_side_length = np.mean(side_lengths)
176
  conversion_factor = marker_real_width_cm / avg_side_length
177
+ print("DEBUG: Marker conversion factor:", conversion_factor)
178
  else:
179
+ print("DEBUG: No ArUco marker detected.")
180
  conversion_factor = None
181
  except Exception as e:
182
+ print("DEBUG: Exception in ArUco detection:", e)
183
  conversion_factor = None
184
 
185
  for pred in box_detections: