asmaa1's picture
Update app.py
d1db14a verified
import gradio as gr
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from PIL import Image
# Fixed image URL
fixed_image_url = "222.PNG"
# Example images and their descriptions
examples = [
["Avulsion fracture.jpg", "Avulsion fracture."]
]
# Load your trained model
model = load_model("bone_break_classification_model.h5")
# Define your class names dictionary
class_names_dict = {
0: 'Avulsion fracture',
1: 'Comminuted fracture',
2: 'Fracture Dislocation',
3: 'Greenstick fracture',
4: 'Hairline Fracture',
5: 'Imapacted fracture',
6: 'Longitudinal fracture',
7: 'Oblique fracture',
8: 'Pathological fracture',
9: 'Spiral Fracture'
}
def predict_image(img_path):
# If no image is uploaded, return a message
if img_path is None:
return "Please upload an image.", fixed_image_url
# Load the image using PIL
img = Image.open(img_path)
img = img.resize((256, 256)) # Resize the image
img_array = np.array(img) / 255.0 # Normalize
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
# Make prediction
prediction = model.predict(img_array)
predicted_class_index = np.argmax(prediction, axis=-1)[0]
predicted_class_name = class_names_dict.get(predicted_class_index, "Unknown Class")
return predicted_class_name ,fixed_image_url
# Create Gradio interface
iface = gr.Interface(
fn=predict_image,
inputs=gr.Image(type="filepath", label="Upload an Image"),
outputs=[
gr.Textbox(label="Prediction"),
gr.Image(label=" Bone Fracture Detection ", value=fixed_image_url)
],
title="Bone Break Classification",
description=(
"Upload an X-ray image, and the model will predict the type of bone break. \n\n"
"Classes Available:\n"
"Avulsion fracture ,\n"
"Comminuted fracture ,\n"
"Fracture Dislocation ,\n"
"Greenstick fracture ,\n"
"Hairline Fracture ,\n"
"Impacted fracture ,\n"
"Longitudinal fracture ,\n"
"Oblique fracture ,\n"
"Pathological fracture ,\n"
"Spiral Fracture ,"
),
theme="ParityError/Interstellar",
examples=examples,
)
# Launch the interface
iface.launch(debug=True)