diff --git a/README.md b/README.md index a8e1cbd..1d323d9 100755 --- a/README.md +++ b/README.md @@ -19,3 +19,8 @@ Ensure that OpenCV 3.3.0 is installed Begins a multithread session that will establish communications with N7 Tablet, Arduino and PC. Also starts a video stream that will attempt to detect symbols in front of it. Program will conclude the ID of the detected symbol depending on an arbitrary threshold value. Still a work in progress - multithread communication is commented out for now to test the detection alone. + +## Connecting a new bluetooth device +`sudo hciconfig hci0 piscan` + +`hcitool scan` diff --git a/main.py b/main.py index 6232a20..d09a6ce 100644 --- a/main.py +++ b/main.py @@ -1,28 +1,24 @@ import os - -# Using Multiprocess instead of Multithread to employ all 4 cores of RPI -# from src.communicator.MultiThread import MultiThread -from src.communicator.MultiProcess import MultiProcess -# from src.detector.SymbolDetector import SymbolDetector +import argparse +from src.communicator.MultiThread import MultiThread +# from src.communicator.MultiProcess import MultiProcess +from src.communicator.MultiProcess_v2 import MultiProcess from src.Logger import Logger log = Logger() +parser = argparse.ArgumentParser(description='Main Program for MDP') +parser.add_argument('-v', '--verbose', const=True, default=False, nargs='?') + def init(): + args = parser.parse_args() + verbose = args.verbose os.system("sudo hciconfig hci0 piscan") try: - multi_thread = MultiProcess() + multi_thread = MultiProcess(verbose) multi_thread.start() - - # Currently SymbolDetector seems to be running really slow - # on a thread. So if just testing SymbolDetector, comment and - # uncomment respectively the code above and below - - # detector = SymbolDetector() - # detector.detect() except KeyboardInterrupt: multi_thread.end() - # detector.end() if __name__ == '__main__': init() diff --git a/playgrounds/Multiprocess_comm.py b/playgrounds/Multiprocess_comm.py index 3e81ba7..98e86d4 100644 --- a/playgrounds/Multiprocess_comm.py +++ b/playgrounds/Multiprocess_comm.py @@ -5,63 +5,38 @@ import time from src.Logger import Logger -from src.communicator.PC import PC +from src.detector.VideoStream import VideoStream log = Logger() class MultiThread: def __init__(self): log.info('Initializing Multithread Communication') - self.pc = PC() - self.pc.connect() - - self.pc_queue = Queue() + self.vid = VideoStream((540, 480), 30) def start(self): - - read_pc = Process(target=self.read_pc, args=(self.pc_queue,)) - read_pc.start() + self.vid.start() + time.sleep(3) + + Process(target=self.detect_symbols, args=()).start() + while True: + # but this is fine + print('ff: ' + str(self.vid.read()[0][0])) + time.sleep(2) - write_pc = Process(target=self.write_pc, args=(self.pc_queue,)) - write_pc.start() - def end(self): log.info('Multithread Communication Session Ended') - - def read_pc(self, pc_queue): - print('read read') - while True: - msg = self.pc.read() - if msg is not None: - print('Received: ' + str(msg)) - pc_queue.put_nowait(msg['payload']) - ''' - log.info('Read PC: ' + str(msg['target']) + '; ' + str(msg['payload'])) - if msg['target'] == 'android': - android_queue.put_nowait(msg['payload']) - elif msg['target'] == 'arduino': - arduino_queue.put_nowait(msg['payload']) - elif msg['target'] == 'both': - android_queue.put_nowait(msg['payload']['android']) - arduino_queue.put_nowait(msg['payload']['arduino']) - ''' - - def write_pc(self, pc_queue): - print('write write') - while True: - if not pc_queue.empty(): - msg = pc_queue.get_nowait() - print('Write: ' + str(msg)) - self.pc.write(msg) - log.info('Write PC: ' + str(msg)) - def detect_symbols(self, android_queue): + def detect_symbols(self): while True: - frame = self.detector.get_frame() - symbol_match = self.detector.detect(frame) - if symbol_match is not None: - print('Symbol Match ID: ' + str(symbol_match)) - android_queue.put_nowait('SID|' + str(symbol_match)) + # frame = self.vid.read() + # Weird problem that .read() does not update frame + print('frame:' + str(self.vid.read()[0][0])) + time.sleep(2) + # symbol_match = self.detector.detect(frame) + # if symbol_match is not None: + # print('Symbol Match ID: ' + str(symbol_match)) + # android_queue.put_nowait('SID|' + str(symbol_match)) mp = MultiThread() mp.start() diff --git a/playgrounds/Multithread_backup.py b/playgrounds/Multithread_backup.py index e119f65..074a81c 100644 --- a/playgrounds/Multithread_backup.py +++ b/playgrounds/Multithread_backup.py @@ -12,29 +12,17 @@ log = Logger() -''' -Multithreading essentially refers to running multiple processes in parallel -Communications between Rpi and other devices involve a session, which means -the Rpi will be waiting for a trigger. Hence if single threaded, Rpi can only -do one thing at one time. With multhreading, Rpi can have multiple sessions -simultaneuously. Image Recognition will have to be run as a thread as well, but -it seems to be running really slow at the moment. There's also an occasional -error: Camera component couldn't be enabled: Out of resources. If hit that error -just restart the process, wait a little for the resources to be released, then re -run the main program -''' - class MultiThread: def __init__(self): log.info('Initializing Multithread Communication') # self.android = Android() - self.arduino = Arduino() - self.pc = PC() + # self.arduino = Arduino() + # self.pc = PC() self.detector = SymbolDetector() # self.android.connect() - self.arduino.connect() - self.pc.connect() + # self.arduino.connect() + # self.pc.connect() self.android_queue = queue.Queue(maxsize= 0) self.arduino_queue = queue.Queue(maxsize=0) @@ -44,14 +32,14 @@ def start(self): self.detector.start() # _thread.start_new_thread(self.read_android, (self.pc_queue,)) - _thread.start_new_thread(self.read_arduino, (self.pc_queue,)) - _thread.start_new_thread(self.read_pc,(self.android_queue, self.arduino_queue,)) + # _thread.start_new_thread(self.read_arduino, (self.pc_queue,)) + # _thread.start_new_thread(self.read_pc,(self.android_queue, self.arduino_queue,)) # _thread.start_new_thread(self.write_android, (self.android_queue,)) - _thread.start_new_thread(self.write_arduino, (self.arduino_queue,)) - _thread.start_new_thread(self.write_pc, (self.pc_queue,)) + # _thread.start_new_thread(self.write_arduino, (self.arduino_queue,)) + # _thread.start_new_thread(self.write_pc, (self.pc_queue,)) - # _thread.start_new_thread(self.detect_symbols, (self.android_queue,)) + _thread.start_new_thread(self.detect_symbols, (self.android_queue,)) log.info('Multithread Communication Session Started') @@ -124,8 +112,14 @@ def write_pc(self, pc_queue): def detect_symbols(self, android_queue): while True: frame = self.detector.get_frame() + print(frame[0][0]) + time.sleep(2) + ''' symbol_match = self.detector.detect(frame) if symbol_match is not None: print('Symbol Match ID: ' + str(symbol_match)) android_queue.put_nowait('SID|' + str(symbol_match)) + ''' +mt = MultiThread() +mt.start() diff --git a/playgrounds/android_test.py b/playgrounds/android_test.py index 2ac3555..e4554d0 100644 --- a/playgrounds/android_test.py +++ b/playgrounds/android_test.py @@ -10,9 +10,11 @@ android = Android() android.connect() -# android.write("MDF|FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF|010002000400000000000043F0800100000003C08081000200080020F880100000000000040|N|18|1|0") -android.write("F|LLS4RS5RS4LS9S3") +# android.write("MDF|FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF|010002000400000000000043F0800100000003C08081000200080020F880100000000000040|N|18|1|15") +# android.write("F|LLS4RS5RS4LS9S3") while True: + msg = input("Send to android: ") + android.write(msg) ''' for status in statuses: message = {'bot_status' : status} @@ -31,6 +33,6 @@ android.write(json.dumps(message)) time.sleep(1) ''' - msg = android.read() - if msg is not None: - print('message from android: ' + str(msg)) + # msg = android.read() + # if msg is not None: + # print('message from android: ' + str(msg)) diff --git a/playgrounds/arduino_command.py b/playgrounds/arduino_command.py index 5b910eb..53df1e2 100644 --- a/playgrounds/arduino_command.py +++ b/playgrounds/arduino_command.py @@ -4,21 +4,24 @@ arduino = Arduino() arduino.connect() -msg_read = "FP|(1,1,N);(2,1,N);(2,2,E)" -pc_msg = pcMsgParser(msg_read) - -print(pc_msg) +# msg_read = "FP|(1,1,N);(2,1,N);(2,2,E)" +# pc_msg = pcMsgParser(msg_read) +# print(pc_msg) while True: + ''' try: msg = arduino.read() if msg is not None: print(msg) except Exception as e: pass + ''' command = input("Enter command to send to Arduino:") + ''' if command == 'demo': print('Init demo') arduino.write(pc_msg['payload']['arduino']) else: - arduino.write(command) + ''' + arduino.write(command) diff --git a/playgrounds/camera_test.py b/playgrounds/camera_test.py index b93acc9..08f3456 100755 --- a/playgrounds/camera_test.py +++ b/playgrounds/camera_test.py @@ -1,8 +1,13 @@ from picamera import PiCamera +from picamera.array import PiRGBArray from time import sleep camera = PiCamera() -camera.vflip = True -camera.start_preview() -sleep(10) -camera.stop_preview() +camera.resolution = (540, 480) +camera.framerate = 30 +camera.vflip = False +camera.hflip = False + +# camera.start_preview() +# sleep(10) +# camera.stop_preview() diff --git a/playgrounds/symbol_detect_test.py b/playgrounds/symbol_detect_test.py index 28ef0ef..9bafb69 100644 --- a/playgrounds/symbol_detect_test.py +++ b/playgrounds/symbol_detect_test.py @@ -1,12 +1,12 @@ from src.detector.SymbolDetector import SymbolDetector from imutils.video import FPS -detector = SymbolDetector() +detector = SymbolDetector(width=650, height=480, framerate=32) detector.start() fps = FPS().start() frame_count = 0 -while frame_count < 100: +while frame_count < 10000: frame = detector.get_frame() symbol_match = detector.detect(frame) if symbol_match is not None: diff --git a/src/communicator/Android.py b/src/communicator/Android.py index 1be5e10..4d50f44 100644 --- a/src/communicator/Android.py +++ b/src/communicator/Android.py @@ -66,7 +66,7 @@ def read(self): def write(self, message): try: self.client_sock.send(message) - log.info('Successfully wrote to Android: ' + str(message)) + # log.info('Successfully wrote to Android: ' + str(message)) except Exception as error: raise diff --git a/src/communicator/MultiProcess.py b/src/communicator/MultiProcess.py index 52ae421..2ab2d9b 100644 --- a/src/communicator/MultiProcess.py +++ b/src/communicator/MultiProcess.py @@ -37,38 +37,40 @@ def __init__(self): self.android_queue = Queue() self.arduino_queue = Queue() self.pc_queue = Queue() + self.detector_queue = Queue() + + self.frame_count = 0 def start(self): - # self.detector.start() + self.detector.start() + time.sleep(3) + + r_android = Process(target=self.read_android, args=(self.pc_queue, self.detector_queue,)).start() + w_android = Process(target=self.write_android, args=(self.android_queue,)).start() + + r_arduino = Process(target=self.read_arduino, args=(self.pc_queue,)).start() + w_arduino = Process(target=self.write_arduino, args=(self.arduino_queue,)).start() - r_android = Process(target=self.read_android, args=(self.pc_queue,)) - r_android.start() - w_android = Process(target=self.write_android, args=(self.android_queue,)) - w_android.start() - - r_arduino = Process(target=self.read_arduino, args=(self.pc_queue,)) - r_arduino.start() - w_arduino = Process(target=self.write_arduino, args=(self.arduino_queue,)) - w_arduino.start() - - r_pc = Process(target=self.read_pc, args=(self.android_queue, self.arduino_queue,)) - r_pc.start() - w_pc = Process(target=self.write_pc, args=(self.pc_queue,)) - w_pc.start() - - # symbol_detect = Process(target=self.detect_symbols, args=(self.android_queue,)) + r_pc = Process(target=self.read_pc, args=(self.android_queue, self.arduino_queue,)).start() + w_pc = Process(target=self.write_pc, args=(self.pc_queue,)).start() + + symbol_detect = Process(target=self.detect_symbols, args=(self.detector_queue, self.android_queue,)).start() + log.info('Multithread Communication Session Started') def end(self): self.detector.end() log.info('Multithread Communication Session Ended') - def read_android(self, pc_queue): + def read_android(self, pc_queue, detector_queue): while True: try: msg = self.android.read() if msg is not None: log.info('Read Android:' + str(msg)) - pc_queue.put_nowait(msg) + if msg == 'TP': + detector_queue.put_nowait(msg) + else: + pc_queue.put_nowait(msg) except Exception as e: log.error("Android read failed: " + str(e)) @@ -119,11 +121,26 @@ def write_pc(self, pc_queue): self.pc.write(msg) log.info('Write PC: ' + str(msg)) - def detect_symbols(self, android_queue): + def detect_symbols(self, detector_queue, android_queue): while True: + if not detector_queue.empty(): + msg = detector_queue.get_nowait() + if msg == 'TP': + print('Take photo') + frame = self.detector.get_frame() + self.frame_count = self.frame_count + 1 + print(frame[0][0], self.frame_count) + + # frame = self.detector.get_frame() + # self.frame_count = self.frame_count + 1 + # print(frame[0][0], self.frame_count) + # cv.imshow("Video Stream", frame) + # key = cv.waitKey(1) & 0xFF + ''' frame = self.detector.get_frame() symbol_match = self.detector.detect(frame) if symbol_match is not None: print('Symbol Match ID: ' + str(symbol_match)) android_queue.put_nowait('SID|' + str(symbol_match)) + ''' diff --git a/src/communicator/MultiProcess_v2.py b/src/communicator/MultiProcess_v2.py new file mode 100644 index 0000000..0216857 --- /dev/null +++ b/src/communicator/MultiProcess_v2.py @@ -0,0 +1,126 @@ +import json +import time +from multiprocessing import Process, Queue + +from src.detector.SymbolDetector import SymbolDetector + +from src.communicator.Android import Android +from src.communicator.Arduino import Arduino +from src.communicator.PC import PC +from src.communicator.utils import format_for +from src.Logger import Logger + +log = Logger() + +class MultiProcess: + def __init__(self, verbose): + log.info('Initializing Multiprocessing Communication') + self.verbose = verbose + self.android = Android() + self.arduino = Arduino() + self.pc = PC() + self.detector = SymbolDetector() + + # Just see if this makes the latency worse + # Otherwise try see if can do a mutex + # Essentially a separate process must be able to trigger the parent program + self.msg_queue = Queue() + self.img_queue = Queue() + + def start(self): + try: + self.android.connect() + self.arduino.connect() + self.pc.connect() + + Process(target=self.read_android, args=(self.msg_queue,)).start() + Process(target=self.read_arduino, args=(self.msg_queue,)).start() + Process(target=self.read_pc, args=(self.msg_queue, self.img_queue,)).start() + + Process(target=self.write_target, args=(self.msg_queue,)).start() + + log.info('Launching Symbol Detector') + self.detector.start() + + log.info('Multiprocess Communication Session Started') + + while True: + if not self.img_queue.empty(): + msg = self.img_queue.get_nowait() + if msg == 'TP': + log.info('Detecting for Symbols') + frame = self.detector.get_frame() + symbol_match = self.detector.detect(frame) + if symbol_match is not None: + log.info('Symbol Match ID: ' + str(symbol_match)) + self.pc.write('TC|' + str(symbol_match)) + else: + log.info('No Symbols Detected') + self.pc.write('TC|0') + except KeyboardInterrupt: + raise + + def end(self): + log.info('Multiprocess Communication Session Ended') + + def read_android(self, msg_queue): + while True: + try: + msg = self.android.read() + if msg is not None: + if self.verbose: + log.info('Read Android: ' + str(msg)) + if msg in ['w1', 'a', 'd', 'h']: + msg_queue.put_nowait(format_for('ARD', msg)) + else: + msg_queue.put_nowait(format_for('PC', msg)) + + except Exception as e: + log.error('Android read failed: ' + str(e)) + self.android.connect() + + def read_arduino(self, msg_queue): + while True: + msg = self.arduino.read() + if msg is not None and msg != "Connected": + if self.verbose: + log.info('Read Arduino: ' + str(msg)) + msg_queue.put_nowait(format_for('PC', msg)) + + def read_pc(self, msg_queue, img_queue): + while True: + msg = self.pc.read() + if msg is not None: + if self.verbose: + log.info('Read PC: ' + str(msg['target']) + '; ' + str(msg['payload'])) + if msg['target'] == 'android': + msg_queue.put_nowait(format_for('AND', msg['payload'])) + elif msg['target'] == 'arduino': + msg_queue.put_nowait(format_for('ARD', msg['payload'])) + elif msg['target'] == 'rpi': + img_queue.put_nowait(msg['payload']) + elif msg['target'] == 'both': + msg_queue.put_nowait(format_for('AND', msg['payload']['android'])) + msg_queue.put_nowait(format_for('ARD', msg['payload']['arduino'])) + + def write_target(self, msg_queue): + while True: + if not msg_queue.empty(): + msg = msg_queue.get_nowait() + msg = json.loads(msg) + payload = msg['payload'] + + if msg['target'] == 'PC': + if self.verbose: + log.info('Write PC:' + str(payload)) + self.pc.write(payload) + + elif msg['target'] == 'AND': + if self.verbose: + log.info('Write Android:' + str(payload)) + self.android.write(payload) + + elif msg['target'] == 'ARD': + if self.verbose: + log.info('Write Arduino:' + str(payload)) + self.arduino.write(payload) diff --git a/src/communicator/MultiThread.py b/src/communicator/MultiThread.py index e119f65..650be55 100644 --- a/src/communicator/MultiThread.py +++ b/src/communicator/MultiThread.py @@ -1,7 +1,8 @@ import _thread +from threading import Thread import queue import os -import cv2 as cv +# import cv2 as cv import time from src.Logger import Logger @@ -11,7 +12,7 @@ from src.communicator.Android import Android log = Logger() - + ''' Multithreading essentially refers to running multiple processes in parallel Communications between Rpi and other devices involve a session, which means @@ -27,31 +28,42 @@ class MultiThread: def __init__(self): log.info('Initializing Multithread Communication') - # self.android = Android() + self.android = Android() self.arduino = Arduino() self.pc = PC() self.detector = SymbolDetector() - # self.android.connect() + self.android.connect() self.arduino.connect() self.pc.connect() self.android_queue = queue.Queue(maxsize= 0) self.arduino_queue = queue.Queue(maxsize=0) self.pc_queue = queue.Queue(maxsize=0) + self.detector_queue = queue.Queue(maxsize=0) def start(self): - self.detector.start() + # self.detector.start() + - # _thread.start_new_thread(self.read_android, (self.pc_queue,)) + _thread.start_new_thread(self.read_android, (self.pc_queue, self.detector_queue)) _thread.start_new_thread(self.read_arduino, (self.pc_queue,)) _thread.start_new_thread(self.read_pc,(self.android_queue, self.arduino_queue,)) - # _thread.start_new_thread(self.write_android, (self.android_queue,)) + _thread.start_new_thread(self.write_android, (self.android_queue,)) _thread.start_new_thread(self.write_arduino, (self.arduino_queue,)) _thread.start_new_thread(self.write_pc, (self.pc_queue,)) + + ''' + Thread(target=self.read_android, args=(self.pc_queue, self.detector_queue,)).start() + Thread(target=self.read_arduino, args=(self.pc_queue,)).start() + Thread(target=self.read_pc, args=(self.android_queue, self.arduino_queue,)).start() - # _thread.start_new_thread(self.detect_symbols, (self.android_queue,)) + Thread(target=self.write_android, args=(self.android_queue,)).start() + Thread(target=self.write_arduino, args=(self.arduino_queue,)).start() + Thread(target=self.write_pc, args=(self.pc_queue,)).start() + ''' + # _thread.start_new_thread(self.detect_symbols, (self.detector_queue, self.android_queue,)) log.info('Multithread Communication Session Started') @@ -62,15 +74,16 @@ def end(self): self.detector.end() log.info('Multithread Communication Session Ended') - def read_android(self, pc_queue): + def read_android(self, pc_queue, detector_queue): while True: try: msg = self.android.read() if msg is not None: log.info('Read Android:' + str(msg)) - pc_queue.put_nowait(msg) - # if msg == 'SV': - # arduino_queue.put_nowait(msg) + if msg == 'TP': + detector_queue.put_nowait(msg) + else: + pc_queue.put_nowait(msg) except Exception as e: log.error("Android read failed: " + str(e)) @@ -82,7 +95,6 @@ def write_android(self, android_queue): try: msg = android_queue.get_nowait() self.android.write(msg) - # log.info('Write Android: ' + str(msg)) except Exception as e: log.error("Android write failed " + str(e)) self.android.connect() @@ -121,11 +133,18 @@ def write_pc(self, pc_queue): self.pc.write(msg) log.info('Write PC: ' + str(msg)) - def detect_symbols(self, android_queue): + def detect_symbols(self, detector_queue, android_queue): while True: frame = self.detector.get_frame() - symbol_match = self.detector.detect(frame) - if symbol_match is not None: - print('Symbol Match ID: ' + str(symbol_match)) - android_queue.put_nowait('SID|' + str(symbol_match)) - + # print(frame[0][0]) + if not detector_queue.empty(): + # Try match confidence of 1 frame + # If not good enough, will have to find a way to bump match_confidence + msg = detector_queue.get_nowait() + log.info('Detecting Symbols') + symbol_match = self.detector.detect(frame) + if symbol_match is not None: + log.info('Symbol Match ID: ' + str(symbol_match)) + android_queue.put_nowait('SID|' + str(symbol_match)) + else: + log.info('No symbols detected in frame') diff --git a/src/communicator/utils.py b/src/communicator/utils.py index b554cfd..3440f3e 100644 --- a/src/communicator/utils.py +++ b/src/communicator/utils.py @@ -1,9 +1,16 @@ +import json from src.Logger import Logger log = Logger() arduino_commands = ['H', 'F', 'S', 'Z'] arduino_out = ['SD', 'MC', 'CC', 'EC'] +def format_for(target, payload): + return json.dumps({ + 'target': target, + 'payload': payload + }) + ''' Parse messages received from Arduino, essentially filters out unnecessary messages @@ -46,6 +53,11 @@ def pcMsgParser(msg): 'android': payload, 'arduino': payload, } + + elif command == 'TP': + target = 'rpi' + payload = command + elif command in arduino_commands: target = 'arduino' payload = command diff --git a/src/config.py b/src/config.py index a97dbb8..b2bd841 100755 --- a/src/config.py +++ b/src/config.py @@ -6,7 +6,7 @@ RFCOMM_CHANNEL = 8 # raspberryHotPotato: B8:27:EB:14:A1:9C # MDPGrp14: B8:27:EB:12:0D:6F -RPI_MAC_ADDR = "B8:27:EB:12:0D:6F" +RPI_MAC_ADDR = "B8:27:EB:14:A1:9C" N7_MAC_ADDR = "" UUID = "00000000-0000-1000-8000-00805F9B34FC" @@ -18,16 +18,16 @@ # Arduino USB connection settings SERIAL_PORT = "/dev/ttyACM0" -BAUD_RATE = 9600 +BAUD_RATE = 115200 # Image Recognition Settings CAMERA_RES_WIDTH = 540 CAMERA_RES_HEIGHT = 480 CAMERA_FRAMERATE = 32 -MIN_CONTOUR_AREA = 1800 # Assuming at a distance of 20 - 25cm -MAX_CONTOUR_AREA = 6800 # Assuming min distance of 10 - 15cm -MATCH_CONFIDENCE_COUNT = 3 +MIN_CONTOUR_AREA = 2000 # Assuming at a distance of 20 - 25cm +MAX_CONTOUR_AREA = 9000 # Assuming min distance of 10 - 15cm +MATCH_CONFIDENCE_COUNT = 5 THRESHOLD = 75 MATCH_THRESHOLD = 0.23 ARROW_PIXEL_THRESHOLD = 10 diff --git a/src/detector/SymbolDetector.py b/src/detector/SymbolDetector.py index 64c81cb..1e472e8 100755 --- a/src/detector/SymbolDetector.py +++ b/src/detector/SymbolDetector.py @@ -48,6 +48,7 @@ def detect(self, image): _, contours, hierarchy = cv.findContours(pre_proc_frame, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contours = sorted(contours, key=cv.contourArea, reverse=True) + # cv.drawContours(image, contours, -1, (0, 0, 255), 3) filtered_contours = filter_contour_size(contours) cv.drawContours(image, filtered_contours, -1, (255, 0, 0), 3) @@ -96,16 +97,18 @@ def detect(self, image): if self.match_symbol_id == closest_match['id']: self.match_count = self.match_count + 1 - if (self.match_count == MATCH_CONFIDENCE_COUNT): - return(closest_match['id']) else: self.match_symbol_id = closest_match['id'] self.match_count = 1 + + if (self.match_count == MATCH_CONFIDENCE_COUNT): + return(closest_match['id']) else: self.match_symbol_id = None self.match_count = 0 + # Uncomment to visualize stream cv.imshow("Video Stream", image) key = cv.waitKey(1) & 0xFF diff --git a/src/detector/VideoStream.py b/src/detector/VideoStream.py index 774720e..3ee8b1f 100755 --- a/src/detector/VideoStream.py +++ b/src/detector/VideoStream.py @@ -21,6 +21,7 @@ def start(self): def update(self): for frame in self.stream: self.frame = frame.array + # print('frame update', self.frame[0][0]) self.raw_capture.truncate(0) if self.stopped: