smiler488 commited on
Commit
1c2a6a6
·
verified ·
1 Parent(s): 90297ca

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -97,7 +97,7 @@ def segment(img_bgr, sample_type, hsv_low_h, hsv_high_h, color_tol, min_area_px,
97
  continue
98
  rect = cv2.minAreaRect(cnt)
99
  box = cv2.boxPoints(rect)
100
- box = np.int0(box)
101
  m = cv2.moments(cnt)
102
  if m["m00"] == 0:
103
  cx, cy = 0, 0
@@ -192,27 +192,30 @@ def render_overlay(img_bgr, px_per_mm, ref, comps, df):
192
  return cv2.cvtColor(out, cv2.COLOR_BGR2RGB)
193
 
194
  def analyze(image, sample_type, expected_count, ref_mode, ref_size_mm, min_area_px, max_area_px, color_tol, hsv_low_h, hsv_high_h):
195
- if image is None:
196
- return None, pd.DataFrame(), None, []
197
- img_rgb = np.array(image)
198
- img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)
199
- img_bgr, scale = downscale_bgr(img_bgr)
200
- px_per_mm, ref_center, ref_type = detect_reference(img_bgr, ref_mode, ref_size_mm)
201
- comps = segment(img_bgr, sample_type, hsv_low_h, hsv_high_h, color_tol, min_area_px, max_area_px)
202
- if sample_type == "leaves":
203
- comps.sort(key=lambda c: c["center"][0])
204
- else:
205
- comps.sort(key=lambda c: c["center"][1] * 0.3 + c["center"][0] * 0.7)
206
- if expected_count and expected_count > 0:
207
- comps = comps[:int(expected_count)]
208
- df = compute_metrics(img_bgr, comps, px_per_mm)
209
- overlay = render_overlay(img_bgr.copy(), px_per_mm, (ref_center, ref_type), comps, df)
210
- csv = df.to_csv(index=False)
211
- tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
212
- tmp.write(csv.encode("utf-8"))
213
- tmp.close()
214
- js = df.to_dict(orient="records")
215
- return overlay, df, tmp.name, js
 
 
 
216
 
217
  with gr.Blocks(theme=gr.themes.Default()) as demo:
218
  gr.Markdown("# Biological Sample Quantifier (Leaves / Seeds)")
 
97
  continue
98
  rect = cv2.minAreaRect(cnt)
99
  box = cv2.boxPoints(rect)
100
+ box = box.astype(np.int32)
101
  m = cv2.moments(cnt)
102
  if m["m00"] == 0:
103
  cx, cy = 0, 0
 
192
  return cv2.cvtColor(out, cv2.COLOR_BGR2RGB)
193
 
194
  def analyze(image, sample_type, expected_count, ref_mode, ref_size_mm, min_area_px, max_area_px, color_tol, hsv_low_h, hsv_high_h):
195
+ try:
196
+ if image is None:
197
+ return None, pd.DataFrame(), None, []
198
+ img_rgb = np.array(image)
199
+ img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)
200
+ img_bgr, scale = downscale_bgr(img_bgr)
201
+ px_per_mm, ref_center, ref_type = detect_reference(img_bgr, ref_mode, ref_size_mm)
202
+ comps = segment(img_bgr, sample_type, hsv_low_h, hsv_high_h, color_tol, min_area_px, max_area_px)
203
+ if sample_type == "leaves":
204
+ comps.sort(key=lambda c: c["center"][0])
205
+ else:
206
+ comps.sort(key=lambda c: c["center"][1] * 0.3 + c["center"][0] * 0.7)
207
+ if expected_count and expected_count > 0:
208
+ comps = comps[:int(expected_count)]
209
+ df = compute_metrics(img_bgr, comps, px_per_mm)
210
+ overlay = render_overlay(img_bgr.copy(), px_per_mm, (ref_center, ref_type), comps, df)
211
+ csv = df.to_csv(index=False)
212
+ tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
213
+ tmp.write(csv.encode("utf-8"))
214
+ tmp.close()
215
+ js = df.to_dict(orient="records")
216
+ return overlay, df, tmp.name, js
217
+ except Exception as e:
218
+ return None, pd.DataFrame(), None, [{"error": str(e)}]
219
 
220
  with gr.Blocks(theme=gr.themes.Default()) as demo:
221
  gr.Markdown("# Biological Sample Quantifier (Leaves / Seeds)")