Skip to content

Commit

Permalink
Slight improvement to SymbolDetector FPS in threaded env
Browse files Browse the repository at this point in the history
  • Loading branch information
MDP G14 RPI committed Oct 1, 2019
1 parent 004dc7a commit a5989c2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
19 changes: 17 additions & 2 deletions playgrounds/symbol_detect_test.py
Original file line number Diff line number Diff line change
@@ -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()))
25 changes: 25 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/detector/Symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(self):
self.img = []
self.id = 0
self.name = ''
self.contour = None

def load_symbols(filepath):
'''
Expand All @@ -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
10 changes: 2 additions & 8 deletions src/detector/SymbolDetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a5989c2

Please sign in to comment.