asmaa1 commited on
Commit
355b229
1 Parent(s): a9d3af4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from keras.models import load_model
2
+ from keras.preprocessing import image
3
+ import numpy as np
4
+ import os
5
+ import gradio as gr
6
+ loaded_model = load_model('diabetic_retinopathy_model.h5')
7
+ import gradio as gr
8
+ import numpy as np
9
+ from tensorflow.keras.preprocessing import image
10
+
11
+ # Class mapping
12
+ class_mapping = {
13
+ 0: 'No DR',
14
+ 1: 'Mild',
15
+ 2: 'Moderate',
16
+ 3: 'Severe',
17
+ 4: 'Proliferative DR'
18
+ }
19
+
20
+ # URL of the fixed example image to display
21
+ example_image_url = "1.jpg" # Replace with the actual URL
22
+
23
+ def predict_diabetic_retinopathy(test_image, loaded_model, height=512, width=512):
24
+ # Always return the example image
25
+ try:
26
+ if test_image is None:
27
+ return "No image uploaded. Please upload an image.", example_image_url
28
+
29
+ # Ensure the image is in the correct format
30
+ img = image.img_to_array(test_image)
31
+
32
+ # Resize the image while maintaining the aspect ratio
33
+ img = np.array(image.smart_resize(img, (height, width)))
34
+
35
+ img_array = np.expand_dims(img, axis=0)
36
+ img_array /= 255.0 # Normalize the image array
37
+
38
+ # Make predictions
39
+ predictions = loaded_model.predict(img_array)
40
+
41
+ # Convert predictions to the corresponding class
42
+ predicted_class = np.argmax(predictions)
43
+
44
+ # Return the predicted class and the example image URL
45
+ return f"**Predicted Diabetic Retinopathy Stage:** {class_mapping[predicted_class]}", example_image_url
46
+
47
+ except Exception as e:
48
+ return f"An error occurred: {str(e)}", example_image_url
49
+
50
+ # Create the Gradio interface with fixed example images
51
+ example_images = [
52
+ "No_DR.png",
53
+ "Mild.png",
54
+ "Moderate.png",
55
+ "Proliferate_DR.png"
56
+ ]
57
+
58
+ iface = gr.Interface(
59
+ fn=lambda img: predict_diabetic_retinopathy(img, loaded_model),
60
+ inputs=gr.Image(type="numpy", label="Upload Retina Image"),
61
+ outputs=[gr.Markdown(label="Prediction Result"), gr.Image(value=example_image_url, label="Example Image")],
62
+ title="Diabetic Retinopathy Prediction",
63
+ description="Upload an image of the retina to predict the stage of diabetic retinopathy.",
64
+ theme="default",
65
+ examples=example_images
66
+ )
67
+
68
+ # Launch the interface
69
+ iface.launch()