Nada2001/streamlitSegmentation
0
1import streamlit as st2 3import tensorflow as tf4from PIL import Image5import numpy as np6import cv27 8 9 10from huggingface_hub import from_pretrained_keras11try:12 model=from_pretrained_keras("Nada2001/streamlitSegmentation")13except:14 model=tf.keras.models.load_model('/content/drive/MyDrive/dataX-ray_modelH5/UNet data xray_model3.h5')15 pass16 17st.header("Segmentation of Teeth in Panoramic X-ray Image Using UNet")18 19examples=["1.jpg","2.jpg","3.jpg"]20 21def load_image(image_file):22 img = Image.open(image_file)23 return img24 25def convert_one_channel(img):26 #some images have 3 channels , although they are grayscale image27 if len(img.shape)>2:28 img= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)29 return img30 else:31 return img32 33def convert_rgb(img):34 #some images have 3 channels , although they are grayscale image35 if len(img.shape)==2:36 img= cv2.cvtColor(img,cv2.COLOR_GRAY2RGB) 37 return img38 else:39 return img40 41 42st.subheader("Upload Dental Panoramic X-ray Image Image")43image_file = st.file_uploader("Upload Images", type=["png","jpg","jpeg"])44 45 46col1, col2, col3 = st.columns(3)47with col1:48 ex=load_image(examples[0])49 st.image(ex,width=200)50 if st.button('Example 1'):51 image_file=examples[0]52 53with col2:54 ex1=load_image(examples[1])55 st.image(ex1,width=200)56 if st.button('Example 2'):57 image_file=examples[1]58 59 60with col3:61 ex2=load_image(examples[2])62 st.image(ex2,width=200)63 if st.button('Example 3'):64 image_file=examples[2]65 66 67if image_file is not None:68 69 img=load_image(image_file)70 71 st.text("Making A Prediction ....")72 st.image(img,width=850)73 74 img=np.asarray(img)75 76 img_cv=convert_one_channel(img)77 img_cv=cv2.resize(img_cv,(512,512), interpolation=cv2.INTER_LANCZOS4)78 img_cv=np.float32(img_cv/255)79 80 img_cv=np.reshape(img_cv,(1,512,512,1))81 prediction=model.predict(img_cv)82 predicted=prediction[0]83 predicted = cv2.resize(predicted, (img.shape[1],img.shape[0]), interpolation=cv2.INTER_LANCZOS4)84 mask=np.uint8(predicted*255)# 85 _, mask = cv2.threshold(mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY+cv2.THRESH_OTSU)86 kernel =( np.ones((5,5), dtype=np.float32))87 mask=cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel,iterations=1 ) 88 mask=cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel,iterations=1 )89 cnts,hieararch=cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)90 output = cv2.drawContours(convert_rgb(img), cnts, -1, (255, 0, 0) , 3)91 92 93 if output is not None : 94 st.subheader("Predicted Image") 95 st.write(output.shape)96 st.image(output,width=850)97 98 st.text("DONE ! ....")99 