-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirror_facedetect.py
executable file
·43 lines (36 loc) · 1.19 KB
/
mirror_facedetect.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
#!/usr/bin/env python3
# mirror_facedetect.py: Detect faces in the view of webcam
import cv2
import numpy
import face_recognition
import sys
import os
# Increase this number to speed up and lower the accuracy
k=4
font=cv2.FONT_HERSHEY_PLAIN
# Allow users to change k while running
if len(sys.argv)>=2:
k=int(sys.argv[1])
colors=[(0,0xFF,0),(0xFF,0xFF,0),(0xFF,0,0)]
def mark_faces(frm):
smaller_frm=cv2.resize(frm,(0,0),fx=1/k,fy=1/k) # To increase speed
# Convert BGR (OpenCV uses) to RGB to reduce time on dealing with colors
rgb_frm=smaller_frm[:,:,::-1]
faces=face_recognition.face_locations(rgb_frm)
for i,face in enumerate(faces):
top,right,bottom,left=face
frm=cv2.rectangle(frm,(k*left,k*top),(k*right,k*bottom),colors[i%3],3)
os.system("clear") # To make the screen not that messy
print("Found {} faces".format(len(faces)))
return frm
camera=cv2.VideoCapture(0)
while camera.isOpened():
ret, frm=camera.read()
if ret==False:
break
frm=cv2.flip(frm,1)
# This is just to put rectangle to highlight the faces
frm=mark_faces(frm)
cv2.imshow('The Mirror',frm)
if cv2.waitKey(1) & 0xFF == ord('q'):
break