Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -69,6 +69,8 @@ def load_model(model_path):
|
|
| 69 |
|
| 70 |
model = load_model("segmentation_model_final.pth")
|
| 71 |
|
|
|
|
|
|
|
| 72 |
def fig_to_image(fig):
|
| 73 |
buf = BytesIO()
|
| 74 |
canvas = FigureCanvas(fig)
|
|
@@ -134,7 +136,7 @@ def analyze(image):
|
|
| 134 |
elif length <= thresholds[2]: color = colors[2]
|
| 135 |
elif length <= thresholds[3]: color = colors[3]
|
| 136 |
else: color = colors[4]
|
| 137 |
-
cv2.drawContours(color_mask, [box], 0, color,
|
| 138 |
|
| 139 |
fig2 = plt.figure(figsize=(6, 6))
|
| 140 |
plt.imshow(cv2.cvtColor(color_mask, cv2.COLOR_BGR2RGB))
|
|
@@ -159,8 +161,6 @@ def analyze(image):
|
|
| 159 |
dist_px = np.linalg.norm(pt1 - pt2)
|
| 160 |
dist_mm = dist_px * pixel_length_mm
|
| 161 |
edge_lengths.append(dist_mm)
|
| 162 |
-
midpoint = (pt1 + pt2) / 2
|
| 163 |
-
plt.text(midpoint[1], midpoint[0], f"{dist_mm:.1f}", color="blue", fontsize=6, ha="center")
|
| 164 |
plt.title("Delaunay Triangulation")
|
| 165 |
else:
|
| 166 |
plt.title("Not Enough Aggregates for Triangulation")
|
|
@@ -181,6 +181,21 @@ def analyze(image):
|
|
| 181 |
else:
|
| 182 |
avg_feret_length_mm = avg_feret_width_mm = max_feret_length_mm = roundness_aggregate = 0
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
summary = f"""📏 **Measurements Summary**:
|
| 185 |
- Pixel Size: `{pixel_length_mm:.4f}` mm/pixel
|
| 186 |
- Aggregate Area: `{aggregate_area_mm2:.2f}` mm²
|
|
|
|
| 69 |
|
| 70 |
model = load_model("segmentation_model_final.pth")
|
| 71 |
|
| 72 |
+
csv_output_path = "measurement_summary.csv"
|
| 73 |
+
|
| 74 |
def fig_to_image(fig):
|
| 75 |
buf = BytesIO()
|
| 76 |
canvas = FigureCanvas(fig)
|
|
|
|
| 136 |
elif length <= thresholds[2]: color = colors[2]
|
| 137 |
elif length <= thresholds[3]: color = colors[3]
|
| 138 |
else: color = colors[4]
|
| 139 |
+
cv2.drawContours(color_mask, [box], 0, color, 5)
|
| 140 |
|
| 141 |
fig2 = plt.figure(figsize=(6, 6))
|
| 142 |
plt.imshow(cv2.cvtColor(color_mask, cv2.COLOR_BGR2RGB))
|
|
|
|
| 161 |
dist_px = np.linalg.norm(pt1 - pt2)
|
| 162 |
dist_mm = dist_px * pixel_length_mm
|
| 163 |
edge_lengths.append(dist_mm)
|
|
|
|
|
|
|
| 164 |
plt.title("Delaunay Triangulation")
|
| 165 |
else:
|
| 166 |
plt.title("Not Enough Aggregates for Triangulation")
|
|
|
|
| 181 |
else:
|
| 182 |
avg_feret_length_mm = avg_feret_width_mm = max_feret_length_mm = roundness_aggregate = 0
|
| 183 |
|
| 184 |
+
# Save to CSV
|
| 185 |
+
data = {
|
| 186 |
+
"Pixel_Size_mm_per_pixel": [pixel_length_mm],
|
| 187 |
+
"Aggregate_Area_mm2": [aggregate_area_mm2],
|
| 188 |
+
"Aggregate_Ratio": [aggregate_ratio],
|
| 189 |
+
"Avg_Length_mm": [avg_feret_length_mm],
|
| 190 |
+
"Avg_Width_mm": [avg_feret_width_mm],
|
| 191 |
+
"Max_Length_mm": [max_feret_length_mm],
|
| 192 |
+
"Roundness": [roundness_aggregate],
|
| 193 |
+
"Avg_Dist_mm": [np.mean(edge_lengths) if edge_lengths else 0],
|
| 194 |
+
"Max_Dist_mm": [np.max(edge_lengths) if edge_lengths else 0]
|
| 195 |
+
}
|
| 196 |
+
df = pd.DataFrame(data)
|
| 197 |
+
df.to_csv(csv_output_path, index=False)
|
| 198 |
+
|
| 199 |
summary = f"""📏 **Measurements Summary**:
|
| 200 |
- Pixel Size: `{pixel_length_mm:.4f}` mm/pixel
|
| 201 |
- Aggregate Area: `{aggregate_area_mm2:.2f}` mm²
|