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 cv2
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
|
import tempfile
|
||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
MQTT_BROKER = os.environ.get('MQTT_BROKER', '10.59.221.172')
|
MQTT_BROKER = os.environ.get('MQTT_BROKER', '10.59.221.172')
|
||||||
@@ -69,15 +70,19 @@ def process_snapshot(payload):
|
|||||||
print("Processing snapshot...")
|
print("Processing snapshot...")
|
||||||
try:
|
try:
|
||||||
image_data = base64.b64decode(payload)
|
image_data = base64.b64decode(payload)
|
||||||
nparr = np.frombuffer(image_data, np.uint8)
|
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_image_file:
|
||||||
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
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:
|
if image is not None:
|
||||||
rating = calculate_dayglo_rating(image)
|
rating = calculate_dayglo_rating(image)
|
||||||
print("Dayglo Rating calculated:", rating)
|
print("Dayglo Rating calculated:", rating)
|
||||||
publish_rating(rating)
|
publish_rating(rating)
|
||||||
else:
|
else:
|
||||||
print("Invalid image received")
|
print("Invalid image format or corrupted image received")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error processing snapshot: {e}")
|
print(f"Error processing snapshot: {e}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user