johnometalman commited on
Commit
3fca42b
1 Parent(s): 16dfb02

Refactoring de la app

Browse files
Files changed (1) hide show
  1. app.py +22 -24
app.py CHANGED
@@ -4,32 +4,30 @@ 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.subheader("Este es una iteraci贸n para bucar mejorar el demo")
10
 
11
  st.markdown(
12
  """
13
- Este es un demo prueba para la clase de Machine leardning
14
  """
15
  )
16
 
17
  ## Seleccionamos y cargamos el modelo
18
- model_id = "SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net"
19
  model = from_pretrained_keras(model_id)
20
 
21
  ## Permitimos a la usuaria cargar una imagen
22
- archivo_imagen = st.file_uploader("Sube aqu铆 tu imagen.", type=["png", "jpg", "jpeg"])
23
 
24
  ## Si una imagen tiene m谩s de un canal entonces se convierte a escala de grises (1 canal)
25
- def convertir_one_channel(img):
26
  if len(img.shape) > 2:
27
  img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
28
  return img
29
  else:
30
  return img
31
 
32
-
33
  def convertir_rgb(img):
34
  if len(img.shape) == 2:
35
  img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
@@ -40,40 +38,40 @@ def convertir_rgb(img):
40
 
41
  ## Manipularemos la interfaz para que podamos usar im谩genes ejemplo
42
  ## Si el usuario da click en un ejemplo entonces el modelo correr谩 con 茅l
43
- ejemplos = ["dientes_1.png", "dientes_2.png", "dientes_3.png"]
44
 
45
  ## Creamos tres columnas; en cada una estar谩 una imagen ejemplo
46
  col1, col2, col3 = st.columns(3)
47
  with col1:
48
  ## Se carga la imagen y se muestra en la interfaz
49
- ex = Image.open(ejemplos[0])
50
  st.image(ex, width=200)
51
  ## Si oprime el bot贸n entonces usaremos ese ejemplo en el modelo
52
- if st.button("Corre este ejemplo 1"):
53
- archivo_imagen = ejemplos[0]
54
 
55
  with col2:
56
- ex1 = Image.open(ejemplos[1])
57
  st.image(ex1, width=200)
58
- if st.button("Corre este ejemplo 2"):
59
- archivo_imagen = ejemplos[1]
60
 
61
  with col3:
62
- ex2 = Image.open(ejemplos[2])
63
  st.image(ex2, width=200)
64
- if st.button("Corre este ejemplo 3"):
65
- archivo_imagen = ejemplos[2]
66
 
67
  ## Si tenemos una imagen para ingresar en el modelo entonces
68
  ## la procesamos e ingresamos al modelo
69
- if archivo_imagen is not None:
70
  ## Cargamos la imagen con PIL, la mostramos y la convertimos a un array de NumPy
71
- img = Image.open(archivo_imagen)
72
  st.image(img, width=850)
73
  img = np.asarray(img)
74
 
75
  ## Procesamos la imagen para ingresarla al modelo
76
- img_cv = convertir_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
  img_cv = np.reshape(img_cv, (1, 512, 512, 1))
@@ -94,12 +92,12 @@ if archivo_imagen is not None:
94
  mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1)
95
  mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)
96
  cnts, hieararch = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
97
- output = cv2.drawContours(convertir_one_channel(img), cnts, -1, (255, 0, 0), 3)
98
 
99
  ## Si obtuvimos exitosamente un resultadod entonces lo mostramos en la interfaz
100
  if output is not None:
101
- st.subheader("Segmentaci贸n:")
102
  st.write(output.shape)
103
  st.image(output, width=850)
104
 
105
- st.markdown("Gracias por usar este demo! Nos vemos pronto")
 
4
  import cv2
5
  from huggingface_hub import from_pretrained_keras
6
 
7
+ st.header("X-ray segmentation of dientes / 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-dientes-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)
 
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 = ["dientes_1.png", "dientes_2.png", "dientes_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))
 
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")