Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,14 @@ import tensorflow as tf
|
|
3 |
import numpy as np
|
4 |
import cv2
|
5 |
from PIL import Image
|
6 |
-
from
|
|
|
7 |
|
8 |
# Load the model
|
9 |
-
model = tf.keras.models.load_model('DiabeticModel.keras')
|
10 |
|
11 |
# Define class labels
|
12 |
-
class_labels = ['
|
13 |
|
14 |
# Function to preprocess the uploaded image
|
15 |
def preprocess_image(image: Image.Image):
|
@@ -19,6 +20,27 @@ def preprocess_image(image: Image.Image):
|
|
19 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
20 |
return img_array
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# Streamlit interface
|
23 |
st.title("Diabetic Retinopathy Detection App")
|
24 |
st.write("Welcome to our Diabetic Retinopathy Detection App! This app utilizes deep learning models to detect diabetic retinopathy in retinal images. Diabetic retinopathy is a common complication of diabetes and early detection is crucial for effective treatment.")
|
@@ -27,17 +49,24 @@ st.write("Welcome to our Diabetic Retinopathy Detection App! This app utilizes d
|
|
27 |
tab1, tab2 = st.tabs(["π Upload Image", "π· Use Camera"])
|
28 |
|
29 |
with tab1:
|
|
|
|
|
|
|
|
|
|
|
30 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
31 |
if uploaded_file is not None:
|
32 |
# Open and display the uploaded image
|
33 |
image = Image.open(uploaded_file)
|
34 |
-
st.image(image, caption='Uploaded Image', use_column_width=200)
|
35 |
|
36 |
# Preprocess the image
|
37 |
img_array = preprocess_image(image)
|
38 |
|
|
|
|
|
|
|
39 |
# Make prediction
|
40 |
-
predictions = model.predict(
|
41 |
|
42 |
# Convert predictions to percentages
|
43 |
prediction_percentages = predictions * 100
|
@@ -45,43 +74,30 @@ with tab1:
|
|
45 |
# Find the class with the highest probability
|
46 |
highest_index = np.argmax(prediction_percentages)
|
47 |
predicted_class = class_labels[highest_index]
|
48 |
-
st.write("Classifying...")
|
49 |
|
50 |
-
# Display the predictions
|
51 |
-
st.
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
st.write(f"{label}: {prediction_percentages[i]:.2f}%")
|
57 |
-
|
58 |
-
with tab2:
|
59 |
-
st.write("Use your webcam to capture an image for prediction.")
|
60 |
-
|
61 |
-
# Define a custom video transformer for Streamlit WebRTC
|
62 |
-
class VideoTransformer(VideoTransformerBase):
|
63 |
-
def __init__(self):
|
64 |
-
self.result = None
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
image = Image.fromarray(img_rgb)
|
70 |
-
|
71 |
-
# Preprocess the image
|
72 |
-
img_array = preprocess_image(image)
|
73 |
-
|
74 |
-
# Make prediction
|
75 |
-
predictions = model.predict([img_array, img_array])[0]
|
76 |
-
prediction_percentages = predictions * 100
|
77 |
-
highest_index = np.argmax(prediction_percentages)
|
78 |
-
self.result = class_labels[highest_index]
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import numpy as np
|
4 |
import cv2
|
5 |
from PIL import Image
|
6 |
+
from fpdf import FPDF
|
7 |
+
from io import BytesIO
|
8 |
|
9 |
# Load the model
|
10 |
+
model = tf.keras.models.load_model(r'DiabeticModel.keras')
|
11 |
|
12 |
# Define class labels
|
13 |
+
class_labels = ['Healthy', 'Mild DR', 'Moderate DR', 'Severe DR', 'Proliferative DR']
|
14 |
|
15 |
# Function to preprocess the uploaded image
|
16 |
def preprocess_image(image: Image.Image):
|
|
|
20 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
21 |
return img_array
|
22 |
|
23 |
+
# Function to create PDF report
|
24 |
+
def create_pdf(patient_name, patient_age, predicted_class, prediction_percentages):
|
25 |
+
pdf = FPDF()
|
26 |
+
pdf.add_page()
|
27 |
+
pdf.set_font("Arial", size=12)
|
28 |
+
pdf.cell(200, 10, txt="Diabetic Retinopathy Detection Report", ln=True, align='C')
|
29 |
+
pdf.cell(200, 10, txt=f"Patient Name: {patient_name}", ln=True, align='L')
|
30 |
+
pdf.cell(200, 10, txt=f"Patient Age: {patient_age}", ln=True, align='L')
|
31 |
+
pdf.cell(200, 10, txt=f"Predicted Level: {predicted_class}", ln=True, align='L')
|
32 |
+
|
33 |
+
# Add the DR level prediction details
|
34 |
+
for i, label in enumerate(class_labels):
|
35 |
+
pdf.cell(200, 10, txt=f"{label}: {prediction_percentages[i]:.2f}%", ln=True, align='L')
|
36 |
+
|
37 |
+
# Generate PDF content as a byte string (dest='S' for return as string)
|
38 |
+
pdf_content = pdf.output(dest='S').encode('latin1') # Encoding to 'latin1' ensures compatibility with PDF format
|
39 |
+
|
40 |
+
# Convert the byte string to BytesIO object
|
41 |
+
pdf_output = BytesIO(pdf_content)
|
42 |
+
return pdf_output
|
43 |
+
|
44 |
# Streamlit interface
|
45 |
st.title("Diabetic Retinopathy Detection App")
|
46 |
st.write("Welcome to our Diabetic Retinopathy Detection App! This app utilizes deep learning models to detect diabetic retinopathy in retinal images. Diabetic retinopathy is a common complication of diabetes and early detection is crucial for effective treatment.")
|
|
|
49 |
tab1, tab2 = st.tabs(["π Upload Image", "π· Use Camera"])
|
50 |
|
51 |
with tab1:
|
52 |
+
# Patient details input
|
53 |
+
st.write("### Enter Patient Details")
|
54 |
+
patient_name = st.text_input("Patient Name")
|
55 |
+
patient_age = st.number_input("Patient Age", min_value=0)
|
56 |
+
|
57 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
58 |
if uploaded_file is not None:
|
59 |
# Open and display the uploaded image
|
60 |
image = Image.open(uploaded_file)
|
|
|
61 |
|
62 |
# Preprocess the image
|
63 |
img_array = preprocess_image(image)
|
64 |
|
65 |
+
# Prepare inputs for model
|
66 |
+
img_array_pair = [img_array, img_array] # Model expects two inputs
|
67 |
+
|
68 |
# Make prediction
|
69 |
+
predictions = model.predict(img_array_pair)[0]
|
70 |
|
71 |
# Convert predictions to percentages
|
72 |
prediction_percentages = predictions * 100
|
|
|
74 |
# Find the class with the highest probability
|
75 |
highest_index = np.argmax(prediction_percentages)
|
76 |
predicted_class = class_labels[highest_index]
|
|
|
77 |
|
78 |
+
# Display the image and predictions side by side
|
79 |
+
col1, col2 = st.columns([1, 2]) # Set the width ratio between columns
|
80 |
|
81 |
+
# Display image in the first column with limited width
|
82 |
+
with col1:
|
83 |
+
st.image(image, caption='Uploaded Image', width=150)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
+
# Display the predictions in the second column
|
86 |
+
with col2:
|
87 |
+
st.write(f"### Predicted Level: **{predicted_class}**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
st.write("### Prediction Results")
|
90 |
+
for i, label in enumerate(class_labels):
|
91 |
+
st.progress(int(prediction_percentages[i]))
|
92 |
+
st.write(f"{label}: {prediction_percentages[i]:.2f}%")
|
93 |
+
|
94 |
+
# Create and download PDF report
|
95 |
+
pdf_output = create_pdf(patient_name, patient_age, predicted_class, prediction_percentages)
|
96 |
+
|
97 |
+
# Button to download the PDF
|
98 |
+
st.download_button(
|
99 |
+
label="Download Report",
|
100 |
+
data=pdf_output,
|
101 |
+
file_name=f"Diabetic_Retinopathy_Report_{patient_name.replace(' ', '_')}.pdf",
|
102 |
+
mime='application/octet-stream'
|
103 |
+
)
|