File size: 915 Bytes
acac8bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
import cv2

# Load model
model = tf.keras.models.load_model("model/model.h5")

def preprocess_image(image):
    image = np.array(image)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    image = cv2.resize(image, (224, 224))
    image = image / 255.0
    image = image.reshape(1, 224, 224, 1)
    return image

def predict(image):
    image = preprocess_image(image)
    prediction = model.predict(image)[0][0]

    if prediction > 0.5:
        return {"Fractured": float(prediction), "Normal": float(1 - prediction)}
    else:
        return {"Normal": float(1 - prediction), "Fractured": float(prediction)}

gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=2),
    title="Bone Fracture Detection",
    description="Upload an X-ray image to detect bone fracture"
).launch()