/
project.py
68 lines (58 loc) · 1.97 KB
/
project.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import cv2
import face_recognition
import os
import numpy as np
# region Vars
path = 'images_pro'
images = []
names = []
myList = os.listdir(path)
# print(myList)
# endregion
# region Fetch Known Images
for item in myList:
curImage = cv2.imread(f'{path}/{item}')
images.append(curImage)
names.append(os.path.splitext(item)[0])
# print(images)
# print(names)
# endregion
# region Encode Known Images
def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)
return encodeList
known_encode_list = findEncodings(images)
# print(known_encode_list[0])
print("Encoding Completed!")
# endregion
cap = cv2.VideoCapture(0)
# region Process Frames and compare them with Known Images
while True:
success, frame = cap.read()
frame_small = cv2.resize(frame, (0,0), None, 0.25, 0.25)
fame_small = cv2.cvtColor(frame_small, cv2.COLOR_BGR2RGB)
faces_loc = face_recognition.face_locations(frame_small)
faces_encode = face_recognition.face_encodings(frame_small, faces_loc)
for encodeFace, faceLoc in zip(faces_encode, faces_loc):
matches = face_recognition.compare_faces(known_encode_list, encodeFace)
face_distances = face_recognition.face_distance(known_encode_list, encodeFace)
matchIndex = np.argmin(face_distances)
if matches[matchIndex]:
name = names[matchIndex].upper()
# print(name)
y1,x2,y2,x1 = faceLoc
y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4
cv2.rectangle(frame,(x1,y1), (x2,y2), (255,0,255), 2)
cv2.rectangle(frame, (x1,y2-35), (x2,y2), (255,0,255), cv2.FILLED)
cv2.putText(frame,name,
(x1+6, y2-6), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
cv2.imshow('webcam', frame)
if cv2.waitKey(1) == ord('q'):
break
# endregion
cap.release()
cv2.destroyAllWindows()