Commit ·
e79863e
1
Parent(s): 0f67bbb
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,10 +30,36 @@ def segmentImage(img_path):
|
|
| 30 |
img = cv2.dilate(img, kernel, iterations=1)
|
| 31 |
img = ndimage.binary_fill_holes(img).astype(int)
|
| 32 |
labels, nlabels = ndimage.label(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
colors = np.random.randint(0, 255, (nlabels + 1, 3))
|
| 34 |
colors[0] = 0
|
| 35 |
img_color = colors[labels]
|
| 36 |
-
return img_color
|
| 37 |
|
| 38 |
def predict_segmentation(img):
|
| 39 |
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
@@ -45,11 +71,11 @@ def predict_segmentation(img):
|
|
| 45 |
temp_file = 'temp.png'
|
| 46 |
output_image.save(temp_file)
|
| 47 |
# Call the segmentImage function
|
| 48 |
-
segmented_image = segmentImage(temp_file)
|
| 49 |
-
return output_image, segmented_image
|
| 50 |
|
| 51 |
input_image = gr.inputs.Image()
|
| 52 |
output_image1 = gr.outputs.Image(type='pil')
|
| 53 |
output_image2 = gr.outputs.Image(type='pil')
|
| 54 |
-
app = gr.Interface(fn=predict_segmentation, inputs=input_image, outputs=[output_image1, output_image2], title='Microstructure Segmentation', description='Segment the input image into grain and background.')
|
| 55 |
app.launch()
|
|
|
|
| 30 |
img = cv2.dilate(img, kernel, iterations=1)
|
| 31 |
img = ndimage.binary_fill_holes(img).astype(int)
|
| 32 |
labels, nlabels = ndimage.label(img)
|
| 33 |
+
|
| 34 |
+
# Get grain sizes
|
| 35 |
+
sizes = ndimage.sum(img, labels, range(nlabels + 1))
|
| 36 |
+
scale_factor = 3072 / 1152
|
| 37 |
+
c = 0.4228320313
|
| 38 |
+
# Divide sizes by pixel_to_micrometer to get the sizes in micrometers and store them in a list new_sizes
|
| 39 |
+
new_sizes = [size * scale_factor * scale_factor * c * c for size in sizes]
|
| 40 |
+
# Round the grain sizes to 2 decimal places
|
| 41 |
+
new_sizes = [round(size, 2) for size in new_sizes]
|
| 42 |
+
gradient_img = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
|
| 43 |
+
colors = []
|
| 44 |
+
for i in range(len(new_sizes)):
|
| 45 |
+
if new_sizes[i] < 250 * c * c:
|
| 46 |
+
colors.append((255, 255, 255))
|
| 47 |
+
elif new_sizes[i] < 7500 * c * c:
|
| 48 |
+
colors.append((2, 106, 248))
|
| 49 |
+
elif new_sizes[i] < 20000 * c * c:
|
| 50 |
+
colors.append((0, 255, 107))
|
| 51 |
+
elif new_sizes[i] < 45000 * c * c:
|
| 52 |
+
colors.append((255, 201, 60))
|
| 53 |
+
else:
|
| 54 |
+
colors.append((255, 0, 0))
|
| 55 |
+
for i in range(img.shape[0]):
|
| 56 |
+
for j in range(img.shape[1]):
|
| 57 |
+
if labels[i][j] != 0:
|
| 58 |
+
gradient_img[i][j] = colors[labels[i][j]]
|
| 59 |
colors = np.random.randint(0, 255, (nlabels + 1, 3))
|
| 60 |
colors[0] = 0
|
| 61 |
img_color = colors[labels]
|
| 62 |
+
return img_color, gradient_img
|
| 63 |
|
| 64 |
def predict_segmentation(img):
|
| 65 |
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 71 |
temp_file = 'temp.png'
|
| 72 |
output_image.save(temp_file)
|
| 73 |
# Call the segmentImage function
|
| 74 |
+
segmented_image, gradient_image = segmentImage(temp_file)
|
| 75 |
+
return output_image, segmented_image, gradient_image
|
| 76 |
|
| 77 |
input_image = gr.inputs.Image()
|
| 78 |
output_image1 = gr.outputs.Image(type='pil')
|
| 79 |
output_image2 = gr.outputs.Image(type='pil')
|
| 80 |
+
app = gr.Interface(fn=predict_segmentation, inputs=input_image, outputs=[output_image1, output_image2, output_image3], title='Microstructure Segmentation', description='Segment the input image into grain and background.')
|
| 81 |
app.launch()
|