diff --git a/edge_impulse_linux/runner.py b/edge_impulse_linux/runner.py index fecaa40..f371371 100755 --- a/edge_impulse_linux/runner.py +++ b/edge_impulse_linux/runner.py @@ -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 @@ -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): @@ -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: @@ -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() diff --git a/examples/audio/classify.py b/examples/audio/classify.py index 892bc71..4cfd604 100644 --- a/examples/audio/classify.py +++ b/examples/audio/classify.py @@ -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'] + '"') diff --git a/examples/custom/classify.py b/examples/custom/classify.py index d470dbd..36e2e3a 100644 --- a/examples/custom/classify.py +++ b/examples/custom/classify.py @@ -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'] + '"') diff --git a/examples/image/classify-full-frame.py b/examples/image/classify-full-frame.py index 85bde9b..3096246 100644 --- a/examples/image/classify-full-frame.py +++ b/examples/image/classify-full-frame.py @@ -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'] diff --git a/examples/image/classify-image.py b/examples/image/classify-image.py index 0a5cba5..e2e30b4 100644 --- a/examples/image/classify-image.py +++ b/examples/image/classify-image.py @@ -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'] diff --git a/examples/image/classify-video.py b/examples/image/classify-video.py index aa1b8bd..4c69472 100644 --- a/examples/image/classify-video.py +++ b/examples/image/classify-video.py @@ -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'] diff --git a/examples/image/classify.py b/examples/image/classify.py index c35d9da..4ffbc60 100755 --- a/examples/image/classify.py +++ b/examples/image/classify.py @@ -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: diff --git a/examples/image/set-thresholds.py b/examples/image/set-thresholds.py index 907dee6..8a11fa8 100755 --- a/examples/image/set-thresholds.py +++ b/examples/image/set-thresholds.py @@ -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']: