-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detector.py
27 lines (23 loc) · 989 Bytes
/
face_detector.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
import cv2
class FaceDetector:
def __init__(self, video=0, cascade_classifier_file='haarcascade_frontalface_default.xml',
debug=False):
self._capture = cv2.VideoCapture(video, cv2.CAP_DSHOW)
self._classifier = cv2.CascadeClassifier(cascade_classifier_file)
self._debug = debug
def get_face_pos(self):
ret, frame = self._capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
face = self._classifier.detectMultiScale(gray, 1.1, 3, cv2.CASCADE_FIND_BIGGEST_OBJECT, (175, 175))
if self._debug:
print(face)
if not isinstance(face, tuple):
x, y, w, h = face[0]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('camera', frame)
return x, y, (w + h) // 2, True
else:
cv2.imshow('camera', frame)
return None, None, None, False
def release(self):
self._capture.release()