/
faces.py
168 lines (133 loc) · 4.85 KB
/
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
import glob
import os
import re
import string
import sys
import random
import sqlite3 as sql3
import cv2
import detectors
from images import load_matrix_3d
from images import sample_patches, tile_images
import config as cnf
__author__ = 'dracz'
lfw_pattern = os.path.join(cnf.data_root, "lfw/*/*.jpg")
lfwc_pattern = os.path.join(cnf.data_root, "lfwcrop_grey/*/*.pgm")
alfw_db_file = os.path.join(cnf.data_root, "aflw/aflw/data/aflw.sqlite")
lfwc_count = 13232
def load_lfw(pattern=lfw_pattern, shuffle=True, limit=-1):
"""load lfw data into 3d ndarray of shape [m, patch_rows, patch_cols]"""
print("Loading lfw data from {}...".format(pattern))
return load_matrix_3d(lfw_paths(pattern, shuffle=shuffle, limit=limit))
def load_lfwc(pattern=lfwc_pattern, shuffle=True, limit=-1):
"""load lfwc data into 3d ndarray of shape [m, patch_rows, patch_cols]
This set contains 13232 images"""
print("Loading lfwc data from {}...".format(pattern))
return load_matrix_3d(lfwc_paths(pattern, shuffle=shuffle, limit=limit))
def load_alfw():
conn = sql3.connect(alfw_db_file)
c = conn.cursor()
c.execute("select name from sqlite_master where type = 'table'")
def lfw_paths(pattern=lfw_pattern, shuffle=True, limit=-1):
"""
Get "labeled faces in the wild" (lfw) image files downloaded from:
http://vis-www.cs.umass.edu/lfw/lfw.tgz
:param path: local path to the root of the downloaded images
:param shuffle: whether to read in random order; default=True
:return: iterator of path-name strings
"""
paths = glob.glob(pattern)
if shuffle:
random.shuffle(paths)
return paths[:limit]
def lfwc_paths(patter=lfwc_pattern, shuffle=True, limit=-1):
"""
Get "cropped labeled faces in the wild" (lfwc) image files downloaded from:
http://conradsanderson.id.au/lfwcrop/lfwcrop_grey.zip
:param path: local path to the root of the downloaded images
:param shuffle: whether to read in random order; default=True
:return: iterator of path-name strings
"""
paths = glob.glob(lfwc_pattern)
if shuffle:
random.shuffle(paths)
return paths[:limit]
def img_info(path):
file_name = path[string.rfind(path, "/") + 1:]
match = re.match(r'^(.*)_(\d+).\w+$', file_name)
name, number = match.group(1), match.group(2)
return path, name, number
def show_images(paths, detector=None):
"""
Show lfw images from path one at a time,
run through detectors, wait for keyboard input
"""
print "Press 'q' to exit, 'b' to view previous, or any other key to advance"
i = 0
while True:
path, name, number = img_info(paths[i])
img = cv2.imread(path)
show_image("{} {}".format(name, number), img, detector)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
if k == ord('q'):
sys.exit(0)
elif k == ord('b'):
if i > 0:
i -= 1
else:
print("Reached the first image")
elif i < len(paths) - 1:
i += 1
else:
print("Reached the last image")
def show_image(title, img, detector=None, size=None):
"""
:param title: to use for title of image
:param img: ndarray of image
:param detector: detector to run image through
"""
if detector is None:
display_img = img
else:
display_img = img.copy()
detected = detector.detect(img)
detectors.draw_detected(display_img, detected)
if size is not None:
display_img = cv2.resize(display_img, size)
cv2.imshow(title, display_img)
def main(cmd, args):
"""process the command"""
data_set = "lfw" if len(args) > 0 and args[0] == "lfw" else "lfwc"
if cmd == "show":
if len(args) > 0 and args[0] == "lfw":
show_images(lfw_paths())
else:
show_images(lfwc_paths())
return
n_samples = 10000
sh = (20, 20)
out_shape = (8, 8)
data = load_lfw() if data_set == "lfw" else load_lfwc()
out_dir = "./img/"
shape_range = range(8, 21, 2)
if cmd == "sample":
patches = sample_patches(data, patch_shape=sh, n_samples=n_samples)
tile_images(patches, patch_shape=sh, output_shape=out_shape, show=True)
elif cmd == "sweep":
shapes = [(i, i) for i in shape_range]
for sh in shapes:
patches = sample_patches(data, n_samples=n_samples, patch_shape=sh)
img = tile_images(patches, sh, output_shape=out_shape, show=True)
fn = os.path.join(out_dir, "{}_patches_{}.png".format(data_set, sh))
print("saving {}...".format(fn))
img.save(fn)
elif cmd == "alfw":
print(load_alfw())
else:
print("unexpected cmd")
if __name__ == "__main__":
argv = sys.argv[1:]
cmd = argv[0] if len(argv) > 0 else "sweep"
args = [] if len(argv) < 2 else argv[1:]
main(cmd, args)