-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathface_collect.py
62 lines (59 loc) · 1.79 KB
/
face_collect.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
import cv2
import time
cascPath = "opencv_xml/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,400)
target = "faces/"
#name = "matthai+philipose/"
#name = "seungyeop+han/"
name = "test/"
faceCount = 0
lastshot = 0
turn = 0
collectFace = False
lastTurn = 0
while True:
ret, frame = cap.read()
#if faceCount % 20 == 0 and lastTurn != faceCount:
# raw_input("turn %d" % turn)
# lastTurn = faceCount
# turn += 1
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
now = time.time()
faces = []
if collectFace and (now-lastshot) > 0.2:
faces = faceCascade.detectMultiScale(
gray,
scaleFactor = 1.2,
minNeighbors = 5,
minSize=(152, 152),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
for x, y, w, h in faces:
print("Face found! %d" % faceCount)
face = frame[y:y+h, x:x+w]
face = cv2.resize(face, (152, 152), interpolation = cv2.INTER_CUBIC)
cv2.imwrite("%s%s%d.jpg" % (target, name, faceCount), face)
faceCount += 1
lastshot = now
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255))
cv2.imshow('frame', frame)
if collectFace and (faceCount % 20 == 0) and lastTurn != faceCount:
collectFace = False
lastTurn = faceCount
print("paused %d" % faceCount)
if(faceCount >= 100): break
else:
print("wrong")
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
elif key & 0xFF == ord('f'):
print("restarted %d" % turn)
turn += 1
collectFace = True
cap.release()
cv2.destroyAllWindows()