Rida/Semantic-Segmentation
1
1# Data Handling2from huggingface_hub import from_pretrained_keras3import numpy as np4import cv25import imutils6import tensorflow as tf7from tensorflow import keras8import gradio as gr9from tensorflow.keras.models import load_model10 11model = load_model('./augmented_unet_pretrained.h5', compile=False)12 13def segmentation(inp):14 15 #inp = cv2.cvtColor(inp, cv2.COLOR_BGR2RGB) # Input image16 inp = cv2.resize(inp, (256, 256)) # Resize17 inp = (inp.astype('float32')) / 255. 18 test_input = inp19 # (Must Add cropping for real time images)20 21 # Predictions22 prediction_on_test = np.expand_dims(test_input, 0)23 prediction_on_test = model.predict(prediction_on_test)24 prediction_on_test = prediction_on_test > 0.525 predicted_img = prediction_on_test[0,:,:,0] 26 27 # EXTRACTING CONTOURS28 29 predicted = predicted_img.astype(np.uint8)30 cnts = cv2.findContours(image=predicted, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)31 contours = imutils.grab_contours(cnts)32 contoured = test_input.copy()33 contoured = (contoured * 255).astype(np.uint8)34 cv2.drawContours(image=contoured, contours=contours, contourIdx=-1, color=(255, 0, 0), thickness=1, lineType=cv2.LINE_AA)35 36 37 # Circumference of detected Mask38 if contours :39 a = "Polynya Detected"40 for i in range(len(contours)):41 circum = cv2.arcLength(contours[i], True)42 circum = round(circum,2)43 b = str(circum) + '\t' + "px"44 else:45 a = "No Polynya Detected"46 b = "0.0 px"47 48 return(contoured, a, b)49 50image = gr.Image(label = 'Input Image')51out1 = gr.Image(label = 'Result')52out2 = gr.Textbox(label = 'Label')53out3 = gr.Textbox(label = 'Circumference in Pixel Unit')54 55interface = gr.Interface(fn = segmentation, inputs = image, outputs = [out1, out2, out3], 56 title= 'Polynya Detection',57 description= 'Let the system detect if there is a Polynya in your image or not.', 58 share=True)59 60interface.launch()