crabbly commited on
Commit
05bbee6
·
verified ·
1 Parent(s): 1f82f1a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +35 -35
main.py CHANGED
@@ -22,11 +22,11 @@ from cv_helpers import blend_mask_overlays, stem_tip_tangent_deg
22
 
23
  # --- CONFIGURATION ---
24
  MODEL_PATH = "best.pt"
25
- MAX_IMAGE_SIZE = 2048 # Large enough to ensure the color checker is clearly visible
26
- CHECKER_WIDTH_CM = 6.3 # Physical width of the X-Rite ColorChecker Classic
27
 
28
  # ==============================================================================
29
- # --- COLOR CALIBRATION LOGIC (Embedded) ---
30
  # ==============================================================================
31
  def to_linear_srgb(u8_bgr):
32
  rgb = cv2.cvtColor(u8_bgr, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
@@ -46,7 +46,7 @@ def detect_checker_corners(img_bgr):
46
  return np.array(cc.getBox() if hasattr(cc, "getBox") else cc.getCorners(), dtype=np.float32)
47
 
48
  def warp_checker(img, corners, out_w=600, out_h=400):
49
- dst = np.float32([[0, out_h-1],[0, 0], [out_w-1, 0],[out_w-1, out_h-1]])
50
  H_mat = cv2.getPerspectiveTransform(corners, dst)
51
  return cv2.warpPerspective(img, H_mat, (out_w, out_h), flags=cv2.INTER_CUBIC)
52
 
@@ -68,27 +68,23 @@ def compute_deltaE_00(lin_src, lin_ref):
68
  return color.deltaE_ciede2000(color.rgb2lab(srgb_src.reshape(1, -1, 3)), color.rgb2lab(srgb_ref.reshape(1, -1, 3))).flatten()
69
 
70
  def apply_color_pipeline(target_bgr, ref24, tgt24):
71
- # White Balance
72
  gains = np.median(ref24[18:24], axis=0) / np.maximum(np.median(tgt24[18:24], axis=0), 1e-6)
73
  lin = to_linear_srgb(target_bgr)
74
  lin_wb = lin * gains.reshape(1, 1, 3)
75
  tgt24_wb = tgt24 * gains
76
 
77
- # CCM
78
  M = np.linalg.lstsq(tgt24_wb[:18], ref24[:18], rcond=None)[0].T
79
  corrected = (lin_wb.reshape(-1, 3) @ M.T).reshape(lin_wb.shape)
80
  tgt24_ccm = tgt24_wb @ M.T
81
 
82
- # Luma Curve
83
  w = np.array([0.2126, 0.7152, 0.0722], np.float32)
84
  Ls, Lt = (tgt24_ccm[19:23] @ w), (ref24[19:23] @ w)
85
  sort_idx = np.argsort(Ls)
86
  Ls, Lt = np.concatenate([[0.01], Ls[sort_idx], [0.98]]), np.concatenate([[0.01], Lt[sort_idx],[0.98]])
87
 
88
- L = np.clip(np.tensordot(corrected, w, axes=([2], [0])), 0, 1)
89
  Lt_mapped = np.interp(L, Ls, Lt)
90
 
91
- # Soft Rolloff
92
  knee, strength = 0.90, 0.6
93
  below = Lt_mapped < knee
94
  Lt_final = np.empty_like(Lt_mapped)
@@ -119,7 +115,6 @@ class WatermelonProcessor:
119
  def __init__(self, model_path: str):
120
  self.model = YOLO(model_path)
121
 
122
- # Load Golden Reference Patches
123
  self.ref24 = None
124
  if os.path.exists("reference.png"):
125
  try:
@@ -196,13 +191,16 @@ class WatermelonProcessor:
196
  y_span, y_mean = max(y_max - y_min, 1), (y_max + y_min) / 2.0
197
  def parabola(y_n, a, b, c): return a*(y_n**2) + b*y_n + c
198
  try:
199
- popt_mid, _ = curve_fit(parabola, (gap_points[:,0]-y_mean)/y_span, gap_points[:,1], bounds=([-w*0.08, -np.inf, -np.inf], [w*0.08, np.inf, np.inf]))
200
  except: popt_mid = [0.0, 0.0, cx]
201
- ys_ex = np.linspace(0, h, 500)
202
- pts_rot = np.vstack([parabola((ys_ex - y_mean)/y_span, *popt_mid), ys_extrap, np.ones_like(ys_ex)])
 
 
 
203
  else:
204
- ys_ex = np.linspace(0, h, 500)
205
- pts_rot = np.vstack([np.full_like(ys_ex, cx), ys_ex, np.ones_like(ys_ex)])
206
 
207
  pts_orig = (m_inv @ pts_rot).T
208
  pred_cnt_cv = pred_cnt.reshape(-1, 1, 2).astype(np.int32)
@@ -212,12 +210,10 @@ class WatermelonProcessor:
212
  if image is None: return ProcessResult(success=False, message="Could not decode image.")
213
  h, w = image.shape[:2]
214
 
215
- # --- 1. CALIBRATION & SCALING ---
216
  dE_initial, dE_final, cm_per_px, checker_corners = None, None, None, None
217
 
218
  try:
219
  checker_corners = detect_checker_corners(image)
220
- # Physical width logic
221
  top_width = np.linalg.norm(checker_corners[1] - checker_corners[2])
222
  bot_width = np.linalg.norm(checker_corners[0] - checker_corners[3])
223
  cm_per_px = CHECKER_WIDTH_CM / ((top_width + bot_width) / 2.0)
@@ -226,16 +222,13 @@ class WatermelonProcessor:
226
  tgt24 = sample_24_patches(warp_checker(image, checker_corners))
227
  dE_initial = float(np.mean(compute_deltaE_00(tgt24, self.ref24)))
228
 
229
- # Apply pipeline to full image BEFORE YOLO inference
230
  image = apply_color_pipeline(image, self.ref24, tgt24)
231
 
232
- # Re-check the fully corrected image
233
  tgt24_corr = sample_24_patches(warp_checker(image, checker_corners))
234
  dE_final = float(np.mean(compute_deltaE_00(tgt24_corr, self.ref24)))
235
  except Exception as e:
236
  print(f"Calibration skipped for {source_name}: {e}")
237
 
238
- # --- 2. YOLO INFERENCE (3 CLASSES) ---
239
  results = self.model(image, conf=0.25, retina_masks=True, verbose=False)
240
  rind_mask, flesh_l, flesh_r = np.zeros((h, w), dtype=np.uint8), np.zeros((h, w), dtype=np.uint8), np.zeros((h, w), dtype=np.uint8)
241
 
@@ -255,11 +248,14 @@ class WatermelonProcessor:
255
 
256
  t_data, r_raw, (cx, cy), rind_cnt = perimeter_data
257
  scale = np.mean(r_raw)
 
258
 
259
  try:
260
- popt, _ = curve_fit(self.watermelon_model, t_data, r_raw / scale,
 
261
  p0=[1.0, 1.1, 0.0, 0.05, 3.0, 0.05, 3.0, 0.0, 0.0, 0.0],
262
- bounds=([0.5, 0.5, -0.4, 0.0, 0.1, 0.0, 0.1, -1.5, -0.2, -0.2],[2.0, 2.0, 0.4, 0.5, 50.0, 0.5, 50.0, 1.5, 0.2, 0.2]))
 
263
  except Exception as exc: return ProcessResult(success=False, message=f"Fit failed: {exc}")
264
 
265
  r2 = 1 - (np.sum((r_raw / scale - self.watermelon_model(t_data, *popt)) ** 2) / np.sum((r_raw / scale - 1) ** 2))
@@ -267,15 +263,17 @@ class WatermelonProcessor:
267
  r_fit = self.watermelon_model(t_fit, *popt) * scale
268
  fit_pts = np.array([[r * np.cos(t) + cx, cy - r * np.sin(t)] for t, r in zip(t_fit, r_fit)])
269
 
270
- # --- 3. DIMENSIONAL MATH (IN CM) ---
271
  orig_scale = 1.0 / scale_ratio
272
- if cm_per_px is None: cm_per_px = 1.0
273
 
274
- width_val = float(np.max(fit_pts[:, 0]) - np.min(fit_pts[:, 0])) * cm_per_px * orig_scale
275
- height_val = float(np.max(fit_pts[:, 1]) - np.min(fit_pts[:, 1])) * cm_per_px * orig_scale
276
- perimeter_val = float(np.sum(np.linalg.norm(np.diff(fit_pts, axis=0), axis=1)) + np.linalg.norm(fit_pts[-1] - fit_pts[0])) * cm_per_px * orig_scale
 
 
 
 
277
 
278
- # --- 4. DRAWING ---
279
  midline = self.get_dual_mask_midline(flesh_l, flesh_r, rind_cnt, fit_pts, cx, cy)
280
  output = blend_mask_overlays(image, rind_mask, flesh_combined)
281
 
@@ -288,22 +286,23 @@ class WatermelonProcessor:
288
  stem = stem_tip_tangent_deg(rind_cnt, (cx, cy))
289
  if stem is not None:
290
  tx, ty, tdeg = stem
 
291
  rad = np.deg2rad(tdeg)
292
  p1 = (int(round(tx)), int(round(ty)))
293
- p2 = (int(round(tx + min(w, h)*0.08 * np.cos(rad))), int(round(ty + min(w, h)*0.08 * np.sin(rad))))
294
  cv2.circle(output, p1, 6, (255, 0, 255), -1)
295
  cv2.line(output, p1, p2, (255, 0, 255), 2)
296
 
297
- _, buffer = cv2.imencode('.jpg', output,[cv2.IMWRITE_JPEG_QUALITY, 85])
 
298
 
299
  return ProcessResult(
300
  success=True, message="Success", r2_score=float(r2),
301
  width_val=width_val, height_val=height_val, perimeter_val=perimeter_val,
302
  delta_e_initial=dE_initial, delta_e_final=dE_final,
303
- image_base64=base64.b64encode(buffer).decode('utf-8'), filename=source_name
304
  )
305
 
306
-
307
  app = FastAPI()
308
 
309
  app.add_middleware(
@@ -318,17 +317,18 @@ def read_root(): return {"status": "Watermelon API is awake!"}
318
  @app.post("/process_single")
319
  async def process_single(file: UploadFile = File(...)):
320
  contents = await file.read()
321
- img = cv2.imdecode(np.frombuffer(contents, np.uint8), cv2.IMREAD_COLOR)
 
322
 
323
- scale_ratio = 1.0
324
  h, w = img.shape[:2]
 
325
  if max(h, w) > MAX_IMAGE_SIZE:
326
  scale_ratio = MAX_IMAGE_SIZE / float(max(h, w))
327
  img = cv2.resize(img, (int(w * scale_ratio), int(h * scale_ratio)), interpolation=cv2.INTER_AREA)
328
 
329
  res = processor.process_image(img, file.filename, scale_ratio)
330
 
331
- del img, contents
332
  gc.collect()
333
 
334
  return res.__dict__
 
22
 
23
  # --- CONFIGURATION ---
24
  MODEL_PATH = "best.pt"
25
+ MAX_IMAGE_SIZE = 2048
26
+ CHECKER_WIDTH_CM = 6.3
27
 
28
  # ==============================================================================
29
+ # --- COLOR CALIBRATION LOGIC ---
30
  # ==============================================================================
31
  def to_linear_srgb(u8_bgr):
32
  rgb = cv2.cvtColor(u8_bgr, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
 
46
  return np.array(cc.getBox() if hasattr(cc, "getBox") else cc.getCorners(), dtype=np.float32)
47
 
48
  def warp_checker(img, corners, out_w=600, out_h=400):
49
+ dst = np.float32([[0, out_h-1],[0, 0],[out_w-1, 0],[out_w-1, out_h-1]])
50
  H_mat = cv2.getPerspectiveTransform(corners, dst)
51
  return cv2.warpPerspective(img, H_mat, (out_w, out_h), flags=cv2.INTER_CUBIC)
52
 
 
68
  return color.deltaE_ciede2000(color.rgb2lab(srgb_src.reshape(1, -1, 3)), color.rgb2lab(srgb_ref.reshape(1, -1, 3))).flatten()
69
 
70
  def apply_color_pipeline(target_bgr, ref24, tgt24):
 
71
  gains = np.median(ref24[18:24], axis=0) / np.maximum(np.median(tgt24[18:24], axis=0), 1e-6)
72
  lin = to_linear_srgb(target_bgr)
73
  lin_wb = lin * gains.reshape(1, 1, 3)
74
  tgt24_wb = tgt24 * gains
75
 
 
76
  M = np.linalg.lstsq(tgt24_wb[:18], ref24[:18], rcond=None)[0].T
77
  corrected = (lin_wb.reshape(-1, 3) @ M.T).reshape(lin_wb.shape)
78
  tgt24_ccm = tgt24_wb @ M.T
79
 
 
80
  w = np.array([0.2126, 0.7152, 0.0722], np.float32)
81
  Ls, Lt = (tgt24_ccm[19:23] @ w), (ref24[19:23] @ w)
82
  sort_idx = np.argsort(Ls)
83
  Ls, Lt = np.concatenate([[0.01], Ls[sort_idx], [0.98]]), np.concatenate([[0.01], Lt[sort_idx],[0.98]])
84
 
85
+ L = np.clip(np.tensordot(corrected, w, axes=([2],[0])), 0, 1)
86
  Lt_mapped = np.interp(L, Ls, Lt)
87
 
 
88
  knee, strength = 0.90, 0.6
89
  below = Lt_mapped < knee
90
  Lt_final = np.empty_like(Lt_mapped)
 
115
  def __init__(self, model_path: str):
116
  self.model = YOLO(model_path)
117
 
 
118
  self.ref24 = None
119
  if os.path.exists("reference.png"):
120
  try:
 
191
  y_span, y_mean = max(y_max - y_min, 1), (y_max + y_min) / 2.0
192
  def parabola(y_n, a, b, c): return a*(y_n**2) + b*y_n + c
193
  try:
194
+ popt_mid, _ = curve_fit(parabola, (gap_points[:,0]-y_mean)/y_span, gap_points[:,1], bounds=([-w*0.08, -np.inf, -np.inf],[w*0.08, np.inf, np.inf]))
195
  except: popt_mid = [0.0, 0.0, cx]
196
+
197
+ # FIXED TYPO HERE (ys_extrap)
198
+ ys_extrap = np.linspace(0, h, 500)
199
+ xs_extrap = parabola((ys_extrap - y_mean)/y_span, *popt_mid)
200
+ pts_rot = np.vstack([xs_extrap, ys_extrap, np.ones_like(ys_extrap)])
201
  else:
202
+ ys_extrap = np.linspace(0, h, 500)
203
+ pts_rot = np.vstack([np.full_like(ys_extrap, cx), ys_extrap, np.ones_like(ys_extrap)])
204
 
205
  pts_orig = (m_inv @ pts_rot).T
206
  pred_cnt_cv = pred_cnt.reshape(-1, 1, 2).astype(np.int32)
 
210
  if image is None: return ProcessResult(success=False, message="Could not decode image.")
211
  h, w = image.shape[:2]
212
 
 
213
  dE_initial, dE_final, cm_per_px, checker_corners = None, None, None, None
214
 
215
  try:
216
  checker_corners = detect_checker_corners(image)
 
217
  top_width = np.linalg.norm(checker_corners[1] - checker_corners[2])
218
  bot_width = np.linalg.norm(checker_corners[0] - checker_corners[3])
219
  cm_per_px = CHECKER_WIDTH_CM / ((top_width + bot_width) / 2.0)
 
222
  tgt24 = sample_24_patches(warp_checker(image, checker_corners))
223
  dE_initial = float(np.mean(compute_deltaE_00(tgt24, self.ref24)))
224
 
 
225
  image = apply_color_pipeline(image, self.ref24, tgt24)
226
 
 
227
  tgt24_corr = sample_24_patches(warp_checker(image, checker_corners))
228
  dE_final = float(np.mean(compute_deltaE_00(tgt24_corr, self.ref24)))
229
  except Exception as e:
230
  print(f"Calibration skipped for {source_name}: {e}")
231
 
 
232
  results = self.model(image, conf=0.25, retina_masks=True, verbose=False)
233
  rind_mask, flesh_l, flesh_r = np.zeros((h, w), dtype=np.uint8), np.zeros((h, w), dtype=np.uint8), np.zeros((h, w), dtype=np.uint8)
234
 
 
248
 
249
  t_data, r_raw, (cx, cy), rind_cnt = perimeter_data
250
  scale = np.mean(r_raw)
251
+ if scale <= 0: return ProcessResult(success=False, message="Invalid perimeter scale.")
252
 
253
  try:
254
+ popt, _ = curve_fit(
255
+ self.watermelon_model, t_data, r_raw / scale,
256
  p0=[1.0, 1.1, 0.0, 0.05, 3.0, 0.05, 3.0, 0.0, 0.0, 0.0],
257
+ bounds=([0.5, 0.5, -0.4, 0.0, 0.1, 0.0, 0.1, -1.5, -0.2, -0.2],[2.0, 2.0, 0.4, 0.5, 50.0, 0.5, 50.0, 1.5, 0.2, 0.2]),
258
+ )
259
  except Exception as exc: return ProcessResult(success=False, message=f"Fit failed: {exc}")
260
 
261
  r2 = 1 - (np.sum((r_raw / scale - self.watermelon_model(t_data, *popt)) ** 2) / np.sum((r_raw / scale - 1) ** 2))
 
263
  r_fit = self.watermelon_model(t_fit, *popt) * scale
264
  fit_pts = np.array([[r * np.cos(t) + cx, cy - r * np.sin(t)] for t, r in zip(t_fit, r_fit)])
265
 
 
266
  orig_scale = 1.0 / scale_ratio
267
+ if cm_per_px is None: cm_per_px = 1.0
268
 
269
+ width_px = float(np.max(fit_pts[:, 0]) - np.min(fit_pts[:, 0]))
270
+ height_px = float(np.max(fit_pts[:, 1]) - np.min(fit_pts[:, 1]))
271
+ perimeter_px = float(np.sum(np.linalg.norm(np.diff(fit_pts, axis=0), axis=1)) + np.linalg.norm(fit_pts[-1] - fit_pts[0]))
272
+
273
+ width_val = width_px * cm_per_px * orig_scale
274
+ height_val = height_px * cm_per_px * orig_scale
275
+ perimeter_val = perimeter_px * cm_per_px * orig_scale
276
 
 
277
  midline = self.get_dual_mask_midline(flesh_l, flesh_r, rind_cnt, fit_pts, cx, cy)
278
  output = blend_mask_overlays(image, rind_mask, flesh_combined)
279
 
 
286
  stem = stem_tip_tangent_deg(rind_cnt, (cx, cy))
287
  if stem is not None:
288
  tx, ty, tdeg = stem
289
+ L = min(w, h) * 0.08
290
  rad = np.deg2rad(tdeg)
291
  p1 = (int(round(tx)), int(round(ty)))
292
+ p2 = (int(round(tx + L * np.cos(rad))), int(round(ty + L * np.sin(rad))))
293
  cv2.circle(output, p1, 6, (255, 0, 255), -1)
294
  cv2.line(output, p1, p2, (255, 0, 255), 2)
295
 
296
+ _, buffer = cv2.imencode('.jpg', output, [cv2.IMWRITE_JPEG_QUALITY, 85])
297
+ img_base64 = base64.b64encode(buffer).decode('utf-8')
298
 
299
  return ProcessResult(
300
  success=True, message="Success", r2_score=float(r2),
301
  width_val=width_val, height_val=height_val, perimeter_val=perimeter_val,
302
  delta_e_initial=dE_initial, delta_e_final=dE_final,
303
+ image_base64=img_base64, filename=source_name
304
  )
305
 
 
306
  app = FastAPI()
307
 
308
  app.add_middleware(
 
317
  @app.post("/process_single")
318
  async def process_single(file: UploadFile = File(...)):
319
  contents = await file.read()
320
+ nparr = np.frombuffer(contents, np.uint8)
321
+ img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
322
 
 
323
  h, w = img.shape[:2]
324
+ scale_ratio = 1.0
325
  if max(h, w) > MAX_IMAGE_SIZE:
326
  scale_ratio = MAX_IMAGE_SIZE / float(max(h, w))
327
  img = cv2.resize(img, (int(w * scale_ratio), int(h * scale_ratio)), interpolation=cv2.INTER_AREA)
328
 
329
  res = processor.process_image(img, file.filename, scale_ratio)
330
 
331
+ del img, nparr, contents
332
  gc.collect()
333
 
334
  return res.__dict__