marianna13 commited on
Commit
772d191
1 Parent(s): 037c4aa

Add app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import kornia as K
4
+ import cv2
5
+ import numpy as np
6
+ from torchvision import transforms
7
+ from torchvision.utils import make_grid
8
+
9
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
+
11
+ def resize_images(f_names):
12
+ for i, f_name in enumerate(f_names):
13
+ img = cv2.imread(f_name, cv2.IMREAD_COLOR)
14
+ resized_image = cv2.resize(img,(70, 70))
15
+ cv2.imwrite(f_name,resized_image)
16
+
17
+ def predict(images, eps):
18
+ eps = float(eps)
19
+ f_names = [img.name for img in images]
20
+ resize_images(f_names)
21
+ convert_tensor = transforms.ToTensor()
22
+ images = [convert_tensor(cv2.imread(f, cv2.IMREAD_COLOR)) for f in f_names]
23
+ images = torch.stack(images, dim = 0).to(device)
24
+ zca = K.enhance.ZCAWhitening(eps=eps, compute_inv=True)
25
+ zca.fit(images)
26
+ zca_images = zca(images)
27
+ grid_zca = make_grid(zca_images, nrow=2, normalize=True).cpu().numpy()
28
+ return np.transpose(grid_zca,[1,2,0])
29
+
30
+ title = 'ZCA Whitening with Kornia!'
31
+ description = '''[ZCA Whitening](https://paperswithcode.com/method/zca-whitening) is an image preprocessing method that leads to a transformation of data such that the covariance matrix is the identity matrix, leading to decorrelated features:
32
+ *Note that you can upload only image files, e.g. jpg, png etc and there sjould be atleast 2 images!*
33
+ Learn more about [ZCA Whitening and Kornia](https://kornia.readthedocs.io/en/latest/_modules/kornia/enhance/zca.html)'''
34
+
35
+ iface = gr.Interface(fn=predict,
36
+ inputs=['files', gr.Slider(0.01, 1)],
37
+ outputs=gr.Image(),
38
+ allow_flagging="never",
39
+ title=title,
40
+ description=description
41
+ )
42
+
43
+ if __name__ == "__main__":
44
+ iface.launch(show_error=True)