File size: 1,149 Bytes
1d6d9eb
cebe88e
 
 
dc28481
 
 
cebe88e
dc28481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cebe88e
dc28481
cebe88e
dc28481
 
 
1d6d9eb
dc28481
 
 
1d6d9eb
cebe88e
dc28481
 
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
import gradio as gr
import cv2
import numpy as np

def apply_filter(image, filter_type):
    if image is None:
        return None

    if filter_type == "blur":
        image = cv2.GaussianBlur(image, (21, 21), 0)
    elif filter_type == "canny":
        image = cv2.Canny(image, 100, 200)
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
    elif filter_type == "grayscale":
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
    elif filter_type == "sepia":
        kernel = np.array(
            [[0.272, 0.534, 0.131],
             [0.349, 0.686, 0.168],
             [0.393, 0.769, 0.189]]
        )
        image = cv2.transform(image, kernel)
    elif filter_type == "invert":
        image = cv2.bitwise_not(image)
    # else "none", do nothing

    return image

filter_options = ["none", "blur", "grayscale", "sepia", "canny", "invert"]

demo = gr.Interface(
    fn=apply_filter,
    inputs=[gr.Camera(), gr.Radio(filter_options, label="Filter")],
    outputs="image",
    live=True
)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)