espejelomar commited on
Commit
f2beadf
1 Parent(s): a5b1053

agregar app y req

Browse files
Files changed (2) hide show
  1. app.py +105 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 usa UNet para segmentar imágenes
12
+ de dientes 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
+ El modelo fue creado por [SerdarHelli](https://huggingface.co/SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net).
16
+ """
17
+ )
18
+
19
+ ## Seleccionamos y cargamos el modelo
20
+ model_id = "SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net"
21
+ model = from_pretrained_keras(model_id)
22
+
23
+ ## Permitimos a la usuaria cargar una imagen
24
+ archivo_imagen = st.file_uploader("Sube aquí tu imagen.", type=["png", "jpg", "jpeg"])
25
+
26
+ ## Si una imagen tiene más de un canal entonces se convierte a escala de grises (1 canal)
27
+ def convertir_one_channel(img):
28
+ if len(img.shape) > 2:
29
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
30
+ return img
31
+ else:
32
+ return img
33
+
34
+
35
+ def convertir_rgb(img):
36
+ if len(img.shape) == 2:
37
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
38
+ return img
39
+ else:
40
+ return img
41
+
42
+
43
+ ## Manipularemos la interfaz para que podamos usar imágenes ejemplo
44
+ ## Si el usuario da click en un ejemplo entonces el modelo correrá con él
45
+ ejemplos = ["dientes_1.png", "dientes_2.png", "dientes_3.png"]
46
+
47
+ ## Creamos tres columnas; en cada una estará una imagen ejemplo
48
+ col1, col2, col3 = st.columns(3)
49
+ with col1:
50
+ ## Se carga la imagen y se muestra en la interfaz
51
+ ex = Image.open(ejemplos[0])
52
+ st.image(ex, width=200)
53
+ ## Si oprime el botón entonces usaremos ese ejemplo en el modelo
54
+ if st.button("Corre este ejemplo 1"):
55
+ archivo_imagen = ejemplos[0]
56
+
57
+ with col2:
58
+ ex1 = Image.open(ejemplos[1])
59
+ st.image(ex1, width=200)
60
+ if st.button("Corre este ejemplo 2"):
61
+ archivo_imagen = ejemplos[1]
62
+
63
+ with col3:
64
+ ex2 = Image.open(ejemplos[2])
65
+ st.image(ex2, width=200)
66
+ if st.button("Corre este ejemplo 3"):
67
+ archivo_imagen = ejemplos[2]
68
+
69
+ ## Si tenemos una imagen para ingresar en el modelo entonces
70
+ ## la procesamos e ingresamos al modelo
71
+ if archivo_imagen is not None:
72
+ ## Cargamos la imagen con PIL, la mostramos y la convertimos a un array de NumPy
73
+ img = Image.open(archivo_imagen)
74
+ st.image(img, width=850)
75
+ img = np.asarray(img)
76
+
77
+ ## Procesamos la imagen para ingresarla al modelo
78
+ img_cv = convertir_one_channel(img)
79
+ img_cv = cv2.resize(img_cv, (512, 512), interpolation=cv2.INTER_LANCZOS4)
80
+ img_cv = np.float32(img_cv / 255)
81
+ img_cv = np.reshape(img_cv, (1, 512, 512, 1))
82
+
83
+ ## Ingresamos el array de NumPy al modelo
84
+ predicted = model.predict(img_cv)
85
+ predicted = predicted[0]
86
+
87
+ ## Regresamos la imagen a su forma original y agregamos las máscaras de la segmentación
88
+ predicted = cv2.resize(
89
+ predicted, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_LANCZOS4
90
+ )
91
+ mask = np.uint8(predicted * 255) #
92
+ _, mask = cv2.threshold(
93
+ mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY + cv2.THRESH_OTSU
94
+ )
95
+ kernel = np.ones((5, 5), dtype=np.float32)
96
+ mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1)
97
+ mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)
98
+ cnts, hieararch = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
99
+ output = cv2.drawContours(convertir_one_channel(img), cnts, -1, (255, 0, 0), 3)
100
+
101
+ ## Si obtuvimos exitosamente un resultadod entonces lo mostramos en la interfaz
102
+ if output is not None:
103
+ st.subheader("Segmentación:")
104
+ st.write(output.shape)
105
+ st.image(output, width=850)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
1
+ numpy
2
+ Pillow
3
+ scipy
4
+ opencv-python
5
+ tensorflow