Agung Aldevando
commited on
Commit
·
fa53d0e
1
Parent(s):
274b40f
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import kornia as K
|
3 |
+
import numpy as np
|
4 |
+
import kornia.geometry as KG
|
5 |
+
import torch
|
6 |
+
import cv2
|
7 |
+
|
8 |
+
def geometry_transform(images: list,
|
9 |
+
translation: float,
|
10 |
+
scale: float,
|
11 |
+
angle: float) -> np.ndarray:
|
12 |
+
|
13 |
+
file_names: list = [f.name for f in images]
|
14 |
+
image_list: list = [K.io.load_image(f, K.io.ImageLoadType(0)).float().unsqueeze(0)/255 for f in file_names]
|
15 |
+
image_batch: torch.Tensor = torch.cat(image_list, 0)
|
16 |
+
center: torch.Tensor = torch.tensor([x.shape[1:] for x in image_batch])/2
|
17 |
+
translation = torch.tensor(translation).repeat(len(image_list), 2)
|
18 |
+
scale = torch.tensor(scale).repeat(len(image_list), 2)
|
19 |
+
angle = torch.tensor(angle).repeat(len(image_list))
|
20 |
+
affine_matrix: torch.Tensor = KG.get_affine_matrix2d(translation, center, scale, angle)
|
21 |
+
|
22 |
+
transformed: torch.Tensor = KG.transform.warp_affine(image_batch, affine_matrix[:, :2], dsize=image_batch.shape[2:])
|
23 |
+
np_images: list = [K.tensor_to_image(f*255).astype(np.uint8) for f in transformed]
|
24 |
+
final_images: np.ndarray = cv2.hconcat(np_images)
|
25 |
+
|
26 |
+
return final_images
|
27 |
+
|
28 |
+
def main():
|
29 |
+
|
30 |
+
title = """
|
31 |
+
<h1 align="center">
|
32 |
+
Geometry Image Transforms with Kornia!
|
33 |
+
</h1>
|
34 |
+
"""
|
35 |
+
|
36 |
+
with gr.Blocks() as demo:
|
37 |
+
gr.Markdown(title)
|
38 |
+
|
39 |
+
with gr.Row():
|
40 |
+
images_input = gr.Files()
|
41 |
+
with gr.Column():
|
42 |
+
translation = gr.Number(label= "Translation")
|
43 |
+
scale = gr.Number(label = "Scale", value= 1.0)
|
44 |
+
angle = gr.Number(label = "Angle")
|
45 |
+
|
46 |
+
button = gr.Button('Transform')
|
47 |
+
result = gr.Image()
|
48 |
+
|
49 |
+
button.click(
|
50 |
+
geometry_transform,
|
51 |
+
inputs=[images_input,translation, scale, angle],
|
52 |
+
outputs=result
|
53 |
+
)
|
54 |
+
demo.launch()
|
55 |
+
|
56 |
+
|
57 |
+
if __name__ == '__main__':
|
58 |
+
main()
|