Skip to content

Commit

Permalink
add sound remind
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco-ardu committed Mar 15, 2022
1 parent cb67262 commit a974fe3
Show file tree
Hide file tree
Showing 19 changed files with 761 additions and 2,019 deletions.
14 changes: 7 additions & 7 deletions Controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from loguru import logger
from View import ViewWindow
from Worker import Worker
from setDirection import isExist


class MainController:
Expand Down Expand Up @@ -36,6 +35,7 @@ def btnStop_clicked(self):
def shutdown(self):
self.view.Worker.stop()
self.view.btnStart.setEnabled(True)
logger.info('quit app')
self.view.app.quit()

def getConfig(self):
Expand Down Expand Up @@ -67,18 +67,18 @@ def change_sens_ios(self):

def change_checked_left(self):
if self.view.leftCameraButton.isChecked():
logger.info('setting left camera')
self.view.Worker.command.value = 1
else:
logger.info('setting right camera')
self.view.Worker.command.value = 2

def barcode_edit(self):
barcode = self.view.BarCodeValue.text().strip()
if len(barcode) != 0:
try:
self.view.Worker.barcode.put_nowait(barcode)
self.view.BarCodeValue.clear()
except queue.Full:
pass
if len(barcode) != 0 and not self.view.btnStart.isEnabled():
self.view.Worker.left_send_barcode.send(barcode)
self.view.Worker.right_send_barcode.send(barcode)
self.view.BarCodeValue.clear()

def change_auto_exp(self):
self.view.Worker.status['auto_exp_status'].value = 2
Expand Down
2 changes: 1 addition & 1 deletion Program.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

if __name__ == "__main__":
setLogPath()
logger.info('start')
logger.info('start app')
mp.freeze_support()
app = QApplication(sys.argv)
c = MainController(ViewWindow())
Expand Down
55 changes: 34 additions & 21 deletions View.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from PyQt5.QtWidgets import (
QMainWindow
)
from loguru import logger

from ui.ui_main_new import Ui_MainWindow
from factories.AlertFactory import AlertEnum, AlertDict
from ui.ui_main import Ui_MainWindow

with open('config.yml', 'r') as stream:
config = yaml.load(stream, Loader=yaml.FullLoader)
Expand All @@ -26,6 +28,7 @@ def __init__(self, parent=None):

def setup(self, controller):
self.controller = controller
self.sound_dict = self.setSounddict()
self.btnStart.clicked.connect(controller.btnStart_clicked)
self.btnStop.clicked.connect(controller.btnStop_clicked)
self.ShutDown.clicked.connect(controller.shutdown)
Expand All @@ -36,9 +39,16 @@ def setup(self, controller):
self.leftCameraButton.toggled.connect(controller.change_checked_left)
self.autoexp.clicked.connect(controller.change_auto_exp)
self.autofocus.clicked.connect(controller.change_auto_focus)
self.qs = QSound('sound/welcome.wav')
if config["PRODUCTION"] is True:
self.qs.play()
# self.qs = QSound('sound/welcome.wav')
# self.qs.play()

def setSounddict(self):
sound_dict = {}
for key in AlertDict:
alert = AlertDict[key]
sound_dict[key] = QSound(alert.warn_file)

return sound_dict

@pyqtSlot()
def setDefaultView(self):
Expand All @@ -62,23 +72,26 @@ def UpdateRearSlot(self, Image):
def keyPressEvent(self, event):
self.BarCodeValue.setFocus()
self.BarCodeValue.editingFinished.connect(self.controller.barcode_edit)
if event.key() == Qt.Key_Backspace:
text = self.BarCodeValue.text()
self.BarCodeValue.setText(text[:-1])
elif event.key() == Qt.Key_Delete:
self.BarCodeValue.clear()
else:
text = self.BarCodeValue.text()
self.BarCodeValue.setText(text + event.text())

def runAlert(self, WarnAlert):
if not self.qs.isFinished():
return

# self.labelMessage.setText(WarnAlert.warn_message)
sound_file = WarnAlert.warn_file
self.qs = QSound(sound_file)
self.qs.play()
# print(event.key(), Qt.Key_Delete)
# if event.key() == Qt.Key_Backspace:
# text = self.BarCodeValue.text()
# self.BarCodeValue.setText(text[:-1])
# elif event.key() == Qt.Key_Delete:
# self.BarCodeValue.clear()
# else:
# text = self.BarCodeValue.text()
# self.BarCodeValue.setText(text + event.text())

@pyqtSlot(AlertEnum)
def runAlert(self, alertKey):
for key in self.sound_dict:
if not self.sound_dict[key].isFinished():
return
WarnAlert = AlertDict[alertKey]
logger.info(alertKey)
logger.info(WarnAlert.warn_file)
current_sound = self.sound_dict[alertKey]
current_sound.play()

def setImg(self, frame, label):

Expand Down
42 changes: 32 additions & 10 deletions Worker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import multiprocessing as mp
import time
import traceback

import keyboard
import numpy
Expand All @@ -19,40 +20,61 @@ class Worker(QThread):

def __init__(self):
super().__init__()

self.new_value = {'lenPos_new': mp.Value('Q', 156), 'exp_time_new': mp.Value('Q', 20000),
'sens_ios_new': mp.Value('Q', 800)}
self.status = {'auto_exp_status': mp.Value('i', 1), 'auto_focus_status': mp.Value('i', 1)}
self.command = mp.Value('i', 0)
self.barcode = mp.Queue(4)
self.right_location = mp.Value('i', 2)
self.right_isQualified = mp.Value('i', 2)
self.left_location = mp.Value('i', 2)
self.left_isQualified = mp.Value('i', 2)
self.left_send_barcode, self.left_recv_barcode = mp.Pipe()
self.right_send_barcode, self.right_recv_barcode = mp.Pipe()
self.Mxid = isExist
self.save_yml = save_yml

def run(self):
logger.info('loading camera')
time.sleep(4)
self.Mxids = self.Mxid()
try:
LeftCamera = LeftCameraProcess(self.FrontImage, self.Alert, self.Mxids[0], self.new_value, self.status, self.barcode, self.command)
RightCamera = RightCameraProcess(self.RearImage, self.Alert, self.Mxids[1], self.new_value, self.status, self.barcode, self.command)
LeftCamera = LeftCameraProcess(self.FrontImage, self.Alert, self.Mxids[0], self.new_value, self.status,
self.left_recv_barcode, self.command, self.left_location, self.left_isQualified, self.right_location,self.right_isQualified)
RightCamera = RightCameraProcess(self.RearImage, self.Alert, self.Mxids[1], self.new_value, self.status,
self.right_recv_barcode, self.command, self.left_location, self.left_isQualified, self.right_location,self.right_isQualified)
Cameras = [LeftCamera, RightCamera]

for Camera in Cameras:
Camera.runCamera()

self.ThreadActive = True
self.leftActive = False
self.rightActive = False

while self.ThreadActive:
for Camera in Cameras:
Camera.getFrame()
Camera.getAlert()
# for Camera in Cameras:
if LeftCamera.recv_result.poll():
self.leftActive = True
LeftCamera.parse_left_result()
LeftCamera.getFrame()
LeftCamera.getAlert()
if RightCamera.recv_result.poll():
self.rightActive = True
RightCamera.parse_right_result()
RightCamera.getFrame()
RightCamera.getAlert()
if self.leftActive and self.rightActive:
self.leftActive = False
self.rightActive = False
RightCamera.setAlert()

for Camera in Cameras:
logger.info('stop camera')
Camera.endCamera()

self.quit()
except Exception as e:
logger.error(e)
self.run()
except Exception:
logger.error(traceback.print_exc())

def stop(self):
self.ThreadActive = False
59 changes: 32 additions & 27 deletions cameraFunc/DetectScrawCamera.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
args = yaml.load(stream, Loader=yaml.FullLoader)


def run_Scraw_Camera(frame_queue, command, alert, device_mxid, repeat_times, new_value, old_value, left_right, status,
def run_Scraw_Camera(frame_queue, command, alert, device_mxid, repeat_times, new_value, old_value, direction, status,
barcode, result):
setLogPath()
frames_qualified = ({})
max_count = args['max_count']
Barcode = None
logger.info('start {} camera'.format(left_right))
logger.info('start {} camera'.format(direction))
show_frame = None
pipeline = dai.Pipeline() # type: dai.Pipeline
resolution = rgb_resolutions[args['resolution']]
Expand Down Expand Up @@ -82,17 +82,17 @@ def run_Scraw_Camera(frame_queue, command, alert, device_mxid, repeat_times, new
device = dai.Device(pipeline, device_info, True)
mxid = device.getMxId()
usb_speed = device.getUsbSpeed()
print(" >>> MXID:", mxid)
names_list = {'SUPER': 'USB3.0', 'HIGH': 'USB2.0'}
print(" >>> USB speed:", names_list.get(usb_speed.name))
logger.info("MXID: {}".format(mxid))
logger.info("USB speed: {}".format(names_list.get(usb_speed.name)))
cam_out = device.getOutputQueue("cam_out", 1, True)
yolox_det_nn = device.getOutputQueue("yolox_det_nn", 30, False)
controlQueue = device.getInputQueue("control")
ctrl = dai.CameraControl()
ctrl.setManualFocus(new_value['lenPos_new'].value)
ctrl.setManualExposure(new_value['exp_time_new'].value, new_value['sens_ios_new'].value)
controlQueue.send(ctrl)
flag = '1' if left_right == 'left' else '2'
flag = '1' if direction == 'left' else '2'
while command.value != 0:
in_rgb = cam_out.get()
if in_rgb is not None:
Expand All @@ -117,28 +117,28 @@ def run_Scraw_Camera(frame_queue, command, alert, device_mxid, repeat_times, new
dets = multiclass_nms(boxes_xyxy, scores, nms_thr=0.2, score_thr=0.2)
if command.value == eval(flag):
if status['auto_focus_status'].value == 2:
logger.info('{} Camera Autoexposure enable'.format(left_right))
logger.info('{} Camera Autoexposure enable'.format(direction))
ctrl = dai.CameraControl()
ctrl.setAutoFocusMode(dai.CameraControl.AutoFocusMode.CONTINUOUS_VIDEO)
controlQueue.send(ctrl)
status['auto_focus_status'].value = 1
if status['auto_exp_status'].value == 2:
logger.info('{} Camera Autofocus enable, continuous'.format(left_right))
logger.info('{} Camera Autofocus enable, continuous'.format(direction))
ctrl = dai.CameraControl()
ctrl.setAutoExposureEnable()
controlQueue.send(ctrl)
status['auto_exp_status'].value = 1
if new_value['lenPos_new'].value != old_value['lenPos_old'].value \
and status['auto_focus_status'].value != 2:
logger.info('set {} Camera lenPos: {}'.format(left_right, new_value['lenPos_new'].value))
logger.info('set {} Camera lenPos: {}'.format(direction, new_value['lenPos_new'].value))
ctrl = dai.CameraControl()
ctrl.setManualFocus(new_value['lenPos_new'].value)
old_value['lenPos_old'].value = new_value['lenPos_new'].value
controlQueue.send(ctrl)
if new_value['exp_time_new'].value != old_value['exp_time_old'].value \
or new_value['sens_ios_new'].value != old_value['sens_ios_old'].value \
and status['auto_exp_status'].value != 2:
logger.info('set {} Camera exp_time: {}, sens_ios: {}'.format(left_right,
logger.info('set {} Camera exp_time: {}, sens_ios: {}'.format(direction,
new_value['exp_time_new'].value,
new_value['sens_ios_new'].value))
ctrl = dai.CameraControl()
Expand All @@ -147,6 +147,7 @@ def run_Scraw_Camera(frame_queue, command, alert, device_mxid, repeat_times, new
old_value['sens_ios_old'].value = new_value['sens_ios_new'].value
controlQueue.send(ctrl)
try:
no_screw, screw = [], []
if dets is not None:
final_boxes = dets[:, :4]
final_scores, final_cls_inds = dets[:, 4], dets[:, 5]
Expand All @@ -160,28 +161,32 @@ def run_Scraw_Camera(frame_queue, command, alert, device_mxid, repeat_times, new
conf=args['confidence_threshold'],
class_names=CLASSES,
)
if not barcode.empty():
max_count = 20
Barcode = barcode.get_nowait()
print(Barcode)
frames_qualified.setdefault(Barcode, {}).setdefault(left_right, {"res": [],
"count": []})
if max_count != 0 and Barcode is not None:
frames_qualified[Barcode][left_right]['res'].append(len(no_screw) == 0 and len(screw) == 3)
frames_qualified[Barcode][left_right]['count'].append(len(no_screw) + len(screw))
max_count -= 1
elif max_count == 0 and Barcode is not None:
result.put_nowait(frames_qualified)
print(result.get_nowait())
Barcode = None
if barcode.poll():
max_count = args['max_count']
Barcode = barcode.recv()
logger.info('{} camera get Barcode: {}'.format(direction, Barcode))
frames_qualified.setdefault("res", [])
frames_qualified.setdefault("count", [])
if max_count != 0 and Barcode is not None:
frames_qualified['res'].append(len(no_screw) == 0 and len(screw) == 3)
frames_qualified['count'].append(len(no_screw) + len(screw))
max_count -= 1
elif max_count == 0 and Barcode is not None:
result.send(frames_qualified)
logger.info('send {} camera result of detections to front'.format(direction))
frames_qualified = {}
max_count = args['max_count']
Barcode = None
frame_queue.put_nowait(show_frame)
except queue.Full:
pass
except Exception as e:
print(traceback.print_exc())
if repeat_times != 10:
logger.error(e)
if repeat_times.value < 10:
repeat_times.value += 1
run_Scraw_Camera(frame_queue, command, alert, device_mxid, repeat_times, new_value, old_value, left_right,
logger.info('try start {} camera {} time'.format(direction, repeat_times.value))
run_Scraw_Camera(frame_queue, command, alert, device_mxid, repeat_times, new_value, old_value, direction,
status,
barcode, result)
logger.error(f"Device {device_mxid} not found!\n" + str(e))
logger.error(f"Device {device_mxid} not found!")
raise RuntimeError(f"Device {device_mxid} not found!")
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ left_camera_exp_time: 20000
left_camera_lensPos: 156
left_camera_mxid: 14442C1031CD62D300
left_camera_sens_ios: 800
max_count: 20
max_count: 10
resolution: 720
right_camera_exp_time: 20000
right_camera_lensPos: 156
Expand Down
1 change: 1 addition & 0 deletions demo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def save_yml(config_camera):
config['{}_camera_sens_ios'.format(direction)] = config_camera[2]
config = yaml.dump(config)
stream.write(config)
logger.info('save {} camera parameters'.format(direction))


def play_sound(path):
Expand Down
18 changes: 14 additions & 4 deletions factories/AlertFactory.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
from enum import IntEnum

from model.AlertModel import FailedAlert, QualifiedAlert, ReminderLocationAlert
from model.AlertModel import FailedAlert, QualifiedAlert, ReminderLocationAlert, LeftFailedAlert, RightFailedAlert, \
LeftReminderLocationAlert, RightReminderLocationAlert


class AlertEnum(IntEnum):
NoAlert = 99
AlertIndex_Failed = 0
AlertIndex_Qualified = 1
AlertIndex_ReminderLocation = 2
AlertIndex_LeftFailed = 1
AlertIndex_RightFailed = 2
AlertIndex_Qualified = 3
AlertIndex_ReminderLocation = 4
AlertIndex_LeftReminderLocation = 5
AlertIndex_RightReminderLocation = 6


AlertDict = {
AlertEnum.AlertIndex_Failed: FailedAlert(),
AlertEnum.AlertIndex_LeftFailed: LeftFailedAlert(),
AlertEnum.AlertIndex_RightFailed: RightFailedAlert(),
AlertEnum.AlertIndex_Qualified: QualifiedAlert(),
AlertEnum.AlertIndex_ReminderLocation: ReminderLocationAlert()
AlertEnum.AlertIndex_ReminderLocation: ReminderLocationAlert(),
AlertEnum.AlertIndex_LeftReminderLocation: LeftReminderLocationAlert,
AlertEnum.AlertIndex_RightReminderLocation: RightReminderLocationAlert
}
Loading

0 comments on commit a974fe3

Please sign in to comment.