From 6b1b820dadf37c92fb2a16b5cad4859f8a2a017a Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 24 Nov 2023 14:29:26 +0700 Subject: [PATCH] ref --- access_control_python/main copy.py | 243 ----------------------- access_control_python/main.py | 297 +++++++++++++++++++---------- access_control_python/main_old.py | 140 ++++++++++++++ 3 files changed, 340 insertions(+), 340 deletions(-) delete mode 100644 access_control_python/main copy.py create mode 100644 access_control_python/main_old.py diff --git a/access_control_python/main copy.py b/access_control_python/main copy.py deleted file mode 100644 index 2ef4c96..0000000 --- a/access_control_python/main copy.py +++ /dev/null @@ -1,243 +0,0 @@ - -import cv2 -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 stm32 import stm32_distance - -SERIAL_PORT_DISPLAY = "COM11" -CAMERA_INDEX = 0 - -display = Display(SERIAL_PORT_DISPLAY) -cam = cv2.VideoCapture(CAMERA_INDEX) -actrl = access_control() -db = Database() - -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 - -''' -display.set_page("student") -display.set_string("msg.txt","Door Open") -''' - -student_info = None -parent_info = None -faces = None - - -threading.Thread(target=read_webcam).start() - - -def door_open(): - actrl.unlock_door() - for i in range(10): - display.set_string("msg.txt",f'Door will close in {10-i}') - time.sleep(1) - - i = 5 - #TODO Check if door close, lock - alarm_active: bool = False - display.set_page("leftopen") - while(i>=0): - if (not actrl.get_door_state() and not alarm_active): # 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() - display.set_string("msg.txt","Alarm Active, Please Close Door.") - #actrl.activate_LED_alarm() - - elif(actrl.get_door_state()): - actrl.lock_door() - display.set_page("scan") - should_restart = True - break - #display.set_page("scan") - - -SD = stm32_distance("COM13") -#start_time = time.perf_counter() - -flag: bool = False - -""" -while True: - state = round((perf_counter())%1) - if(flag!=state): - flag = state - print(f"change to : {state}") - print(state) - delay(0.05) -""" - -while True: - - while not frame_ready: - time.sleep(1) - - should_restart = True - distance = SD.get_distance() - print(distance) - state = distance < 800 - if state!= flag: - flag = state - if(state): - display.set_string("msg.txt","Scanning") - else: - display.set_page("scan") - - if (state): # Scan state - while should_restart: - #Try to identify face - should_restart = False - #if actrl.get_scan_state(): - if(True): - #print("SCAN ACTIVE!") - #display.set_string("msg.txt","Scanning") - faces = fp.identify_face(img, target_condidence=0.6) ## Scan student face - if(len(faces)==1): - #print("faces == 1") - facefile = faces[0]['name'] ## File name - print(facefile) - student_info = db.get_student_info(facefile)## Check is it student? - parent_info = db.get_parent_info(facefile) - if(student_info!=None): #is student, request another scan for parent - print("Find student info") - #db.log_access_student_file() - parent_info = None - display.set_page("student") - #display.set_string("msg.txt",f'Scan parent face ({10-i})') - display.set_string("name_std.txt", f'Name: {student_info.name}') - display.set_string("surname_std.txt",f'Surname: {student_info.surname}') - display.set_string("id_std.txt", f'ID: {str(student_info.id)}') - """ - parent_face = fp.identify_face(img, target_condidence=0.6) - parent_info = db.get_parent_info(parent_face)""" - #DONE TODO Fill in student info Resolve - - print(student_info) - - i = 0 - start_time = time.perf_counter() - while(time.perf_counter()-start_time < 10): - parent_face = fp.identify_face(img, target_condidence=0.6) - - if (len(parent_face)!= 0): - parent_facefile = parent_face[0]['name'] - parent_info = db.get_parent_info(parent_facefile) - 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. - print("Relation is OKAY") - #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_string("name_pt.txt", f'Name: {parent_info.name}') - display.set_string("surname_pt.txt", f'Surname: {parent_info.surname}') - display.set_string("id_pt.txt", f'ID: {parent_info.id}') - actrl.unlock_door() # Door open - display.set_string("msg.txt","Get your kid") - #DONE TODO Send timer to display - door_open() - """ - for i in range(10): - display.set_string("msg.txt",f'Door will close in {10-i}') - time.sleep(1) - - i = 5 - #TODO Check if door close, lock - alarm_active: bool = False - while(i>=0): - if (not actrl.get_door_state() and not alarm_active): # 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_LED_alarm() - alarm_active = True - - elif(actrl.get_door_state()): - actrl.lock_door() - display.set_page("scan") - should_restart = True - break - display.set_page("scan") - - """ - - - else: - display.set_page("scan") - display.set_string("msg.txt","Wrong parent. \t Try Again") - time.sleep(1) - should_restart = True - break - #TODO Try again does not try again - - elif(len(parent_face) == 0): - time.sleep(1) - display.set_string("msg.txt",f'Scan parent face ({10-i})') - i += 1 - - else: - pass - display.set_page("scan") - - elif(parent_info!=None): #is parent, open door - #parent_facefile = faces[0]['name'] - #parent_info = db.get_parent_info(parent_facefile) - #if(parent_info!= None): - print("Find parent info") - display.set_page("prt_wo_stu") - display.set_string("name_pt.txt", f'Name: {parent_info.name}') - display.set_string("surname_pt.txt", f'Surname: {parent_info.surname}') - display.set_string("id_pt.txt", f'ID: {parent_info.id}') - - door_open() - 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: - display.set_string("msg.txt","Move your face closer to the scanner") - 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]['name']) - 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 -''' \ No newline at end of file diff --git a/access_control_python/main.py b/access_control_python/main.py index 88e899f..2ef4c96 100644 --- a/access_control_python/main.py +++ b/access_control_python/main.py @@ -1,3 +1,4 @@ + import cv2 import threading import time @@ -5,17 +6,19 @@ import face_processing as fp from access_control_mqtt import access_control from database import Database, Student, Parent from display import Display +from stm32 import stm32_distance -SERIAL_PORT_DISPLAY = "/dev/serial0" +SERIAL_PORT_DISPLAY = "COM11" CAMERA_INDEX = 0 +display = Display(SERIAL_PORT_DISPLAY) cam = cv2.VideoCapture(CAMERA_INDEX) actrl = access_control() -display = Display(SERIAL_PORT_DISPLAY) db = Database() global img global frame_ready + frame_ready = False def read_webcam(): @@ -30,111 +33,211 @@ display.set_page("student") display.set_string("msg.txt","Door Open") ''' +student_info = None +parent_info = None +faces = None + + threading.Thread(target=read_webcam).start() + +def door_open(): + actrl.unlock_door() + for i in range(10): + display.set_string("msg.txt",f'Door will close in {10-i}') + time.sleep(1) + + i = 5 + #TODO Check if door close, lock + alarm_active: bool = False + display.set_page("leftopen") + while(i>=0): + if (not actrl.get_door_state() and not alarm_active): # 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() + display.set_string("msg.txt","Alarm Active, Please Close Door.") + #actrl.activate_LED_alarm() + + elif(actrl.get_door_state()): + actrl.lock_door() + display.set_page("scan") + should_restart = True + break + #display.set_page("scan") + + +SD = stm32_distance("COM13") +#start_time = time.perf_counter() + +flag: bool = False + +""" while True: + state = round((perf_counter())%1) + if(flag!=state): + flag = state + print(f"change to : {state}") + print(state) + delay(0.05) +""" + +while True: + while not frame_ready: time.sleep(1) + + should_restart = True + distance = SD.get_distance() + print(distance) + state = distance < 800 + if state!= flag: + flag = state + if(state): + display.set_string("msg.txt","Scanning") + else: + display.set_page("scan") - #Try to identify face - #if actrl.get_scan_state(): - if True: - print("SCAN ACTIVE!") - faces = fp.identify_face(img, target_condidence=0.6) ## Scan student face - if(len(faces)==1): - 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 - time_count = time.perf_counter() - endtime = time_count + 10 - while (time_count != endtime): - #db.log_access_student_file() - time_count = time.perf_counter() - time.sleep(1) - 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) - #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) - time_count = time.perf_counter() - - 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. - #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) - access_control.unlock_door() # Door open - display.set_string("msg.txt","Get your kid") - #TODO Send timer to display - #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() - - 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: - display.set_string("msg.txt", "Alarm Active!! Please close the door") - actrl.activate_alarm() - else: - actrl.lock_door() - break - - else: - display.set_page("scan") - display.set_string("msg.txt","Wrong parent. \t Try Again") - #TODO Try again does not try again - else: + if (state): # Scan state + while should_restart: + #Try to identify face + should_restart = False + #if actrl.get_scan_state(): + if(True): + #print("SCAN ACTIVE!") + #display.set_string("msg.txt","Scanning") + faces = fp.identify_face(img, target_condidence=0.6) ## Scan student face + if(len(faces)==1): + #print("faces == 1") + facefile = faces[0]['name'] ## File name + print(facefile) + student_info = db.get_student_info(facefile)## Check is it student? + parent_info = db.get_parent_info(facefile) + if(student_info!=None): #is student, request another scan for parent + print("Find student info") + #db.log_access_student_file() + parent_info = None display.set_page("student") - display.set_string("msg.txt","Cannot find parent data. \t Try Again") - should_start = True + #display.set_string("msg.txt",f'Scan parent face ({10-i})') + display.set_string("name_std.txt", f'Name: {student_info.name}') + display.set_string("surname_std.txt",f'Surname: {student_info.surname}') + display.set_string("id_std.txt", f'ID: {str(student_info.id)}') + """ + parent_face = fp.identify_face(img, target_condidence=0.6) + parent_info = db.get_parent_info(parent_face)""" + #DONE TODO Fill in student info Resolve + + print(student_info) + + i = 0 + start_time = time.perf_counter() + while(time.perf_counter()-start_time < 10): + parent_face = fp.identify_face(img, target_condidence=0.6) + + if (len(parent_face)!= 0): + parent_facefile = parent_face[0]['name'] + parent_info = db.get_parent_info(parent_facefile) + 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. + print("Relation is OKAY") + #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_string("name_pt.txt", f'Name: {parent_info.name}') + display.set_string("surname_pt.txt", f'Surname: {parent_info.surname}') + display.set_string("id_pt.txt", f'ID: {parent_info.id}') + actrl.unlock_door() # Door open + display.set_string("msg.txt","Get your kid") + #DONE TODO Send timer to display + door_open() + """ + for i in range(10): + display.set_string("msg.txt",f'Door will close in {10-i}') + time.sleep(1) + + i = 5 + #TODO Check if door close, lock + alarm_active: bool = False + while(i>=0): + if (not actrl.get_door_state() and not alarm_active): # 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_LED_alarm() + alarm_active = True + + elif(actrl.get_door_state()): + actrl.lock_door() + display.set_page("scan") + should_restart = True + break + display.set_page("scan") + + """ + + + else: + display.set_page("scan") + display.set_string("msg.txt","Wrong parent. \t Try Again") + time.sleep(1) + should_restart = True + break + #TODO Try again does not try again + + elif(len(parent_face) == 0): + time.sleep(1) + display.set_string("msg.txt",f'Scan parent face ({10-i})') + i += 1 + + else: + pass + display.set_page("scan") + + elif(parent_info!=None): #is parent, open door + #parent_facefile = faces[0]['name'] + #parent_info = db.get_parent_info(parent_facefile) + #if(parent_info!= None): + print("Find parent info") + display.set_page("prt_wo_stu") + display.set_string("name_pt.txt", f'Name: {parent_info.name}') + display.set_string("surname_pt.txt", f'Surname: {parent_info.surname}') + display.set_string("id_pt.txt", f'ID: {parent_info.id}') + + door_open() + 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: - 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!!") + display.set_string("msg.txt","Move your face closer to the scanner") + 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]['name']) - 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 + + ''' + #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]['name']) + 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 ''' \ No newline at end of file diff --git a/access_control_python/main_old.py b/access_control_python/main_old.py new file mode 100644 index 0000000..88e899f --- /dev/null +++ b/access_control_python/main_old.py @@ -0,0 +1,140 @@ +import cv2 +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 + +SERIAL_PORT_DISPLAY = "/dev/serial0" +CAMERA_INDEX = 0 + +cam = cv2.VideoCapture(CAMERA_INDEX) +actrl = access_control() +display = Display(SERIAL_PORT_DISPLAY) +db = Database() + +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 + +''' +display.set_page("student") +display.set_string("msg.txt","Door Open") +''' + +threading.Thread(target=read_webcam).start() + +while True: + while not frame_ready: + time.sleep(1) + + #Try to identify face + #if actrl.get_scan_state(): + if True: + print("SCAN ACTIVE!") + faces = fp.identify_face(img, target_condidence=0.6) ## Scan student face + if(len(faces)==1): + 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 + time_count = time.perf_counter() + endtime = time_count + 10 + while (time_count != endtime): + #db.log_access_student_file() + time_count = time.perf_counter() + time.sleep(1) + 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) + #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) + time_count = time.perf_counter() + + 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. + #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) + access_control.unlock_door() # Door open + display.set_string("msg.txt","Get your kid") + #TODO Send timer to display + #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() + + 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: + display.set_string("msg.txt", "Alarm Active!! Please close the door") + actrl.activate_alarm() + else: + actrl.lock_door() + break + + else: + display.set_page("scan") + display.set_string("msg.txt","Wrong parent. \t Try Again") + #TODO Try again does not try again + else: + display.set_page("student") + display.set_string("msg.txt","Cannot find parent data. \t Try Again") + should_start = True + + else: + 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!!") + + +''' + #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]['name']) + 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 +''' \ No newline at end of file