File size: 2,123 Bytes
cdd4621
 
 
 
 
615f871
cdd4621
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
615f871
 
 
cdd4621
64614f4
 
 
 
 
 
 
 
 
 
 
 
cdd4621
 
 
 
 
 
 
ab2f9ad
64614f4
bcb038f
64614f4
 
 
 
bcb038f
64614f4
 
 
 
bcb038f
cdd4621
 
 
 
06c8325
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing import image
from PIL import Image
import os

# Load the trained model
model = tf.keras.models.load_model("my_keras_model.h5")

# Define image size based on the model's input requirement
image_size = (224, 224)

# Function to make predictions
def predict_image(img):
    img = img.resize(image_size)  # Resize image to model's expected size
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0) / 255.0  # Normalize
    prediction = model.predict(img_array)

    # Assuming binary classification (fractured or normal)
    class_names = ['Fractured', 'Normal']
    predicted_class = class_names[int(prediction[0] > 0.5)]  # Threshold at 0.5

    return f"Prediction: {predicted_class} (Confidence: {prediction[0][0]:.2f})"

# Get image paths dynamically
sample_images_dir = "samples"
sample_images = [os.path.join(sample_images_dir, f) for f in os.listdir(sample_images_dir) if f.endswith(('.jpg', '.png'))]

# File download paths
report_path = "report.pdf"
notebook_path = "bfd.ipynb"

# Function to provide file download links
def get_download_links():
    return f"""  
    **Download Project Files:**  
    - [Download Report (PDF)]({report_path})  
    - [Download Notebook (IPYNB)]({notebook_path})  
    """

# Define Gradio Interface
interface = gr.Interface(
    fn=predict_image,
    inputs=gr.Image(type="pil"),
    outputs=gr.Textbox(),
    examples=sample_images,  # Preloaded images for testing
    title="Bone Fracture Detection",
    description="Upload an X-ray image or select a sample image to check for fractures.",
    article=f"""
    ## Instructions
    - Upload an X-ray image to check for fractures.
    - Select a sample image for quick testing.
    - The model will predict if the bone is fractured or normal.
    
    ## Capabilities
    - Detects bone fractures from X-ray images.
    - Uses a trained deep learning model for classification.
    
    {get_download_links()}  
    """
)

# Launch the Gradio app
if __name__ == "__main__":
    interface.launch()