Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +70 -0
- models.py +82 -0
- requirements.txt +11 -0
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
import plotly.graph_objects as go
|
5 |
+
from models import predict
|
6 |
+
import cv2
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
google_form_link = 'https://docs.google.com/forms/d/1xKeveRFf90_wCX-tjMInFC48XmFF8HOsPSQ47ruOFk0/edit'
|
11 |
+
# Load the pre-trained Haar Cascade for eye detection
|
12 |
+
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
|
13 |
+
|
14 |
+
# Function to check if the image contains an eye
|
15 |
+
def contains_eye(image):
|
16 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
17 |
+
eyes = eye_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
18 |
+
return len(eyes) > 0
|
19 |
+
|
20 |
+
# Function to load the image
|
21 |
+
def load_image(image_file):
|
22 |
+
img = Image.open(image_file)
|
23 |
+
return img
|
24 |
+
|
25 |
+
# Streamlit app title
|
26 |
+
st.title("Eye Cataract Detection")
|
27 |
+
|
28 |
+
# Streamlit header and subheader
|
29 |
+
st.header('Upload the image')
|
30 |
+
|
31 |
+
# File uploader widget
|
32 |
+
image_file = st.file_uploader("Upload Images", type=["png", "jpg", "jpeg"])
|
33 |
+
|
34 |
+
# Check if an image file is uploaded
|
35 |
+
if image_file is not None:
|
36 |
+
img = load_image(image_file)
|
37 |
+
|
38 |
+
# Display the uploaded image
|
39 |
+
st.image(img, width=250)
|
40 |
+
|
41 |
+
# Convert image to OpenCV format
|
42 |
+
open_cv_image = np.array(img)
|
43 |
+
|
44 |
+
if contains_eye(open_cv_image):
|
45 |
+
# Button for detection
|
46 |
+
if st.button('Detect'): # This line adds a 'Detect' button
|
47 |
+
# Predict the label and probability
|
48 |
+
label, prob = predict(open_cv_image)
|
49 |
+
|
50 |
+
# Use markdown to style the text and include emojis
|
51 |
+
if prob > 0.5:
|
52 |
+
st.markdown(f"<h2 style='color: red;'>Cataract Detected 😟</h2>", unsafe_allow_html=True)
|
53 |
+
#st.markdown(f"### Probability: **{prob:.2f}**")
|
54 |
+
else:
|
55 |
+
st.markdown(f"<h2 style='color: green;'>No Cataract Detected 😄</h2>", unsafe_allow_html=True)
|
56 |
+
#st.markdown(f"### Probability: **{prob:.2f}**")
|
57 |
+
|
58 |
+
# Pie chart visualization
|
59 |
+
fig = go.Figure(data=[go.Pie(labels=['Cataract', 'No Cataract'],
|
60 |
+
values=[prob, 1 - prob],
|
61 |
+
hoverinfo='label+percent',
|
62 |
+
pull=[0, 0])])
|
63 |
+
fig.update_layout(title_text='Cataract Detection Probability')
|
64 |
+
st.plotly_chart(fig)
|
65 |
+
|
66 |
+
st.subheader("Doctor's Verification")
|
67 |
+
st.markdown(f"[Click here to provide feedback on the cataract detection results]({google_form_link})", unsafe_allow_html=True)
|
68 |
+
|
69 |
+
else:
|
70 |
+
st.error("No eyes detected in the image. Please upload a relevant eye image.")
|
models.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import cv2 as cv
|
4 |
+
import streamlit as st
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from skimage.feature import graycomatrix, graycoprops
|
7 |
+
import joblib
|
8 |
+
|
9 |
+
indextable = ['dissimilarity', 'contrast',
|
10 |
+
'homogeneity', 'energy', 'correlation', 'Label']
|
11 |
+
obj = {
|
12 |
+
0.0: "Normal",
|
13 |
+
1.0: "Cataract",
|
14 |
+
2.0: "Glaucoma",
|
15 |
+
3.0: 'Retina Disease'
|
16 |
+
}
|
17 |
+
width, height = 400, 400
|
18 |
+
distance = 10
|
19 |
+
teta = 90
|
20 |
+
|
21 |
+
# Code to extract features from Image using Gray Level Co occurrence Image
|
22 |
+
|
23 |
+
|
24 |
+
def get_feature(matrix, name):
|
25 |
+
feature = graycoprops(matrix, name)
|
26 |
+
result = np.average(feature)
|
27 |
+
return result
|
28 |
+
|
29 |
+
|
30 |
+
def preprocessingImage(image):
|
31 |
+
test_img = cv.cvtColor(image, cv.COLOR_BGR2RGB)
|
32 |
+
test_img_gray = cv.cvtColor(test_img, cv.COLOR_RGB2GRAY)
|
33 |
+
test_img_thresh = cv.adaptiveThreshold(
|
34 |
+
test_img_gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 11, 3)
|
35 |
+
|
36 |
+
cnts = cv.findContours(
|
37 |
+
test_img_thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
|
38 |
+
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
|
39 |
+
cnts = sorted(cnts, key=cv.contourArea, reverse=True)
|
40 |
+
|
41 |
+
for c in cnts:
|
42 |
+
x, y, w, h = cv.boundingRect(c)
|
43 |
+
test_img_ROI = test_img[y:y+h, x:x+w]
|
44 |
+
break
|
45 |
+
|
46 |
+
test_img_ROI_resize = cv.resize(test_img_ROI, (width, height))
|
47 |
+
test_img_ROI_resize_gray = cv.cvtColor(
|
48 |
+
test_img_ROI_resize, cv.COLOR_RGB2GRAY)
|
49 |
+
|
50 |
+
return test_img_ROI_resize_gray
|
51 |
+
|
52 |
+
|
53 |
+
def extract(path):
|
54 |
+
data_eye = np.zeros((5, 1))
|
55 |
+
|
56 |
+
# path = cv.imread(path)
|
57 |
+
img = preprocessingImage(path)
|
58 |
+
|
59 |
+
glcm = graycomatrix(img, [distance], [teta],
|
60 |
+
levels=256, symmetric=True, normed=True)
|
61 |
+
|
62 |
+
for i in range(len(indextable[:-1])):
|
63 |
+
features = []
|
64 |
+
feature = get_feature(glcm, indextable[i])
|
65 |
+
features.append(feature)
|
66 |
+
data_eye[i, 0] = features[0]
|
67 |
+
return pd.DataFrame(np.transpose(data_eye), columns=indextable[:-1])
|
68 |
+
|
69 |
+
|
70 |
+
"""
|
71 |
+
Return predicted class with its probability
|
72 |
+
"""
|
73 |
+
|
74 |
+
model = joblib.load("model.pkl")
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
def predict(path):
|
79 |
+
X = extract(path)
|
80 |
+
y = model.predict(X)[0]
|
81 |
+
prob = model.predict_proba(X)[0, int(y)]
|
82 |
+
return (obj[y], prob)
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
pandas
|
3 |
+
opencv-python
|
4 |
+
matplotlib
|
5 |
+
scikit-learn
|
6 |
+
scikit-image
|
7 |
+
streamlit
|
8 |
+
joblib
|
9 |
+
pillow
|
10 |
+
plotly
|
11 |
+
opencv-python-headless==4.5.4.60
|