forked from J-Singh99/Facial-Recognition-Interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.py
More file actions
152 lines (115 loc) · 3.97 KB
/
Base.py
File metadata and controls
152 lines (115 loc) · 3.97 KB
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
import face_recognition
import cv2
import numpy as np
from csv import writer
from csv import DictWriter
import PrebuiltBill
db, bill = 0, 0
print("If you want to use a prebuilt model, type in 'Y'.")
print("The model is trained on Obama, Elon Musk, Scarlet Johansen, and The Creator!")
a = input()
if a == 'Y':
import PrebuiltDB
db = PrebuiltDB.DB
print('Importing data from pre-built model...')
else:
import DataStore
db = DataStore.DB
n = len(db)
# Preparing CSV file for people spotted on the camera
field_names = ['ID', 'Name', 'Age', 'Gender', 'Number', 'Balance', 'FileName']
file_name = 'PeoplesList.csv'
with open(file_name, 'a+', newline='') as write_obj:
csv_writer = writer(write_obj)
csv_writer.writerow(field_names)
# What to do if a face is spotted
checklist = [False]*n
BigCheck = True
def ExtractInfo(mathes):
if not False in checklist: BigCheck = False
if not True in matches:
pass #print('Unknown')
else:
for i in range(n):
if (checklist[i] == False) and (matches[i] == True):
checklist[i] = True
# Preparing the data to be manipulated
known_face_encodings = []
known_face_names =[]
for j in db:
i = str(db[j]['ID'])
known_face_names.append(db[j]['Name'])
ImCode = 'image' + i + ' = face_recognition.load_image_file("' + db[j]['FileName'] + '")'
exec(ImCode)
EnCode = 'face_encoding_' + i + '= face_recognition.face_encodings(image' + i + ')[0]'
exec(EnCode)
ApCode = 'known_face_encodings.append(face_encoding_' + i + ')'
exec(ApCode)
# Initialize these variables to pre-allot space
face_encodings, face_locations, face_names, process_this_frame = [], [], [], True
# Run loop to capture and give output
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) #Resizing captured frame
rgb_small_frame = small_frame[:, :, ::-1] #BGR -> RGB
if process_this_frame: # Processing only half the frames for speed
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
if BigCheck: ExtractInfo(matches)
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
process_this_frame = not process_this_frame
for (top, right, bottom, left), name in zip(face_locations, face_names): #Rescale
top *= 4
right *= 4
bottom *= 4
left *= 4
#Style the Box
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
cv2.imshow('Video', frame) #Display output box in video
if cv2.waitKey(1) & 0xFF == ord('q'): # Quit if Q/q is pressed
break
video_capture.release()
cv2.destroyAllWindows()
#Build the billing system
print("If you want to use a automatic billing mechanism, type in 'Y'.")
b = input()
if b == 'Y':
bill = PrebuiltBill.BILL
print('Importing bill...')
print()
print()
else:
import BillMaker
bill = BillMaker.Billing(checklist, known_face_names, db)
def HandleFace(name):
for j in db:
if name == db[j]['Name']:
with open(file_name, 'a+', newline='') as write_obj:
dict_writer = DictWriter(write_obj, fieldnames=field_names)
dict_writer.writerow(db[j])
ID = db[j]['ID']
print('Calculating bill for ' + db[j]['Name'])
if b == 'Y':
PrebuiltBill.DoCalc(ID, bill)
else:
BillMaker.DoCalc(ID, bill)
print()
print()
def FinalBilling(checklist):
for i in range(n):
if checklist[i] == True:
HandleFace(known_face_names[i])
# Printing the Bill
FinalBilling(checklist)