-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
executable file
·111 lines (96 loc) · 3.65 KB
/
utils.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import cv2
import numpy as np
import os
from glob import glob
def m4_image_save_cv(images, savepath, rows=4, zero_mean=True):
# introduction: a series of images save as a picture
# image: 4 dims
# rows: how many images in a row
# cols: how many images in a col
# zero_mean:
if zero_mean:
images = images * 127.5 + 127.5
if images.dtype != np.uint8:
images = images.astype(np.uint8)
img_num, img_height, img_width, nc = images.shape
h_nums = rows
w_nums = img_num // h_nums
merge_image_height = h_nums * img_height
merge_image_width = w_nums * img_width
merge_image = np.ones([merge_image_height, merge_image_width, nc], dtype=np.uint8)
for i in range(h_nums):
for j in range(w_nums):
merge_image[i * img_height:(i + 1) * img_height, j * img_width:(j + 1) * img_width] = images[
i * w_nums + j]
merge_image = cv2.cvtColor(merge_image, cv2.COLOR_BGR2RGB) # cv2默认为bgr顺序
# merge_image = cv2.resize(merge_image, (512, 512), interpolation=cv2.INTER_CUBIC)
cv2.imwrite(savepath, merge_image)
def m4_image_onebyone_cv(images, savepath, ff='', rows=4, zero_mean=True):
# introduction: a series of images save as a picture
# image: 4 dims
# rows: how many images in a row
# cols: how many images in a col
# zero_mean:
if zero_mean:
images = images * 127.5 + 127.5
if images.dtype != np.uint8:
images = images.astype(np.uint8)
counter = 0
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # cv2默认为bgr顺序
cv2.imwrite(savepath + str(counter) + ff + '.jpg', img)
counter += 1
def m4_get_open_image_name(file_list,dataset_dir):
for i in range(len(file_list)):
file_list[i] = os.path.join(dataset_dir,file_list[i])
return file_list
def m4_face_label_maker(filepath,savefilename):
namelist = os.listdir(filepath)
filename = filepath + '/'
labelall = []
idx = 0
for name in namelist:
imagename = []
foldername = filename + name
imagename = imagename + glob(foldername + '/*.jpg') + glob(foldername + '/*.png') \
+ glob(foldername + '/*.jpeg') + glob(foldername + '/*.bmp')
for i in range(len(imagename)):
label = [name,imagename[i].split('/')[-1],idx]
labelall.append(label)
idx +=1
f = open(savefilename,'w+')
for j in range(len(labelall)):
f.writelines('\n'+str(labelall[j][0])+'/'+str(labelall[j][1])+ ' ' + str(labelall[j][2]))
f.close()
def m4_get_image_name(filepath):
namelist = os.listdir(filepath)
filename = filepath + '/'
imgs = []
for name in namelist:
name = filename + name
imgs = imgs + glob(name + '/*.jpg') + glob(name + '/*.png')+ glob(name + '/*.jpeg')+ glob(name + '/*.bmp')
return imgs
def m4_get_file_label_name(filepath_name,save_data_path_name):
data = np.loadtxt(filepath_name,dtype=str)
filename = data[:,0].tolist()
label=data[:,1].tolist()
filename_list = []
label_list=[]
for i in range(data.shape[0]):
filename_list.append(os.path.join(save_data_path_name,filename[i].lstrip("b'").rstrip("'")))
label_list.append(int(label[i].lstrip("b'").rstrip("'")))
return filename_list,label_list
def m4_np_one_change_to_np_two(np_1, np_2, steps):
'''
:param np_1:
:param np_2:
:param steps:
:return:
'''
m4_distance = np_2 - np_1
m4_step_distance = m4_distance / steps
m4_intermediary_list = []
for i in range(steps):
np_1 = np_1 + m4_step_distance
m4_intermediary_list.append(np_1)
return m4_intermediary_list