rpi_assignments/assignment_2/04_FrontalFaceDetection.py

34 lines
1020 B
Python

import face_recognition
import cv2
# read webcam
video_capture = cv2.VideoCapture(0)
# visualize webcam
winname = "Test"
while True:
ret, frame = video_capture.read()
face_location = face_recognition.face_locations(frame)
print(face_location)
#print("There are " + str(len(face_location)) + " people in this image.")
if(len(face_location) > 0):
for index in range(0, len(face_location)):
x0 = face_location[index][3] #left
y0 = face_location[index][0] #Top
x1 = face_location[index][1] #Bottom
y1 = face_location[index][2] #Right
cv2.rectangle(frame, (x0, y0), (x1,y1), (255,0,0),3)
cv2.putText(frame, 'Total People: ' + str(len(face_location)), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 4)
cv2.namedWindow(winname)
cv2.moveWindow(winname, 40,30) # Move it to (40,30)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
webcam.release()
cv2.destroyAllWindows()