-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_predict.py
52 lines (42 loc) · 1.54 KB
/
face_predict.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
import cv2
import os
import dlib
import face_recognition
import pickle
# load the known faces and embeddings
print("[INFO] loading encodings...")
data = pickle.loads(open("encodings.pickle", "rb").read())
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
video = cv2.VideoCapture(0)
while True:
check, frame = video.read()
#print(check)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
#rgb = cv2.resize(rgb, (int(rgb.shape[1]/2), int(rgb.shape[0]/2)))
faces = face_cascade.detectMultiScale(rgb, 1.3, 5)
#print(faces)
encodings =face_recognition.face_encodings(rgb, faces)
names = []
#print(encodings)
for encoding in encodings:
matches = face_recognition.compare_faces(data["encodings"],encoding, tolerance=0.45)
name = "Unknown"
if True in matches:
matchedIdxs = [i for (i, b) in enumerate(matches) if b]
counts = {}
for i in matchedIdxs:
name = data["names"][i]
counts[name] = counts.get(name, 0) + 1
name = max(counts, key=counts.get)
names.append(name)
for ((sx, sy, sw, sh), name) in zip(faces, names):
cv2.rectangle(frame, (sx, sy), ((sx + sw), (sy + sh)), (0, 255, 0), 2)
y = sy - 15 if sy - 15 > 15 else sy + 15
cv2.putText(frame, name, (sx, y), cv2.FONT_HERSHEY_SIMPLEX,0.75, (0, 255, 0), 2)
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
video.release()
cv2.destroyALLWindows()