teleware commited on
Commit
a8b8e57
1 Parent(s): cb6ce15

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import numpy as np
4
+ import cv2
5
+ from huggingface_hub import from_pretrained_keras
6
+
7
+ st.header("Segmentación de dientes con rayos X")
8
+
9
+ st.markdown('''
10
+
11
+ Hola estudiantes de Platzi 🚀. Este modelo usan UNet para segmentar imágenes
12
+ de dientos en rayos X. Se utila un modelo de Keras importado con la función
13
+ `huggingface_hub.from_pretrained_keras`. Recuerda que el Hub de Hugging Face está integrado
14
+ con muchas librerías como Keras, scikit-learn, fastai y otras.
15
+
16
+ El modelo fue creado por [SerdarHelli](https://huggingface.co/SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net).
17
+
18
+ ''')
19
+
20
+ model_id = "SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net"
21
+ model=from_pretrained_keras(model_id)
22
+
23
+ ## Si una imagen tiene más de un canal entonces se convierte a escala de grises (1 canal)
24
+ def convertir_one_channel(img):
25
+ if len(img.shape)>2:
26
+ img= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
27
+ return img
28
+ else:
29
+ return img
30
+
31
+ def convertir_rgb(img):
32
+ if len(img.shape)==2:
33
+ img= cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
34
+ return img
35
+ else:
36
+ return img
37
+
38
+
39
+ image_file = st.file_uploader("Sube aquí tu imagen.", type=["png","jpg","jpeg"])
40
+
41
+
42
+ if image_file is not None:
43
+
44
+ img= Image.open(image_file)
45
+
46
+ st.image(img,width=850)
47
+
48
+ img=np.asarray(img)
49
+
50
+ img_cv=convertir_one_channel(img)
51
+ img_cv=cv2.resize(img_cv,(512,512), interpolation=cv2.INTER_LANCZOS4)
52
+ img_cv=np.float32(img_cv/255)
53
+
54
+ img_cv=np.reshape(img_cv,(1,512,512,1))
55
+ prediction=model.predict(img_cv)
56
+ predicted=prediction[0]
57
+ predicted = cv2.resize(predicted, (img.shape[1],img.shape[0]), interpolation=cv2.INTER_LANCZOS4)
58
+ mask=np.uint8(predicted*255)#
59
+ _, mask = cv2.threshold(mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY+cv2.THRESH_OTSU)
60
+ kernel =( np.ones((5,5), dtype=np.float32))
61
+ mask=cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel,iterations=1 )
62
+ mask=cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel,iterations=1 )
63
+ cnts,hieararch=cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
64
+ output = cv2.drawContours(convertir_one_channel(img), cnts, -1, (255, 0, 0) , 3)
65
+
66
+
67
+ if output is not None :
68
+ st.subheader("Segmentación:")
69
+ st.write(output.shape)
70
+ st.image(output,width=850)