From a5989c21be547886045e447d874259455ab5047b Mon Sep 17 00:00:00 2001 From: MDP G14 RPI Date: Tue, 1 Oct 2019 06:27:41 +0000 Subject: [PATCH] Slight improvement to SymbolDetector FPS in threaded env --- playgrounds/symbol_detect_test.py | 19 +++++++++++++++++-- requirements.txt | 25 +++++++++++++++++++++++++ src/config.py | 2 +- src/detector/Symbol.py | 7 +++++++ src/detector/SymbolDetector.py | 10 ++-------- 5 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 requirements.txt diff --git a/playgrounds/symbol_detect_test.py b/playgrounds/symbol_detect_test.py index 0ff901d..28ef0ef 100644 --- a/playgrounds/symbol_detect_test.py +++ b/playgrounds/symbol_detect_test.py @@ -1,4 +1,19 @@ from src.detector.SymbolDetector import SymbolDetector - +from imutils.video import FPS detector = SymbolDetector() -detector.detect() +detector.start() +fps = FPS().start() + +frame_count = 0 + +while frame_count < 100: + frame = detector.get_frame() + symbol_match = detector.detect(frame) + if symbol_match is not None: + print('Symbol Match ID: ' + str(symbol_match)) + fps.update() + frame_count = frame_count + 1 + +fps.stop() +print('Elapsed Time: ' + str(fps.elapsed())) +print('Frames per Sec: ' + str(fps.fps())) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..be4f16a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,25 @@ +Pillow==2.6.1 +PyBluez==0.22 +RPi.GPIO==0.6.3 +RTIMULib==7.2.1 +chardet==2.3.0 +colorama==0.3.2 +gpiozero==1.4.0 +html5lib==0.999 +imutils==0.5.3 +mcpi==0.1.1 +numpy==1.8.2 +pgzero==1.1 +picamera==1.10 +pifacecommon==4.2.1 +pifacedigitalio==3.1.0 +pygame==1.9.2a0 +pyserial==2.6 +python-apt==0.9.3.12 +python-debian==0.1.27 +requests==2.4.3 +sense-hat==2.2.0 +six==1.8.0 +spidev==3.0 +urllib3==1.9.1 +wheel==0.24.0 diff --git a/src/config.py b/src/config.py index 0bc4434..a30f309 100755 --- a/src/config.py +++ b/src/config.py @@ -23,7 +23,7 @@ # Image Recognition Settings CAMERA_RES_WIDTH = 540 CAMERA_RES_HEIGHT = 480 -CAMERA_FRAMERATE = 30 +CAMERA_FRAMERATE = 32 MIN_CONTOUR_AREA = 1800 # Assuming at a distance of 20 - 25cm MAX_CONTOUR_AREA = 6800 # Assuming min distance of 10 - 15cm diff --git a/src/detector/Symbol.py b/src/detector/Symbol.py index fb68750..648b5f9 100755 --- a/src/detector/Symbol.py +++ b/src/detector/Symbol.py @@ -13,6 +13,7 @@ def __init__(self): self.img = [] self.id = 0 self.name = '' + self.contour = None def load_symbols(filepath): ''' @@ -26,5 +27,11 @@ def load_symbols(filepath): symbols[i].id = SYMBOL_ID_MAP[symbol] filename = '{}.jpg'.format(symbol) symbols[i].img = cv.imread(filepath + '/' + filename) + + train_symbol_gray = cv.cvtColor(symbols[i].img, cv.COLOR_BGR2GRAY) + _, train_symbol_thresh = cv.threshold(train_symbol_gray, 127, 255, 0) + _, train_symbol_ctrs, _ = cv.findContours(train_symbol_thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) + train_symbol_ctrs = sorted(train_symbol_ctrs, key=cv.contourArea, reverse=True) + symbols[i].contour = train_symbol_ctrs[0] return symbols diff --git a/src/detector/SymbolDetector.py b/src/detector/SymbolDetector.py index 67a5288..37f876f 100755 --- a/src/detector/SymbolDetector.py +++ b/src/detector/SymbolDetector.py @@ -35,7 +35,7 @@ def __init__(self, width=CAMERA_RES_WIDTH, height=CAMERA_RES_HEIGHT, framerate=C def start(self): self.video_stream.start() time.sleep(3) - log.info('Detecting for Symbols') + log.info('Detecting for Symbols') def get_frame(self): return self.video_stream.read() @@ -63,13 +63,7 @@ def detect(self, image): match_results = [] for train_symbol in self.train_symbols: - train_symbol_gray = cv.cvtColor(train_symbol.img, cv.COLOR_BGR2GRAY) - _, train_symbol_thresh = cv.threshold(train_symbol_gray, 127, 255, 0) - _, train_symbol_ctrs, _ = cv.findContours(train_symbol_thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) - train_symbol_ctrs = sorted(train_symbol_ctrs, key=cv.contourArea, reverse=True) - train_symbol_ctr = train_symbol_ctrs[0] - - match_score = cv.matchShapes(symbol_contour, train_symbol_ctr, 1, 0.0) + match_score = cv.matchShapes(symbol_contour, train_symbol.contour, 1, 0.0) match_results.append({ 'score': match_score, 'contour': symbol_contour,