From e9e5ffb9313b2517caf705664e39d09dfd47e060 Mon Sep 17 00:00:00 2001 From: Marcus Brown Date: Sun, 20 Oct 2024 17:35:04 +1100 Subject: [PATCH] Improve snapshot handling to support different image formats - Save snapshot as a temporary file before decoding, allowing better handling of JFIF or non-standard JPEG formats. - Improved error handling for invalid or corrupted image data. --- dayglo_detector/dayglo_detector.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/dayglo_detector/dayglo_detector.py b/dayglo_detector/dayglo_detector.py index 85f4ad7..bfce9c3 100644 --- a/dayglo_detector/dayglo_detector.py +++ b/dayglo_detector/dayglo_detector.py @@ -5,6 +5,7 @@ import numpy as np import cv2 import base64 import json +import tempfile # Configuration MQTT_BROKER = os.environ.get('MQTT_BROKER', '10.59.221.172') @@ -69,15 +70,19 @@ def process_snapshot(payload): print("Processing snapshot...") try: image_data = base64.b64decode(payload) - nparr = np.frombuffer(image_data, np.uint8) - image = cv2.imdecode(nparr, cv2.IMREAD_COLOR) + with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_image_file: + temp_image_file.write(image_data) + temp_image_path = temp_image_file.name + + # Attempt to read the saved image with OpenCV + image = cv2.imread(temp_image_path) if image is not None: rating = calculate_dayglo_rating(image) print("Dayglo Rating calculated:", rating) publish_rating(rating) else: - print("Invalid image received") + print("Invalid image format or corrupted image received") except Exception as e: print(f"Error processing snapshot: {e}")