-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
166 lines (135 loc) · 5.64 KB
/
run.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import sys,json,numpy as np
import face_recognition
import cv2
# for feeding data
import glob,os
from pathlib import Path
import numpy as np
home = str(os.path.dirname(os.path.abspath(__file__))) + "/" # "/../../"
file_names = glob.glob(home + "/known_people/*.jp*g")
#end
#Read data from stdin
def read_in():
lines = sys.stdin.readline()
# Since our input would only be having one line, parse our JSON data from that
return lines
#Function to check if the person is authorised based on certain parameters
def authorised(name):
# Assuming if person is not in Database then it is Un-authorised
return not "Unknown" in name
def main():
print("in the main Fucntion")
# GETTING KNOWN ENCODINGS AND NAMES
home = str(os.path.dirname(os.path.abspath(__file__))) + "/" # "/../../"
print("home : "+ home)
known_encodings_file_path = home + "/data/known_encodings_file.csv"
people_file_path = home + "/data/people_file.csv"
# For storing the encoding of a face
known_encodings_file = Path(known_encodings_file_path)
if known_encodings_file.is_file():
known_encodings = np.genfromtxt(known_encodings_file, delimiter=',')
else:
known_encodings = []
# #For Storing the name corresponding to the encoding
people_file = Path(people_file_path)
if people_file.is_file():
people = np.genfromtxt(people_file, dtype='U',delimiter=',')
else:
people = []
# MAIN WORK
#Capture Video indefinitely
video_capture = cv2.VideoCapture(0)
original_width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)
original_height = video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
# TODO: GET FROM DATABASE
# known encodings of persons in database.
# known_encodings = []
# people = []
#Some important variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
#Eat the Meat, Hmm process the image
while True:
#
# 1.) Capture the frame from the video.
# 2.) Compress it to its 1/4th size for faster speed.
# 3.) If this frame has to be processed, find face_location, face_encodings.
# 4.) Match with the known_encodings and set the name for each face else Unknown
# 5.) Add a border around face.
# if RED:
# unverified or not authenticated
# elif GREEN:
# everything OK ;)
# 6.) Show the frame
#
# Due to QR Code scanning, video element changes the size of video capture,
# which also affected this process(don't know why) so to convert it to original size
if video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)!=original_width or video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)!= original_height:
video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, original_width)
video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, original_height)
ret, frame = video_capture.read()
# Don't proceed further until camera is able to capture pics
if not ret:
continue
#smaller frame 1/4th of original size
small_frame = cv2.resize(frame, (0,0), fx=.25, fy=.25)
if process_this_frame:
#Find the face locations
face_locations = face_recognition.face_locations(small_frame)
#Find the face encodings 128 Dimensional!!
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names=[]
other = 0 #Count of un-authorised people
for face_encoding in face_encodings:
match = face_recognition.compare_faces(known_encodings, face_encoding)
name = "Unknown"
#Find if this person is in the present people array
for i in range(len(match)):
if match[i]:
name = people[i]
print(name+ "\n")
#file = open("testname.txt", "a")
#file.write(name + "\n")
#file.close()
#break
#continue
#Change it, run the loop to find no. of Unknown
if "Unknown" in name:
other += 1
name += str(other)
face_names.append(name)
print(face_names, flush=True)
process_this_frame = not process_this_frame
#Display the border
for (top, right, bottom, left),name in zip(face_locations, face_names):
#Scale up the coordinates by 4 to get face
top *= 4
right *= 4
bottom *= 4
left *= 4
#Assuming person in authenticated
color = (0,255,0) #GREEN
if not authorised(name):
#Unauthenticated person
color = (0,0,255) #RED
#print so that parent process in Node.js can use it
# print(name,flush=True)
#Display border
cv2.rectangle(frame, (left,top), (right,bottom), color, 2)
# Draw a label with name
cv2.rectangle(frame, (left,bottom-35), (right, bottom), color, cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name,(left+6, bottom-6), font, 1.0, (255,255,255), 1)
# Display the resulting image with borders and names
cv2.imshow('Video', frame)
# Hit 'q' on keyboard to quit
if cv2.waitKey(100) == 27:
break
#Release handle to the webcam
video_capture.release()
cv2.closeAllWindows()
#start process
if __name__ == '__main__':
main()