forked from biesseck/MICA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize_cropped_faces.py
211 lines (158 loc) · 5.86 KB
/
visualize_cropped_faces.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import argparse
import os, sys
import random
from glob import glob
from pathlib import Path
import cv2
import numpy as np
import torch
import torch.backends.cudnn as cudnn
from loguru import logger
from pytorch3d.io import save_ply, save_obj
from skimage.io import imread
from tqdm import tqdm
from configs.config import get_cfg_defaults
from datasets.creation.util import get_arcface_input, get_center
from utils import util
def parse_args(argv):
parser = argparse.ArgumentParser(description='visualize_cropped_faces.py')
parser.add_argument('-i', default='/home/bjgbiesseck/GitHub/BOVIFOCR_MICA_3Dreconstruction/demo/arcface/lfw/Aaron_Eckhart/Aaron_Eckhart_0001.npy', type=str, help='Input file (.jpg, .png, .npy)')
parser.add_argument('-un', action='store_true', help='')
parser.add_argument('-p', action='store_true', help='')
parser.add_argument('-l', default=0, type=int, help='')
args = parser.parse_args()
# print('args:', args)
# sys.exit(0)
return args
class Tree:
def walk(self, dir_path: Path):
contents = list(dir_path.iterdir())
for path in contents:
if path.is_dir(): # extend the prefix and recurse:
yield str(path)
yield from self.walk(path)
def get_all_sub_folders(self, dir_path: str):
folders = [dir_path]
for folder in Tree().walk(Path(os.getcwd()) / dir_path):
# print(folder)
folders.append(folder)
return sorted(folders)
def show_image(img, title='img'):
ENTER = 13
ESC = 27
cv2.imshow(title, img)
while True:
keyCode = cv2.waitKey(1)
# print('keyCode:', keyCode)
if cv2.getWindowProperty(title, cv2.WND_PROP_VISIBLE) == 0 or keyCode == ENTER or keyCode == ESC:
break
cv2.destroyAllWindows()
def load_img(path_img):
if path_img.endswith('.npy'):
img = np.load(path_img)
if path_img.endswith('.jpg') or args.i.endswith('.png'):
img = cv2.imread(path_img)
return img
def load_show_one_img(args):
print('Loading \'' + args.i + '\'')
img = load_img(args.i)
if args.i.endswith('.npy'):
args.un = True
print('Original img.shape:', img.shape)
if img.shape[0] == 3 and img.shape[0] < img.shape[1] and img.shape[0] < img.shape[2]:
print('Transposing axis...')
img = img.transpose(1, 2, 0)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print('Current img.shape: ', img.shape)
if args.un == True:
print('Unnormalizing image...')
img = ((img + 1.0) * 127.5).astype(np.uint8)
if args.p == True:
print('img:', img)
print('Showing image...')
show_image(img)
# cv2.imshow('img', img)
# cv2.waitKey()
def load_all_neighboor_images(img_path, level=1, limit=-1):
splited_path = os.path.splitext(img_path)
img_file = img_path.split('/')[-1]
img_ext = splited_path[1]
path_dir_img = os.path.dirname(img_path)
# print('img_file:', img_file, ' img_ext:', img_ext, ' path_dir_img:', path_dir_img)
sub_dirs = path_dir_img.split('/')
path_to_search = '/'.join(sub_dirs[:len(sub_dirs)-(level-1)]) + '/*'*level + img_ext
print('path_to_search:', path_to_search)
neighboor_paths = sorted(glob(path_to_search))
# print('neighboor_paths', neighboor_paths)
return neighboor_paths
def show_image_neighboors(args, paths, idx_img):
ENTER = 13
ESC = 27
LEFT = 81
RIGHT = 83
print_img_path = True
while True:
img_path = paths[idx_img]
if print_img_path:
print('----------')
print('idx_img: ', idx_img)
print('Loading \'' + img_path + '\'')
img = load_img(img_path)
if img_path.endswith('.npy'):
args.un = True
if print_img_path:
print('Original img.shape:', img.shape)
if img.shape[0] == 3 and img.shape[0] < img.shape[1] and img.shape[0] < img.shape[2]:
if print_img_path:
print('Transposing axis...')
img = img.transpose(1, 2, 0)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
if print_img_path:
print('Current img.shape: ', img.shape)
if args.un == True:
if print_img_path:
print('Unnormalizing image...')
img = ((img + 1.0) * 127.5).astype(np.uint8)
if args.p == True:
if print_img_path:
print('img:', img)
if print_img_path:
print('Showing image...')
title = 'img'
cv2.imshow(title, img)
keyCode = cv2.waitKey(1)
# print('keyCode:', keyCode)
if cv2.getWindowProperty(title, cv2.WND_PROP_VISIBLE) == 0 or keyCode == ENTER or keyCode == ESC:
break
# if print_img_path:
# print('----------')
print_img_path = False
if len(paths) > 0:
if keyCode == LEFT:
idx_img -= 1
if idx_img < 0:
idx_img = len(paths) - 1
print_img_path = True
elif keyCode == RIGHT:
idx_img += 1
if idx_img == len(paths):
idx_img = 0
print_img_path = True
cv2.destroyAllWindows()
def load_show_img_neighboors(args, limit):
print('Loading neighboor files...')
neighboor_paths = load_all_neighboor_images(args.i, args.l, limit)
print('Neighboors:', len(neighboor_paths))
if len(neighboor_paths) > 0:
idx_curr_img = neighboor_paths.index(args.i)
show_image_neighboors(args, neighboor_paths, idx_curr_img)
def main(args):
if args.l > 0:
limit = -1 # load all neighboors
load_show_img_neighboors(args, limit)
else:
load_show_one_img(args)
print('\nFinished')
if __name__ == '__main__':
args = parse_args(sys.argv)
main(args)