dhanushreddy29 commited on
Commit
a9d976a
1 Parent(s): e79863e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -16
app.py CHANGED
@@ -5,19 +5,29 @@ import gradio as gr
5
  from scipy import ndimage
6
 
7
  fnames = get_image_files("./albumentations/original")
8
- def label_func(fn): return "./albumentations/labelled/"f"{fn.stem}.png"
9
- codes = np.loadtxt('labels.txt', dtype=str)
 
 
 
 
 
10
  w, h = 768, 1152
11
- img_size = (w,h)
12
- im_size = (h,w)
13
 
14
  dls = SegmentationDataLoaders.from_label_func(
15
- ".", bs=3, fnames = fnames, label_func = label_func, codes = codes,
16
- item_tfms=Resize(img_size)
 
 
 
 
17
  )
18
 
19
  learn = unet_learner(dls, resnet34)
20
- learn.load('learn')
 
21
 
22
  def segmentImage(img_path):
23
  img = cv2.imread(img_path, 0)
@@ -25,7 +35,7 @@ def segmentImage(img_path):
25
  for j in range(img.shape[1]):
26
  if img[i][j] > 0:
27
  img[i][j] = 1
28
- kernel = np.ones((3,3), np.uint8)
29
  img = cv2.erode(img, kernel, iterations=1)
30
  img = cv2.dilate(img, kernel, iterations=1)
31
  img = ndimage.binary_fill_holes(img).astype(int)
@@ -56,10 +66,21 @@ def segmentImage(img_path):
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)
@@ -68,14 +89,23 @@ def predict_segmentation(img):
68
  scaled_pred = (pred[0].numpy() * 255).astype(np.uint8)
69
  output_image = PILImage.create(scaled_pred)
70
  # Save the image to a temporary file
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()
 
 
 
 
 
 
 
 
 
5
  from scipy import ndimage
6
 
7
  fnames = get_image_files("./albumentations/original")
8
+
9
+
10
+ def label_func(fn):
11
+ return "./albumentations/labelled/" f"{fn.stem}.png"
12
+
13
+
14
+ codes = np.loadtxt("labels.txt", dtype=str)
15
  w, h = 768, 1152
16
+ img_size = (w, h)
17
+ im_size = (h, w)
18
 
19
  dls = SegmentationDataLoaders.from_label_func(
20
+ ".",
21
+ bs=3,
22
+ fnames=fnames,
23
+ label_func=label_func,
24
+ codes=codes,
25
+ item_tfms=Resize(img_size),
26
  )
27
 
28
  learn = unet_learner(dls, resnet34)
29
+ learn.load("learn")
30
+
31
 
32
  def segmentImage(img_path):
33
  img = cv2.imread(img_path, 0)
 
35
  for j in range(img.shape[1]):
36
  if img[i][j] > 0:
37
  img[i][j] = 1
38
+ kernel = np.ones((3, 3), np.uint8)
39
  img = cv2.erode(img, kernel, iterations=1)
40
  img = cv2.dilate(img, kernel, iterations=1)
41
  img = ndimage.binary_fill_holes(img).astype(int)
 
66
  for j in range(img.shape[1]):
67
  if labels[i][j] != 0:
68
  gradient_img[i][j] = colors[labels[i][j]]
69
+ Sum = 0
70
+ count = 0
71
+ for i in range(len(new_sizes)):
72
+ if new_sizes[i] > 250 * c * c:
73
+ Sum += new_sizes[i]
74
+ count += 1
75
  colors = np.random.randint(0, 255, (nlabels + 1, 3))
76
  colors[0] = 0
77
  img_color = colors[labels]
78
+ return (
79
+ img_color,
80
+ gradient_img,
81
+ "Average Area of grains: " + str(Sum / count) + " µm^2",
82
+ )
83
+
84
 
85
  def predict_segmentation(img):
86
  gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
89
  scaled_pred = (pred[0].numpy() * 255).astype(np.uint8)
90
  output_image = PILImage.create(scaled_pred)
91
  # Save the image to a temporary file
92
+ temp_file = "temp.png"
93
  output_image.save(temp_file)
94
  # Call the segmentImage function
95
+ segmented_image, gradient_image, avg_area = segmentImage(temp_file)
96
+ return output_image, segmented_image, gradient_image, avg_area
97
+
98
 
99
  input_image = gr.inputs.Image()
100
+ output_image1 = gr.outputs.Image(type="pil")
101
+ output_image2 = gr.outputs.Image(type="pil")
102
+ output_image3 = gr.outputs.Image(type="pil")
103
+ output_image4 = gr.outputs.Textbox()
104
+ app = gr.Interface(
105
+ fn=predict_segmentation,
106
+ inputs=input_image,
107
+ outputs=[output_image1, output_image2, output_image3, output_image4],
108
+ title="Microstructure Segmentation",
109
+ description="Segment the input image into grain and background.",
110
+ )
111
+ app.launch()