Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras.preprocessing import image
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Load the trained model
|
8 |
+
model = tf.keras.models.load_model("my_keras_model.h5")
|
9 |
+
|
10 |
+
# Define image size based on the model's input requirement
|
11 |
+
image_size = (224, 224)
|
12 |
+
|
13 |
+
# Function to make predictions
|
14 |
+
def predict_image(img):
|
15 |
+
img = img.resize(image_size) # Resize image to model's expected size
|
16 |
+
img_array = image.img_to_array(img)
|
17 |
+
img_array = np.expand_dims(img_array, axis=0) / 255.0 # Normalize
|
18 |
+
prediction = model.predict(img_array)
|
19 |
+
|
20 |
+
# Assuming binary classification (fractured or normal)
|
21 |
+
class_names = ['Fractured', 'Normal']
|
22 |
+
predicted_class = class_names[int(prediction[0] > 0.5)] # Threshold at 0.5
|
23 |
+
|
24 |
+
return f"Prediction: {predicted_class} (Confidence: {prediction[0][0]:.2f})"
|
25 |
+
|
26 |
+
# Preloaded images for testing
|
27 |
+
sample_images = [
|
28 |
+
("fracture1.jpg", "Fractured Example"),
|
29 |
+
("fracture2.jpg", "Fractured Example"),
|
30 |
+
("normal1.jpg", "Normal Example"),
|
31 |
+
("normal2.jpg", "Normal Example"),
|
32 |
+
]
|
33 |
+
|
34 |
+
# Define Gradio Interface
|
35 |
+
interface = gr.Interface(
|
36 |
+
fn=predict_image,
|
37 |
+
inputs=gr.Image(type="pil"),
|
38 |
+
outputs=gr.Textbox(),
|
39 |
+
examples=sample_images, # Preloaded images for testing
|
40 |
+
title="Bone Fracture Detection",
|
41 |
+
description="""
|
42 |
+
<h1 style='color: blue;'>Bone Fracture Detection Model</h1>
|
43 |
+
<p>This AI model predicts whether a given X-ray image shows a fracture or not.</p>
|
44 |
+
<p><b>Upload an image</b> or select from the provided samples to get a prediction.</p>
|
45 |
+
""",
|
46 |
+
)
|
47 |
+
|
48 |
+
# Launch the Gradio app
|
49 |
+
if __name__ == "__main__":
|
50 |
+
interface.launch()
|