import gradio as gr import cv2 import numpy as np from PIL import Image def convolve(img, kernel): out_img = cv2.filter2D(src=img, ddepth =-1, kernel=kernel) out_img = cv2.cvtColor(out_img, cv2.COLOR_BGR2RGB) out_img = cv2.resize(out_img, (226,226)) return Image.fromarray(out_img) def app(img, filter): sobel_verticle = np.array([[-1,0,1], [-1,0,1], [-1,0,1]]) sobel_horizontal = np.array([[-1,-1,-1], [ 0, 0, 0], [ 1, 1, 1]]) laplacian = np.array([[ 0,-1, 0], [-1, 5,-1], [ 0,-1, 0]]) average = np.array([[1/9,1/9, 1/9], [1/9,1/9, 1/9], [1/9,1/9, 1/9]]) weighted_average = np.array([[1/16,2/16, 1/16], [2/16,4/16, 2/16], [1/16,2/16, 1/16]]) if filter =="sobel_verticle": return convolve(img, sobel_verticle) elif filter =="sobel_horizontal": return convolve(img, sobel_horizontal) elif filter == "laplacian": return convolve(img, laplacian) elif filter == "average": return convolve(img, average) else: return convolve(img, weighted_average) demo = gr.Interface( app, [ gr.Image(shape=(226, 226)), gr.Radio(["sobel_verticle", "sobel_horizontal", "laplacian", "average", "weighted_average"]) ], gr.Image(shape=(226, 226)), ) demo.launch()