marianna13 commited on
Commit
1a2ad19
1 Parent(s): 4877909

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ import imageio
4
+ import cv2
5
+ import kornia as K
6
+ import kornia.geometry as KG
7
+ from copy import deepcopy
8
+ from tqdm import tqdm
9
+ from base64 import b64encode
10
+ import torch
11
+ import torch.nn.functional as F
12
+
13
+ use_cuda: bool = torch.cuda.is_available()
14
+ device = torch.device('cuda' if use_cuda else 'cpu')
15
+ registrator = KG.ImageRegistrator('similarity',
16
+ loss_fn = F.mse_loss,
17
+ lr=8e-4, pyramid_levels=3, num_iterations=500).to(device)
18
+
19
+ models = []
20
+
21
+ def check_image_sizes(f_names):
22
+ sizes = []
23
+ for f_name in f_names:
24
+ img = cv2.imread(f_name, cv2.IMREAD_COLOR)
25
+ sizes.append(img.shape)
26
+
27
+ return len(set(sizes))==1
28
+
29
+
30
+ def convert_img(f_name):
31
+ img = cv2.imread(f_name, cv2.IMREAD_COLOR)
32
+ # convert image to torch tensor
33
+ tensor = K.image_to_tensor(img, None).float() / 255.
34
+ return K.color.bgr_to_rgb(tensor)
35
+
36
+ def merge_sharp1_into2(timg1, timg2, trans1to2, verbose=False):
37
+ curr_img = timg2.clone()
38
+ warped = KG.homography_warp(timg1, torch.inverse(trans1to2), timg1.shape[-2:])
39
+ mask1 = K.filters.laplacian(K.color.rgb_to_grayscale(timg1), 7).abs()
40
+ mask1_norm = (mask1-mask1.min()) / (mask1.max() - mask1.min())
41
+ mask1_blur = K.filters.gaussian_blur2d(mask1_norm, (9,9), (1.6, 1.6))
42
+ mask1_blur = mask1_blur / mask1_blur.max()
43
+ warped_mask = KG.homography_warp(mask1_blur.float(), torch.inverse(trans1to2), timg1.shape[-2:])
44
+ curr_img = warped_mask * warped + (1-warped_mask) * curr_img
45
+ return curr_img
46
+
47
+ def img_registration(images):
48
+ f_names = [f.name for f in images]
49
+ # print(check_image_sizes(f_names))
50
+ # if not check_image_sizes(f_names):
51
+ # return "All images should be same size!"
52
+
53
+ for i, f_name in tqdm(enumerate(f_names)):
54
+ if i == 0:
55
+ continue
56
+ prev_img = convert_img(f_names[i-1]).to(device)
57
+ curr_img = convert_img(f_name).to(device)
58
+ model = registrator.register(prev_img, curr_img)
59
+ models.append(deepcopy(model.detach()))
60
+
61
+ models_to_final = [torch.eye(3, device=device)[None]]
62
+ for m in models[::-1]:
63
+ models_to_final.append(m @ models_to_final[-1])
64
+ models_to_final = models_to_final[::-1]
65
+
66
+ base_img = convert_img(f_names[-1])
67
+ curr_img = deepcopy(base_img)
68
+ _, layers, height, width = curr_img.shape
69
+ video_file = 'video.avi'
70
+ video = cv2.VideoWriter(video_file, 0, 1, (width,height))
71
+
72
+ with torch.no_grad():
73
+ for i, image in tqdm(enumerate(f_names)):
74
+ timg = convert_img(image)
75
+ curr_img = merge_sharp1_into2(timg.to(device), curr_img.to(device), models_to_final[i].to(device))
76
+ video.write(cv2.cvtColor(K.tensor_to_image(curr_img.float()*255).astype(np.uint8), cv2.COLOR_BGR2RGB))
77
+ video.release()
78
+
79
+ return K.tensor_to_image(curr_img.float()), video_file
80
+
81
+ title = 'Image Registration with Kornia!'
82
+ description = '''Image registration is the process of transforming different sets of data into one coordinate system. Data may be multiple photographs, data from different sensors, times, depths, or viewpoints. It is used in computer vision, medical imaging, and compiling and analyzing images and data from satellites. Registration is necessary in order to be able to compare or integrate the data obtained from these different measurements.
83
+
84
+ *Note that you can upload only image files, e.g. jpg, png etc and all images should have same width and height!*
85
+
86
+ Learn more about [image registraion and Kornia](https://kornia.readthedocs.io/en/latest/applications/image_registration.html)'''
87
+
88
+ examples = [["IMG_3020.JPG", "IMG_3027.JPG", "IMG_3034.JPG", "IMG_3040.JPG", "IMG_3058.JPG", "IMG_3070.JPG", "IMG_3083.JPG", "IMG_3100.JPG", "IMG_3106.JPG", "IMG_3112.JPG"]]
89
+
90
+ iface = gr.Interface(
91
+ img_registration,
92
+ inputs='files',
93
+ outputs=["image", gr.Video()],
94
+ allow_flagging="never",
95
+ title=title,
96
+ description=description
97
+ )
98
+
99
+ if __name__ == "__main__":
100
+ iface.launch(show_error=True)