|
import gradio as gr |
|
import tensorflow as tf |
|
import numpy as np |
|
from tensorflow.keras.preprocessing import image |
|
from PIL import Image |
|
|
|
|
|
model = tf.keras.models.load_model("my_keras_model.h5") |
|
|
|
|
|
image_size = (224, 224) |
|
|
|
|
|
def predict_image(img): |
|
img = img.resize(image_size) |
|
img_array = image.img_to_array(img) |
|
img_array = np.expand_dims(img_array, axis=0) / 255.0 |
|
prediction = model.predict(img_array) |
|
|
|
|
|
class_names = ['Fractured', 'Normal'] |
|
predicted_class = class_names[int(prediction[0] > 0.5)] |
|
|
|
return f"Prediction: {predicted_class} (Confidence: {prediction[0][0]:.2f})" |
|
|
|
|
|
sample_images = [ |
|
("fracture1.jpg", "Fractured Example"), |
|
("fracture2.jpg", "Fractured Example"), |
|
("normal1.jpg", "Normal Example"), |
|
("normal2.jpg", "Normal Example"), |
|
] |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict_image, |
|
inputs=gr.Image(type="pil"), |
|
outputs=gr.Textbox(), |
|
examples=sample_images, |
|
title="Bone Fracture Detection", |
|
description=""" |
|
<h1 style='color: blue;'>Bone Fracture Detection Model</h1> |
|
<p>This AI model predicts whether a given X-ray image shows a fracture or not.</p> |
|
<p><b>Upload an image</b> or select from the provided samples to get a prediction.</p> |
|
""", |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |