Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/SamYuen101234/BSMFR into main
Browse files Browse the repository at this point in the history
  • Loading branch information
SamYuen101234 committed Apr 6, 2021
1 parent d6134fe commit 07ceb7e
Show file tree
Hide file tree
Showing 6 changed files with 2,012 additions and 5 deletions.
49 changes: 44 additions & 5 deletions cam_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import pandas as pd
import logging
import time
from door import *
from pymata import *
from multiprocessing import Process, Lock, Value

VERSION = "Face Recognition"

Expand Down Expand Up @@ -164,7 +167,7 @@ class MyWindow(QMainWindow):
text_update = pyqtSignal(str)

# Create main window
def __init__(self, parent=None):
def __init__(self, ENTRANCE, parent=None):
QMainWindow.__init__(self, parent)

self.central = QWidget(self)
Expand Down Expand Up @@ -202,6 +205,9 @@ def __init__(self, parent=None):
self.detect_mask_count = 0
self.recognize_count = 0
self.recognize_result = None

self.door = ENTRANCE
self.lock = Value('d', 0.0, lock=False)

# Start image capture & display
def start(self):
Expand All @@ -216,14 +222,14 @@ def start(self):
# Fetch camera image from queue, and display it
def show_image(self, imageq, display, scale):

if self.recognize_result is not None:
time.sleep(5)
'''if self.recognize_result is not None:
#time.sleep(5)
with imageq.mutex:
imageq.queue.clear()
self.mask = 2
self.detect_mask_count = 0
self.recognize_count = 0
self.recognize_result = None
self.recognize_result = None'''

if not imageq.empty():
img, box, depth = imageq.get()
Expand Down Expand Up @@ -280,6 +286,10 @@ def show_image(self, imageq, display, scale):
self.recognize_result = result.json()['user']['first_name'] + " " + result.json()['user']['last_name']
else:
self.recognize_result = None
if self.lock.value == 0.0:
p2 = Process(target=self.door.reject, args=[self.lock])
p2.start()


add_bounding_box(img, box, self.mask, self.recognize_result)
self.recognize_count += 1
Expand All @@ -288,9 +298,20 @@ def show_image(self, imageq, display, scale):
print("Please put on a mask")
if self.recognize_result is not None:
print("Welcome " + self.recognize_result + ", please put on your mask before entering")
#self.door.open()
if self.lock.value == 0.0:
p1 = Process(target=self.door.open, args=[self.lock])
p1.start()
elif self.mask == 0:
if self.recognize_result is not None:
print("Welcome " + self.recognize_result)
if self.lock.value == 0.0:
p1 = Process(target=self.door.open, args=[self.lock])
p1.start()

#print(self.lock.value)



elif self.mask != 2:
self.mask = 2
Expand Down Expand Up @@ -331,6 +352,7 @@ def closeEvent(self, event):
capturing = False
self.capture_thread.join()


if __name__ == '__main__':
if len(sys.argv) > 1:
try:
Expand All @@ -340,9 +362,26 @@ def closeEvent(self, event):
if camera_num < 1:
print("Invalid camera number '%s'" % sys.argv[1])
else:
try:
# create a PyMata instance
# set the COM port string specifically for your platform
Arduino = PyMata("/dev/cu.usbmodem142101")
# create an entrance
ENTRANCE = door(Arduino)
except:
try:
# the second port
Arduino = PyMata("/dev/cu.usbmodem14101")
# create an entrance
ENTRANCE = door(Arduino)
except:
print("No door exist")
ENTRANCE = None


app = None
app = QApplication(sys.argv)
win = MyWindow()
win = MyWindow(ENTRANCE)
win.show()
win.setWindowTitle(VERSION)
win.start()
Expand Down
151 changes: 151 additions & 0 deletions door.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import sys
import signal
from pymata import *
from multiprocessing import Process


class door:
def __init__(self, board):
self.board = board
# listen for SIGINT
self.signal = signal.signal(signal.SIGINT, self.signal_handler)

# signal handler function called when Control-C occurs
def signal_handler(self, signal, frame):
print('You pressed Ctrl+C!!!!')
if self.board != None:
self.board.reset()
sys.exit(0)

def sound1(self):
BEEPER = 5 # pin that piezo device is attached to
start = time.time()
counter = 0.3
while (time.time() - start <= 10):
self.board.play_tone(BEEPER, self.board.TONE_TONE, 523, 100)
time.sleep(counter)
self.board.play_tone(BEEPER, self.board.TONE_TONE, 587, 100)
time.sleep(counter)
self.board.play_tone(BEEPER, self.board.TONE_TONE, 659, 100)
time.sleep(counter)
self.board.play_tone(BEEPER, self.board.TONE_TONE, 698, 100)
time.sleep(counter)
self.board.play_tone(BEEPER, self.board.TONE_TONE, 784, 100)
time.sleep(counter)
self.board.play_tone(BEEPER, self.board.TONE_TONE, 880, 100)
time.sleep(counter)
self.board.play_tone(BEEPER, self.board.TONE_TONE, 988, 100)
time.sleep(counter)
self.board.play_tone(BEEPER, self.board.TONE_TONE, 1046, 100)
time.sleep(0.5)
counter -= 0.05

# alarm for reject access
def sound2(self):
BEEPER = 5
start = time.time()
while (time.time() - start <= 3):
self.board.play_tone(BEEPER, self.board.TONE_TONE, 650, 230)
time.sleep(0.05)
self.board.play_tone(BEEPER, self.board.TONE_TONE, 2000, 100)
time.sleep(0.05)

# open door function
def blue_LED(self):
# pin3 on the arduino board
'''RED_LED = 4
self.board.set_pin_mode(RED_LED, self.board.OUTPUT, self.board.DIGITAL)
self.board.digital_write(RED_LED, 0)'''
print('hello')

BLUE_LED = 3
self.board.set_pin_mode(BLUE_LED, self.board.OUTPUT, self.board.DIGITAL)


counter = 1
# the timer for the LED
start = time.time()
### the LED light will turn on and off
while True:
### Stop the red LED first

# the first five second
if (time.time() - start) < 4:
self.board.digital_write(BLUE_LED, counter)
time.sleep(0.2)
# the first 5 - 9 sec
elif (time.time() - start) <= 8:
if counter == 1:
self.board.digital_write(BLUE_LED, counter)
counter = 0
else:
self.board.digital_write(BLUE_LED, counter)
counter = 1
time.sleep(0.5)
# the last 3 sec
elif (time.time() - start) <= 10:
if counter == 1:
self.board.digital_write(BLUE_LED, counter)
counter = 0
else:
self.board.digital_write(BLUE_LED, counter)
counter = 1
time.sleep(0.1)
# turn off the LED/close the door
else:
self.board.digital_write(BLUE_LED, 0)
time.sleep(3)
return


# reject signal
def red_LED(self):
# pin4 on the arduino board
BOARD_LED = 4
self.board.set_pin_mode(BOARD_LED, self.board.OUTPUT, self.board.DIGITAL)
counter = 1
# the timer for the LED
start = time.time()
### the LED light will turn on and off
while True:
if (time.time() - start) <= 3:
if counter == 1:
self.board.digital_write(BOARD_LED, counter)
counter = 0
else:
self.board.digital_write(BOARD_LED, counter)
counter = 1
time.sleep(0.5)
else:
self.board.digital_write(BOARD_LED, 0)
time.sleep(3)
return

def open(self, lock):
lock.value = 1.0
p1 = Process(target=self.sound1)
p1.start()
p2 = Process(target=self.blue_LED)
p2.start()
# wait the two processes
p1.join()
p2.join()
lock.value = 0.0

def reject(self, lock):
lock.value = 1.0
# parallel program to turn on the red LED and the buzzer at the same time
p1 = Process(target=self.sound2)
p1.start()
p2 = Process(target=self.red_LED)
p2.start()
# wait the two processes
p1.join()
p2.join()
lock.value = 0.0






Loading

0 comments on commit 07ceb7e

Please sign in to comment.