Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
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.
|
| 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 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
| 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)")
|