-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniquefaceregonitionvideo.py
94 lines (79 loc) · 3.32 KB
/
uniquefaceregonitionvideo.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import face_recognition
import os
import cv2
import pickle
import time
knownfacedir="knownvface"
#unknownfacedir="unknownface"
video=cv2.VideoCapture("4.mp4")
#video=cv2.VideoCapture(0)
tolerance=0.51
framethickness=3
fontthickness=2
MODEL="cnn"
print("Loading known faces")
knownfaces=[]
knownnames=[]
for name in os.listdir(knownfacedir):
for filename in os.listdir(f"{knownfacedir}/{name}"):
#image=face_recognition.load_image_file(f"{knownfacedir}/{name}/{filename}")
#encoding=face_recognition.face_encodings(image)[0]
encoding=pickle.loads(open(f"{name}/{filename}","rb"))
knownfaces.append(encoding)
knownnames.append(int(name))
print("1over")
if(len(knownnames)>0):
nextid=max(knownnames)+1
else:
nextid=0
print("Processign unknown faces")
while True:
#print(filename)
print("in for")
#image=face_recognition.load_image_file(f"{unknownfacedir}/{filename}")
ret, image=video.read()
print("Processign unknown loading")
locations=face_recognition.face_locations(image,model=MODEL)
print("Processign unknown location")
encodings=face_recognition.face_encodings(image,locations)
print("encoding over")
#image=cv2.cvtColor(image,cv2.COLOR_RGB2BGR)
for face_encoding, face_location in zip(encodings,locations):
result=face_recognition.compare_faces(knownfaces,face_encoding,tolerance)
print("finding match")
match=None
if True in result:
match=knownnames[result.index(True)]
print(f"Match found: {match}")
topleft=(face_location[3],face_location[0])
bottomright=(face_location[1],face_location[2])
color=[0,255,0]
cv2.rectangle(image,topleft,bottomright,color,framethickness)
topleft=(face_location[3],face_location[2])
bottomright=(face_location[1],face_location[2]+22)
cv2.rectangle(image,topleft,bottomright,color,cv2.FILLED)
cv2.putText(image,match,(face_location[3]+10,face_location[2]+15),cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,0),fontthickness)
else:
print("Match NOT found")
match=str(nextid)
nextid+=1
knownnames.append(match)
knownfaces.append(face_encoding)
os.mkdir(f"{knownfacedir}/{match}")
pickle.dump(face_encoding,open(f"{knownfacedir}/{match}/{match}-{int(time.time())}.pkl","wb"))
topleft=(face_location[3],face_location[0])
bottomright=(face_location[1],face_location[2])
color=[0,255,0]
cv2.rectangle(image,topleft,bottomright,color,framethickness)
topleft=(face_location[3],face_location[2])
bottomright=(face_location[1],face_location[2]+22)
cv2.rectangle(image,topleft,bottomright,color,cv2.FILLED)
cv2.putText(image,match,(face_location[3]+10,face_location[2]+15),cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,0),fontthickness)
cv2.imshow("",image)
if cv2.waitKey(1)&0xFF==ord("q"):
break
#cv2.waitKey(10000)
#cv2.destroyWindow(filename)
#video.release()
# Destroy all the windows
#cv2.destroyAllWindows()