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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -20
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 original image
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 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,17 +97,16 @@ 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 so that small boxes become more prominent.
101
- scale_factor = 1 # default: no upscaling
102
  if img_width < 1000 or img_height < 1000:
103
- scale_factor = 2 # adjust factor as needed
104
 
105
- # Run detection on the appropriate image version.
106
  if scale_factor > 1:
107
  upscaled_image = cv2.resize(image, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
108
  temp_path = "upscaled.jpg"
109
  cv2.imwrite(temp_path, upscaled_image)
110
- # Lower confidence threshold to 30 and overlap to 20 for better sensitivity
111
  results = box_model.predict(temp_path, confidence=30, overlap=20).json()
112
  else:
113
  results = box_model.predict(image_path, confidence=30, overlap=20).json()
@@ -117,7 +116,6 @@ def process_image(job_id, image_path, object_type, multiplier):
117
  for prediction in predictions:
118
  try:
119
  if scale_factor > 1:
120
- # Adjust coordinates from the upscaled image back to the original scale.
121
  x = prediction["x"] / scale_factor
122
  y = prediction["y"] / scale_factor
123
  width = prediction["width"] / scale_factor
@@ -144,24 +142,21 @@ def process_image(job_id, image_path, object_type, multiplier):
144
  except Exception as e:
145
  continue
146
 
147
- # Increase the NMS IoU threshold to 0.7 so that touching boxes are less likely to be suppressed.
148
  box_detections = custom_nms(processed_preds, iou_threshold=0.7)
149
  jobs[job_id]['progress'] = 60
150
 
151
- # (Optional) Detect ArUco marker for measurement.
152
- # Set the real-world width of your marker in centimeters.
153
- # For a marker that is 5cm x 5cm, use 5.0.
154
  marker_real_width_cm = 5.0
155
  try:
156
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
157
- # Use a predefined dictionary. You can try DICT_6X6_250 (or DICT_4X4_50 if you prefer).
 
 
158
  aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)
159
- # Alternatively:
160
- # aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_4X4_50)
161
- if hasattr(cv2.aruco, 'DetectorParameters_create'):
162
- aruco_params = cv2.aruco.DetectorParameters_create()
163
- else:
164
- aruco_params = cv2.aruco.DetectorParameters()
165
  corners, ids, _ = cv2.aruco.detectMarkers(gray, aruco_dict, parameters=aruco_params)
166
  if ids is not None and len(corners) > 0:
167
  # Use the first detected marker as reference.
 
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
 
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
  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
104
 
 
105
  if scale_factor > 1:
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()
 
116
  for prediction in predictions:
117
  try:
118
  if scale_factor > 1:
 
119
  x = prediction["x"] / scale_factor
120
  y = prediction["y"] / scale_factor
121
  width = prediction["width"] / scale_factor
 
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.