EliseoBaquero commited on
Commit
42187ef
1 Parent(s): 8ff90d5

cargando app y req

Browse files
Files changed (2) hide show
  1. app.py +103 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("X-ray segmentation of teeth / Segmentación de dientes con rayos X")
8
+ st.subheader("Iteration to improve demo / Iteración para mejorar la demo")
9
+
10
+ st.markdown(
11
+ """
12
+ Demo for Platzi class / Demo para la clase de Platzi
13
+ """
14
+ )
15
+
16
+ ## Seleccionamos y cargamos el modelo
17
+ model_id = "SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net"
18
+ model = from_pretrained_keras(model_id)
19
+
20
+ ## Permitimos a la usuaria cargar una imagen
21
+ image_file = st.file_uploader("Upload your image here / Sube aquí tu imagen", type=["png", "jpg", "jpeg"])
22
+
23
+ ## Si una imagen tiene más de un canal entonces se convierte a escala de grises (1 canal)
24
+ def convert_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
+ ## Manipularemos la interfaz para que podamos usar imágenes ejemplo
40
+ ## Si el usuario da click en un ejemplo entonces el modelo correrá con él
41
+ examples = ["teeth_1.png", "teeth_2.png", "teeth_3.png"]
42
+
43
+ ## Creamos tres columnas; en cada una estará una imagen ejemplo
44
+ col1, col2, col3 = st.columns(3)
45
+ with col1:
46
+ ## Se carga la imagen y se muestra en la interfaz
47
+ ex = Image.open(examples[0])
48
+ st.image(ex, width=200)
49
+ ## Si oprime el botón entonces usaremos ese ejemplo en el modelo
50
+ if st.button("Run example 1 / Corre ejemplo 1"):
51
+ image_file = examples[0]
52
+
53
+ with col2:
54
+ ex1 = Image.open(examples[1])
55
+ st.image(ex1, width=200)
56
+ if st.button("Run example 2 / Corre ejemplo 2"):
57
+ image_file = examples[1]
58
+
59
+ with col3:
60
+ ex2 = Image.open(examples[2])
61
+ st.image(ex2, width=200)
62
+ if st.button("Run example 3 / Corre ejemplo 3"):
63
+ image_file = examples[2]
64
+
65
+ ## Si tenemos una imagen para ingresar en el modelo entonces
66
+ ## la procesamos e ingresamos al modelo
67
+ if image_file is not None:
68
+ ## Cargamos la imagen con PIL, la mostramos y la convertimos a un array de NumPy
69
+ img = Image.open(image_file)
70
+ st.image(img, width=850)
71
+ img = np.asarray(img)
72
+
73
+ ## Procesamos la imagen para ingresarla al modelo
74
+ img_cv = convert_one_channel(img)
75
+ img_cv = cv2.resize(img_cv, (512, 512), interpolation=cv2.INTER_LANCZOS4)
76
+ img_cv = np.float32(img_cv / 255)
77
+ img_cv = np.reshape(img_cv, (1, 512, 512, 1))
78
+
79
+ ## Ingresamos el array de NumPy al modelo
80
+ predicted = model.predict(img_cv)
81
+ predicted = predicted[0]
82
+
83
+ ## Regresamos la imagen a su forma original y agregamos las máscaras de la segmentación
84
+ predicted = cv2.resize(
85
+ predicted, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_LANCZOS4
86
+ )
87
+ mask = np.uint8(predicted * 255) #
88
+ _, mask = cv2.threshold(
89
+ mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY + cv2.THRESH_OTSU
90
+ )
91
+ kernel = np.ones((5, 5), dtype=np.float32)
92
+ mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1)
93
+ mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)
94
+ cnts, hieararch = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
95
+ output = cv2.drawContours(convert_one_channel(img.astype(np.uint8)), cnts, -1, (255, 0, 0), 3)
96
+
97
+ ## Si obtuvimos exitosamente un resultadod entonces lo mostramos en la interfaz
98
+ if output is not None:
99
+ st.subheader("Segmentation / Segmentación:")
100
+ st.write(output.shape)
101
+ st.image(output, width=850)
102
+
103
+ st.markdown("Thanks for use our demo! / Gracias por usar esta demo")
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ numpy
2
+ Pillow
3
+ scipy
4
+ opencv-python
5
+ tensorflow
6
+ streamlit
7
+ huggingface_hub