camera.close() should be in a finally block to make sure that we don't

run out of resources because of failed camera initializations
This commit is contained in:
Fabio Manganiello 2019-08-25 16:51:03 +02:00
parent 69a03fa025
commit b9360cf2c5

View file

@ -124,26 +124,30 @@ class CameraPiPlugin(CameraPlugin):
""" """
camera = self._get_camera(**opts) camera = None
image_file = os.path.abspath(os.path.expanduser(image_file))
if preview: try:
camera.start_preview() camera = self._get_camera(**opts)
image_file = os.path.abspath(os.path.expanduser(image_file))
if warmup_time: if preview:
time.sleep(warmup_time) camera.start_preview()
capture_opts = {} if warmup_time:
if resize: time.sleep(warmup_time)
capture_opts['resize'] = tuple(resize)
camera.capture(image_file, **capture_opts) capture_opts = {}
if resize:
capture_opts['resize'] = tuple(resize)
if preview: camera.capture(image_file, **capture_opts)
camera.stop_preview()
camera.close() if preview:
return {'image_file': image_file} camera.stop_preview()
return {'image_file': image_file}
finally:
if camera:
camera.close()
@action @action
def capture_sequence(self, n_images, directory, name_format='image_%04d.jpg', preview=False, warmup_time=2, 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) camera = None
directory = os.path.abspath(os.path.expanduser(directory))
if preview: try:
camera.start_preview() camera = self._get_camera(**opts)
directory = os.path.abspath(os.path.expanduser(directory))
if warmup_time: if preview:
time.sleep(warmup_time) camera.start_preview()
camera.exposure_mode = 'off'
camera.shutter_speed = camera.exposure_speed if warmup_time:
g = camera.awb_gains time.sleep(warmup_time)
camera.awb_mode = 'off' camera.exposure_mode = 'off'
camera.awb_gains = g
capture_opts = {}
if resize: camera.shutter_speed = camera.exposure_speed
capture_opts['resize'] = tuple(resize) 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)] if resize:
camera.capture_sequence(images, **capture_opts) capture_opts['resize'] = tuple(resize)
if preview: images = [os.path.join(directory, name_format % (i+1)) for i in range(0, n_images)]
camera.stop_preview() camera.capture_sequence(images, **capture_opts)
camera.close() if preview:
return {'image_files': images} camera.stop_preview()
return {'image_files': images}
finally:
camera.close()
@action @action
def start_time_lapse(self, directory, n_images=None, interval=0, warmup_time=2, def start_time_lapse(self, directory, n_images=None, interval=0, warmup_time=2,