This commit is contained in:
Siwat Sirichai 2023-11-13 19:12:30 +07:00
parent 4c06ea5b60
commit 0e6a41d6e5
8 changed files with 94 additions and 20 deletions

View File

@ -1,7 +1,7 @@
from espmega_mqtt import ESPMegaMQTT
from time import sleep
class ac:
class access_control:
def __init__(self):
self.plc = ESPMegaMQTT()
pass

View File

@ -59,7 +59,7 @@ class Database:
statement = f'INSERT INTO face_detect.`access-log` (id, student_id, parent_id, `timestamp`) VALUES(NULL, NULL, (SELECT id FROM parents WHERE parents.imagefile = "{parent_filename}"), NULL)'
self.cursor.execute(statement)
self.db.commit()
def log_access_student(self, student: Student, parent: Parent):
def log_access_student_with_parent(self, student: Student, parent: Parent):
statement = f'INSERT INTO face_detect.`access-log` (id, student_id, parent_id, `timestamp`) VALUES(NULL, {student.id}, {parent.id}, NULL)'
self.cursor.execute(statement)
self.db.commit()
@ -67,5 +67,11 @@ class Database:
statement = f'INSERT INTO face_detect.`access-log` (id, student_id, parent_id, `timestamp`) VALUES(NULL, NULL, {parent.id}, NULL)'
self.cursor.execute(statement)
self.db.commit()
def log_access_student(self, parent: Parent):
statement = f'INSERT INTO face_detect.`access-log` (id, student_id, parent_id, `timestamp`) VALUES(NULL, {student.id}, NULL, NULL)'
self.cursor.execute(statement)
self.db.commit()
def fetch_log() -> list:
pass

View File

@ -9,6 +9,7 @@ class Display:
def set_string(self, field, text):
command = f'{field}="{text}"'.encode("ascii")
self.serial_adapter.write(command)
self.send_stop_bit()
def send_stop_bit(self):
self.serial_adapter.write(0xFF)
self.serial_adapter.write(0xFF)

View File

@ -6,7 +6,7 @@ from access_control_mqtt import access_control
from database import Database, Student, Parent
from display import Display
SERIAL_PORT_DISPLAY = "COM15"
SERIAL_PORT_DISPLAY = "COM26"
CAMERA_INDEX = 0
cam = cv2.VideoCapture(CAMERA_INDEX)
@ -25,6 +25,11 @@ def read_webcam():
ret, img = cam.read()
frame_ready = True
'''
display.set_page("student")
display.set_string("msg.txt","Door Open")
'''
threading.Thread(target=read_webcam).start()
while True:
@ -34,20 +39,64 @@ while True:
#Try to identify face
if actrl.get_scan_state():
print("SCAN ACTIVE!")
faces = fp.identify_face(img, target_condidence=0.6)
faces = fp.identify_face(img, target_condidence=0.6) ## Scan student face
if(len(faces)==1):
facefile = faces[0]
parent_info = db.get_parent_info_file(facefile)
if(parent_info!=None):
pass
#Open Door for parent
facefile = faces[0]['name'] ## File name
student_info = db.get_student_info(facefile)## Check is it student?
db.log_access_student(student_info) # Log student data
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)
#TODO Fill in student info
db.log_access_parent(parent_info) # Log student data
if(parent_info != None): # Identified parent
print(parent_info)
if(db.check_relationship(student_info,parent_info)): # Check if the detected parent is right for the detected student.
#TODO Log Access
display.set_page("Parent")
access_control.unlock_door() # Door open
time.sleep(10)
#display.set_string("msg.txt","Get your kid")
else:
access_control.lock_door() # Lock Door
display.set_page("Scan")
display.set_string("msg.txt","Wrong student. \t Try Again")
else:
display.set_page("student")
display.set_string("msg.txt","Cannot find parent data. \t Try Again")
else:
student_info = db.get_student_info(facefile)
if(student_info!=None):
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_page("Scan")
display.set_string("msg.txt","More than one people. \t try again")
time.sleep(2)
display.set_page("Scan")
pass
else:
#No one detected
print("No one detected \t try again")\
display.set_page("Scan")
pass
print("DONE")
else:
print("Scan inactivate!!")
'''
#is student, request another scan for parent
parent_face = fp.identify_face(img, target_condidence=0.6)
if(len(faces)==1):
parent_info = db.get_parent_info(faces[0])
parent_info = db.get_parent_info(faces[0]['name'])
if(parent_info!=None):
if(db.check_relationship(student_info,parent_info)):
#Student is under parent, open door.
@ -58,11 +107,4 @@ while True:
else:
#Not a parent, retry
pass
elif (len(faces)>1):
#More than one people, error.
pass
else:
#No one detected
pass
print("DONE")
'''

View File

@ -0,0 +1,25 @@
import threading
import time
import face_processing as fp
from access_control_mqtt import access_control
from database import Database, Student, Parent
from display import Display
from espmega_mqtt import ESPMegaMQTT
#SERIAL_PORT_DISPLAY = "COM26"
CAMERA_INDEX = 0
#actrl = access_control()
#display = Display(SERIAL_PORT_DISPLAY)
db = Database()
plc = ESPMegaMQTT()
while True:
for i in range(8,16):
plc.write_pwm(i, 0, 4095)
time.sleep(1)
for i in range(8,16):
plc.write_pwm(i, 1, 4095)
time.sleep(1)