forked from samwestby/OpenCV-Python-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6_facial_detection.py
61 lines (50 loc) · 2.09 KB
/
6_facial_detection.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
import cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
"haarcascade_frontalface_default.xml")
smile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
"haarcascade_smile.xml")
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
"haarcascade_eye.xml")
def detect_features(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# You can also specify minSize and maxSize
for (x, y, w, h) in faces:
frame = cv2.rectangle(frame, (x, y), (x+w, y+h),
color=(0, 255, 0), thickness=5)
face = frame[y : y+h, x : x+w]
gray_face = gray[y : y+h, x : x+w]
smiles = smile_cascade.detectMultiScale(gray_face,
2.5, minNeighbors=9)
for (xp, yp, wp, hp) in smiles:
face = cv2.rectangle(face, (xp, yp), (xp+wp, yp+hp),
color=(0, 0, 255), thickness=5)
eyes = eye_cascade.detectMultiScale(gray_face,
2.5, minNeighbors=7)
for (xp, yp, wp, hp) in eyes:
face = cv2.rectangle(face, (xp, yp), (xp+wp, yp+hp),
color=(255, 0, 0), thickness=5)
return frame
stream = cv2.VideoCapture(0)
if not stream.isOpened():
print("No stream :(")
exit()
fps = stream.get(cv2.CAP_PROP_FPS)
width = int(stream.get(3))
height = int(stream.get(4))
# list of FourCC video codes: https://softron.zendesk.com/hc/en-us/articles/207695697-List-of-FourCC-codes-for-video-codecs
output = cv2.VideoWriter("assets/6_facial_detection.mp4",
cv2.VideoWriter_fourcc('m', 'p', '4', 'v'),
fps=fps, frameSize=(width, height))
while(True):
ret, frame = stream.read()
if not ret:
print("No more stream :(")
break
frame = detect_features(frame)
output.write(frame)
cv2.imshow("Webcam!", frame)
if cv2.waitKey(1) == ord('q'):
break
stream.release()
cv2.destroyAllWindows() #!