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.
This commit is contained in:
@@ -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}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user