From 09209b2872a56e47888f4819970dc31c5c9cb08f Mon Sep 17 00:00:00 2001 From: Mark Jessop Date: Sat, 21 Sep 2024 16:49:02 +0930 Subject: [PATCH] Allow a few attempt to initialise the camera on startup. Also catch any exceptions from the capture function and re-initialise the camera --- tx/WenetPiCamera2.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tx/WenetPiCamera2.py b/tx/WenetPiCamera2.py index 025e772..ade6586 100644 --- a/tx/WenetPiCamera2.py +++ b/tx/WenetPiCamera2.py @@ -54,7 +54,8 @@ class WenetPiCamera2(object): whitebalance = 'auto', lens_position = -1, temp_filename_prefix = 'picam_temp', - debug_ptr = None + debug_ptr = None, + init_retries = 10 ): """ Instantiate a WenetPiCam Object @@ -103,8 +104,13 @@ class WenetPiCamera2(object): self.whitebalance = self.wb_lookup['auto'] - - self.init_camera() + while init_retries > 0: + try: + self.init_camera() + except Exception as e: + self.debug_message(f"Error initialising camera, retrying in 10 seconds: - {str(e)}") + time.sleep(10) + init_retries -= 1 def init_camera(self): @@ -323,7 +329,11 @@ class WenetPiCamera2(object): capture_filename = destination_directory + "/%s_picam.jpg" % capture_time # Attempt to capture. - capture_successful = self.capture(capture_filename) + try: + capture_successful = self.capture(capture_filename) + except Exception as e: + self.debug_message(f"Exception on capture - {str(e)}") + capture_successful = False # If capture was unsuccessful, try again in a little bit if not capture_successful: