From 1d78becdd4487b8607379c00f33ecf4936596331 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 17 Nov 2023 22:43:10 +0700 Subject: [PATCH] final_2 --- .../access_control_mqtt.cpython-311.pyc | Bin 3375 -> 3453 bytes .../__pycache__/stm32.cpython-311.pyc | Bin 0 -> 1757 bytes access_control_python/main copy.py | 266 ++++++++++-------- access_control_python/main.py | 2 +- access_control_python/stm32.py | 37 ++- access_control_python/test.py | 7 +- access_control_python/test2.py | 13 + access_control_python/test3.py | 11 + 8 files changed, 202 insertions(+), 134 deletions(-) create mode 100644 access_control_python/__pycache__/stm32.cpython-311.pyc create mode 100644 access_control_python/test2.py create mode 100644 access_control_python/test3.py diff --git a/access_control_python/__pycache__/access_control_mqtt.cpython-311.pyc b/access_control_python/__pycache__/access_control_mqtt.cpython-311.pyc index afb72a4469918e04f1e6cdce133823da35abec9c..9f2311eeb3738deba9d4629d2c55dcc4262d23ac 100644 GIT binary patch delta 181 zcmZ24^;e2_IWI340}wpT3{Oqo$oq?(=@sMThin>~4LLL!S$%347O+pA$SE?}o0D&H zBd01SQwr-cCI*JpOp_})T^Vmo-p{#*@y_HLE=5L;$@91h7+EIsbL&i2=9Xa+Z;ALvd10m(0v delta 195 zcmew>wO)#MIWI340}wd7gr}NqS$p2z`?-D+rx34L+%oX+>F8nf)_cIuW%?|;86a+#6I~7R{+O2HKU&St=1`%xfMN>=~6KY5gwGT~7i7+f%%{q~5uhQ8u zjTMQg;DZl7D8`@^deF&DNe}%4dP)8Qy9g=D50^#~akE`sGGFP~ADDZg6-E{7ixsWFJ#5VU=i^=xTP;CPb3un)f zG@(kW;2;+dp)?m`Bk`)jMt2c6+kas6PJD!&n8Ak8< zycMx>(&;a!<0uO!DyJEnO9Ly-MP8FM@HW?@_<<_V?CgsVmKWDRU~yL6IWYG;>leW+ul_J@AACVsOf zPOPc6HFey5&oC+!lO~{$fZ|sanqVWgYs7}$#l1!jqP2KLUdXD%mU! zo>38odeBjse5G{>a#XrNAgM#B4cTLzOm=TM)M)z6PHyV(ezSH^d-U+|VS{!uxyM%y zmKsZ)(TUE;=wtdQ)u5q`hT$NLPd6VNJZN1x$z5*eF7JIBYI=I?bb6*`eZTUqJ)bBrXbx ziV`UEi^Hezb|rtE%G67z?^RBsQ=opX2;Sk${?4&A9(5%@sF3fwTb|>{LC4uDv+YgA zryS>-ZFe))MESXjA%`8u_5HGNMXBruXsBaP_$aFQup%=^eu5}=)F7_f6%oY*>IYo! zc{(Gn*O&J$)mQ2(->qOfjOo@VA>j06IhtN3sgyAmLPDmAF%iP6B+jog=5?-)%@OqR zHu&3$Q;3ZA_rkgwpZOY}!!=dh;4>tlP6+8hx*k0p7&-2~A=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): - 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!!") + 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): diff --git a/access_control_python/main.py b/access_control_python/main.py index d806d1a..92c80ed 100644 --- a/access_control_python/main.py +++ b/access_control_python/main.py @@ -85,12 +85,12 @@ while True: 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() diff --git a/access_control_python/stm32.py b/access_control_python/stm32.py index 55aa517..0e9e242 100644 --- a/access_control_python/stm32.py +++ b/access_control_python/stm32.py @@ -1,13 +1,30 @@ import serial +import threading + +class stm32_distance(): + data: int = 0 + def __init__(self, serial_port: str) -> None: + self.serial_port = serial_port + self.ser = serial.Serial(self.serial_port, 115200) + self.thread = threading.Thread(target=self.__read_data) + self.thread.start() + + def __read_data(self): + while True: + data = b'' + while True: + byte = self.ser.read() + if byte == b'\x03': + break + data += byte + data_str = data.decode('utf-8') + try: + self.data = int(data_str) + except: + pass + + def get_distance(self): + return self.data + -ser = serial.Serial('COM13', 115200) -while True: - data = b'' - while True: - byte = ser.read() - if byte == b'\x03': - break - data += byte - data_str = data.decode('utf-8') - print(data_str) diff --git a/access_control_python/test.py b/access_control_python/test.py index 22b2c18..c40e8d0 100644 --- a/access_control_python/test.py +++ b/access_control_python/test.py @@ -1,5 +1,6 @@ +from stm32 import stm32_distance -from espmega.espmega_r3 import ESPMega_standalone as ESPMega -plc = ESPMega("/facescan","192.168.0.239",1883) +SD = stm32_distance("COM13") -plc.digital_write(2,0)`` \ No newline at end of file +while True: + print(SD.get_distance()) \ No newline at end of file diff --git a/access_control_python/test2.py b/access_control_python/test2.py new file mode 100644 index 0000000..37dd2a4 --- /dev/null +++ b/access_control_python/test2.py @@ -0,0 +1,13 @@ +import serial +ser = serial.Serial('COM13', 115200) + +data = b'' + +while True: + byte = ser.read() + if byte == b'\x03': + break + data += byte + +data_str = data.decode('utf-8') +print(data_str) \ No newline at end of file diff --git a/access_control_python/test3.py b/access_control_python/test3.py new file mode 100644 index 0000000..9d4c86b --- /dev/null +++ b/access_control_python/test3.py @@ -0,0 +1,11 @@ +from time import perf_counter +from time import sleep as delay + +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) \ No newline at end of file