From 6a9407744aaaa958e63dc9e9fb2ffba36bb7afd6 Mon Sep 17 00:00:00 2001 From: Casten Riepling Date: Thu, 10 Apr 2025 22:31:49 -0700 Subject: [PATCH 1/3] Detect max quality level dynamically. Detect max quality level dynamically. This will select 2 for standard Pico 2's and 5 for standard Pico's. --- PiCowbell_Camera_Demos/JPEG_Capture/code.py | 23 +++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/PiCowbell_Camera_Demos/JPEG_Capture/code.py b/PiCowbell_Camera_Demos/JPEG_Capture/code.py index b660016c9..49abce008 100644 --- a/PiCowbell_Camera_Demos/JPEG_Capture/code.py +++ b/PiCowbell_Camera_Demos/JPEG_Capture/code.py @@ -76,8 +76,27 @@ def open_next_image(): return open(filename, "wb") cam.colorspace = adafruit_ov5640.OV5640_COLOR_JPEG -cam.quality = 3 -b = bytearray(cam.capture_buffer_size) + +# Different platforms have different amounts of memory available. +# Typically a Pico 2 can handle quality = 2 and a Pico can handle quality = 5. +# Rather than detect and select sizes, let's try to detect the best dynamically +# for broader platform support. +# Start with the highest quality setting and attempt to allocate a buffer +# of the necessary size. If it fails, try the next lowest. +b = None +for quality in range(2,55): #valid range is 2 to 54 inclusive + try: + cam.quality = quality + print(f"Attempting to use quality {quality}.") + b = bytearray(cam.capture_buffer_size) + print(f"Quality {quality} successfully selected.") + break + except Exception: + print(f"Quality {quality} was too big. Trying next lowest.") + +if b==None: + print("There wasn't enough system memory to allocate the lowest quality buffer.") + jpeg = cam.capture(b) while True: From 3357fefbb0417f1478d849f64cba487363f9fe4e Mon Sep 17 00:00:00 2001 From: Casten Riepling Date: Fri, 11 Apr 2025 22:51:53 +0000 Subject: [PATCH 2/3] fix lint error --- PiCowbell_Camera_Demos/JPEG_Capture/code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PiCowbell_Camera_Demos/JPEG_Capture/code.py b/PiCowbell_Camera_Demos/JPEG_Capture/code.py index 49abce008..f84513764 100644 --- a/PiCowbell_Camera_Demos/JPEG_Capture/code.py +++ b/PiCowbell_Camera_Demos/JPEG_Capture/code.py @@ -94,7 +94,7 @@ def open_next_image(): except Exception: print(f"Quality {quality} was too big. Trying next lowest.") -if b==None: +if b is None: print("There wasn't enough system memory to allocate the lowest quality buffer.") jpeg = cam.capture(b) From b49083177b22a46da5de60ab99c0c44a0948db58 Mon Sep 17 00:00:00 2001 From: Casten Riepling Date: Sat, 12 Apr 2025 20:52:39 +0000 Subject: [PATCH 3/3] Tighten up allocation excaption catching --- PiCowbell_Camera_Demos/JPEG_Capture/code.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/PiCowbell_Camera_Demos/JPEG_Capture/code.py b/PiCowbell_Camera_Demos/JPEG_Capture/code.py index f84513764..c2d196f9b 100644 --- a/PiCowbell_Camera_Demos/JPEG_Capture/code.py +++ b/PiCowbell_Camera_Demos/JPEG_Capture/code.py @@ -78,10 +78,10 @@ def open_next_image(): cam.colorspace = adafruit_ov5640.OV5640_COLOR_JPEG # Different platforms have different amounts of memory available. -# Typically a Pico 2 can handle quality = 2 and a Pico can handle quality = 5. -# Rather than detect and select sizes, let's try to detect the best dynamically -# for broader platform support. -# Start with the highest quality setting and attempt to allocate a buffer +# Typically a Pico 2 can handle quality = 2 and a Pico can handle quality = 5. +# Rather than detect and select sizes, let's try to detect the best dynamically +# for broader platform support. +# Start with the highest quality setting and attempt to allocate a buffer # of the necessary size. If it fails, try the next lowest. b = None for quality in range(2,55): #valid range is 2 to 54 inclusive @@ -91,12 +91,12 @@ def open_next_image(): b = bytearray(cam.capture_buffer_size) print(f"Quality {quality} successfully selected.") break - except Exception: + except MemoryError: print(f"Quality {quality} was too big. Trying next lowest.") if b is None: print("There wasn't enough system memory to allocate the lowest quality buffer.") - + jpeg = cam.capture(b) while True: