Spaces:
Sleeping
Sleeping
Commit ·
6f7e88c
1
Parent(s): f835cae
adding app and req files
Browse files- app.py +115 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
st.subheader("Iteration to improve demo")
|
| 9 |
+
st.markdown(
|
| 10 |
+
"""
|
| 11 |
+
Demo for testing image segmentation
|
| 12 |
+
"""
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
'''
|
| 17 |
+
Technical Overview
|
| 18 |
+
Architecture: It utilizes the U-Net architecture, a popular "encoder-decoder" convolutional neural network (CNN) specifically optimized for biomedical image segmentation where pixel-level accuracy is critical.
|
| 19 |
+
Performance: In the accompanying research, the model achieved a Dice overlap score of 95.4% for overall teeth segmentation.
|
| 20 |
+
Post-Processing: A key highlight of this specific implementation is the use of grayscale morphological filtering and operations applied to the sigmoid output. This reduces tooth counting errors significantly (from 26.8% down to roughly 6.2%).Dataset: The model was trained on a relatively small but highly curated dataset (approximately 105–116 panoramic images) based on work by Abdi et al. (2015).Key ApplicationsClinical Diagnosis: Assists dentists in identifying the boundaries of individual teeth to detect caries, lesions, or bone loss.
|
| 21 |
+
Forensics and Identification: Automates the process of identifying dental patterns for human remains or age/gender determination.
|
| 22 |
+
Treatment Planning: Provides a baseline for orthodontic therapy workups by isolating dental structures from the surrounding mandible and maxilla.
|
| 23 |
+
'''
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
## Select and load the model
|
| 27 |
+
model_id = "SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net"
|
| 28 |
+
model = from_pretrained_keras(model_id)
|
| 29 |
+
|
| 30 |
+
## Allow the user to upload an image
|
| 31 |
+
archivo_imagen = st.file_uploader("Sube aquí tu imagen.", type=["png", "jpg", "jpeg"])
|
| 32 |
+
|
| 33 |
+
## If an image has more than one channel, it is converted to grayscale (1 channel)
|
| 34 |
+
def convertir_one_channel(img):
|
| 35 |
+
if len(img.shape) > 2:
|
| 36 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 37 |
+
return img
|
| 38 |
+
else:
|
| 39 |
+
return img
|
| 40 |
+
|
| 41 |
+
def convertir_rgb(img):
|
| 42 |
+
if len(img.shape) == 2:
|
| 43 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
|
| 44 |
+
return img
|
| 45 |
+
else:
|
| 46 |
+
return img
|
| 47 |
+
|
| 48 |
+
## We will manipulate the interface so we can use example images
|
| 49 |
+
## If the user clicks on an example, the model will run with the following:
|
| 50 |
+
ejemplos = ["dientes_1.png", "dientes_2.png", "dientes_3.png"]
|
| 51 |
+
|
| 52 |
+
## Create three columns; an example image will be in each one
|
| 53 |
+
col1, col2, col3 = st.columns(3)
|
| 54 |
+
|
| 55 |
+
with col1:
|
| 56 |
+
## The image is loaded and displayed in the interface
|
| 57 |
+
ex = Image.open(ejemplos[0])
|
| 58 |
+
st.image(ex, width=200)
|
| 59 |
+
## If the button is pressed, we will use this example in the model
|
| 60 |
+
if st.button("Corre este ejemplo 1"):
|
| 61 |
+
archivo_imagen = ejemplos[0]
|
| 62 |
+
|
| 63 |
+
with col2:
|
| 64 |
+
ex1 = Image.open(ejemplos[1])
|
| 65 |
+
st.image(ex1, width=200)
|
| 66 |
+
if st.button("Corre este ejemplo 2"):
|
| 67 |
+
archivo_imagen = ejemplos[1]
|
| 68 |
+
|
| 69 |
+
with col3:
|
| 70 |
+
ex2 = Image.open(ejemplos[2])
|
| 71 |
+
st.image(ex2, width=200)
|
| 72 |
+
if st.button("Corre este ejemplo 3"):
|
| 73 |
+
archivo_imagen = ejemplos[2]
|
| 74 |
+
|
| 75 |
+
## If we have an image to input into the model,
|
| 76 |
+
## we process it and feed it to the model
|
| 77 |
+
if archivo_imagen is not None:
|
| 78 |
+
## Load the image with PIL, display it, and convert it to a NumPy array
|
| 79 |
+
img = Image.open(archivo_imagen)
|
| 80 |
+
st.image(img, width=850)
|
| 81 |
+
img = np.asarray(img)
|
| 82 |
+
|
| 83 |
+
## Process the image for model input
|
| 84 |
+
img_cv = convertir_one_channel(img)
|
| 85 |
+
img_cv = cv2.resize(img_cv, (512, 512), interpolation=cv2.INTER_LANCZOS4)
|
| 86 |
+
img_cv = np.float32(img_cv / 255)
|
| 87 |
+
img_cv = np.reshape(img_cv, (1, 512, 512, 1))
|
| 88 |
+
|
| 89 |
+
## Feed the NumPy array into the model
|
| 90 |
+
predicted = model.predict(img_cv)
|
| 91 |
+
predicted = predicted[0]
|
| 92 |
+
|
| 93 |
+
## Resize the image back to its original shape and add the segmentation masks
|
| 94 |
+
predicted = cv2.resize(
|
| 95 |
+
predicted, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_LANCZOS4)
|
| 96 |
+
|
| 97 |
+
mask = np.uint8(predicted * 255)
|
| 98 |
+
_, mask = cv2.threshold(
|
| 99 |
+
mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY + cv2.THRESH_OTSU
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
kernel = np.ones((5, 5), dtype=np.float32)
|
| 103 |
+
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 104 |
+
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)
|
| 105 |
+
|
| 106 |
+
cnts, hieararch = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 107 |
+
output = cv2.drawContours(convertir_one_channel(img), cnts, -1, (255, 0, 0), 3)
|
| 108 |
+
|
| 109 |
+
## If we successfully obtained a result, display it in the interface
|
| 110 |
+
if output is not None:
|
| 111 |
+
st.subheader("Segmentación:")
|
| 112 |
+
st.write(output.shape)
|
| 113 |
+
st.image(output, width=850)
|
| 114 |
+
|
| 115 |
+
st.markdown("Thanks for using our demo!")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
Pillow
|
| 3 |
+
scipy
|
| 4 |
+
opencv-python
|
| 5 |
+
tensorflow
|