Skip to content

Commit

Permalink
Correct logical errors in __init__ and solve various related problems
Browse files Browse the repository at this point in the history
Signed-off-by: Asadullah Shaikh <[email protected]>
  • Loading branch information
pantheraleo-7 committed Dec 17, 2024
1 parent 31e381f commit db79106
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions picamera2/picamera2.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,19 @@ def __init__(self, camera_num=0, verbose_console=None, tuning=None, allocator=No
self.helpers = Helpers(self)
try:
self._open_camera()
except RuntimeError:
_log.error("Camera __init__ sequence did not complete.")
raise RuntimeError("Camera __init__ sequence did not complete.")
else:
_log.debug(f"{self.camera_manager}")
# We deliberately make raw streams with no size so that it will be filled in
# later once the main stream size has been set.
self.preview_configuration = self.create_preview_configuration()
self.preview_configuration.enable_raw() # causes the size to be reset to None
self.still_configuration = self.create_still_configuration()
self.still_configuration.enable_raw() # ditto
self.video_configuration = self.create_video_configuration()
self.video_configuration.enable_raw() # ditto
except Exception:
_log.error("Camera __init__ sequence did not complete.")
raise RuntimeError("Camera __init__ sequence did not complete.")
self.preview_configuration_ = CameraConfiguration(self.create_preview_configuration(), self)
self.preview_configuration_.enable_raw() # causes the size to be reset to None
self.still_configuration_ = CameraConfiguration(self.create_still_configuration(), self)
self.still_configuration_.enable_raw() # ditto
self.video_configuration_ = CameraConfiguration(self.create_video_configuration(), self)
self.video_configuration_.enable_raw() # ditto
finally:
if tuning_file is not None:
tuning_file.close() # delete the temporary file
Expand Down Expand Up @@ -425,12 +426,10 @@ def _grab_camera(self, idx):
elif isinstance(idx, int):
return self.camera_manager.cameras[idx]

def _initialize_camera(self) -> bool:
def _initialize_camera(self) -> None:
"""Initialize camera
:raises RuntimeError: Failure to initialise camera
:return: True if success
:rtype: bool
"""
if not self.camera_manager.cameras:
_log.error("Camera(s) not found (Do not forget to disable legacy camera with raspi-config).")
Expand Down Expand Up @@ -458,7 +457,6 @@ def _initialize_camera(self) -> bool:
self.sensor_format = self._native_mode['format']

_log.info('Initialization successful.')
return True

def __identify_camera(self):
for idx, address in enumerate(self.camera_manager.cameras):
Expand All @@ -471,7 +469,9 @@ def _open_camera(self) -> None:
:raises RuntimeError: Failed to setup camera
"""
if not self._initialize_camera():
try:
self._initialize_camera()
except RuntimeError:
raise RuntimeError("Failed to initialize camera")

# This now throws an error if it can't open the camera.
Expand Down Expand Up @@ -628,12 +628,12 @@ def close(self) -> None:
self.streams = None
self.stream_map = None
self.camera = None
self.camera_ctrl_info = None
self.camera_ctrl_info = {}
self.camera_config = None
self.libcamera_config = None
self.preview_configuration_ = None
self.still_configuration_ = None
self.video_configuration_ = None
self.preview_configuration = {}
self.still_configuration = {}
self.video_configuration = {}
self.notifymeread.close()
os.close(self.notifyme_w)
# Clean up the allocator
Expand Down

0 comments on commit db79106

Please sign in to comment.