Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import kornia as K
|
| 4 |
from kornia.core import concatenate, Tensor
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
def rescale_aa(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
img_rescale: Tensor = K.geometry.rescale(img, (float(height),float(width)),antialias=False)
|
| 12 |
-
img_rescale_aa: Tensor = K.geometry.rescale(img, (float(height),float(width)),antialias=True)
|
| 13 |
img_out = concatenate([img_rescale, img_rescale_aa], -1)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
return K.utils.tensor_to_image(img_out.clamp_(0, 1))
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
gr.
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import kornia as K
|
| 3 |
from kornia.core import concatenate, Tensor
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
+
def rescale_aa(image, height, width):
|
| 8 |
+
# Convert PIL Image to Tensor
|
| 9 |
+
img_np = np.array(image)
|
| 10 |
+
img: Tensor = K.utils.image_to_tensor(img_np).float() / 255.0
|
| 11 |
+
img = img.unsqueeze(0) # Add batch dimension
|
| 12 |
|
| 13 |
+
img_rescale: Tensor = K.geometry.resize(img, (int(img.shape[2]*height), int(img.shape[3]*width)), antialias=False)
|
| 14 |
+
img_rescale_aa: Tensor = K.geometry.resize(img, (int(img.shape[2]*height), int(img.shape[3]*width)), antialias=True)
|
|
|
|
|
|
|
|
|
|
| 15 |
img_out = concatenate([img_rescale, img_rescale_aa], -1)
|
| 16 |
|
| 17 |
+
# Clip the output values from 0 to 1
|
| 18 |
return K.utils.tensor_to_image(img_out.clamp_(0, 1))
|
| 19 |
|
| 20 |
+
title = "Kornia Resizing Demo"
|
| 21 |
+
description = """
|
| 22 |
+
This demo shows the difference between resizing with and without antialiasing using Kornia.
|
| 23 |
+
The left half of the output image is resized without antialiasing, and the right half is resized with antialiasing.
|
| 24 |
+
"""
|
| 25 |
|
| 26 |
+
with gr.Blocks(title=title) as demo:
|
| 27 |
+
gr.Markdown(f"# {title}")
|
| 28 |
+
gr.Markdown(description)
|
| 29 |
+
|
| 30 |
+
with gr.Row():
|
| 31 |
+
with gr.Column():
|
| 32 |
+
image_input = gr.Image(type="pil", label="Input Image")
|
| 33 |
+
height_slider = gr.Slider(minimum=0.005, maximum=2, step=0.005, value=0.25, label="Height Scale")
|
| 34 |
+
width_slider = gr.Slider(minimum=0.005, maximum=2, step=0.005, value=0.25, label="Width Scale")
|
| 35 |
+
|
| 36 |
+
image_output = gr.Image(type="pil", label="Resized Image")
|
| 37 |
+
|
| 38 |
+
rescale_button = gr.Button("Rescale")
|
| 39 |
+
rescale_button.click(
|
| 40 |
+
fn=rescale_aa,
|
| 41 |
+
inputs=[image_input, height_slider, width_slider],
|
| 42 |
+
outputs=image_output
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
gr.Examples(
|
| 46 |
+
examples=[
|
| 47 |
+
["examples/a.png", 0.25, 0.25],
|
| 48 |
+
["examples/iron_man.jpeg", 0.25, 0.25],
|
| 49 |
+
],
|
| 50 |
+
inputs=[image_input, height_slider, width_slider],
|
| 51 |
+
outputs=image_output,
|
| 52 |
+
fn=rescale_aa,
|
| 53 |
+
cache_examples=True
|
| 54 |
+
)
|
| 55 |
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
demo.launch()
|