-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoelsefabrik.py
105 lines (89 loc) · 2.82 KB
/
poelsefabrik.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
import cv2
import sys
import os
import pdb
import shutil
# Get user supplied values
videoPath = sys.argv[1]
cascPath = sys.argv[2]
resultDirPath = "faces_result"
scaleFactorValue=1.1
minNeighborsValue=5
sizeValue=(30, 30)
##############
# face detect function
##############
def getFaces(faceCascade, grayScale):
faces = faceCascade.detectMultiScale(
grayScale,
scaleFactor=scaleFactorValue,
minNeighbors=minNeighborsValue,
minSize=sizeValue,
flags=cv2.CASCADE_SCALE_IMAGE
)
return faces
#prepare result dir
resultPath = "{0}/{1}_{2}_{3}_{4}".format(resultDirPath, videoPath, scaleFactorValue, minNeighborsValue, sizeValue)
if not os.path.exists(resultPath):
os.makedirs(resultPath)
for the_file in os.listdir(resultPath):
file_path = os.path.join(resultPath, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(e)
cap = cv2.VideoCapture(videoPath)
frameCounter = 0
matches = 0
targetMatches = 11
imageCounter=0
burstingMode = None
framesInCurrentBurst = 0
defaultSkipFrameCount=24 * 2
faceCascade = cv2.CascadeClassifier(cascPath)
eyesCascade = cv2.CascadeClassifier("haarcascade_eye.xml")
while (cap.isOpened() and matches < targetMatches):
imageCounter += 1
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = getFaces(faceCascade, gray)
goodFaces = 0
if len(faces) > 0:
print "Found {0} faces in frame {1}".format(len(faces), frameCounter)
if burstingMode:
framesInCurrentBurst+=1
else:
burstingMode=True
framesInCurrentBurst=0
matches += 1
for (x, y, w, h) in faces:
#cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
faceCropGray = gray[y:y+h,x:x+w]
faceCropColor = frame[y:y+h,x:x+w]
eyes = eyesCascade.detectMultiScale(faceCropGray)
if(len(eyes) == 2):
#TODO figure out if the eyes are are next to each other...
goodFaces += 1
#for (ex,ey,ew,eh) in eyes:
# cv2.rectangle(faceCropColor,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
if(goodFaces > 0):
cv2.imwrite("{0}/f_{1}.png".format(resultPath, frameCounter), frame)
else:
print "No faces in frame {0}".format(frameCounter)
#if no faces break any burst
burstingMode=None
framesInCurrentBurst=0
skipFramesCount= defaultSkipFrameCount
#bursting?
if burstingMode and framesInCurrentBurst<3:
skipFramesCount=1
else:
burstingMode=None
framesInCurrentBurst = 0
frameCounter+=skipFramesCount
for x in xrange(0, skipFramesCount):
cap.grab()
cap.release()
cv2.destroyAllWindows()