crabbly commited on
Commit
d5c8e37
·
verified ·
1 Parent(s): a752532

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +73 -37
main.py CHANGED
@@ -16,13 +16,13 @@ import uvicorn
16
  # OOM PREVENTION 1: Force PyTorch to use minimal memory overhead
17
  torch.set_num_threads(1)
18
 
19
- # Import your helpers (assuming cv_helpers.py is in the same folder)
20
  from cv_helpers import blend_mask_overlays, stem_tip_tangent_deg
21
 
22
  # --- CONFIGURATION ---
23
  MODEL_PATH = "best.pt"
24
  PIXELS_TO_CM = 1.0
25
- MAX_IMAGE_SIZE = 1024 # OOM PREVENTION 2: Max pixels on the longest side
26
 
27
  @dataclass
28
  class ProcessResult:
@@ -52,6 +52,7 @@ class WatermelonProcessor:
52
  def get_stable_perimeter_data(rind_mask, flesh_mask):
53
  cnts, _ = cv2.findContours(rind_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
54
  if not cnts: return None
 
55
  best_cnt = None
56
  max_overlap = -1
57
  for cnt in cnts:
@@ -61,6 +62,7 @@ class WatermelonProcessor:
61
  if overlap_area > max_overlap:
62
  max_overlap = overlap_area
63
  best_cnt = cnt
 
64
  if best_cnt is None: best_cnt = max(cnts, key=cv2.contourArea)
65
  moments = cv2.moments(best_cnt)
66
  if moments["m00"] == 0: return None
@@ -84,8 +86,8 @@ class WatermelonProcessor:
84
  return final_theta, final_r, (cx, cy), best_cnt
85
 
86
  @staticmethod
87
- def get_ray_scan_midline(flesh_mask, rind_cnt, predicted_cnt, cx, cy):
88
- h, w = flesh_mask.shape
89
  if len(rind_cnt) > 5:
90
  _, (ma, Ma), angle = cv2.fitEllipse(rind_cnt)
91
  rot_angle = angle if ma < Ma else angle + 90
@@ -93,32 +95,50 @@ class WatermelonProcessor:
93
 
94
  m_rot = cv2.getRotationMatrix2D((cx, cy), rot_angle, 1.0)
95
  m_inv = cv2.getRotationMatrix2D((cx, cy), -rot_angle, 1.0)
96
- f_rot = cv2.warpAffine(flesh_mask, m_rot, (w, h))
 
 
 
 
 
 
 
 
 
97
 
98
  gap_points =[]
99
- y_indices, _ = np.where(f_rot > 0)
100
- if len(y_indices) > 0:
101
- for y in range(np.min(y_indices), np.max(y_indices)):
102
- row = f_rot[y, :]
103
- white_px = np.where(row > 0)[0]
104
- if len(white_px) >= 2:
105
- blanks = np.where(row[white_px[0]:white_px[-1]] == 0)[0] + white_px[0]
106
- if len(blanks) > 0: gap_points.append([y, np.median(blanks)])
 
 
 
 
 
107
 
108
  gap_points = np.array(gap_points)
109
  if len(gap_points) > 10:
110
- y_min, y_max = np.min(gap_points[:, 0]), np.max(gap_points[:, 0])
111
- y_span, y_mean = max(y_max - y_min, 1), (y_max + y_min) / 2.0
 
112
  y_norm = (gap_points[:, 0] - y_mean) / y_span
113
  x_data = gap_points[:, 1]
114
 
115
  def parabola(y_n, a, b, c): return a * (y_n**2) + b * y_n + c
 
116
  try:
117
- popt_mid, _ = curve_fit(parabola, y_norm, x_data, bounds=([-w*0.08, -np.inf, -np.inf],[w*0.08, np.inf, np.inf]))
118
- except: popt_mid =[0.0, 0.0, cx]
 
119
 
120
  ys_extrap = np.linspace(0, h, 500)
121
- xs_extrap = parabola((ys_extrap - y_mean) / y_span, *popt_mid)
 
122
  pts_rot = np.vstack([xs_extrap, ys_extrap, np.ones_like(xs_extrap)])
123
  else:
124
  ys_extrap = np.linspace(0, h, 500)
@@ -132,22 +152,35 @@ class WatermelonProcessor:
132
  if image is None: return ProcessResult(success=False, message="Could not decode image.")
133
  h, w = image.shape[:2]
134
 
135
- results = self.model(image, conf=0.25, verbose=False)
136
- rind_mask, flesh_mask = np.zeros((h, w), dtype=np.uint8), np.zeros((h, w), dtype=np.uint8)
 
 
 
 
137
 
138
  if results[0].masks is None:
139
  return ProcessResult(success=False, message="No masks detected.")
140
 
 
141
  for mask_data, cls in zip(results[0].masks.xy, results[0].boxes.cls):
142
  contour = np.array(mask_data, dtype=np.int32)
143
- if int(cls) == 0: cv2.drawContours(rind_mask, [contour], -1, 255, -1)
144
- elif int(cls) == 1: cv2.drawContours(flesh_mask, [contour], -1, 255, -1)
 
 
 
 
 
 
 
145
 
146
- perimeter_data = self.get_stable_perimeter_data(rind_mask, flesh_mask)
147
  if perimeter_data is None: return ProcessResult(success=False, message="No stable perimeter.")
148
 
149
  t_data, r_raw, (cx, cy), rind_cnt = perimeter_data
150
  scale = np.mean(r_raw)
 
151
 
152
  try:
153
  popt, _ = curve_fit(
@@ -157,35 +190,37 @@ class WatermelonProcessor:
157
  )
158
  except Exception as exc: return ProcessResult(success=False, message=f"Fit failed: {exc}")
159
 
160
- r2 = 1 - (np.sum((r_raw / scale - self.watermelon_model(t_data, *popt)) ** 2) / np.sum((r_raw / scale - 1) ** 2))
161
-
 
 
162
  t_fit = np.linspace(-np.pi, np.pi, 500)
163
  r_fit = self.watermelon_model(t_fit, *popt) * scale
164
  fit_pts = np.array([[r * np.cos(t) + cx, cy - r * np.sin(t)] for t, r in zip(t_fit, r_fit)])
165
 
166
- # --- FEATURE EXTRACTION (WITH TRUE-SIZE CORRECTION) ---
167
  width_px = float(np.max(fit_pts[:, 0]) - np.min(fit_pts[:, 0]))
168
  height_px = float(np.max(fit_pts[:, 1]) - np.min(fit_pts[:, 1]))
169
  diffs = np.diff(fit_pts, axis=0)
170
  perimeter_px = float(np.sum(np.linalg.norm(diffs, axis=1)) + np.linalg.norm(fit_pts[-1] - fit_pts[0]))
171
 
172
- # We divide by scale_ratio to perfectly undo the downscaling for measurements!
173
- orig_scale = 1.0 / scale_ratio
174
  width_val = width_px * PIXELS_TO_CM * orig_scale
175
  height_val = height_px * PIXELS_TO_CM * orig_scale
176
  perimeter_val = perimeter_px * PIXELS_TO_CM * orig_scale
177
 
178
- # --- DRAWING ---
179
- midline = self.get_ray_scan_midline(flesh_mask, rind_cnt, fit_pts, cx, cy)
180
- output = blend_mask_overlays(image, rind_mask, flesh_mask)
181
- if len(midline) > 1: cv2.polylines(output,[midline.astype(np.int32)], False, (0, 255, 255), 3)
182
- cv2.polylines(output,[fit_pts.astype(np.int32)], True, (0, 255, 0), 3)
 
 
183
 
184
  stem = stem_tip_tangent_deg(rind_cnt, (cx, cy))
185
  if stem is not None:
186
  tx, ty, tdeg = stem
187
- L = min(w, h) * 0.08
188
  rad = np.deg2rad(tdeg)
 
189
  p1 = (int(round(tx)), int(round(ty)))
190
  p2 = (int(round(tx + L * np.cos(rad))), int(round(ty + L * np.sin(rad))))
191
  cv2.circle(output, p1, 6, (255, 0, 255), -1)
@@ -224,7 +259,6 @@ async def process_single(file: UploadFile = File(...)):
224
  nparr = np.frombuffer(contents, np.uint8)
225
  img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
226
 
227
- # OOM PREVENTION 3: Resize image if it's massive
228
  h, w = img.shape[:2]
229
  scale_ratio = 1.0
230
  if max(h, w) > MAX_IMAGE_SIZE:
@@ -233,8 +267,10 @@ async def process_single(file: UploadFile = File(...)):
233
 
234
  res = processor.process_image(img, file.filename, scale_ratio)
235
 
236
- # OOM PREVENTION 4: Force garbage collection immediately after processing
237
  del img, nparr, contents
238
  gc.collect()
239
 
240
- return res.__dict__
 
 
 
 
16
  # OOM PREVENTION 1: Force PyTorch to use minimal memory overhead
17
  torch.set_num_threads(1)
18
 
19
+ # Import your helpers
20
  from cv_helpers import blend_mask_overlays, stem_tip_tangent_deg
21
 
22
  # --- CONFIGURATION ---
23
  MODEL_PATH = "best.pt"
24
  PIXELS_TO_CM = 1.0
25
+ MAX_IMAGE_SIZE = 1024
26
 
27
  @dataclass
28
  class ProcessResult:
 
52
  def get_stable_perimeter_data(rind_mask, flesh_mask):
53
  cnts, _ = cv2.findContours(rind_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
54
  if not cnts: return None
55
+
56
  best_cnt = None
57
  max_overlap = -1
58
  for cnt in cnts:
 
62
  if overlap_area > max_overlap:
63
  max_overlap = overlap_area
64
  best_cnt = cnt
65
+
66
  if best_cnt is None: best_cnt = max(cnts, key=cv2.contourArea)
67
  moments = cv2.moments(best_cnt)
68
  if moments["m00"] == 0: return None
 
86
  return final_theta, final_r, (cx, cy), best_cnt
87
 
88
  @staticmethod
89
+ def get_dual_mask_midline(f_left, f_right, rind_cnt, predicted_cnt, cx, cy):
90
+ h, w = f_left.shape
91
  if len(rind_cnt) > 5:
92
  _, (ma, Ma), angle = cv2.fitEllipse(rind_cnt)
93
  rot_angle = angle if ma < Ma else angle + 90
 
95
 
96
  m_rot = cv2.getRotationMatrix2D((cx, cy), rot_angle, 1.0)
97
  m_inv = cv2.getRotationMatrix2D((cx, cy), -rot_angle, 1.0)
98
+
99
+ l_rot = cv2.warpAffine(f_left, m_rot, (w, h))
100
+ r_rot = cv2.warpAffine(f_right, m_rot, (w, h))
101
+
102
+ # Failsafe: Swap left/right if YOLO got labels crossed
103
+ l_idx = np.where(l_rot > 0)[1]
104
+ r_idx = np.where(r_rot > 0)[1]
105
+ if len(l_idx) > 0 and len(r_idx) > 0:
106
+ if np.mean(l_idx) > np.mean(r_idx):
107
+ l_rot, r_rot = r_rot, l_rot
108
 
109
  gap_points =[]
110
+ y_l = np.where(l_rot > 0)[0]
111
+ y_r = np.where(r_rot > 0)[0]
112
+
113
+ if len(y_l) > 0 and len(y_r) > 0:
114
+ y_min = max(np.min(y_l), np.min(y_r))
115
+ y_max = min(np.max(y_l), np.max(y_r))
116
+ for y in range(y_min, y_max):
117
+ row_l = np.where(l_rot[y, :] > 0)[0]
118
+ row_r = np.where(r_rot[y, :] > 0)[0]
119
+ if len(row_l) > 0 and len(row_r) > 0:
120
+ edge_l = row_l[-1]
121
+ edge_r = row_r[0]
122
+ gap_points.append([y, (edge_l + edge_r) / 2.0])
123
 
124
  gap_points = np.array(gap_points)
125
  if len(gap_points) > 10:
126
+ y_min_g, y_max_g = np.min(gap_points[:, 0]), np.max(gap_points[:, 0])
127
+ y_span = max(y_max_g - y_min_g, 1)
128
+ y_mean = (y_max_g + y_min_g) / 2.0
129
  y_norm = (gap_points[:, 0] - y_mean) / y_span
130
  x_data = gap_points[:, 1]
131
 
132
  def parabola(y_n, a, b, c): return a * (y_n**2) + b * y_n + c
133
+ max_bend = w * 0.08
134
  try:
135
+ popt_mid, _ = curve_fit(parabola, y_norm, x_data, bounds=([-max_bend, -np.inf, -np.inf],[max_bend, np.inf, np.inf]))
136
+ except:
137
+ popt_mid =[0.0, 0.0, cx]
138
 
139
  ys_extrap = np.linspace(0, h, 500)
140
+ ys_extrap_norm = (ys_extrap - y_mean) / y_span
141
+ xs_extrap = parabola(ys_extrap_norm, *popt_mid)
142
  pts_rot = np.vstack([xs_extrap, ys_extrap, np.ones_like(xs_extrap)])
143
  else:
144
  ys_extrap = np.linspace(0, h, 500)
 
152
  if image is None: return ProcessResult(success=False, message="Could not decode image.")
153
  h, w = image.shape[:2]
154
 
155
+ # retina_masks=True removes the plateau artifacts during inference!
156
+ results = self.model(image, conf=0.25, retina_masks=True, verbose=False)
157
+
158
+ rind_mask = np.zeros((h, w), dtype=np.uint8)
159
+ flesh_l = np.zeros((h, w), dtype=np.uint8)
160
+ flesh_r = np.zeros((h, w), dtype=np.uint8)
161
 
162
  if results[0].masks is None:
163
  return ProcessResult(success=False, message="No masks detected.")
164
 
165
+ # --- THE FIX: Load 0 (Whole), 1 (Left), and 2 (Right) ---
166
  for mask_data, cls in zip(results[0].masks.xy, results[0].boxes.cls):
167
  contour = np.array(mask_data, dtype=np.int32)
168
+ c_id = int(cls)
169
+ if c_id == 0:
170
+ cv2.drawContours(rind_mask, [contour], -1, 255, -1)
171
+ elif c_id == 1:
172
+ cv2.drawContours(flesh_l, [contour], -1, 255, -1)
173
+ elif c_id == 2:
174
+ cv2.drawContours(flesh_r, [contour], -1, 255, -1)
175
+
176
+ flesh_combined = cv2.bitwise_or(flesh_l, flesh_r)
177
 
178
+ perimeter_data = self.get_stable_perimeter_data(rind_mask, flesh_combined)
179
  if perimeter_data is None: return ProcessResult(success=False, message="No stable perimeter.")
180
 
181
  t_data, r_raw, (cx, cy), rind_cnt = perimeter_data
182
  scale = np.mean(r_raw)
183
+ if scale <= 0: return ProcessResult(success=False, message="Invalid perimeter scale.")
184
 
185
  try:
186
  popt, _ = curve_fit(
 
190
  )
191
  except Exception as exc: return ProcessResult(success=False, message=f"Fit failed: {exc}")
192
 
193
+ denom = np.sum((r_raw / scale - 1) ** 2)
194
+ if denom == 0: return ProcessResult(success=False, message="R2 denominator became zero.")
195
+
196
+ r2 = 1 - (np.sum((r_raw / scale - self.watermelon_model(t_data, *popt)) ** 2) / denom)
197
  t_fit = np.linspace(-np.pi, np.pi, 500)
198
  r_fit = self.watermelon_model(t_fit, *popt) * scale
199
  fit_pts = np.array([[r * np.cos(t) + cx, cy - r * np.sin(t)] for t, r in zip(t_fit, r_fit)])
200
 
201
+ orig_scale = 1.0 / scale_ratio
202
  width_px = float(np.max(fit_pts[:, 0]) - np.min(fit_pts[:, 0]))
203
  height_px = float(np.max(fit_pts[:, 1]) - np.min(fit_pts[:, 1]))
204
  diffs = np.diff(fit_pts, axis=0)
205
  perimeter_px = float(np.sum(np.linalg.norm(diffs, axis=1)) + np.linalg.norm(fit_pts[-1] - fit_pts[0]))
206
 
 
 
207
  width_val = width_px * PIXELS_TO_CM * orig_scale
208
  height_val = height_px * PIXELS_TO_CM * orig_scale
209
  perimeter_val = perimeter_px * PIXELS_TO_CM * orig_scale
210
 
211
+ # --- THE FIX: Dual-Mask Midline ---
212
+ midline = self.get_dual_mask_midline(flesh_l, flesh_r, rind_cnt, fit_pts, cx, cy)
213
+
214
+ output = blend_mask_overlays(image, rind_mask, flesh_combined)
215
+ if len(midline) > 1:
216
+ cv2.polylines(output, [midline.astype(np.int32)], False, (0, 255, 255), 3)
217
+ cv2.polylines(output, [fit_pts.astype(np.int32)], True, (0, 255, 0), 3)
218
 
219
  stem = stem_tip_tangent_deg(rind_cnt, (cx, cy))
220
  if stem is not None:
221
  tx, ty, tdeg = stem
 
222
  rad = np.deg2rad(tdeg)
223
+ L = min(w, h) * 0.08
224
  p1 = (int(round(tx)), int(round(ty)))
225
  p2 = (int(round(tx + L * np.cos(rad))), int(round(ty + L * np.sin(rad))))
226
  cv2.circle(output, p1, 6, (255, 0, 255), -1)
 
259
  nparr = np.frombuffer(contents, np.uint8)
260
  img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
261
 
 
262
  h, w = img.shape[:2]
263
  scale_ratio = 1.0
264
  if max(h, w) > MAX_IMAGE_SIZE:
 
267
 
268
  res = processor.process_image(img, file.filename, scale_ratio)
269
 
 
270
  del img, nparr, contents
271
  gc.collect()
272
 
273
+ return res.__dict__
274
+
275
+ if __name__ == "__main__":
276
+ uvicorn.run(app, host="0.0.0.0", port=7860)