-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathface-detection-multi-files.py
81 lines (66 loc) · 2.42 KB
/
face-detection-multi-files.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
import numpy as np
import cv2
import datetime
import time
import os
import subprocess
openCVPath = '/usr/local/opt/opencv/share/OpenCV/haarcascades/'
faceCascadeFile = openCVPath + 'haarcascade_frontalface_alt2.xml'
eyeCascadeFile = openCVPath + 'haarcascade_eye_tree_eyeglasses.xml'
faceCascade = cv2.CascadeClassifier(faceCascadeFile)
eyeCascade = cv2.CascadeClassifier(eyeCascadeFile)
outDirectory = './outputStream/'
if not os.path.exists(outDirectory):
os.makedirs(outDirectory)
FRAME_WIDTH = 1280
FRAME_HEIGHT = 720
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH,FRAME_WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'avc1')
while(True):
numFrame = 0
quitLoop = False
ts = time.time()
outputFileName = outDirectory + datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d-%H-%M-%S') + '.mkv'
out = cv2.VideoWriter(outputFileName,fourcc, 15.0, (FRAME_WIDTH, FRAME_HEIGHT))
while(numFrame <= 15):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.5,
minNeighbors=5,
minSize=(30, 30)
)
for (x, y, w, h) in faces:
# Draw a rectangle around the faces
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Draw eyes
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eyeCascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
# draw the text and timestamp on the frame
tsz = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
cv2.putText(frame, tsz, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
out.write(frame)
# Display the resulting frame
cv2.imshow('video',frame)
numFrame += 1;
# break when user click 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
quitLoop = True
break
# release the output file
out.release()
if (quitLoop):
break
# When everything done, release the capture
cap.release()
out.release()
cv2.destroyAllWindows()