-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_face.py
33 lines (25 loc) · 1020 Bytes
/
detect_face.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
import cv2
import face_recognition
# Get a reference to webcam
video_capture = cv2.VideoCapture("/dev/video1")
face_locations = []
while True:
# Grab a single frame of video
ret, frame = video_capture.read()
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1] # reverse order
# Find all the faces in the current frame of video
face_locations = face_recognition.face_locations(rgb_frame)
# Display the results
for top, right, bottom, left in face_locations:
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)# green rectangle around where face is
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print(f'Number of faces detected : {len(face_locations)}')
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()