Spaces:
Runtime error
Runtime error
__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) |