dhanushreddy29 commited on
Commit
fab2111
1 Parent(s): b1f3d7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -16
app.py CHANGED
@@ -1,23 +1,38 @@
1
- import pickle
2
  import cv2
3
  from fastai.vision.all import *
4
  import gradio as gr
5
 
6
- im_size = (1152, 768)
7
- with open("learn.pkl", 'rb') as f:
8
- model = pickle.load(f)
 
 
 
9
 
10
- def predict_mask(file):
11
- img = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_GRAYSCALE)
12
- img = cv2.resize(img, im_size)
13
- pred = model.predict(img)
14
- return pred[0]
15
 
16
- iface = gr.Interface(fn=predict_mask,
17
- inputs=gr.inputs.Image(label="Upload Image"),
18
- outputs="image",
19
- capture_session=True,
20
- interpretation="default")
21
 
22
- if __name__ == "__main__":
23
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import cv2
2
  from fastai.vision.all import *
3
  import gradio as gr
4
 
5
+ fnames = get_image_files("./albumentations/original")
6
+ def label_func(fn): return "./albumentations/labelled/"f"{fn.stem}.png"
7
+ codes = np.loadtxt('labels.txt', dtype=str)
8
+ w, h = 768, 1152
9
+ img_size = (w,h)
10
+ im_size = (h,w)
11
 
12
+ dls = SegmentationDataLoaders.from_label_func(
13
+ ".", bs=3, fnames = fnames, label_func = label_func, codes = codes,
14
+ item_tfms=Resize(img_size)
15
+ )
 
16
 
17
+ learn = unet_learner(dls, resnet34)
18
+ learn.load('learn')
 
 
 
19
 
20
+ def predict_segmentation(img):
21
+ # Convert the input image to grayscale
22
+ gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
23
+ # Resize the image to the size of the training images
24
+ resized_img = cv2.resize(gray_img, im_size)
25
+ # Predict the segmentation mask
26
+ pred = learn.predict(resized_img)
27
+ # Convert the predicted mask to a color image
28
+ color_pred = pred[0].show(ctx=None, cmap='gray', alpha=None)
29
+ # Convert the color image to a numpy array
30
+ color_pred_array = np.array(color_pred)
31
+ # Convert the numpy array back to a PIL image
32
+ output_image = Image.fromarray(color_pred_array)
33
+ return output_image
34
+
35
+ input_image = gr.inputs.Image()
36
+ output_image = gr.outputs.Image()
37
+ app = gr.Interface(fn=predict_segmentation, inputs=input_image, outputs=output_image, title='Microstructure Segmentation', description='Segment the input image into grain and background.')
38
+ app.launch()