Add command-line image processing option and fix deprecated MQTT client usage

- Removed deprecated MQTTv311 protocol usage.
- Added functionality to process an image provided via command line.
- Retained MQTT handling for normal message flow.
This commit is contained in:
2024-10-20 18:01:31 +11:00
parent df5e959ff4
commit 53807d79c1
+17 -9
View File
@@ -6,6 +6,7 @@ import cv2
import base64
import json
import tempfile
import sys
# Configuration
MQTT_BROKER = os.environ.get('MQTT_BROKER', '10.59.221.172')
@@ -34,8 +35,6 @@ def on_connect(client, userdata, flags, rc):
publish_discovery_configurations()
# Publish initial rating of 0
publish_rating(0)
# Request Frigate or the source to publish a fresh snapshot
request_fresh_snapshot()
else:
print(f"Failed to connect, return code {rc}")
@@ -55,12 +54,6 @@ def publish_rating(rating):
last_rating = rating
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
def request_fresh_snapshot():
# Assuming Frigate or another system listens to this topic to generate a snapshot
SNAPSHOT_REQUEST_TOPIC = "/frigate/patiocam/request_snapshot"
print(f"Requesting fresh snapshot by publishing to topic: {SNAPSHOT_REQUEST_TOPIC}")
client.publish(SNAPSHOT_REQUEST_TOPIC, json.dumps({"request": "snapshot"}), retain=False)
def on_message(client, userdata, msg):
global initial_snapshot_processed
if msg.topic == MQTT_SNAPSHOT_TOPIC:
@@ -115,7 +108,22 @@ def connect_mqtt():
print(f"Connection failed: {e}. Retrying in 5 seconds...")
time.sleep(5)
client = mqtt.Client(protocol=mqtt.MQTTv311)
# Handle command line argument for image file
if len(sys.argv) > 1:
image_file = sys.argv[1]
if os.path.exists(image_file):
print(f"Processing image from file: {image_file}")
image = cv2.imread(image_file)
if image is not None:
rating = calculate_dayglo_rating(image)
print("Dayglo Rating calculated from file:", rating)
else:
print("Invalid image file provided.")
else:
print(f"File not found: {image_file}")
# Set up MQTT client for normal operation
client = mqtt.Client()
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
client.on_connect = on_connect
client.on_message = on_message