marianna13 commited on
Commit
7e7078f
1 Parent(s): 1ac63f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -22
app.py CHANGED
@@ -1,6 +1,7 @@
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
@@ -8,18 +9,16 @@ 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,(50, 50))
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)
@@ -29,7 +28,7 @@ def predict(images, eps):
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 should 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,
@@ -40,19 +39,17 @@ iface = gr.Interface(fn=predict,
40
  description=description,
41
  examples=[[
42
  [
43
- 'irises.jpg',
44
- 'roses.jpg',
45
- 'sunflower.jpg',
46
- 'violets.jpg',
47
- 'chamomile.jpg',
48
- 'tulips.jpg',
49
- 'Alstroemeria.jpg',
50
- 'Carnation.jpg',
51
- 'Orchid.jpg',
52
- 'Peony.jpg',
53
- 'Aster.jpg',
54
- 'Dahlia.jpg'
55
- ], 0.1]]
56
  )
57
 
58
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import torch
3
  import kornia as K
4
+ from kornia.geometry.transform import resize
5
  import cv2
6
  import numpy as np
7
  from torchvision import transforms
 
9
 
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
 
12
+ def read_image(f_name):
13
+ image_to_tensor = transforms.ToTensor()
14
+ img = image_to_tensor(cv2.imread(f_name, cv2.IMREAD_COLOR))
15
+ resized_image = resize(img,(50, 50))
16
+ return resized_image
17
 
18
  def predict(images, eps):
19
  eps = float(eps)
20
  f_names = [img.name for img in images]
21
+ images = [read_image(f) for f in f_names]
 
 
22
  images = torch.stack(images, dim = 0).to(device)
23
  zca = K.enhance.ZCAWhitening(eps=eps, compute_inv=True)
24
  zca.fit(images)
 
28
 
29
  title = 'ZCA Whitening with Kornia!'
30
  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:
31
+ *Note that you can upload only image files, e.g. jpg, png etc and there sjould be atleast 2 images!*
32
  Learn more about [ZCA Whitening and Kornia](https://kornia.readthedocs.io/en/latest/_modules/kornia/enhance/zca.html)'''
33
 
34
  iface = gr.Interface(fn=predict,
 
39
  description=description,
40
  examples=[[
41
  [
42
+ '/content/irises.jpg',
43
+ '/content/roses.jpg',
44
+ '/content/sunflower.jpg',
45
+ '/content/violets.jpg',
46
+ '/content/chamomile.jpg',
47
+ '/content/tulips.jpg',
48
+ '/content/Alstroemeria.jpg',
49
+ '/content/Carnation.jpg',
50
+ '/content/Orchid.jpg',
51
+ '/content/Peony.jpg'
52
+ ], 0.01]]
 
 
53
  )
54
 
55
  if __name__ == "__main__":