Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,122 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def convert_one_channel(
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
if len(
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
st.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
st.image(
|
| 65 |
-
if st.button('Example
|
| 66 |
-
image_file=examples[
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
from huggingface_hub import from_pretrained_keras
|
| 7 |
+
|
| 8 |
+
# Use st.cache_resource to load the model only once, preventing memory errors.
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_keras_model():
|
| 11 |
+
"""Load the pre-trained Keras model from Hugging Face Hub and cache it."""
|
| 12 |
+
try:
|
| 13 |
+
# The model will be downloaded from the Hub and cached.
|
| 14 |
+
model = from_pretrained_keras("SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net")
|
| 15 |
+
return model
|
| 16 |
+
except Exception as e:
|
| 17 |
+
# If model loading fails, show an error and return None.
|
| 18 |
+
st.error(f"Error loading the model: {e}")
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
# --- Helper Functions ---
|
| 22 |
+
def load_image(image_file):
|
| 23 |
+
"""Loads an image from a file path or uploaded file object."""
|
| 24 |
+
img = Image.open(image_file)
|
| 25 |
+
return img
|
| 26 |
+
|
| 27 |
+
def convert_one_channel(img_array):
|
| 28 |
+
"""Ensure the image is single-channel (grayscale)."""
|
| 29 |
+
# If image has 3 channels (like BGR or RGB), convert to grayscale.
|
| 30 |
+
if len(img_array.shape) > 2 and img_array.shape[2] > 1:
|
| 31 |
+
img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)
|
| 32 |
+
return img_array
|
| 33 |
+
|
| 34 |
+
def convert_rgb(img_array):
|
| 35 |
+
"""Ensure the image is 3-channel (RGB) for drawing contours."""
|
| 36 |
+
# If image is grayscale, convert to RGB to draw colored contours.
|
| 37 |
+
if len(img_array.shape) == 2:
|
| 38 |
+
img_array = cv2.cvtColor(img_array, cv2.COLOR_GRAY2RGB)
|
| 39 |
+
return img_array
|
| 40 |
+
|
| 41 |
+
# --- Streamlit App Layout ---
|
| 42 |
+
st.header("Segmentation of Teeth in Panoramic X-ray Image Using UNet")
|
| 43 |
+
|
| 44 |
+
link = 'Check Out Our Github Repo! [link](https://github.com/SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net)'
|
| 45 |
+
st.markdown(link, unsafe_allow_html=True)
|
| 46 |
+
|
| 47 |
+
# Load the model and stop the app if it fails
|
| 48 |
+
model = load_keras_model()
|
| 49 |
+
if model is None:
|
| 50 |
+
st.warning("Model could not be loaded. The application cannot proceed.")
|
| 51 |
+
st.stop()
|
| 52 |
+
|
| 53 |
+
# --- Image Selection Section ---
|
| 54 |
+
st.subheader("Upload a Dental Panoramic X-ray Image or Select an Example")
|
| 55 |
+
image_file = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
|
| 56 |
+
|
| 57 |
+
st.write("---")
|
| 58 |
+
st.write("Or choose an example:")
|
| 59 |
+
examples = ["107.png", "108.png", "109.png"]
|
| 60 |
+
col1, col2, col3 = st.columns(3)
|
| 61 |
+
|
| 62 |
+
# Display example images and buttons to use them
|
| 63 |
+
with col1:
|
| 64 |
+
st.image(examples[0], caption='Example 1', use_column_width=True)
|
| 65 |
+
if st.button('Use Example 1'):
|
| 66 |
+
image_file = examples[0]
|
| 67 |
+
|
| 68 |
+
with col2:
|
| 69 |
+
st.image(examples[1], caption='Example 2', use_column_width=True)
|
| 70 |
+
if st.button('Use Example 2'):
|
| 71 |
+
image_file = examples[1]
|
| 72 |
+
|
| 73 |
+
with col3:
|
| 74 |
+
st.image(examples[2], caption='Example 3', use_column_width=True)
|
| 75 |
+
if st.button('Use Example 3'):
|
| 76 |
+
image_file = examples[2]
|
| 77 |
+
|
| 78 |
+
# --- Processing and Prediction Section ---
|
| 79 |
+
if image_file is not None:
|
| 80 |
+
st.write("---")
|
| 81 |
+
|
| 82 |
+
# Load and display the selected image
|
| 83 |
+
original_pil_img = load_image(image_file)
|
| 84 |
+
st.image(original_pil_img, caption="Original Image", use_column_width=True)
|
| 85 |
+
|
| 86 |
+
with st.spinner("Analyzing image and predicting segmentation..."):
|
| 87 |
+
# Convert PIL image to NumPy array for processing
|
| 88 |
+
original_np_img = np.array(original_pil_img)
|
| 89 |
+
|
| 90 |
+
# 1. Pre-process for the model
|
| 91 |
+
img_gray = convert_one_channel(original_np_img.copy())
|
| 92 |
+
img_resized = cv2.resize(img_gray, (512, 512), interpolation=cv2.INTER_LANCZOS4)
|
| 93 |
+
img_normalized = np.float32(img_resized / 255.0)
|
| 94 |
+
img_input = np.reshape(img_normalized, (1, 512, 512, 1))
|
| 95 |
+
|
| 96 |
+
# 2. Make prediction
|
| 97 |
+
prediction = model.predict(img_input)
|
| 98 |
+
|
| 99 |
+
# 3. Post-process the prediction mask
|
| 100 |
+
predicted_mask = prediction[0]
|
| 101 |
+
resized_mask = cv2.resize(predicted_mask, (original_np_img.shape[1], original_np_img.shape[0]), interpolation=cv2.INTER_LANCZOS4)
|
| 102 |
+
|
| 103 |
+
# Binarize the mask using Otsu's thresholding
|
| 104 |
+
mask_8bit = (resized_mask * 255).astype(np.uint8)
|
| 105 |
+
_, final_mask = cv2.threshold(mask_8bit, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 106 |
+
|
| 107 |
+
# Clean up mask with morphological operations
|
| 108 |
+
kernel = np.ones((5, 5), dtype=np.uint8)
|
| 109 |
+
final_mask = cv2.morphologyEx(final_mask, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 110 |
+
final_mask = cv2.morphologyEx(final_mask, cv2.MORPH_CLOSE, kernel, iterations=1)
|
| 111 |
+
|
| 112 |
+
# Find contours on the final mask
|
| 113 |
+
contours, _ = cv2.findContours(final_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 114 |
+
|
| 115 |
+
# Draw contours on a color version of the original image
|
| 116 |
+
img_for_drawing = convert_rgb(original_np_img.copy())
|
| 117 |
+
output_image = cv2.drawContours(img_for_drawing, contours, -1, (255, 0, 0), 3) # Draw red contours
|
| 118 |
+
|
| 119 |
+
st.subheader("Predicted Segmentation")
|
| 120 |
+
st.image(output_image, caption="Image with Segmented Teeth", use_column_width=True)
|
| 121 |
+
|
| 122 |
+
st.success("Prediction complete!")
|