Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions edge_impulse_linux/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def now():
return round(time.time() * 1000)

class ImpulseRunner:
def __init__(self, model_path: str):
def __init__(self, model_path: str, timeout: int = 5):
self._model_path = model_path
self._tempdir = None
self._runner = None
Expand All @@ -22,6 +22,7 @@ def __init__(self, model_path: str):
self._debug = False
self._hello_resp = None
self._shm = None
self._timeout = timeout

def init(self, debug=False):
if not os.path.exists(self._model_path):
Expand All @@ -39,8 +40,8 @@ def init(self, debug=False):
else:
self._runner = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)

while not os.path.exists(socket_path) or self._runner.poll() is not None:
Expand All @@ -50,6 +51,8 @@ def init(self, debug=False):
raise Exception("Failed to start runner (" + str(self._runner.poll()) + ")")

self._client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# timeout the IPC connection in case the EIM hangs
self._client.settimeout(self._timeout)
self._client.connect(socket_path)

hello_resp = self._hello_resp = self.hello()
Expand Down
2 changes: 1 addition & 1 deletion examples/audio/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def main(argv):
with AudioImpulseRunner(modelfile) as runner:
try:
model_info = runner.init()
# model_info = runner.init(debug=True) # to get debug print out
# model_info = runner.init(debug=True, timeout=10) # to get debug print out and set longer timeout
labels = model_info['model_parameters']['labels']
print('Loaded runner for "' + model_info['project']['owner'] + ' / ' + model_info['project']['name'] + '"')

Expand Down
2 changes: 1 addition & 1 deletion examples/custom/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def main(argv):
runner = ImpulseRunner(modelfile)
try:
model_info = runner.init()
# model_info = runner.init(debug=True) # to get debug print out
# model_info = runner.init(debug=True, timeout=10) # to get debug print out and set longer timeout

print('Loaded runner for "' + model_info['project']['owner'] + ' / ' + model_info['project']['name'] + '"')

Expand Down
2 changes: 1 addition & 1 deletion examples/image/classify-full-frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def main(argv):
with ImageImpulseRunner(modelfile) as runner:
try:
model_info = runner.init()
# model_info = runner.init(debug=True) # to get debug print out
# model_info = runner.init(debug=True, timeout=10) # to get debug print out and set longer timeout

print('Loaded runner for "' + model_info['project']['owner'] + ' / ' + model_info['project']['name'] + '"')
labels = model_info['model_parameters']['labels']
Expand Down
2 changes: 1 addition & 1 deletion examples/image/classify-image.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def main(argv):
with ImageImpulseRunner(modelfile) as runner:
try:
model_info = runner.init()
# model_info = runner.init(debug=True) # to get debug print out
# model_info = runner.init(debug=True, timeout=10) # to get debug print out and set longer timeout

print('Loaded runner for "' + model_info['project']['owner'] + ' / ' + model_info['project']['name'] + '"')
labels = model_info['model_parameters']['labels']
Expand Down
2 changes: 1 addition & 1 deletion examples/image/classify-video.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def main(argv):
with ImageImpulseRunner(modelfile) as runner:
try:
model_info = runner.init()
# model_info = runner.init(debug=True) # to get debug print out
# model_info = runner.init(debug=True, timeout=10) # to get debug print out and set longer timeout
print('Loaded runner for "' + model_info['project']['owner'] + ' / ' + model_info['project']['name'] + '"')
labels = model_info['model_parameters']['labels']

Expand Down
2 changes: 1 addition & 1 deletion examples/image/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def main(argv):
with ImageImpulseRunner(modelfile) as runner:
try:
model_info = runner.init()
# model_info = runner.init(debug=True) # to get debug print out
# model_info = runner.init(debug=True, timeout=10) # to get debug print out and set longer timeout
print('Loaded runner for "' + model_info['project']['owner'] + ' / ' + model_info['project']['name'] + '"')
labels = model_info['model_parameters']['labels']
if len(args)>= 2:
Expand Down
2 changes: 1 addition & 1 deletion examples/image/set-thresholds.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def main(argv):
with ImageImpulseRunner(modelfile) as runner:
try:
model_info = runner.init()
# model_info = runner.init(debug=True) # to get debug print out
# model_info = runner.init(debug=True, timeout=10) # to get debug print out and set longer timeout

print('Loaded runner for "' + model_info['project']['owner'] + ' / ' + model_info['project']['name'] + '"')
if not 'thresholds' in model_info['model_parameters']:
Expand Down