Rida commited on
Commit
e0a1986
·
1 Parent(s): a04b5c2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Data Handling
2
+ import numpy as np
3
+ import cv2
4
+ import imutils
5
+ import tensorflow as tf
6
+ from tensorflow import keras
7
+ import gradio as gr
8
+ from tensorflow.keras.models import load_model
9
+
10
+ model = load_model('./augmented_unet_pretrained.h5', compile=False)
11
+
12
+ def segmentation(inp):
13
+
14
+ #inp = cv2.cvtColor(inp, cv2.COLOR_BGR2RGB) # Input image
15
+ inp = cv2.resize(inp, (256, 256)) # Resize
16
+ inp = (inp.astype('float32')) / 255.
17
+ test_input = inp
18
+ # (Must Add cropping for real time images)
19
+
20
+ # Predictions
21
+ prediction_on_test = np.expand_dims(test_input, 0)
22
+ prediction_on_test = model.predict(prediction_on_test)
23
+ prediction_on_test = prediction_on_test > 0.5
24
+ predicted_img = prediction_on_test[0,:,:,0]
25
+
26
+ # EXTRACTING CONTOURS
27
+
28
+ predicted = predicted_img.astype(np.uint8)
29
+ cnts = cv2.findContours(image=predicted, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
30
+ contours = imutils.grab_contours(cnts)
31
+ contoured = test_input.copy()
32
+ contoured = (contoured * 255).astype(np.uint8)
33
+ cv2.drawContours(image=contoured, contours=contours, contourIdx=-1, color=(255, 0, 0), thickness=1, lineType=cv2.LINE_AA)
34
+
35
+
36
+ # Circumference of detected Mask
37
+ if contours :
38
+ a = "Polynya Detected"
39
+ for i in range(len(contours)):
40
+ circum = cv2.arcLength(contours[i], True)
41
+ circum = round(circum,2)
42
+ b = str(circum) + '\t' + "px"
43
+ else:
44
+ #a = print("No Polynya Detected")
45
+ a = "No Polynya Detected"
46
+ #b = print(f"Circumference of Polynya : 0.0 px")
47
+ b = "0.0 px"
48
+
49
+ return(contoured, a, b)
50
+
51
+ image = gr.Image(label = 'Input Image')
52
+ out1 = gr.Image(label = 'Result')
53
+ out2 = gr.Textbox(label = 'Label')
54
+ out3 = gr.Textbox(label = 'Circumference in Pixel Unit')
55
+
56
+ interface = gr.Interface(fn = segmentation, inputs = image, outputs = [out1, out2, out3],
57
+ title= 'Detection Of Polynya with Artificial Intelligence')
58
+
59
+ interface.launch()