Sajjad Ali commited on
Commit
06d6d2e
1 Parent(s): 6d14492

Added code

Browse files
Files changed (4) hide show
  1. app.py +25 -0
  2. model.pkl +3 -0
  3. models.py +82 -0
  4. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from models import predict
4
+ import numpy
5
+
6
+
7
+ def load_image(image_file):
8
+ img = Image.open(image_file)
9
+ return img
10
+
11
+
12
+ st.title("Cataract Image Classification")
13
+
14
+ st.header('Enter the fundus image')
15
+ st.subheader("KNN Model")
16
+
17
+ image_file = st.file_uploader("Upload Images", type=["png", "jpg", "jpeg"])
18
+
19
+ if image_file is not None:
20
+ img = load_image(image_file)
21
+ st.image(img, width=250)
22
+ open_cv_image = numpy.array(img)
23
+ label, prob = predict(open_cv_image)
24
+ st.write(f"Label : {label}")
25
+ st.write(f"Probability: {prob}")
model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fa462398536a1ad24d9dcd31975a6ffd20e15bdfdc7d9bfde17fbb0bad05a7c4
3
+ size 121565
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
+ @st.cache
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,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ numpy
2
+ pandas
3
+ opencv-python
4
+ matplotlib
5
+ scikit-learn
6
+ scikit-image
7
+ streamlit
8
+ joblib
9
+ pillow