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 d6d3dc3..ff21bb9 100644 Binary files a/access_control_python/__pycache__/access_control_mqtt.cpython-311.pyc and b/access_control_python/__pycache__/access_control_mqtt.cpython-311.pyc differ diff --git a/access_control_python/__pycache__/database.cpython-311.pyc b/access_control_python/__pycache__/database.cpython-311.pyc index 664bef4..3025ef5 100644 Binary files a/access_control_python/__pycache__/database.cpython-311.pyc and b/access_control_python/__pycache__/database.cpython-311.pyc differ diff --git a/access_control_python/__pycache__/display.cpython-311.pyc b/access_control_python/__pycache__/display.cpython-311.pyc index 35c9c60..1361719 100644 Binary files a/access_control_python/__pycache__/display.cpython-311.pyc and b/access_control_python/__pycache__/display.cpython-311.pyc differ diff --git a/access_control_python/__pycache__/espmega_mqtt.cpython-311.pyc b/access_control_python/__pycache__/espmega_mqtt.cpython-311.pyc index 4520db2..8d5233d 100644 Binary files a/access_control_python/__pycache__/espmega_mqtt.cpython-311.pyc and b/access_control_python/__pycache__/espmega_mqtt.cpython-311.pyc differ diff --git a/access_control_python/access_control_mqtt.py b/access_control_python/access_control_mqtt.py index 2287ef8..1d1edf3 100644 --- a/access_control_python/access_control_mqtt.py +++ b/access_control_python/access_control_mqtt.py @@ -1,20 +1,41 @@ -from espmega_mqtt import ESPMegaMQTT +from espmega.espmega_r3 import ESPMega_standalone as ESPMega from time import sleep class access_control: def __init__(self): - self.plc = ESPMegaMQTT() - pass + self.plc = ESPMega("/facescan","192.168.0.239",1883) + + + # def light_on(self): + # self.plc.analog_write(0,1,4095) + # def light_off(self): + # self.plc.analog_write(0,0,4095) + # def get_door_state(self) -> bool: + # return self.plc.digital_read(0) + # def get_scan_state(self) -> bool: + # return self.plc.digital_read(1) + # def lock_door(self): + # if(self.get_door_state()): + # self.plc.analog_write(1,1,4095) + + # def unlock_door(self): + # self.plc.analog_write(1,0,4095) + - def light_on(self): - self.plc.write_pwm(0,1,4095) - def light_off(self): - self.plc.write_pwm(0,0,4095) - def get_door_state(self) -> bool: - return self.plc.read_digital(0) def get_scan_state(self) -> bool: - return self.plc.read_digital(1) - def lock_door(self): - self.plc.write_pwm(1,1,4095) - def unlock_door(self): - self.plc.write_pwm(1,0,4095) \ No newline at end of file + return self.plc.digital_read(1) + + def lock_door(self): #if door close, lock the door. + if(self.plc.digital_read(0)): + self.plc.analog_write(0,1,4095) + + + def unlock_door(self): #if door close, unlock the door. + if(self.plc.digital_read(0)): + self.plc.analog_write(0,0,4095) + + def get_door_state(self) -> bool: + return self.plc.digital_read(0) + + def activate_alarm(): + self.plc.analog_write(1,1,4095) \ No newline at end of file diff --git a/access_control_python/database.py b/access_control_python/database.py index 507c12c..b5fdd7e 100644 --- a/access_control_python/database.py +++ b/access_control_python/database.py @@ -31,7 +31,7 @@ class Database: statement = (f'SELECT * FROM students WHERE imagefile=\"{filename}\"') self.cursor.execute(statement) result = self.cursor.fetchone() - if len(result) == 0: + if result == None: return None student = Student(result[0],result[1],result[2],result[3]) return student @@ -39,7 +39,7 @@ class Database: statement = (f'SELECT * FROM parents WHERE imagefile=\"{filename}\"') self.cursor.execute(statement) result = self.cursor.fetchone() - if len(result) == 0: + if result == None: return None parent = Parent(result[0],result[1],result[2],result[3]) return parent diff --git a/access_control_python/display.py b/access_control_python/display.py index cd2abca..87e99bb 100644 --- a/access_control_python/display.py +++ b/access_control_python/display.py @@ -1,16 +1,32 @@ import serial class Display: def __init__(self, serial_port: str) -> None: - self.serial_adapter = serial.Serial(serial_port,baudrate=115200) + self.serial_adapter = serial.Serial( + port=serial_port, + baudrate =115200, + parity=serial.PARITY_NONE, + stopbits=serial.STOPBITS_ONE, + bytesize=serial.EIGHTBITS, + timeout=1) + self.send_stop_bit() + self.reset_device() def set_page(self, page): - command = f'page {page}'.encode("ascii") - self.serial_adapter.write(command) - self.send_stop_bit() + command = f'page {page}' + self.send_command(command) def set_string(self, field, text): - command = f'{field}="{text}"'.encode("ascii") + command = f'{field}="{text}"' + self.send_command(command) + def send_stop_bit(self): + self.serial_adapter.write(self.get_stop_bit()) + def send_command(self, command: str): + command = bytes(command, '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) - self.serial_adapter.write(0xFF) \ No newline at end of file + def get_stop_bit(self) -> bytearray: + stop_bit = bytearray() + stop_bit.append(0xFF) + stop_bit.append(0xFF) + stop_bit.append(0XFF) + return stop_bit + def reset_device(self): + self.send_command("rest") \ No newline at end of file diff --git a/access_control_python/espmega_mqtt.py b/access_control_python/espmega_mqtt.py deleted file mode 100644 index e91d861..0000000 --- a/access_control_python/espmega_mqtt.py +++ /dev/null @@ -1,25 +0,0 @@ -import paho.mqtt.client as pahomqtt -BROKER = "192.168.0.239" -BASE_TOPIC = "/facescan" -class ESPMegaMQTT: - input_buffer = [0]*16 - mqtt: pahomqtt.Client - def __init__(self) -> None: - self.mqtt = pahomqtt.Client(client_id="pyfacescan") - self.mqtt.connect(host=BROKER,port=1883,keepalive=60) - self.mqtt.subscribe(f'{BASE_TOPIC}/input/#') - self.mqtt.on_message=self.handle_message - self.request_state() - self.mqtt.loop_start() - def write_pwm(self, id: int, state: bool, value: int = 4095): - self.mqtt.publish(f'{BASE_TOPIC}/pwm/{"%02d"}/set/state'%id,"on" if state else "off") - self.mqtt.publish(f'{BASE_TOPIC}/pwm/{"%02d"}/set/value'%id, str(value)) - def read_digital(self, id: int): - return self.input_buffer[id] - def request_state(self): - self.mqtt.publish(f'{BASE_TOPIC}/requeststate',"req") - def handle_message(self, client: pahomqtt.Client, data, message: pahomqtt.MQTTMessage): - if (message.topic.startswith(BASE_TOPIC+"/input/")): - id = int(message.topic[len(BASE_TOPIC)+7:len(message.topic)]) - state = int(message.payload) - self.input_buffer[id] = state \ No newline at end of file diff --git a/access_control_python/main copy.py b/access_control_python/main copy.py new file mode 100644 index 0000000..71f6976 --- /dev/null +++ b/access_control_python/main copy.py @@ -0,0 +1,162 @@ +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 = "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() + +while True: + while not frame_ready: + time.sleep(1) + + should_restart = True + + + 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? + if(student_info!=None): #is student, request another scan for parent + print("Find student info") + #db.log_access_student_file() + display.set_page("student") + display.set_string("msg.txt","Scan parent face") + 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 + + time.sleep(1) + print(student_info) + + i = 0 + while(i <= 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) + print(i) + i += 1 + 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 + for i in range(10): + display.set_string("msg.txt",f'Door will close in {10-i}') + time.sleep(1) + + 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 + else: + display.set_page("scan") + display.set_string("msg.txt","Wrong parent. \t Try Again") + should_restart = True + break + #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_restart = True + time.sleep(1) + else: + pass + else: + #TODO is this a parent? + 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: + 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 34de08b..ddce4be 100644 --- a/access_control_python/main.py +++ b/access_control_python/main.py @@ -6,7 +6,7 @@ from access_control_mqtt import access_control from database import Database, Student, Parent from display import Display -SERIAL_PORT_DISPLAY = "COM26" +SERIAL_PORT_DISPLAY = "COM11" CAMERA_INDEX = 0 cam = cv2.VideoCapture(CAMERA_INDEX) @@ -43,7 +43,6 @@ while True: if(len(faces)==1): 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) @@ -51,24 +50,52 @@ while True: 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 + #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) 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. - #TODO Log Access STDw/P - #TODO write parrent info to display + #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 - time.sleep(10) + #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() - #TODO Check if door can be locked - # if can lock, lock + 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 + + + # #TODO If can't lock, say left open #Countdown # Activate Alarm else: - display.set_page("scan") display.set_string("msg.txt","Wrong parent. \t Try Again") #TODO Try again does not try again diff --git a/access_control_python/test.py b/access_control_python/test.py index 84a5814..bdc6e44 100644 --- a/access_control_python/test.py +++ b/access_control_python/test.py @@ -16,10 +16,10 @@ plc = ESPMegaMQTT() while True: - for i in range(8,16): - plc.write_pwm(i, 0, 4095) + for i in range(0,16): + plc.write_pwm(i, 0, 2000) time.sleep(1) - for i in range(8,16): - plc.write_pwm(i, 1, 4095) + for i in range(0,16): + plc.write_pwm(i, 1, 2000) time.sleep(1) diff --git a/access_control_python/testdispaly.py b/access_control_python/testdispaly.py new file mode 100644 index 0000000..01a1def --- /dev/null +++ b/access_control_python/testdispaly.py @@ -0,0 +1,33 @@ +import time +import serial + +device = serial.Serial( + port="COM11", + baudrate =115200, + parity=serial.PARITY_NONE, + stopbits=serial.STOPBITS_ONE, + bytesize=serial.EIGHTBITS, + timeout=1) + +def get_stop_bit() -> bytearray: + stop_bit = bytearray() + stop_bit.append(0xFF) + stop_bit.append(0xFF) + stop_bit.append(0XFF) + return stop_bit +def send_stop_bit(): + device.write(get_stop_bit()) +def reset_device(): + send_command("rest") +def send_command(command: str): + command = bytes(command, 'ascii') + device.write(command) + send_stop_bit() +def send_command(command: str): + command = bytes(command, 'ascii') + device.write(command) + send_stop_bit() + +send_command("rest") +time.sleep(1) +send_command("page parent") \ No newline at end of file diff --git a/display/display.HMI b/display/display.HMI index c09bcb2..ce911d0 100644 Binary files a/display/display.HMI and b/display/display.HMI differ diff --git a/face_recognition/boom.jpg b/face_recognition/face_recog_old/face_recog_2/boom.jpg similarity index 100% rename from face_recognition/boom.jpg rename to face_recognition/face_recog_old/face_recog_2/boom.jpg diff --git a/face_recognition/cv_test.py b/face_recognition/face_recog_old/face_recog_2/cv_test.py similarity index 100% rename from face_recognition/cv_test.py rename to face_recognition/face_recog_old/face_recog_2/cv_test.py diff --git a/face_recognition/face_recog_2/datacollect.py b/face_recognition/face_recog_old/face_recog_2/datacollect.py similarity index 100% rename from face_recognition/face_recog_2/datacollect.py rename to face_recognition/face_recog_old/face_recog_2/datacollect.py diff --git a/face_recognition/face_main.py b/face_recognition/face_recog_old/face_recog_2/face_main.py similarity index 100% rename from face_recognition/face_main.py rename to face_recognition/face_recog_old/face_recog_2/face_main.py diff --git a/face_recognition/face_main_2.py b/face_recognition/face_recog_old/face_recog_2/face_main_2.py similarity index 100% rename from face_recognition/face_main_2.py rename to face_recognition/face_recog_old/face_recog_2/face_main_2.py diff --git a/face_recognition/face_recog_2/haarcascade_frontalface_default.xml b/face_recognition/face_recog_old/face_recog_2/haarcascade_frontalface_default.xml similarity index 100% rename from face_recognition/face_recog_2/haarcascade_frontalface_default.xml rename to face_recognition/face_recog_old/face_recog_2/haarcascade_frontalface_default.xml diff --git a/face_recognition/tat.jpg b/face_recognition/face_recog_old/face_recog_2/tat.jpg similarity index 100% rename from face_recognition/tat.jpg rename to face_recognition/face_recog_old/face_recog_2/tat.jpg diff --git a/face_recognition/face_recog_2/test.py b/face_recognition/face_recog_old/face_recog_2/test.py similarity index 100% rename from face_recognition/face_recog_2/test.py rename to face_recognition/face_recog_old/face_recog_2/test.py diff --git a/face_recognition/faces_data/Sukatat/WIN_20231114_15_24_32_Pro.jpg b/face_recognition/faces_data/Sukatat/WIN_20231114_15_24_32_Pro.jpg new file mode 100644 index 0000000..a740d9f Binary files /dev/null and b/face_recognition/faces_data/Sukatat/WIN_20231114_15_24_32_Pro.jpg differ diff --git a/face_recognition/faces_data/Sukatat/WIN_20231114_15_26_56_Pro.jpg b/face_recognition/faces_data/Sukatat/WIN_20231114_15_26_56_Pro.jpg new file mode 100644 index 0000000..a8aafdd Binary files /dev/null and b/face_recognition/faces_data/Sukatat/WIN_20231114_15_26_56_Pro.jpg differ diff --git a/face_recognition/faces_data/Sukatat/WIN_20231114_15_26_58_Pro.jpg b/face_recognition/faces_data/Sukatat/WIN_20231114_15_26_58_Pro.jpg new file mode 100644 index 0000000..3817242 Binary files /dev/null and b/face_recognition/faces_data/Sukatat/WIN_20231114_15_26_58_Pro.jpg differ diff --git a/face_recognition/faces_data/Sukatat/WIN_20231114_15_26_59_Pro.jpg b/face_recognition/faces_data/Sukatat/WIN_20231114_15_26_59_Pro.jpg new file mode 100644 index 0000000..47e573f Binary files /dev/null and b/face_recognition/faces_data/Sukatat/WIN_20231114_15_26_59_Pro.jpg differ diff --git a/face_recognition/faces_data/Sukatat/WIN_20231114_15_27_03_Pro.jpg b/face_recognition/faces_data/Sukatat/WIN_20231114_15_27_03_Pro.jpg new file mode 100644 index 0000000..bb31216 Binary files /dev/null and b/face_recognition/faces_data/Sukatat/WIN_20231114_15_27_03_Pro.jpg differ diff --git a/face_recognition/faces_data/Sukatat/WIN_20231114_15_27_04_Pro.jpg b/face_recognition/faces_data/Sukatat/WIN_20231114_15_27_04_Pro.jpg new file mode 100644 index 0000000..45e52fb Binary files /dev/null and b/face_recognition/faces_data/Sukatat/WIN_20231114_15_27_04_Pro.jpg differ diff --git a/face_recognition/faces_data/Sukatat/WIN_20231114_15_27_06_Pro.jpg b/face_recognition/faces_data/Sukatat/WIN_20231114_15_27_06_Pro.jpg new file mode 100644 index 0000000..8ed92f5 Binary files /dev/null and b/face_recognition/faces_data/Sukatat/WIN_20231114_15_27_06_Pro.jpg differ diff --git a/face_recognition/faces_data/Sukatat/img_2.jpg b/face_recognition/faces_data/Sukatat/img_2.jpg new file mode 100644 index 0000000..a740d9f Binary files /dev/null and b/face_recognition/faces_data/Sukatat/img_2.jpg differ diff --git a/face_recognition/faces_data/Sukatat/representations_vgg_face.pkl b/face_recognition/faces_data/Sukatat/representations_vgg_face.pkl new file mode 100644 index 0000000..887be51 Binary files /dev/null and b/face_recognition/faces_data/Sukatat/representations_vgg_face.pkl differ diff --git a/face_recognition/img_1.jpg b/face_recognition/img_1.jpg new file mode 100644 index 0000000..8bf880e Binary files /dev/null and b/face_recognition/img_1.jpg differ diff --git a/face_recognition/test.py b/face_recognition/test.py new file mode 100644 index 0000000..85c48fb --- /dev/null +++ b/face_recognition/test.py @@ -0,0 +1,11 @@ +from deepface import DeepFace +import pandas as pd + +image_path = 'D:\Git\stm32-fmt-code\\face_recognition\img_1.jpg' +database_path = 'D:\Git\stm32-fmt-code\\face_recognition\\faces_data\Sukatat' + + + +DeepFace.stream(db_path = database_path) + +#result.head() \ No newline at end of file