Skip to content

Commit

Permalink
standardize image size, add requirement, add readme, add image sample
Browse files Browse the repository at this point in the history
  • Loading branch information
anasnafis77 committed Oct 29, 2022
1 parent 1df1d9f commit 6531a5e
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv
Binary file added Code/__pycache__/classification.cpython-38.pyc
Binary file not shown.
Binary file added Code/__pycache__/localization.cpython-38.pyc
Binary file not shown.
Binary file added Code/__pycache__/segmentation.cpython-38.pyc
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions Code/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np
import pickle
class inference_glaucoma():
def __init__(self, model_path='Code/Models/Inference model'):
def __init__(self, model_path='Models/Inference model/inference_model.sav'):
# load model
self.model = pickle.load(open(model_path, 'rb'))
def CDR_calc(self, OD_mask, OC_mask):
Expand All @@ -28,7 +28,7 @@ def CDR_calc(self, OD_mask, OC_mask):
return VCDR, HCDR, ACDR


def predict(self, model, feature):
def predict(self, feature):
predict = self.model.predict([feature])
if predict[0] == 1:
return 'Glaucoma'
Expand Down
14 changes: 7 additions & 7 deletions Code/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
class OD_localization():
def __init__(self):
# loading template
image_R = cv2.imread('Code/Templates/ROItemplateRed.png', 0)
image_G = cv2.imread('Code/Templates/ROItemplateGreen.png', 0)
image_B = cv2.imread('Code/Templates/ROItemplateBlue.png', 0)
image_R = cv2.imread('Templates/ROItemplateRed.png', 0)
image_G = cv2.imread('Templates/ROItemplateGreen.png', 0)
image_B = cv2.imread('Templates/ROItemplateBlue.png', 0)
self.image_templates = [image_R, image_G, image_B]

def preprocessing(self, src, std_size):
return resize(src, output_shape=std_size, mode = 'constant')
return cv2.resize(src, std_size, interpolation=cv2.INTER_CUBIC)

def extract_BR_map(self, src, mask, numSegments=50, sigma=10):
"""
Expand Down Expand Up @@ -121,7 +121,7 @@ def image_template_matching(self, clahe_retinal_img, template, mask):
return NCC_maps


def predict(self, src, coeff_args, test_on=False):
def locate(self, src, coeff_args, test_on=False):
"""
This is the main function for localization of Optic Disc
Expand All @@ -142,7 +142,7 @@ def predict(self, src, coeff_args, test_on=False):
test_on is True.
"""

R_COEFF, G_COEFF, B_COEFF, BR_COEFF = coeff_args
h, w = src.shape[:2]

# Implement CLAHE to the input image
Expand All @@ -169,7 +169,7 @@ def predict(self, src, coeff_args, test_on=False):
brightness_map = self.extract_BR_map(src, mask)

# Combining localization maps
combined_map = red_NCC * r_coeff + green_NCC*g_coeff + blue_NCC*b_coeff + brightness_map*br_coeff
combined_map = red_NCC * R_COEFF + green_NCC*G_COEFF + blue_NCC*B_COEFF + brightness_map*BR_COEFF
all_maps = [combined_map, red_NCC, green_NCC, blue_NCC, brightness_map]

# Extracting maximum value of NCC
Expand Down
30 changes: 12 additions & 18 deletions Code/Inference_script.py → Code/main.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import cv2
import sys
import time
import os
import math
import pickle
import numpy as np
import tkinter as tk
from skimage.transform import resize
from skimage.segmentation import slic
from matplotlib import pyplot as plt
from keras import backend as K
from tkinter import filedialog
from localization import OD_localization
from segmentation import optic_segmentation
from classification import inference_glaucoma
from supporting_function import *

if __name__ == "__main__":
# take path from argument
file_path = sys.argv[1]

# Load module objects
ODFinder = OD_localization() # localization model
segModel = optic_segmentation() # segmentation model
Expand All @@ -37,31 +32,30 @@

# Localize Optic Disc
start_loc = time.time()
disc_center = ODFinder(ret_img, coeff_args=coeff_args)
disc_center = ODFinder.locate(ret_img, coeff_args=coeff_args)
end_loc = time.time()

print(disc_center)
# OD and OC segmentation
start_seg = time.time()
clahe = cv2.createCLAHE(clipLimit =2.0, tileGridSize=(8,8))
cl_img = clahe.apply(retinal_image[:, :, 1])
ROI, coordinate = ekstrakROI(ret_img, ROI_size, cl_img)
OD_pred, OC_pred = segModel.do_segmentation(ROI, coordinate, )
cl_img = clahe.apply(ret_img[:, :, 1]) # apply clahe on green channel
ROI, coordinate = ekstrakROI(disc_center, ROI_SIZE, cl_img)
OD_pred, OC_pred = segModel.do_segmentation(ROI, coordinate, ret_img.shape[:2])
end_seg = time.time()


print('Localization time: {:.2f} s'.format(end_loc-start_loc))
print('Segmentation time: {:.2f} s'.format(end_seg-start_seg))

# Inference the feature
VCDR,HCDR,ACDR = CDR_calc(OD_mask, OC_mask)
VCDR,HCDR,ACDR = glPredictor.CDR_calc(OD_pred, OC_pred)
feature = np.array([VCDR, ACDR]) # best prediction achieved by using only VCDR and ACDR
pred = glaucoma_predict(inference_model, feature)
pred = glPredictor.predict(feature)

print('Detection report of file ', filename)
print('Detection report')
print('Prediction: {}'.format(pred))

fig, ax = plt.subplots(1, 1, figsize= (5, 5))
ax.imshow(retinal_image)
ax.imshow(ret_img)
ax.contour(OC_pred, colors='b')
ax.contour(OD_pred, colors='w')
ax.grid(False)
Expand Down
1 change: 0 additions & 1 deletion Code/result.json

This file was deleted.

Binary file added Code/sample/V0400.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion Code/segmentation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cv2
import numpy as np
from skimage.transform import resize
from keras import backend as K
import tensorflow as tf

def fscore(y_true, y_pred):
Expand All @@ -9,7 +10,7 @@ def fscore(y_true, y_pred):
return 2*((precision*recall)/(precision+recall+K.epsilon()))

class optic_segmentation():
def __init__(self, model_OD_path='Code/Models/model OD semantic', model_OC_path='Code/Models/model OC semantic'):
def __init__(self, model_OD_path='Models/model OD semantic', model_OC_path='Models/model OC semantic'):
# Load semantic segmentation model
self.model_OD = tf.keras.models.load_model(model_OD_path,custom_objects={'fscore':fscore})
self.model_OC = tf.keras.models.load_model(model_OC_path,custom_objects={'fscore':fscore})
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

## Intoduction:

Glaucoma is the second cause of blindness. Unfortunately, Indonesian people still unaware for the importance of early glaucoma detection. Moreover, Glaucoma Detection is the laborous and subjective job for ophtalmologists. Thus, we need the automation for glaucoma detection. We use glaucoma detection based on Optic Cup and Disc segmentation. This automation allow us to detect glaucoma more faster and less subjective. Furthermore, this system could be implemented in smartphone, hence more patient could monitor their glaucoma stage in much more affordable way. For further algorithm explanation, you can read this paper code [here](http://www.joig.net/index.php?m=content&c=index&a=show&catid=78&id=299)
Glaucoma is the second cause of blindness. Unfortunately, Indonesian people still unaware for the importance of early glaucoma detection. Moreover, Glaucoma Detection is the laborous and subjective job for ophtalmologists. Thus, we need the automation for glaucoma detection. We use glaucoma detection based on Optic Cup and Disc segmentation. This automation allow us to detect glaucoma more faster and less subjective. Furthermore, this system could be implemented in smartphone, hence more patient could monitor their glaucoma stage in much more affordable way. For further algorithm explanation, you can read our paper [here](http://www.joig.net/index.php?m=content&c=index&a=show&catid=78&id=299).

Below are the main algorithm for Glaucoma Detection used in this project:
The main algorithm for our Glaucoma Detection is follow:
1. Preprocessing
2. Optic Disc Localization
3. Optic Disc dan Cup Segmentation
4. Glaucoma Feature Extraction
5. Classification
5. Glaucoma Prediction

[Glaucoma detection algorithm](https://github.com/anasnafis77/Glaucoma-Detection-UNet/tree/main/readme_img/Alur algoritma for poster english.png)

## Usage
Below are the procedure for using this script:
1. Run this: `python inference_script.py` in command prompt. Make sure you are in 'Code' directory.
2. Choose your retinal image.
3. After a few seconds, the detection result should appear in command prompt and json file.
1. Open your command line interface (command prompt, powershell, etc.) and go to project directory.
2. Install all libraries in requirements.txt (pip install -r requirements.txt).
3. Run this: `python main.py /path/to/retinal_image.jpg` in command prompt. Make sure you are in 'Code' directory.
4. After a few seconds, the detection result should appear in your command line interface and segmentation result would appear from matplotlib window.

## Notebooks:
Main Notebook:
Expand All @@ -27,7 +30,7 @@ Localization Notebook:
Segmentation Notebook:
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/anasnafis77/Deteksi-Glaukoma/blob/main/Notebooks/Segmentation_notebook.ipynb)

Inferencing Notebook:
Glaucoma prediction Notebook:
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/anasnafis77/Deteksi-Glaukoma/blob/main/Notebooks/Inferencing_notebook.ipynb)

## Reference
Expand Down
Binary file added readme_img/Alur algoritma for poster english.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
absl-py==1.3.0
astunparse==1.6.3
cachetools==5.2.0
certifi==2022.9.24
charset-normalizer==2.1.1
click==8.1.3
colorama==0.4.6
cycler==0.11.0
docopt==0.6.2
Flask==2.2.2
flatbuffers==22.10.26
gast==0.4.0
google-auth==2.13.0
google-auth-oauthlib==0.4.6
google-pasta==0.2.0
grpcio==1.50.0
h5py==3.7.0
idna==3.4
imageio==2.22.2
importlib-metadata==5.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
joblib==1.2.0
keras==2.9.0
Keras-Preprocessing==1.1.2
kiwisolver==1.4.4
libclang==14.0.6
Markdown==3.4.1
MarkupSafe==2.1.1
matplotlib==3.3.1
networkx==2.8.7
numpy==1.23.2
oauthlib==3.2.2
opencv-python==4.6.0.66
opt-einsum==3.3.0
packaging==21.3
pandas==1.4.3
Pillow==9.2.0
pipreqs==0.4.11
protobuf==3.19.6
pyasn1==0.4.8
pyasn1-modules==0.2.8
pyparsing==3.0.9
python-dateutil==2.8.2
pytz==2022.5
PyWavelets==1.4.1
requests==2.28.1
requests-oauthlib==1.3.1
rsa==4.9
scikit-image==0.19.3
scikit-learn==1.1.3
scipy==1.9.3
six==1.16.0
sklearn==0.0
tensorboard==2.10.1
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.1
tensorflow==2.10.0
tensorflow-estimator==2.10.0
tensorflow-gpu==2.9.1
tensorflow-io-gcs-filesystem==0.27.0
termcolor==2.0.1
threadpoolctl==3.1.0
tifffile==2022.10.10
typing-extensions==4.4.0
urllib3==1.26.12
Werkzeug==2.2.2
wrapt==1.14.1
yarg==0.1.9
zipp==3.10.0

0 comments on commit 6531a5e

Please sign in to comment.