Spaces:
Running
Running
| import cv2 | |
| import numpy as np | |
| import gradio as gr | |
| def apply_blur(image): | |
| return cv2.GaussianBlur(image, (15, 15), 0) | |
| def apply_invert(image): | |
| return cv2.bitwise_not(image) | |
| def apply_sharpen(image): | |
| kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) | |
| return cv2.filter2D(image, -1, kernel) | |
| def apply_black_white(image): | |
| return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
| def apply_filter(image, filter_type): | |
| image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) | |
| if filter_type == 'blur': | |
| result = apply_blur(image) | |
| elif filter_type == 'invert': | |
| result = apply_invert(image) | |
| elif filter_type == 'sharpen': | |
| result = apply_sharpen(image) | |
| elif filter_type == 'black & white': | |
| result = apply_black_white(image) | |
| else: | |
| result = image | |
| if len(result.shape) == 2: | |
| result = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR) | |
| return cv2.cvtColor(result, cv2.COLOR_BGR2RGB) | |
| def run_interface(): | |
| filter_choices = ['blur', 'invert', 'sharpen', 'black & white'] | |
| interface = gr.Interface( | |
| fn=apply_filter, | |
| inputs=[gr.Image(type='numpy'), | |
| gr.Dropdown(filter_choices, label='Select Filter')], | |
| outputs=gr.Image(type='numpy'), | |
| live=2 | |
| ) | |
| return interface | |
| iface = run_interface() | |
| iface.launch() | |