-
Notifications
You must be signed in to change notification settings - Fork 0
/
Taking_Entry.py
110 lines (102 loc) · 4.33 KB
/
Taking_Entry.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
import face_recognition as fr
import cv2
import numpy as np
import pickle
import time
import os
from datetime import datetime,date
from Upload_to_storage import get_url
from Upload_to_firestore import Upload
from Export_Data_from_firebase import get_data
def attend():
notif=""
exist=os.path.isfile("data.dat")
if(exist==False):
notif="No Registration"
return notif
else:
font=cv2.FONT_HERSHEY_SIMPLEX
#Loading data.dat file
file=open("./data.dat",'rb')
Ens,Ids,Names=pickle.load(file)
file.close()
f=0
video = cv2.VideoCapture(0)
width,height = int(video.get(3)), int(video.get(4))
detected = False
while(1):
#Load test image
check,img=video.read()
img=cv2.flip(img,1)
box_x,box_y,box_l =int((width - 0.5*height) // 2),int(0.25*height),int(0.5*height)
cv2.rectangle(img,(box_x,box_y,box_l,box_l),(50,150,40),8)
crop=img[box_y:box_y+box_l,box_x:box_x+box_l]
#test=cv2.resize(img, (0, 0), fx=0.25, fy=0.25)
test=cv2.cvtColor(crop,cv2.COLOR_BGR2RGB)
name="Unknown"
if(f>40):
faces_loc=fr.face_locations(test)
en_faces=fr.face_encodings(test,faces_loc)
notif = "No Face detected, Please retry"
for (top,right,bottom,left),en in zip(faces_loc,en_faces):
#(top,right,bottom,left)=(top*4,right*4,bottom*4,left*4)
name="Unknown"
id=0
matches=fr.compare_faces(Ens,en,0.45)
if(True in matches):
index=matches.index(True)
name=Names[index]
id=Ids[index]
notif="Entry Done Successfully"
else:
notif = "Not Registered"
id = 0
cv2.putText(img,f"{id} {name}",(left+180,top+100),font,1,(255,0,0),2)
cv2.rectangle(crop,(left,top),(right,bottom),(0,0,255),2)
if(name=="Unknown"):
notif="Not Registered"
if(f>43):
cv2.rectangle(img,(left+230,bottom+160),(left+450,bottom+130),(0,0,255),cv2.FILLED)
cv2.putText(img,"Please retry",(left+240,bottom+150),font,1,(255,255,255),2)
else:
cv2.rectangle(img,(left+190,bottom+160),(left+450,bottom+130),(0,0,255),cv2.FILLED)
cv2.putText(img,"Entry Taken",(left+240,bottom+155),font,1,(255,255,255),2)
# Upload to cloud here
# Image_Path1=f"Images/{id} {name}1.jpg"
# print(Image_Path1)
# Image=get_url(Image_Path1)
detected = True
notif="Entry Done Successfully"
f=f+1
cv2.imshow("Capture",img)
if(f>45 and name!="Unknown"):
time.sleep(0.3)
break
if(f>50 and name=="Unknown"):
time.sleep(0.3)
break
f=f+1
key=cv2.waitKey(1)
if(key==ord(' ')):
break
if(cv2.getWindowProperty("Capture",0)):
break
video.release()
cv2.destroyAllWindows()
if(detected):
Time,Date,image,password=get_data(str(id),"User_Data")
#print(Time,Date,image)
Time.append(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
tmp={'User_ID':str(id),'Time':Time,'Name':str(name),'Date':Date,'Image':image,'Password':password}
#print(tmp)
Upload(tmp,'User_Data')
remove=0
if(Time[0].split(" ")[0]!=(datetime.now().strftime("%d/%m/%Y %H:%M:%S")).split(" ")[0]):
for i in range(len(Time)):
dates=Time[i].split(" ")[0]
if(dates!=(datetime.now().strftime("%d/%m/%Y %H:%M:%S")).split(" ")[0]):
remove=i;
del Time[0:remove+1]
tmp={'User_ID':str(id),'Time':Time,'Name':str(name),'Date':Date,'Image':image,'Password':password}
Upload(tmp,'DateTime')
return notif