File size: 2,154 Bytes
47ae0a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
import pandas as pd
import cv2 as cv
import streamlit as st
import matplotlib.pyplot as plt
from skimage.feature import graycomatrix, graycoprops
import joblib

indextable = ['dissimilarity', 'contrast',
              'homogeneity', 'energy', 'correlation', 'Label']
obj = {
    0.0: "Normal",
    1.0: "Cataract",
    2.0: "Glaucoma",
    3.0: 'Retina Disease'
}
width, height = 400, 400
distance = 10
teta = 90

# Code to extract features from Image using Gray Level Co occurrence Image


def get_feature(matrix, name):
    feature = graycoprops(matrix, name)
    result = np.average(feature)
    return result


def preprocessingImage(image):
    test_img = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    test_img_gray = cv.cvtColor(test_img, cv.COLOR_RGB2GRAY)
    test_img_thresh = cv.adaptiveThreshold(
        test_img_gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 11, 3)

    cnts = cv.findContours(
        test_img_thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    cnts = sorted(cnts, key=cv.contourArea, reverse=True)

    for c in cnts:
        x, y, w, h = cv.boundingRect(c)
        test_img_ROI = test_img[y:y+h, x:x+w]
        break

    test_img_ROI_resize = cv.resize(test_img_ROI, (width, height))
    test_img_ROI_resize_gray = cv.cvtColor(
        test_img_ROI_resize, cv.COLOR_RGB2GRAY)

    return test_img_ROI_resize_gray


def extract(path):
    data_eye = np.zeros((5, 1))

    # path = cv.imread(path)
    img = preprocessingImage(path)

    glcm = graycomatrix(img, [distance], [teta],
                        levels=256, symmetric=True, normed=True)

    for i in range(len(indextable[:-1])):
        features = []
        feature = get_feature(glcm, indextable[i])
        features.append(feature)
        data_eye[i, 0] = features[0]
    return pd.DataFrame(np.transpose(data_eye), columns=indextable[:-1])


"""
Return predicted class with its probability
"""

model = joblib.load("model.pkl")



def predict(path):
    X = extract(path)
    y = model.predict(X)[0]
    prob = model.predict_proba(X)[0, int(y)]
    return (obj[y], prob)