RamAnanth1 commited on
Commit
ddd2039
1 Parent(s): 8cbb3f1

Create util.py

Browse files
Files changed (1) hide show
  1. util.py +37 -0
util.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+
4
+
5
+ def HWC3(x):
6
+ assert x.dtype == np.uint8
7
+ if x.ndim == 2:
8
+ x = x[:, :, None]
9
+ assert x.ndim == 3
10
+ H, W, C = x.shape
11
+ assert C == 1 or C == 3 or C == 4
12
+ if C == 3:
13
+ return x
14
+ if C == 1:
15
+ return np.concatenate([x, x, x], axis=2)
16
+ if C == 4:
17
+ color = x[:, :, 0:3].astype(np.float32)
18
+ alpha = x[:, :, 3:4].astype(np.float32) / 255.0
19
+ y = color * alpha + 255.0 * (1.0 - alpha)
20
+ y = y.clip(0, 255).astype(np.uint8)
21
+ return y
22
+
23
+
24
+ def resize_image(input_image, resolution):
25
+ H, W, C = input_image.shape
26
+ H = float(H)
27
+ W = float(W)
28
+ k = float(resolution) / min(H, W)
29
+ H *= k
30
+ W *= k
31
+ H = int(np.round(H / 64.0)) * 64
32
+ W = int(np.round(W / 64.0)) * 64
33
+ img = cv2.resize(input_image, (W, H), interpolation=cv2.INTER_LANCZOS4 if k > 1 else cv2.INTER_AREA)
34
+ return img
35
+
36
+ def apply_canny(img, low_threshold, high_threshold):
37
+ return cv2.Canny(img, low_threshold, high_threshold)