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}")