File size: 2,133 Bytes
e3d027a
 
33d5aa0
4ec77fa
b32bffa
33d5aa0
b32bffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef7fcb2
b32bffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33d5aa0
 
f002879
 
33d5aa0
 
f002879
33d5aa0
4ec77fa
 
 
 
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
__all__ = ['is_glaucoma', 'learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']

from fastai.vision.all import *
import gradio as gr
from PIL import Image


def get_x(row, is_test=False):
    image_path = path_image_combined / (row['id_code'])
    transformed_image = custom_transform(image_path)

    # Check the label of the current image and apply augmentations if it belongs to the minority class
    if not is_test and row['label'] == 1:
        transformed_image = additional_augmentations(transformed_image)        
    
    return Image.fromarray(transformed_image)

# Define how to get the labels
def get_y(row):
    return row['label']  # adjust this depending on how your csv is structured

# Define the transformations
def custom_transform(image_path):
    image = cv2.imread(str(image_path)) # Read the image file.
    if image is None:
        return None
    
    # Convert the image from BGR to RGB
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
    # Apply filters and transformations
    # Gaussian filter
    image = cv2.GaussianBlur(image, (5, 5), 0)
    
    # Histogram Equalization
    img_yuv = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
    img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])
    image = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2RGB)
    
    # Median filter
    image = cv2.medianBlur(image, 3)
    
    # Bypass filter (leaving the image unchanged)
    # (add any specific implementation if needed)
    
    # Sharpening filter
    kernel = np.array([[0, -1, 0], 
                       [-1, 5,-1], 
                       [0, -1, 0]])
    image = cv2.filter2D(image, -1, kernel)
    
    # Resize the image to a target size of 224x224 pixels.
    image = cv2.resize(image, (224, 224))
    return image

learn = load_learner('your_model.pkl')

categories = ('Glaucoma Present','Glaucoma Absent')

def predict(img):
    pred,pred_idx,probs = learn.predict(img)
    return dict(zip(categories, map(float,probs)))

image = gr.inputs.Image(shape=(512,512))
label = gr.outputs.Label()
intf = gr.Interface(fn=predict, inputs=image, outputs=label)
intf.launch(inline=False)