-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessImages.py
91 lines (77 loc) · 2.11 KB
/
processImages.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
"""
Main file for processing all images in a given directory
"""
import getopt
from faceUtils import *
from graphicUtils import *
def resetFiles():
"""helper function, make sure only one frame is analyzed """
files = ['image_list.txt', 'output', 'tmp.jpg', 'tmp.pgm']
for file in files:
if os.path.isfile(file):
os.remove(file)
def pipeline(infile, outfile, trait=0):
"""
infile: input image
outfile: output image name
trait: integer - 0:Trustworthiness, 1:Dominance
"""
graphic = Graphics()
frame = cv2.imread(infile)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = None
faceCoordsList = findFaces(gray)
faceList = []
if faceCoordsList is not None:
for faceCoords in faceCoordsList:
face = Face(gray, faceCoords, trait)
if face.predictValue():
faceList.append(face)
face = None
resetFiles()
display = graphic.draw(frame, faceList, trait, True)
cv2.imwrite(outfile, display)
def main(argv):
"""process arguments"""
def usage():
print 'processes all jpg files in a given directory'
print '\nUsage: python processLive.py <args>'
print '\t--trait=[0, 1]'
print '\t--dir=[string]'
print '\t--help\n'
try:
opts, args = getopt.getopt(argv, 't:d:h', ['trait=', 'dir=', 'multi', 'help'])
except getopt.GetoptError:
print 'incorrect argument'
sys.exit(1)
defaults = (0, None)
traitID, dir = defaults
for opt, arg in opts:
if opt in ['-t', '--trait']:
traitID = int(arg)
elif opt in ['-d', '--dir']:
dir = arg
if not dir.endswith('/'):
dir= dir + '/'
elif opt in ['-h', '--help']:
usage()
sys.exit(0)
else:
print 'invalid option: ' + opt
sys.exit(1)
traits = ['trust', 'dom']
if not os.path.isdir(dir):
print 'directory does not exist'
sys.exit(1)
if not os.path.isdir(dir + 'processed'):
os.mkdir(dir + 'processed')
for image in os.listdir(dir):
if image.endswith('jpg'):
print 'begin: ' + dir + 'processed/' + traits[traitID] + '_' + image
pipeline(
infile=dir + image,
outfile=dir + 'processed/' + traits[traitID] + '_' + image,
trait=traitID,
)
if __name__ == '__main__':
main(sys.argv[1:])