-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfaces.py
executable file
·89 lines (75 loc) · 3.08 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
#!/usr/bin/python
"""
Who is an integration between OpenCV face detection/extraction
and pyfaces face recognition (eigenfaces method).
"""
import sys
import optparse
from os import listdir
from os.path import abspath, dirname, join, isdir, basename
from detect.extract import extract
from pyfaces.pyfaces import PyFaces, THRESHOLD
from pyfaces.eigenfaces import parse_folder
from pyfaces.utils import merge_images
PEOPLE = 'people'
PEOPLE_DIRECTORY = join(dirname(abspath(__file__)), PEOPLE)
def reduce_result(matches):
""" Reduces the matches at `matches` removing the
repetitions keeping the values with small distance """
results = {}
for name, match, dist in matches:
if name not in results or results[name][-1] > dist:
results[name] = (name, match, dist)
values = results.values()
values.sort(key=lambda x: x[-1])
return values
def find_people(image, faces, people, threshold):
""" Finds people in `image` using faces from `faces` that
souits the `threshold` limit
"""
matches = []
for face in extract(image, faces):
for person in parse_folder(people, lambda name: isdir(name)):
dist, match = PyFaces(face, person, threshold=threshold).match()
if match:
matches.append((basename(person), match, dist))
return reduce_result(matches)
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-i', '--image', dest='image')
parser.add_option('-f', '--faces', dest='faces')
parser.add_option('-t', '--threshold', default=THRESHOLD, dest='threshold')
parser.add_option('-e', '--extract', default=False, action='store_true',
dest='extract')
parser.add_option('-d', '--extract-directory', dest='extract_src')
parser.add_option('-s', '--show', default=False, action='store_true',
dest='show')
parser.add_option('-p', '--people', default=PEOPLE_DIRECTORY,
dest='people')
(options, args) = parser.parse_args()
if options.extract:
if not options.faces or not options.extract_src:
parser.print_help()
sys.exit(2)
src = options.extract_src
images = listdir(src)
if images:
for image in images:
faces = extract(join(src, image), options.faces)
print 'Faces found for "%s": %s' % (image, ', '.join(faces))
else:
print 'No faces recognised on the photo'
else:
if not options.image or not options.faces:
parser.print_help()
sys.exit(2)
people = find_people(options.image, options.faces, options.people,
options.threshold)
if people:
print 'People found on "%s": %s' % \
(options.image,
', '.join(('\n\t%s (%s %s)' % person for person in people)))
if options.show:
merge_images([options.image] + [person[1] for person in people]).show()
else:
print 'No body was recognised on the photo'