File size: 3,490 Bytes
5825d5b
 
 
 
 
 
 
 
3caf593
5825d5b
 
 
 
 
e3c3f88
5825d5b
 
 
 
 
 
 
 
3caf593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5825d5b
 
cfa42c2
 
5825d5b
 
e3c3f88
3caf593
5825d5b
 
 
e3c3f88
 
 
 
 
 
 
 
 
 
 
 
 
3caf593
e3c3f88
 
5825d5b
e3c3f88
5825d5b
 
 
 
 
 
 
e3c3f88
5825d5b
 
 
 
e3c3f88
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# created with great guidance from https://github.com/NimaBoscarino

import gradio as gr

import kornia as K
from kornia.core import Tensor


def filters_without(file, blur_pool2d, box_blur, gaussian_blur2d, max_blur_pool2d, median_blur):      
    # load the image using the rust backend          
    img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
    img = img[None]  # 1xCxHxW / fp32 / [0, 1]

    # apply tensor image enhancement
    x_out: Tensor = K.filters.box_blur(img, (int(box_blur), int(box_blur)))
    x_out = K.filters.gaussian_blur2d(x_out, 
                                      (int(gaussian_blur2d), int(gaussian_blur2d)), 
                                      (float(gaussian_blur2d), float(gaussian_blur2d)))
    x_out = K.filters.max_blur_pool2d(x_out, int(max_blur_pool2d))
    x_out = K.filters.median_blur(x_out, (int(median_blur), int(median_blur)))

    return K.utils.tensor_to_image(x_out)

def filters_with(file, blur_pool2d, box_blur, gaussian_blur2d, max_blur_pool2d, median_blur):      
    # load the image using the rust backend          
    img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
    img = img[None]  # 1xCxHxW / fp32 / [0, 1]

    # apply tensor image enhancement
    x_out: Tensor = K.filters.blur_pool2d(x_out, int(blur_pool2d))
    x_out = K.filters.box_blur(img, (int(box_blur), int(box_blur)))
    x_out = K.filters.gaussian_blur2d(x_out, 
                                      (int(gaussian_blur2d), int(gaussian_blur2d)), 
                                      (float(gaussian_blur2d), float(gaussian_blur2d)))
    x_out = K.filters.max_blur_pool2d(x_out, int(max_blur_pool2d))
    x_out = K.filters.median_blur(x_out, (int(median_blur), int(median_blur)))

    return K.utils.tensor_to_image(x_out)


examples = [
    ["examples/monkey.jpg", 1, 1, 1, 1, 1],
    ["examples/pikachu.jpg", 1, 1, 1, 1, 1],
]

without_downsampling_demo = gr.Interface(
    filters_without,
    [
        gr.inputs.Image(type="file"),
        gr.inputs.Slider(minimum=1, maximum=10, step=1, default=1, label="Box Blur"),
        gr.inputs.Slider(minimum=1, maximum=21, step=2, default=1, label="Gaussian Blur"),
        gr.inputs.Slider(minimum=1, maximum=20, step=1, default=1, label="Max Pool"),
        gr.inputs.Slider(minimum=1, maximum=5, step=2, default=1, label="Median Blur"),
    ],
    "image",
    examples=examples,
    # title=title,
    description= 'If you want to use the filters with downsampled image, use tab "With image downsampling"',
    # article=article,
    live=True
)

with_downsampling_demo = gr.Interface(
    filters_with,
    [
        gr.inputs.Image(type="file"),
        gr.inputs.Slider(minimum=1, maximum=10, step=1, default=1, label="Blur Pool"),
        gr.inputs.Slider(minimum=1, maximum=10, step=1, default=1, label="Box Blur"),
        gr.inputs.Slider(minimum=1, maximum=21, step=2, default=1, label="Gaussian Blur"),
        gr.inputs.Slider(minimum=1, maximum=20, step=1, default=1, label="Max Pool"),
        gr.inputs.Slider(minimum=1, maximum=5, step=2, default=1, label="Median Blur"),
    ],
    "image",
    examples=examples,
    # title=title,
    description = 'Blur Pooling downsamples the image in the default setting!',
    # article=article,
    live=True
)

demo = gr.TabbedInterface(
    [
      without_downsampling_demo,
      with_downsampling_demo
    ], 
    [
      "Without image downsampling", 
      "With image downsampling"
    ]
)

demo.launch()