Joaquin Romero Flores commited on
Commit
03df72a
1 Parent(s): 8f3d1a6

laoding the model to hugging face hub

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("X-ray tooth segmentation")
8
+
9
+ st.markdown(
10
+ """
11
+ This model was created by [SerdarHelli](https://huggingface.co/SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net).
12
+ """
13
+ )
14
+
15
+ ## Select and load the model
16
+ model_id = "SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net"
17
+ model = from_pretrained_keras(model_id)
18
+
19
+ ## Allows the user to upload an image
20
+ archivo_imagen = st.file_uploader("Sube aquí tu imagen.", type=["png", "jpg", "jpeg"])
21
+
22
+ ## If an image has more than one channel then it is converted to grayscale (1 channel)
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
+
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
+ ## We'll manipulate the interface so we can use example images
40
+ ## If the user clicks on an example then the model will run with it
41
+ ejemplos = ["dientes_1.png", "dientes_2.png", "dientes_3.png"]
42
+
43
+
44
+ ## Let's create three columns; In each one there will be an example image
45
+ col1, col2, col3 = st.columns(3)
46
+ with col1:
47
+ ## Load the image & show the interface
48
+ ex = Image.open(ejemplos[0])
49
+ st.image(ex, width=200)
50
+ ## If push the button then, let's use that example within the model
51
+ if st.button("Corre este ejemplo 1"):
52
+ archivo_imagen = ejemplos[0]
53
+
54
+ with col2:
55
+ ex1 = Image.open(ejemplos[1])
56
+ st.image(ex1, width=200)
57
+ if st.button("Corre este ejemplo 2"):
58
+ archivo_imagen = ejemplos[1]
59
+
60
+ with col3:
61
+ ex2 = Image.open(ejemplos[2])
62
+ st.image(ex2, width=200)
63
+ if st.button("Corre este ejemplo 3"):
64
+ archivo_imagen = ejemplos[2]
65
+
66
+
67
+ ## If we have an image to input into the model then
68
+ ## we process it and enter the model
69
+ if archivo_imagen is not None:
70
+ ## We load the image with PIL, display it and convert it to a NumPy array
71
+ img = Image.open(archivo_imagen)
72
+ st.image(img, width=850)
73
+ img = np.asarray(img)
74
+
75
+ ## We process the image to enter it into the model
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))
80
+
81
+ ## We enter the NumPy array to the model
82
+ predicted = model.predict(img_cv)
83
+ predicted = predicted[0]
84
+
85
+ ## We return the image to its original shape and add the segmentation masks
86
+ predicted = cv2.resize(
87
+ predicted, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_LANCZOS4
88
+ )
89
+ mask = np.uint8(predicted * 255) #
90
+ _, mask = cv2.threshold(
91
+ mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY + cv2.THRESH_OTSU
92
+ )
93
+ kernel = np.ones((5, 5), dtype=np.float32)
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
+ ## If we successfully got a result then we show it in the interface
100
+ if output is not None:
101
+ st.subheader("Segmentación:")
102
+ st.write(output.shape)
103
+ st.image(output, width=850)
104
+
105
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ numpy
2
+ Pillow
3
+ scipy
4
+ opencv-python
5
+ tensorflow