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