-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
30 lines (26 loc) · 1.18 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
import cv2
import numpy as np
def ffzk(input_dir):#Relative directory for all existing files
imgname_array=[];input_dir=input_dir.strip("\"\'")
for fd_path, _, sb_file in os.walk(input_dir):
for fil in sb_file:imgname_array.append(fd_path.replace('\\','/') + '/' + fil)
if os.path.isfile(input_dir):imgname_array.append(input_dir.replace('\\','/'))
return imgname_array
def img2np(dir=[],img_len=0):
img=[]
for x in dir:
try:img.append(cv2.imread(x))
except:continue
if img_len!=0:img[-1]=cv2.resize(img[-1],(img_len,img_len))
elif img[-1].shape!=img[0].shape:img.pop(-1);continue#Leave only the same shape
height, width, _ = img[-1].shape[:3]
img[-1]=cv2.resize(img[-1],(8*(width//8),8*(height//8)))
img[-1] = img[-1].astype(np.float32)/ 255.
return np.stack(img, axis=0)
def tf2img(tfs,_dir="./",name="",epoch=0,ext=".png"):
os.makedirs(_dir, exist_ok=True)
if type(tfs)!=np.ndarray:tfs=tfs.numpy()
tfs=np.clip(np.round(tfs*255.),0, 255).astype(np.uint8)
for i in range(tfs.shape[0]):
cv2.imwrite(os.path.join(_dir,name),tfs[i])