Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
mischeiwiller
commited on
Commit
•
b8884cd
1
Parent(s):
5079be4
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import kornia as K
|
4 |
-
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
-
import matplotlib
|
8 |
import matplotlib.pyplot as plt
|
9 |
-
|
10 |
-
from scipy.cluster.vq import kmeans,vq,whiten
|
11 |
|
12 |
def get_coordinates_from_mask(mask_in):
|
13 |
x_y = np.where(mask_in != [0,0,0,255])[:2]
|
@@ -15,16 +12,13 @@ def get_coordinates_from_mask(mask_in):
|
|
15 |
x_y = np.float32(x_y)
|
16 |
centroids,_ = kmeans(x_y,4)
|
17 |
centroids = np.int64(centroids)
|
18 |
-
|
19 |
return centroids
|
20 |
|
21 |
def get_top_bottom_coordinates(coords):
|
22 |
top_coord = min(coords, key=lambda x : x[1])
|
23 |
bottom_coord = max(coords, key=lambda x : x[1])
|
24 |
-
|
25 |
return top_coord, bottom_coord
|
26 |
|
27 |
-
|
28 |
def sort_centroids_clockwise(centroids: np.ndarray):
|
29 |
c_list = centroids.tolist()
|
30 |
c_list.sort(key = lambda y : y[0])
|
@@ -37,75 +31,81 @@ def sort_centroids_clockwise(centroids: np.ndarray):
|
|
37 |
|
38 |
return top_left, top_right, bottom_right, bottom_left
|
39 |
|
40 |
-
|
41 |
-
def infer(image_input, dst_height:str, dst_width:str):
|
42 |
image_in = image_input["image"]
|
43 |
mask_in = image_input["mask"]
|
44 |
-
torch_img = K.image_to_tensor(image_in)
|
45 |
|
46 |
centroids = get_coordinates_from_mask(mask_in)
|
47 |
ordered_src_coords = sort_centroids_clockwise(centroids)
|
48 |
# the source points are the region to crop corners
|
49 |
points_src = torch.tensor([list(ordered_src_coords)], dtype=torch.float32)
|
50 |
-
|
51 |
# the destination points are the image vertexes
|
52 |
h, w = int(dst_height), int(dst_width) # destination size
|
53 |
points_dst = torch.tensor([[
|
54 |
[0., 0.], [w - 1., 0.], [w - 1., h - 1.], [0., h - 1.],
|
55 |
]], dtype=torch.float32)
|
56 |
-
|
57 |
# compute perspective transform
|
58 |
-
M: torch.tensor = K.geometry.get_perspective_transform(points_src, points_dst)
|
59 |
-
|
60 |
# warp the original image by the found transform
|
61 |
torch_img = torch.stack([torch_img],)
|
62 |
-
img_warp: torch.tensor = K.geometry.warp_perspective(torch_img
|
63 |
-
|
64 |
|
65 |
# convert back to numpy
|
66 |
-
img_np = K.tensor_to_image(torch_img
|
67 |
-
img_warp_np: np.ndarray = K.tensor_to_image(img_warp
|
68 |
-
|
69 |
# draw points into original image
|
70 |
for i in range(4):
|
71 |
center = tuple(points_src[0, i].long().numpy())
|
72 |
img_np = cv2.circle(img_np.copy(), center, 5, (0, 255, 0), -1)
|
73 |
-
|
74 |
# create the plot
|
75 |
fig, axs = plt.subplots(1, 2, figsize=(16, 10))
|
76 |
axs = axs.ravel()
|
77 |
-
|
78 |
axs[0].axis('off')
|
79 |
axs[0].set_title('image source')
|
80 |
axs[0].imshow(img_np)
|
81 |
-
|
82 |
axs[1].axis('off')
|
83 |
axs[1].set_title('image destination')
|
84 |
axs[1].imshow(img_warp_np)
|
85 |
-
|
86 |
return fig
|
87 |
|
88 |
-
|
89 |
description = """In this space you can warp an image using perspective transform with the Kornia library as seen in [this tutorial](https://kornia.github.io/tutorials/#category=Homography).
|
90 |
-
|
91 |
-
1. Upload an image (e.g. [this one](https://github.com/kornia/data/raw/main/bruce.png))
|
92 |
2. Set 4 points into the image with your cursor, which define the area to warp
|
93 |
3. Set a desired output size (or go with the default)
|
94 |
4. Click Submit to run the demo
|
95 |
"""
|
96 |
|
97 |
-
example_mask = np.
|
98 |
-
example_mask[
|
99 |
example_image_dict = {"image": "bruce.png", "mask": example_mask}
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import kornia as K
|
|
|
4 |
import cv2
|
5 |
import numpy as np
|
|
|
6 |
import matplotlib.pyplot as plt
|
7 |
+
from scipy.cluster.vq import kmeans
|
|
|
8 |
|
9 |
def get_coordinates_from_mask(mask_in):
|
10 |
x_y = np.where(mask_in != [0,0,0,255])[:2]
|
|
|
12 |
x_y = np.float32(x_y)
|
13 |
centroids,_ = kmeans(x_y,4)
|
14 |
centroids = np.int64(centroids)
|
|
|
15 |
return centroids
|
16 |
|
17 |
def get_top_bottom_coordinates(coords):
|
18 |
top_coord = min(coords, key=lambda x : x[1])
|
19 |
bottom_coord = max(coords, key=lambda x : x[1])
|
|
|
20 |
return top_coord, bottom_coord
|
21 |
|
|
|
22 |
def sort_centroids_clockwise(centroids: np.ndarray):
|
23 |
c_list = centroids.tolist()
|
24 |
c_list.sort(key = lambda y : y[0])
|
|
|
31 |
|
32 |
return top_left, top_right, bottom_right, bottom_left
|
33 |
|
34 |
+
def infer(image_input, dst_height: str, dst_width: str):
|
|
|
35 |
image_in = image_input["image"]
|
36 |
mask_in = image_input["mask"]
|
37 |
+
torch_img = K.utils.image_to_tensor(image_in).float() / 255.0
|
38 |
|
39 |
centroids = get_coordinates_from_mask(mask_in)
|
40 |
ordered_src_coords = sort_centroids_clockwise(centroids)
|
41 |
# the source points are the region to crop corners
|
42 |
points_src = torch.tensor([list(ordered_src_coords)], dtype=torch.float32)
|
|
|
43 |
# the destination points are the image vertexes
|
44 |
h, w = int(dst_height), int(dst_width) # destination size
|
45 |
points_dst = torch.tensor([[
|
46 |
[0., 0.], [w - 1., 0.], [w - 1., h - 1.], [0., h - 1.],
|
47 |
]], dtype=torch.float32)
|
|
|
48 |
# compute perspective transform
|
49 |
+
M: torch.tensor = K.geometry.transform.get_perspective_transform(points_src, points_dst)
|
|
|
50 |
# warp the original image by the found transform
|
51 |
torch_img = torch.stack([torch_img],)
|
52 |
+
img_warp: torch.tensor = K.geometry.transform.warp_perspective(torch_img, M, dsize=(h, w))
|
|
|
53 |
|
54 |
# convert back to numpy
|
55 |
+
img_np = K.utils.tensor_to_image(torch_img[0])
|
56 |
+
img_warp_np: np.ndarray = K.utils.tensor_to_image(img_warp[0])
|
|
|
57 |
# draw points into original image
|
58 |
for i in range(4):
|
59 |
center = tuple(points_src[0, i].long().numpy())
|
60 |
img_np = cv2.circle(img_np.copy(), center, 5, (0, 255, 0), -1)
|
|
|
61 |
# create the plot
|
62 |
fig, axs = plt.subplots(1, 2, figsize=(16, 10))
|
63 |
axs = axs.ravel()
|
|
|
64 |
axs[0].axis('off')
|
65 |
axs[0].set_title('image source')
|
66 |
axs[0].imshow(img_np)
|
|
|
67 |
axs[1].axis('off')
|
68 |
axs[1].set_title('image destination')
|
69 |
axs[1].imshow(img_warp_np)
|
|
|
70 |
return fig
|
71 |
|
|
|
72 |
description = """In this space you can warp an image using perspective transform with the Kornia library as seen in [this tutorial](https://kornia.github.io/tutorials/#category=Homography).
|
73 |
+
1. Upload an image or use the example provided
|
|
|
74 |
2. Set 4 points into the image with your cursor, which define the area to warp
|
75 |
3. Set a desired output size (or go with the default)
|
76 |
4. Click Submit to run the demo
|
77 |
"""
|
78 |
|
79 |
+
example_mask = np.zeros((327, 600, 4), dtype=np.uint8)
|
80 |
+
example_mask[:, :, 3] = 255
|
81 |
example_image_dict = {"image": "bruce.png", "mask": example_mask}
|
82 |
|
83 |
+
with gr.Blocks() as demo:
|
84 |
+
gr.Markdown("# Homography Warping")
|
85 |
+
gr.Markdown(description)
|
86 |
+
|
87 |
+
with gr.Row():
|
88 |
+
image_input = gr.Image(tool="sketch", type="numpy", label="Input Image")
|
89 |
+
output_plot = gr.Plot(label="Output")
|
90 |
+
|
91 |
+
with gr.Row():
|
92 |
+
dst_height = gr.Textbox(label="Destination Height", value="64")
|
93 |
+
dst_width = gr.Textbox(label="Destination Width", value="128")
|
94 |
+
|
95 |
+
submit_button = gr.Button("Submit")
|
96 |
+
submit_button.click(
|
97 |
+
fn=infer,
|
98 |
+
inputs=[image_input, dst_height, dst_width],
|
99 |
+
outputs=output_plot
|
100 |
+
)
|
101 |
+
|
102 |
+
gr.Examples(
|
103 |
+
examples=[[example_image_dict, "64", "128"]],
|
104 |
+
inputs=[image_input, dst_height, dst_width],
|
105 |
+
outputs=output_plot,
|
106 |
+
fn=infer,
|
107 |
+
cache_examples=True
|
108 |
+
)
|
109 |
+
|
110 |
+
if __name__ == "__main__":
|
111 |
+
demo.launch()
|