stm32-fmt-code/access_control_python/main.py

133 lines
5.2 KiB
Python
Raw Normal View History

2023-09-24 18:30:29 +00:00
import cv2
import threading
import time
import face_processing as fp
2023-11-06 09:10:46 +00:00
from access_control_mqtt import access_control
from database import Database, Student, Parent
from display import Display
2023-09-17 08:27:41 +00:00
2023-11-14 11:45:43 +00:00
SERIAL_PORT_DISPLAY = "COM11"
2023-09-24 18:30:29 +00:00
CAMERA_INDEX = 0
cam = cv2.VideoCapture(CAMERA_INDEX)
2023-11-06 09:10:46 +00:00
actrl = access_control()
display = Display(SERIAL_PORT_DISPLAY)
db = Database()
2023-09-24 18:30:29 +00:00
global img
global frame_ready
frame_ready = False
def read_webcam():
global img
global frame_ready
while True:
ret, img = cam.read()
frame_ready = True
2023-11-13 12:12:30 +00:00
'''
display.set_page("student")
display.set_string("msg.txt","Door Open")
'''
2023-09-24 18:30:29 +00:00
threading.Thread(target=read_webcam).start()
while True:
while not frame_ready:
time.sleep(1)
#Try to identify face
2023-11-14 15:55:26 +00:00
#if actrl.get_scan_state():
if True:
2023-09-30 08:23:10 +00:00
print("SCAN ACTIVE!")
2023-11-13 12:12:30 +00:00
faces = fp.identify_face(img, target_condidence=0.6) ## Scan student face
2023-11-06 09:10:46 +00:00
if(len(faces)==1):
2023-11-13 12:12:30 +00:00
facefile = faces[0]['name'] ## File name
student_info = db.get_student_info(facefile)## Check is it student?
if(student_info!=None): #is student, request another scan for parent
#db.log_access_student_file()
time.sleep(2)
display.set_page("student")
display.set_string("msg.txt","Scan parent face")
parent_face = fp.identify_face(img, target_condidence=0.6)
parent_info = db.get_parent_info(parent_face)
2023-11-14 11:45:43 +00:00
#DONE TODO Fill in student info Resolve
display.set_page("student")
display.set_string("name_std.txt", student_info.name)
display.set_string("surname_std.txt", student_info.surname)
display.set_string("id_std.txt", student_info.id)
2023-11-13 13:34:08 +00:00
2023-11-13 12:12:30 +00:00
if(parent_info != None): # Identified parent
if(db.check_relationship(student_info,parent_info)): # Check if the detected parent is right for the detected student.
2023-11-14 11:45:43 +00:00
#DONE TODO Log Access STDw/P
db.log_access_student_with_parent(student_info, student_info)
#DONE TODO write parrent info to display
display.set_page("student")
display.set_string("name_std.txt", parent_info.name)
display.set_string("surname_std.txt", parent_info.surname)
display.set_string("id_std.txt", parent_info.id)
2023-11-13 12:12:30 +00:00
access_control.unlock_door() # Door open
2023-11-13 13:34:08 +00:00
display.set_string("msg.txt","Get your kid")
#TODO Send timer to display
2023-11-14 11:45:43 +00:00
#display.set_page("student")
i = 10
while(i>=1):
display.set_string("msg.txt","Door will close in {}".format(i))
time.sleep(1)
i -= 1
actrl.lock_door()
2023-11-13 13:34:08 +00:00
2023-11-14 11:45:43 +00:00
i = 10
#TODO Check if door close, lock
while(i>=0):
if (not actrl.get_door_state()): # False door is left open
display.set_string("msg.txt","Door is left open, Alram in {} sec".format(i))
time.sleep(1)
i -= 1
if i == 0:
actrl.activate_alarm()
else:
actrl.lock_door()
break
2023-11-13 12:12:30 +00:00
else:
2023-11-13 13:34:08 +00:00
display.set_page("scan")
display.set_string("msg.txt","Wrong parent. \t Try Again")
#TODO Try again does not try again
2023-11-13 12:12:30 +00:00
else:
display.set_page("student")
display.set_string("msg.txt","Cannot find parent data. \t Try Again")
2023-11-06 09:10:46 +00:00
else:
2023-11-13 12:12:30 +00:00
display.set_page("student")
display.set_string("msg.txt","Cannot find student data. \t Try Again")
pass
elif (len(faces)>1):
#More than one people, error.
print("more than one people. \t try again")
display.set_string("msg.txt","More than one people. \t try again")
time.sleep(2)
pass
print("DONE")
else:
print("Scan inactivate!!")
'''
2023-11-06 09:10:46 +00:00
#is student, request another scan for parent
parent_face = fp.identify_face(img, target_condidence=0.6)
if(len(faces)==1):
2023-11-13 12:12:30 +00:00
parent_info = db.get_parent_info(faces[0]['name'])
2023-11-06 09:10:46 +00:00
if(parent_info!=None):
if(db.check_relationship(student_info,parent_info)):
#Student is under parent, open door.
pass
else:
#Wrong Parent, retry.
pass
else:
#Not a parent, retry
pass
2023-11-13 12:12:30 +00:00
'''