From b9360cf2c5112afd84bd3253ca5eb91140c310d5 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 25 Aug 2019 16:51:03 +0200 Subject: [PATCH] camera.close() should be in a finally block to make sure that we don't run out of resources because of failed camera initializations --- platypush/plugins/camera/pi.py | 76 +++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/platypush/plugins/camera/pi.py b/platypush/plugins/camera/pi.py index 4c8c68a6..90ed7430 100644 --- a/platypush/plugins/camera/pi.py +++ b/platypush/plugins/camera/pi.py @@ -124,26 +124,30 @@ class CameraPiPlugin(CameraPlugin): """ - camera = self._get_camera(**opts) - image_file = os.path.abspath(os.path.expanduser(image_file)) + camera = None - if preview: - camera.start_preview() + try: + camera = self._get_camera(**opts) + image_file = os.path.abspath(os.path.expanduser(image_file)) - if warmup_time: - time.sleep(warmup_time) + if preview: + camera.start_preview() - capture_opts = {} - if resize: - capture_opts['resize'] = tuple(resize) + if warmup_time: + time.sleep(warmup_time) - camera.capture(image_file, **capture_opts) + capture_opts = {} + if resize: + capture_opts['resize'] = tuple(resize) - if preview: - camera.stop_preview() + camera.capture(image_file, **capture_opts) - camera.close() - return {'image_file': image_file} + if preview: + camera.stop_preview() + return {'image_file': image_file} + finally: + if camera: + camera.close() @action def capture_sequence(self, n_images, directory, name_format='image_%04d.jpg', preview=False, warmup_time=2, @@ -179,33 +183,37 @@ class CameraPiPlugin(CameraPlugin): """ - camera = self._get_camera(**opts) - directory = os.path.abspath(os.path.expanduser(directory)) + camera = None - if preview: - camera.start_preview() + try: + camera = self._get_camera(**opts) + directory = os.path.abspath(os.path.expanduser(directory)) - if warmup_time: - time.sleep(warmup_time) - camera.exposure_mode = 'off' + if preview: + camera.start_preview() - camera.shutter_speed = camera.exposure_speed - g = camera.awb_gains - camera.awb_mode = 'off' - camera.awb_gains = g - capture_opts = {} + if warmup_time: + time.sleep(warmup_time) + camera.exposure_mode = 'off' - if resize: - capture_opts['resize'] = tuple(resize) + camera.shutter_speed = camera.exposure_speed + g = camera.awb_gains + camera.awb_mode = 'off' + camera.awb_gains = g + capture_opts = {} - images = [os.path.join(directory, name_format % (i+1)) for i in range(0, n_images)] - camera.capture_sequence(images, **capture_opts) + if resize: + capture_opts['resize'] = tuple(resize) - if preview: - camera.stop_preview() + images = [os.path.join(directory, name_format % (i+1)) for i in range(0, n_images)] + camera.capture_sequence(images, **capture_opts) - camera.close() - return {'image_files': images} + if preview: + camera.stop_preview() + + return {'image_files': images} + finally: + camera.close() @action def start_time_lapse(self, directory, n_images=None, interval=0, warmup_time=2,