-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenfaces.py
82 lines (69 loc) · 1.96 KB
/
genfaces.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
#!/usr/bin/python2
import sys
import argparse
from urlparse import urlparse
import cv2
import numpy as np
from classifier import *
import pdb
parser = argparse.ArgumentParser(prog="generate faces")
parser.add_argument("-s", "--source", nargs=1, default=["0"], help="video source: default(camera)")
parser.add_argument("-e", "--extension", nargs=1, default=["pgm"], help="output image extension")
parser.add_argument("-o", "--output", nargs=1, help="output class location")
parser.add_argument("-c", "--config", nargs=1, help="face classifier config file")
def main():
args = args = parser.parse_args(sys.argv[1:])
video = args.source[0]
if video == '0': video = int(video)
clsuri = args.output[0]
ext = args.extension[0]
cfg = args.config[0]
cam = cv2.VideoCapture(video)
if cfg:
cvfacerecognizer = CvFaceRecognizer(cfg)
if os.path.exists(clsuri):
try:
os.rmdir(clsuri)
except:
os.remove(clsuri)
os.mkdir(clsuri)
count = 0
sstate = False
while True:
state, frame = cam.read()
if not state:
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#frame = cv2.equalizeHist(frame)
x,y,w,h = (0, 0, -1, -1)
if cfg:
results = cvfacerecognizer.recognize(frame)
if results != tuple() and results.all():
x,y,w,h = results[0]
cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0))
cv2.imshow("preview", frame)
k = cv2.waitKey(30)
if sstate:
save_uri = clsuri + "/" + "%02d"%count + "." + ext
if cfg and x == y == 0 and w == h == -1:
continue
print("save: "+ save_uri)
cv2.imwrite(save_uri, frame[y:y+h, x:x+w])
count += 1
sstate = False
continue
# key: q to quit
if k == ord(b'q'):
break
# key: s to save
elif k == ord(b's'):
sstate = True
save_uri = clsuri + "/" + "%02d"%count + "." + ext
if cfg and x == y == 0 and w == h == -1:
continue
print("save: "+ save_uri)
cv2.imwrite(save_uri, frame[y:y+h, x:x+w])
count += 1
sstate = False
if __name__ == "__main__":
sys.exit(main())