Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,221 +1,164 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
from PIL import Image
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
if
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
pt1, pt2 = centroids[simplex[i]], centroids[simplex[(i + 1) % 3]]
|
| 166 |
-
dist_mm = np.linalg.norm(pt1 - pt2) * pixel_length_mm
|
| 167 |
-
edge_lengths.append(dist_mm)
|
| 168 |
-
midpoint = (pt1 + pt2) / 2
|
| 169 |
-
plt.text(midpoint[1], midpoint[0], f"{dist_mm:.1f}", color='blue', fontsize=6, ha='center')
|
| 170 |
-
plt.title("Delaunay Triangulation")
|
| 171 |
-
plt.axis('off')
|
| 172 |
-
figs.append(fig3)
|
| 173 |
-
|
| 174 |
-
# Summary Stats
|
| 175 |
-
area_mask = np.sum(binary_mask > 0)
|
| 176 |
-
area_gray = np.count_nonzero(gray_img)
|
| 177 |
-
aggregate_area_mm2 = area_mask * (pixel_length_mm ** 2)
|
| 178 |
-
total_area_mm2 = area_gray * (pixel_length_mm ** 2)
|
| 179 |
-
aggregate_ratio = aggregate_area_mm2 / total_area_mm2 if total_area_mm2 > 0 else 0
|
| 180 |
-
|
| 181 |
-
if feret_lengths:
|
| 182 |
-
avg_feret_length_mm = np.mean(feret_lengths) * pixel_length_mm
|
| 183 |
-
avg_feret_width_mm = np.mean(feret_widths) * pixel_length_mm
|
| 184 |
-
max_feret_length_mm = np.max(feret_lengths) * pixel_length_mm
|
| 185 |
-
roundness_aggregate = avg_feret_length_mm / avg_feret_width_mm
|
| 186 |
-
else:
|
| 187 |
-
avg_feret_length_mm = avg_feret_width_mm = max_feret_length_mm = roundness_aggregate = 0
|
| 188 |
-
|
| 189 |
-
summary = f"""
|
| 190 |
-
→ Pixel Size: {pixel_length_mm:.4f} mm/pixel
|
| 191 |
-
→ Aggregate Area: {aggregate_area_mm2:.2f} mm²
|
| 192 |
-
→ Aggregate Ratio: {aggregate_ratio:.4f}
|
| 193 |
-
→ Avg Aggregate Length: {avg_feret_length_mm:.2f} mm
|
| 194 |
-
→ Avg Aggregate Width: {avg_feret_width_mm:.2f} mm
|
| 195 |
-
→ Max Aggregate Length: {max_feret_length_mm:.2f} mm
|
| 196 |
-
→ Avg Aggregate Roundness: {roundness_aggregate:.2f}
|
| 197 |
-
"""
|
| 198 |
-
if edge_lengths:
|
| 199 |
-
summary += f"""
|
| 200 |
-
→ Avg inter_Aggregate Distance: {np.mean(edge_lengths):.2f} mm
|
| 201 |
-
→ Max inter_Aggregate Distance: {np.max(edge_lengths):.2f} mm
|
| 202 |
-
"""
|
| 203 |
-
|
| 204 |
-
images = [fig_to_image(fig) for fig in figs]
|
| 205 |
-
return images[0], images[1], images[2] if len(images) > 2 else images[1], summary
|
| 206 |
-
|
| 207 |
-
iface = gr.Interface(
|
| 208 |
-
fn=predict,
|
| 209 |
-
inputs=[gr.Image(label="Upload Concrete Image")],
|
| 210 |
-
outputs=[
|
| 211 |
-
gr.Image(label="Boundary and Calibration Line"),
|
| 212 |
-
gr.Image(label="Feret Rectangles by Size"),
|
| 213 |
-
gr.Image(label="Delaunay Triangulation"),
|
| 214 |
-
gr.Textbox(label="Summary Measurements")
|
| 215 |
-
],
|
| 216 |
-
title="Concrete Aggregate Analysis App",
|
| 217 |
-
description="Upload a concrete cross-section image. The app segments aggregates, displays Feret rectangles, boundary calibration, Delaunay triangulation, and summary measurements."
|
| 218 |
-
)
|
| 219 |
-
|
| 220 |
-
if __name__ == "__main__":
|
| 221 |
-
iface.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from scipy.spatial.distance import cdist
|
| 7 |
+
from scipy.spatial import Delaunay
|
| 8 |
+
from skimage.measure import label, regionprops
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import io
|
| 11 |
+
from PIL import Image
|
| 12 |
+
|
| 13 |
+
# Constants
|
| 14 |
+
DIA_MM = 152.4
|
| 15 |
+
|
| 16 |
+
# Main processing function
|
| 17 |
+
def analyze_aggregate(image_pil):
|
| 18 |
+
results = {}
|
| 19 |
+
edge_lengths = []
|
| 20 |
+
|
| 21 |
+
# Convert to OpenCV image
|
| 22 |
+
img = np.array(image_pil)
|
| 23 |
+
img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 24 |
+
gray_img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
|
| 25 |
+
|
| 26 |
+
# Simulated label (as if predicted by a model)
|
| 27 |
+
label_img = cv2.cvtColor(gray_img, cv2.COLOR_GRAY2BGR) # Dummy label for placeholder
|
| 28 |
+
_, label_gray = cv2.threshold(gray_img, 127, 255, cv2.THRESH_BINARY)
|
| 29 |
+
binary_mask = (label_gray > 0).astype(np.uint8)
|
| 30 |
+
color_mask = cv2.cvtColor(label_gray, cv2.COLOR_GRAY2BGR)
|
| 31 |
+
|
| 32 |
+
# Pixel calibration
|
| 33 |
+
_, bw = cv2.threshold(gray_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 34 |
+
contours, _ = cv2.findContours(bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
|
| 35 |
+
contours = sorted(contours, key=cv2.contourArea, reverse=True)
|
| 36 |
+
if not contours:
|
| 37 |
+
return "No contours found.", None, None, None, None
|
| 38 |
+
|
| 39 |
+
boundary = contours[0].squeeze()
|
| 40 |
+
dist_matrix = cdist(boundary, boundary)
|
| 41 |
+
i, j = np.unravel_index(np.argmax(dist_matrix), dist_matrix.shape)
|
| 42 |
+
line_pts = np.array([boundary[i], boundary[j]])
|
| 43 |
+
pixel_diameter = np.linalg.norm(boundary[i] - boundary[j])
|
| 44 |
+
pixels_per_mm = pixel_diameter / DIA_MM
|
| 45 |
+
pixel_length_mm = 1 / pixels_per_mm
|
| 46 |
+
line_length_mm = pixel_diameter * pixel_length_mm
|
| 47 |
+
|
| 48 |
+
# Plot 1: Boundary and line
|
| 49 |
+
fig1, ax1 = plt.subplots(figsize=(6, 6))
|
| 50 |
+
ax1.imshow(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
|
| 51 |
+
ax1.plot(boundary[:, 0], boundary[:, 1], 'g', linewidth=2)
|
| 52 |
+
ax1.plot(line_pts[:, 0], line_pts[:, 1], 'r', linewidth=2)
|
| 53 |
+
ax1.set_title(f'Line Length: {line_length_mm:.2f} mm')
|
| 54 |
+
ax1.axis('off')
|
| 55 |
+
|
| 56 |
+
# Aggregate area
|
| 57 |
+
num_white_pixels = np.sum(binary_mask == 1)
|
| 58 |
+
num_nonblack_pixels = np.count_nonzero(gray_img)
|
| 59 |
+
aggregate_area_mm2 = num_white_pixels * (pixel_length_mm ** 2)
|
| 60 |
+
total_area_mm2 = num_nonblack_pixels * (pixel_length_mm ** 2)
|
| 61 |
+
aggregate_ratio = aggregate_area_mm2 / total_area_mm2 if total_area_mm2 > 0 else 0
|
| 62 |
+
|
| 63 |
+
# Feret Rectangles
|
| 64 |
+
feret_lengths, feret_widths = [], []
|
| 65 |
+
rectangles = []
|
| 66 |
+
contours_mask, _ = cv2.findContours(binary_mask * 255, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 67 |
+
for cnt in contours_mask:
|
| 68 |
+
if len(cnt) >= 5:
|
| 69 |
+
rect = cv2.minAreaRect(cnt)
|
| 70 |
+
box = cv2.boxPoints(rect).astype(np.intp)
|
| 71 |
+
width, height = rect[1]
|
| 72 |
+
feret_length = max(width, height)
|
| 73 |
+
feret_lengths.append(feret_length)
|
| 74 |
+
feret_widths.append(min(width, height))
|
| 75 |
+
rectangles.append((box, feret_length))
|
| 76 |
+
|
| 77 |
+
thresholds = np.percentile(feret_lengths, [20, 40, 60, 80]) if feret_lengths else [0, 0, 0, 0]
|
| 78 |
+
colors = [(0, 0, 255), (0, 128, 255), (0, 255, 255), (0, 255, 0), (255, 0, 0)]
|
| 79 |
+
|
| 80 |
+
for box, length in rectangles:
|
| 81 |
+
if length <= thresholds[0]: color = colors[0]
|
| 82 |
+
elif length <= thresholds[1]: color = colors[1]
|
| 83 |
+
elif length <= thresholds[2]: color = colors[2]
|
| 84 |
+
elif length <= thresholds[3]: color = colors[3]
|
| 85 |
+
else: color = colors[4]
|
| 86 |
+
cv2.drawContours(color_mask, [box], 0, color, 3)
|
| 87 |
+
|
| 88 |
+
# Plot 2: Feret rectangles
|
| 89 |
+
fig2, ax2 = plt.subplots(figsize=(6, 6))
|
| 90 |
+
ax2.imshow(cv2.cvtColor(color_mask, cv2.COLOR_BGR2RGB))
|
| 91 |
+
ax2.set_title("Feret Rectangles by Size")
|
| 92 |
+
ax2.axis('off')
|
| 93 |
+
|
| 94 |
+
# Feret Stats
|
| 95 |
+
if feret_lengths:
|
| 96 |
+
avg_feret_length_mm = np.mean(feret_lengths) * pixel_length_mm
|
| 97 |
+
avg_feret_width_mm = np.mean(feret_widths) * pixel_length_mm
|
| 98 |
+
max_feret_length_mm = np.max(feret_lengths) * pixel_length_mm
|
| 99 |
+
roundness_aggregate = avg_feret_length_mm / avg_feret_width_mm
|
| 100 |
+
else:
|
| 101 |
+
avg_feret_length_mm = avg_feret_width_mm = max_feret_length_mm = roundness_aggregate = 0
|
| 102 |
+
|
| 103 |
+
# Delaunay triangulation
|
| 104 |
+
labeled_img = label(binary_mask)
|
| 105 |
+
props = regionprops(labeled_img)
|
| 106 |
+
centroids = np.array([p.centroid for p in props])
|
| 107 |
+
|
| 108 |
+
if len(centroids) >= 3:
|
| 109 |
+
tri = Delaunay(centroids)
|
| 110 |
+
fig3, ax3 = plt.subplots(figsize=(6, 6))
|
| 111 |
+
ax3.imshow(label_gray, cmap='gray')
|
| 112 |
+
ax3.triplot(centroids[:, 1], centroids[:, 0], tri.simplices.copy(), color='red')
|
| 113 |
+
|
| 114 |
+
for simplex in tri.simplices:
|
| 115 |
+
for i in range(3):
|
| 116 |
+
pt1 = centroids[simplex[i]]
|
| 117 |
+
pt2 = centroids[(i + 1) % 3]
|
| 118 |
+
dist_px = np.linalg.norm(pt1 - pt2)
|
| 119 |
+
dist_mm = dist_px * pixel_length_mm
|
| 120 |
+
edge_lengths.append(dist_mm)
|
| 121 |
+
midpoint = (pt1 + pt2) / 2
|
| 122 |
+
ax3.text(midpoint[1], midpoint[0], f"{dist_mm:.1f}", color='blue', fontsize=6, ha='center')
|
| 123 |
+
|
| 124 |
+
ax3.set_title("Delaunay Triangulation")
|
| 125 |
+
ax3.axis('off')
|
| 126 |
+
else:
|
| 127 |
+
fig3 = plt.figure()
|
| 128 |
+
plt.text(0.5, 0.5, 'Not enough centroids for triangulation.', ha='center')
|
| 129 |
+
plt.axis('off')
|
| 130 |
+
|
| 131 |
+
# Summary text
|
| 132 |
+
summary = f"""
|
| 133 |
+
→ Pixel Size: {pixel_length_mm:.4f} mm/pixel
|
| 134 |
+
→ Aggregate Area: {aggregate_area_mm2:.2f} mm²
|
| 135 |
+
→ Aggregate Ratio: {aggregate_ratio:.4f}
|
| 136 |
+
→ Avg Aggregate Length: {avg_feret_length_mm:.2f} mm
|
| 137 |
+
→ Avg Aggregate Width: {avg_feret_width_mm:.2f} mm
|
| 138 |
+
→ Max Aggregate Length: {max_feret_length_mm:.2f} mm
|
| 139 |
+
→ Avg Aggregate Roundness: {roundness_aggregate:.2f}
|
| 140 |
+
"""
|
| 141 |
+
if edge_lengths:
|
| 142 |
+
summary += f"""
|
| 143 |
+
→ Avg inter-Aggregate Distance: {np.mean(edge_lengths):.2f} mm
|
| 144 |
+
→ Max inter-Aggregate Distance: {np.max(edge_lengths):.2f} mm
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
return summary.strip(), fig1, fig2, fig3
|
| 148 |
+
|
| 149 |
+
# Gradio UI
|
| 150 |
+
demo = gr.Interface(
|
| 151 |
+
fn=analyze_aggregate,
|
| 152 |
+
inputs=[gr.Image(label="Upload Image")],
|
| 153 |
+
outputs=[
|
| 154 |
+
gr.Textbox(label="Summary Measurements"),
|
| 155 |
+
gr.Plot(label="Boundary and Calibration Line"),
|
| 156 |
+
gr.Plot(label="Feret Rectangles by Size"),
|
| 157 |
+
gr.Plot(label="Delaunay Triangulation")
|
| 158 |
+
],
|
| 159 |
+
title="Aggregate Analysis from Uploaded Image",
|
| 160 |
+
description="Upload an image with circular calibration. The app will calculate size, aspect ratio, and spacing of aggregates.",
|
| 161 |
+
allow_flagging='never'
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|