File size: 1,330 Bytes
91f3098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()