Kavindu99 commited on
Commit
98d3fb1
·
1 Parent(s): f240077

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -21
app.py CHANGED
@@ -1,40 +1,32 @@
1
  import gradio as gr
2
- import torch
3
- import numpy
4
  from PIL import Image
5
 
6
  rng = range(1,225)
7
- sobel_verticle = torch.tensor([[-1,0,1],
8
  [-1,0,1],
9
- [-1,0,1]]).float()
10
 
11
- sobel_horizontal = torch.tensor([[-1,-1,11],
12
  [ 0, 0, 0],
13
- [ 1, 1, 1]]).float()
14
 
15
- laplacian = torch.tensor([[ 0,-1, 0],
16
  [-1, 5,-1],
17
- [ 0,-1, 0]]).float()
18
 
19
- average = torch.tensor([[1/9,1/9, 1/9],
20
  [1/9,1/9, 1/9],
21
- [1/9,1/9, 1/9]]).float()
22
 
23
 
24
- weighted_average = torch.tensor([[1/16,2/16, 1/16],
25
  [2/16,4/16, 2/16],
26
- [1/16,2/16, 1/16]]).float()
27
-
28
-
29
- def apply_kernel(img, row, col, kernel):
30
- return (img[row-1:row+2,col-1:col+2] * kernel).sum()
31
 
32
  def convolve(img, kernel):
33
- ch1, ch2, ch3 = img[:,:,0], img[:,:,1], img[:,:,2]
34
- ch1 = torch.tensor([[apply_kernel(ch1, i,j,kernel) for j in rng] for i in rng])
35
- ch2 = torch.tensor([[apply_kernel(ch2, i,j,kernel) for j in rng] for i in rng])
36
- ch3 = torch.tensor([[apply_kernel(ch3, i,j,kernel) for j in rng] for i in rng])
37
- out_img = (torch.stack((ch1,ch2,ch3),-1)).numpy()
38
  return Image.fromarray(out_img)
39
 
40
 
 
1
  import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
  from PIL import Image
5
 
6
  rng = range(1,225)
7
+ sobel_verticle = np.array([[-1,0,1],
8
  [-1,0,1],
9
+ [-1,0,1]])
10
 
11
+ sobel_horizontal = np.array([[-1,-1,11],
12
  [ 0, 0, 0],
13
+ [ 1, 1, 1]])
14
 
15
+ laplacian = np.array([[ 0,-1, 0],
16
  [-1, 5,-1],
17
+ [ 0,-1, 0]])
18
 
19
+ average = np.array([[1/9,1/9, 1/9],
20
  [1/9,1/9, 1/9],
21
+ [1/9,1/9, 1/9]])
22
 
23
 
24
+ weighted_average = np.array([[1/16,2/16, 1/16],
25
  [2/16,4/16, 2/16],
26
+ [1/16,2/16, 1/16]])
 
 
 
 
27
 
28
  def convolve(img, kernel):
29
+ out_img = cv2.filter2D(src=img, kernel=kernel)
 
 
 
 
30
  return Image.fromarray(out_img)
31
 
32