Teeth / app.py
joacorf33's picture
Just translation improvements
95af777
raw
history blame contribute delete
No virus
3.59 kB
import streamlit as st
from PIL import Image
import numpy as np
import cv2
from huggingface_hub import from_pretrained_keras
st.header("X-ray tooth segmentation")
st.subheader("Demo improvement Iteration Process")
st.markdown(
"""
This model was created by [SerdarHelli](https://huggingface.co/SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net).
"""
)
## Select and load the model
model_id = "SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net"
model = from_pretrained_keras(model_id)
## Allows the user to upload an image
archivo_imagen = st.file_uploader("Load her you Image.", type=["png", "jpg", "jpeg"])
## If an image has more than one channel then it is converted to grayscale (1 channel)
def convertir_one_channel(img):
if len(img.shape) > 2:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return img
else:
return img
def convertir_rgb(img):
if len(img.shape) == 2:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
return img
else:
return img
## We'll manipulate the interface so we can use example images
## If the user clicks on an example then the model will run with it
ejemplos = ["dientes_1.png", "dientes_2.png", "dientes_3.png"]
## Let's create three columns; In each one there will be an example image
col1, col2, col3 = st.columns(3)
with col1:
## Load the image & show the interface
ex = Image.open(ejemplos[0])
st.image(ex, width=200)
## If push the button then, let's use that example within the model
if st.button("Run this sample 1"):
archivo_imagen = ejemplos[0]
with col2:
ex1 = Image.open(ejemplos[1])
st.image(ex1, width=200)
if st.button("Run sample 2"):
archivo_imagen = ejemplos[1]
with col3:
ex2 = Image.open(ejemplos[2])
st.image(ex2, width=200)
if st.button("Run sample 3"):
archivo_imagen = ejemplos[2]
## If we have an image to input into the model then
## we process it and enter the model
if archivo_imagen is not None:
## We load the image with PIL, display it and convert it to a NumPy array
img = Image.open(archivo_imagen)
st.image(img, width=850)
img = np.asarray(img)
## We process the image to enter it into the model
img_cv = convertir_one_channel(img)
img_cv = cv2.resize(img_cv, (512, 512), interpolation=cv2.INTER_LANCZOS4)
img_cv = np.float32(img_cv / 255)
img_cv = np.reshape(img_cv, (1, 512, 512, 1))
## We enter the NumPy array to the model
predicted = model.predict(img_cv)
predicted = predicted[0]
## We return the image to its original shape and add the segmentation masks
predicted = cv2.resize(
predicted, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_LANCZOS4
)
mask = np.uint8(predicted * 255) #
_, mask = cv2.threshold(
mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY + cv2.THRESH_OTSU
)
kernel = np.ones((5, 5), dtype=np.float32)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)
cnts, hieararch = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
output = cv2.drawContours(convertir_one_channel(img), cnts, -1, (255, 0, 0), 3)
## If we successfully got a result then we show it in the interface
if output is not None:
st.subheader("Segmentación:")
st.write(output.shape)
st.image(output, width=850)
st.markdown("Thanks for using this application. See you soon, Mate!")