-
Notifications
You must be signed in to change notification settings - Fork 0
/
detectionface.py
76 lines (61 loc) · 2.73 KB
/
detectionface.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
from __future__ import print_function
import cv2 as cv
import argparse
def detectAndDisplay(frame):
frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
frame_gray = cv.equalizeHist(frame_gray)
# Detect faces
faces = face_cascade.detectMultiScale(frame_gray)
for (x, y, w, h) in faces:
center = (x + w // 2, y + h // 2)
frame = cv.ellipse(frame, center, (w // 2, h // 2), 0, 0, 360, (255, 0, 255), 4)
faceROI = frame_gray[y:y + h, x:x + w]
# In each face, detect eyes
eyes = eyes_cascade.detectMultiScale(faceROI)
for (x2, y2, w2, h2) in eyes:
eye_center = (x + x2 + w2 // 2, y + y2 + h2 // 2)
radius = int(round((w2 + h2) * 0.25))
frame = cv.circle(frame, eye_center, radius, (255, 0, 0), 4)
mouth = mouth_cascade.detectMultiScale(faceROI, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
for (x2, y2, w2, h2) in mouth:
mouth_center = (x + x2 + w2 // 2, y + y2 + h2 // 2)
radius = int(round((w2 + h2) * 0.25))
frame = cv.circle(frame, mouth_center, radius, (0, 255, 0), 4)
break
cv.imshow('Capture - Face detection', frame)
parser = argparse.ArgumentParser(description='Code teste pour la dectection faciale.')
parser.add_argument('--face_cascade', help='Path to face cascade.', default='data/haarcascades/haarcascade_frontalface_alt.xml')
parser.add_argument('--eyes_cascade', help='Path to eyes cascade.', default='data/haarcascades/haarcascade_eye_tree_eyeglasses.xml')
parser.add_argument('--mouth_cascade', help='Path to mouth cascade.', default='data/haarcascades/haarcascade_smile.xml')
parser.add_argument('--camera', help='Camera divide number.', type=int, default=0)
args = parser.parse_args()
face_cascade_name = args.face_cascade
eyes_cascade_name = args.eyes_cascade
mouth_cascade_name = args.mouth_cascade
face_cascade = cv.CascadeClassifier()
eyes_cascade = cv.CascadeClassifier()
mouth_cascade = cv.CascadeClassifier()
# Load the cascades
if not face_cascade.load(cv.samples.findFile(face_cascade_name)):
print('--(!)Error loading face cascade')
exit(0)
if not eyes_cascade.load(cv.samples.findFile(eyes_cascade_name)):
print('--(!)Error loading eyes cascade')
exit(0)
if not mouth_cascade.load(cv.samples.findFile(mouth_cascade_name)):
print('--(!)Error loading mouth cascade')
exit(0)
camera_device = args.camera
# Read the video stream
cap = cv.VideoCapture(camera_device)
if not cap.isOpened():
print('--(!)Error opening video capture')
exit(0)
while True:
ret, frame = cap.read()
if frame is None:
print('--(!) No captured frame -- Break!')
break
detectAndDisplay(frame)
if cv.waitKey(10) == 27:
break