Spaces:
Sleeping
Sleeping
Rafael Rivera
commited on
Commit
•
97084ab
1
Parent(s):
f40e8b0
Creacion de archivos
Browse files- app.py +71 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.subheader("Esta es una iteración de prueba para buscar mejorar el Demo")
|
10 |
+
|
11 |
+
st.markdown('''
|
12 |
+
|
13 |
+
Hola 🚀. Este es un modelo de prueba como ejercicio de Platzi
|
14 |
+
|
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 |
+
model_id = "SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net"
|
20 |
+
model=from_pretrained_keras(model_id)
|
21 |
+
|
22 |
+
## Si una imagen tiene más de un canal entonces se convierte a escala de grises (1 canal)
|
23 |
+
def convertir_one_channel(img):
|
24 |
+
if len(img.shape)>2:
|
25 |
+
img= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
26 |
+
return img
|
27 |
+
else:
|
28 |
+
return img
|
29 |
+
|
30 |
+
def convertir_rgb(img):
|
31 |
+
if len(img.shape)==2:
|
32 |
+
img= cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
|
33 |
+
return img
|
34 |
+
else:
|
35 |
+
return img
|
36 |
+
|
37 |
+
|
38 |
+
image_file = st.file_uploader("Sube aquí tu imagen.", type=["png","jpg","jpeg"])
|
39 |
+
|
40 |
+
|
41 |
+
if image_file is not None:
|
42 |
+
|
43 |
+
img= Image.open(image_file)
|
44 |
+
|
45 |
+
st.image(img,width=850)
|
46 |
+
|
47 |
+
img=np.asarray(img)
|
48 |
+
|
49 |
+
img_cv=convertir_one_channel(img)
|
50 |
+
img_cv=cv2.resize(img_cv,(512,512), interpolation=cv2.INTER_LANCZOS4)
|
51 |
+
img_cv=np.float32(img_cv/255)
|
52 |
+
|
53 |
+
img_cv=np.reshape(img_cv,(1,512,512,1))
|
54 |
+
prediction=model.predict(img_cv)
|
55 |
+
predicted=prediction[0]
|
56 |
+
predicted = cv2.resize(predicted, (img.shape[1],img.shape[0]), interpolation=cv2.INTER_LANCZOS4)
|
57 |
+
mask=np.uint8(predicted*255)#
|
58 |
+
_, mask = cv2.threshold(mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY+cv2.THRESH_OTSU)
|
59 |
+
kernel =( np.ones((5,5), dtype=np.float32))
|
60 |
+
mask=cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel,iterations=1 )
|
61 |
+
mask=cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel,iterations=1 )
|
62 |
+
cnts,hieararch=cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
|
63 |
+
output = cv2.drawContours(convertir_one_channel(img), cnts, -1, (255, 0, 0) , 3)
|
64 |
+
|
65 |
+
|
66 |
+
if output is not None :
|
67 |
+
st.subheader("Segmentación:")
|
68 |
+
st.write(output.shape)
|
69 |
+
st.image(output,width=850)
|
70 |
+
|
71 |
+
st.markdown("Gracias por utilizar este Demo")
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
Pillow
|
3 |
+
scipy
|
4 |
+
opencv-python
|
5 |
+
tensorflow
|