asmaa1 commited on
Commit
9393c29
·
verified ·
1 Parent(s): c429b4d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from tensorflow.keras.models import load_model
4
+ from tensorflow.keras.preprocessing import image
5
+ from PIL import Image
6
+ # Fixed image URL
7
+ fixed_image_url = "222.PNG"
8
+
9
+ # Example images and their descriptions
10
+ examples = [
11
+ ["Avulsion fracture.jpg", "Avulsion fracture."]
12
+ # Load your trained model
13
+ model = load_model("bone_break_classification_model.h5")
14
+
15
+ # Define your class names dictionary
16
+ class_names_dict = {
17
+ 0: 'Avulsion fracture',
18
+ 1: 'Comminuted fracture',
19
+ 2: 'Fracture Dislocation',
20
+ 3: 'Greenstick fracture',
21
+ 4: 'Hairline Fracture',
22
+ 5: 'Imapacted fracture',
23
+ 6: 'Longitudinal fracture',
24
+ 7: 'Oblique fracture',
25
+ 8: 'Pathological fracture',
26
+ 9: 'Spiral Fracture'
27
+ }
28
+
29
+ def predict_image(img_path):
30
+ # Load the image using PIL
31
+ img = Image.open(img_path)
32
+ img = img.resize((256, 256)) # Resize the image
33
+ img_array = np.array(img) / 255.0 # Normalize
34
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
35
+
36
+ # Make prediction
37
+ prediction = model.predict(img_array)
38
+ predicted_class_index = np.argmax(prediction, axis=-1)[0]
39
+ predicted_class_name = class_names_dict.get(predicted_class_index, "Unknown Class")
40
+
41
+ return predicted_class_name ,fixed_image_url
42
+
43
+ # Create Gradio interface
44
+ iface = gr.Interface(
45
+ fn=predict_image,
46
+ inputs=gr.Image(type="filepath", label="Upload an Image"),
47
+ outputs=[
48
+ gr.Textbox(label="Prediction"),
49
+ gr.Image(label=" Bone Fracture Detection ", value=fixed_image_url)
50
+ ],
51
+ title="Bone Break Classification",
52
+ description="Upload an X-ray image, and the model will predict the type of bone break.",
53
+ theme="ParityError/Interstellar",
54
+ examples=examples,
55
+ )
56
+
57
+ # Launch the interface
58
+ iface.launch(debug=True)