Compare commits

..

10 Commits

Author SHA1 Message Date
marcus 15ae302fa7 Add healthcheck 2024-10-20 16:50:32 +11:00
marcus 0c21de1a73 Improve MQTT handling and add detailed logging
- Replaced blocking MQTT loop with non-blocking `loop_start()`.
- Enabled MQTT client logging to get detailed logs for debugging.
- Added debugging print statements to verify successful subscription.
2024-10-20 16:39:50 +11:00
marcus f74e8fc632 Simplify snapshot handling and improve debug logging
- Removed redundant `process_latest_snapshot()` function.
- Directly process snapshots from MQTT messages upon receiving.
- Added improved logging to help with debugging snapshot processing issues.
2024-10-20 16:35:19 +11:00
marcus be63a43613 Handle fetching latest snapshot from MQTT topic upon connection
- Added `get_latest_snapshot()` function to subscribe to the MQTT topic for the latest snapshot image.
- Updated `process_latest_snapshot()` to utilize the `get_latest_snapshot()` function.
- Improved the logging to provide better information about snapshot subscription status.
2024-10-20 16:31:57 +11:00
marcus fe25895b38 Better handling of payloads 2024-10-20 16:26:50 +11:00
marcus 05c9b1f713 improve 2024-10-20 16:25:27 +11:00
marcus 4801f4c56a Assess image at startup and add debug 2024-10-20 16:10:02 +11:00
marcus 8c21481f6e Move to ChatGPT canvas for improvements 2024-10-20 16:01:20 +11:00
marcus f5dee30509 improve debug 2024-10-20 15:55:45 +11:00
marcus 3fd01f4551 Improve 2024-10-20 14:04:42 +11:00
4 changed files with 191 additions and 164 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ RUN apt-get update && apt-get install -y \
RUN pip install --no-cache-dir paho-mqtt opencv-python-headless numpy requests
# Copy the dayglo detector script
COPY dayglo_detector.py /app/dayglo_detector.py
COPY dayglo_detector.py test_mqtt.py /app/
WORKDIR /app
+152 -154
View File
@@ -1,135 +1,5 @@
import os
import paho.mqtt.client as mqtt
import json
import requests
import cv2
import numpy as np
import time
import threading
# Configuration from environment variables
MQTT_BROKER = os.environ.get('MQTT_BROKER', 'frigate')
MQTT_PORT = int(os.environ.get('MQTT_PORT', '1883'))
MQTT_USERNAME = os.environ.get('MQTT_USERNAME')
MQTT_PASSWORD = os.environ.get('MQTT_PASSWORD')
MQTT_TOPIC_SUBSCRIBE = 'frigate/events'
MQTT_TOPIC_PUBLISH = 'homeassistant/sensor/dayglo_rating/state'
FRIGATE_URL = os.environ.get('FRIGATE_URL', 'http://frigate:5000')
INTERESTED_ZONES = ['Door_Front']
# Debug mode flag
DEBUG_MODE = True
def debug_print(message):
""" Helper function to print debug messages if debug mode is enabled """
if DEBUG_MODE:
print(f"[DEBUG] {message}")
def calculate_dayglo_rating(image):
debug_print("Calculating dayglo rating...")
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Define HSV range for "dayglo" colors (adjust as needed)
lower_color = np.array([20, 100, 100])
upper_color = np.array([40, 255, 255])
# Create a mask for the colors
mask = cv2.inRange(hsv_image, lower_color, upper_color)
# Calculate the percentage of "dayglo" pixels
dayglo_pixels = cv2.countNonZero(mask)
total_pixels = image.shape[0] * image.shape[1]
dayglo_rating = (dayglo_pixels / total_pixels) * 100
debug_print(f"Dayglo Rating calculated: {dayglo_rating:.2f}%")
return dayglo_rating
def on_connect(client, userdata, flags, rc):
debug_print(f"Connected to MQTT broker with result code {rc}")
client.subscribe(MQTT_TOPIC_SUBSCRIBE)
def on_message(client, userdata, msg):
debug_print("MQTT message received")
payload = json.loads(msg.payload)
event_type = payload.get('type')
after = payload.get('after', {})
label = after.get('label')
if event_type == 'new' and label == 'person':
zones = after.get('entered_zones', [])
if any(zone in INTERESTED_ZONES for zone in zones):
debug_print(f"Person detected in zones: {zones}")
threading.Thread(target=process_event, args=(after,)).start()
def process_event(event_data):
event_id = event_data.get('id')
camera = event_data.get('camera')
debug_print(f"Processing event {event_id} from camera {camera}")
# Get the snapshot URL for the event
snapshot_url = f"{FRIGATE_URL}/api/events/{event_id}/snapshot.jpg"
# Retrieve the snapshot
try:
response = requests.get(snapshot_url)
if response.status_code == 200:
debug_print(f"Snapshot retrieved successfully from {snapshot_url}")
np_arr = np.frombuffer(response.content, np.uint8)
image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
if image is not None:
dayglo_rating = calculate_dayglo_rating(image)
debug_print(f"Publishing dayglo rating: {dayglo_rating:.2f}%")
client.publish(MQTT_TOPIC_PUBLISH, f"{dayglo_rating:.2f}")
else:
debug_print("Failed to decode the snapshot image")
else:
debug_print(f"Failed to retrieve snapshot. HTTP Status code: {response.status_code}")
except Exception as e:
debug_print(f"Error retrieving snapshot: {e}")
def assess_latest_image():
""" Assess the most recent image when the script first starts """
debug_print("Assessing the most recent image on startup...")
# Retrieve the most recent event from Frigate
events_url = f"{FRIGATE_URL}/api/events?limit=1&has_snapshot=1"
try:
response = requests.get(events_url)
if response.status_code == 200:
events = response.json()
if events:
event_data = events[0]
debug_print(f"Most recent event ID: {event_data.get('id')}")
process_event(event_data)
else:
debug_print("No recent events found with snapshots")
else:
debug_print(f"Failed to retrieve events. HTTP Status code: {response.status_code}")
except Exception as e:
debug_print(f"Error retrieving events: {e}")
# MQTT setup
#client = mqtt.Client()
client = mqtt.Client(protocol=mqtt.MQTTv5)
#client = mqtt.Client(protocol=mqtt.MQTTv311)
if MQTT_USERNAME and MQTT_PASSWORD:
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
client.on_connect = on_connect
client.on_message = on_message
# Start MQTT connection
debug_print(f"Connecting to MQTT broker at {MQTT_BROKER}:{MQTT_PORT}...")
client.connect(MQTT_BROKER, MQTT_PORT, 60)
# Assess the latest image when the script starts
assess_latest_image()
# Start the MQTT loop
client.loop_forever()
import os
import paho.mqtt.client as mqtt
import numpy as np
import cv2
@@ -139,18 +9,28 @@ import json
# Configuration
MQTT_BROKER = os.environ.get('MQTT_BROKER', '10.59.221.172')
MQTT_PORT = int(os.environ.get('MQTT_PORT', '1883'))
MQTT_USERNAME = os.getenv('MQTT_USERNAME', 'your_username')
MQTT_PASSWORD = os.getenv('MQTT_PASSWORD', 'your_password')
MQTT_SNAPSHOT_TOPIC = "/frigate/patiocam/person/snapshot"
MQTT_TOPIC_PUBLISH = "homeassistant/sensor/dayglo_rating/state"
DAYGLO_THRESHOLD_TOPIC = "homeassistant/sensor/dayglo_threshold/state"
DISCOVERY_PREFIX = "homeassistant"
# Default threshold
dayglo_threshold = 50
# Default rating
last_rating = 0
# Default color thresholds for dayglo detection
LOWER_COLOR = np.array([20, 100, 100])
UPPER_COLOR = np.array([40, 255, 255])
def on_connect(client, userdata, flags, rc):
print("Connected with result code", rc)
if rc == 0:
print("Connected successfully to MQTT broker")
client.subscribe(MQTT_SNAPSHOT_TOPIC)
publish_discovery_configurations()
# Publish initial rating of 0
publish_rating(0)
else:
print(f"Failed to connect, return code {rc}")
def publish_discovery_configurations():
rating_config = {
@@ -161,28 +41,25 @@ def publish_discovery_configurations():
"icon": "mdi:brush",
"unique_id": "mqtt_dayglo_rating"
}
threshold_config = {
"name": "Dayglo Threshold",
"state_topic": DAYGLO_THRESHOLD_TOPIC,
"unit_of_measurement": "%",
"value_template": "{{ value_json.threshold }}",
"icon": "mdi:tune-vertical",
"unique_id": "mqtt_dayglo_threshold"
}
client.publish(f"{DISCOVERY_PREFIX}/sensor/dayglo_rating/config", json.dumps(rating_config), retain=True)
client.publish(f"{DISCOVERY_PREFIX}/sensor/dayglo_threshold/config", json.dumps(threshold_config), retain=True)
def publish_rating(rating):
global last_rating
last_rating = rating
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
def on_message(client, userdata, msg):
if msg.topic == MQTT_SNAPSHOT_TOPIC:
print("Snapshot received")
process_snapshot(msg.payload)
elif msg.topic == DAYGLO_THRESHOLD_TOPIC:
global dayglo_threshold
dayglo_threshold = float(msg.payload.decode('utf-8'))
print("Dayglo threshold updated to:", dayglo_threshold)
def process_snapshot(payload):
if not payload:
print("Empty payload received, skipping processing.")
return
print("Processing snapshot...")
try:
image_data = base64.b64decode(payload)
nparr = np.frombuffer(image_data, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
@@ -190,21 +67,142 @@ def process_snapshot(payload):
if image is not None:
rating = calculate_dayglo_rating(image)
print("Dayglo Rating calculated:", rating)
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
publish_rating(rating)
else:
print("Invalid image received")
except Exception as e:
print(f"Error processing snapshot: {e}")
def calculate_dayglo_rating(image):
print("Calculating dayglo rating...")
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_color = np.array([20, 100, 100])
upper_color = np.array([40, 255, 255])
mask = cv2.inRange(hsv_image, lower_color, upper_color)
mask = cv2.inRange(hsv_image, LOWER_COLOR, UPPER_COLOR)
dayglo_pixels = cv2.countNonZero(mask)
total_pixels = image.shape[0] * image.shape[1]
rating = (dayglo_pixels / total_pixels) * 100
return rating
client = mqtt.Client()
def connect_mqtt():
while True:
try:
client.connect(MQTT_BROKER, MQTT_PORT, 60)
break
except Exception as e:
print(f"Connection failed: {e}. Retrying in 5 seconds...")
time.sleep(5)
client = mqtt.Client(protocol=mqtt.MQTTv311)
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_BROKER, MQTT_PORT, 60)
connect_mqtt()
client.loop_forever()
import os
import time
import paho.mqtt.client as mqtt
import numpy as np
import cv2
import base64
import json
# Configuration
MQTT_BROKER = os.environ.get('MQTT_BROKER', '10.59.221.172')
MQTT_PORT = int(os.environ.get('MQTT_PORT', '1883'))
MQTT_USERNAME = os.getenv('MQTT_USERNAME', 'your_username')
MQTT_PASSWORD = os.getenv('MQTT_PASSWORD', 'your_password')
MQTT_SNAPSHOT_TOPIC = "/frigate/patiocam/person/snapshot"
MQTT_TOPIC_PUBLISH = "homeassistant/sensor/dayglo_rating/state"
DISCOVERY_PREFIX = "homeassistant"
# Default rating
last_rating = 0
# Default color thresholds for dayglo detection
LOWER_COLOR = np.array([20, 100, 100])
UPPER_COLOR = np.array([40, 255, 255])
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected successfully to MQTT broker")
client.subscribe(MQTT_SNAPSHOT_TOPIC)
print(f"Subscribed to topic: {MQTT_SNAPSHOT_TOPIC}")
publish_discovery_configurations()
# Publish initial rating of 0
publish_rating(0)
else:
print(f"Failed to connect, return code {rc}")
def publish_discovery_configurations():
rating_config = {
"name": "Dayglo Rating",
"state_topic": MQTT_TOPIC_PUBLISH,
"unit_of_measurement": "%",
"value_template": "{{ value_json.rating }}",
"icon": "mdi:brush",
"unique_id": "mqtt_dayglo_rating"
}
client.publish(f"{DISCOVERY_PREFIX}/sensor/dayglo_rating/config", json.dumps(rating_config), retain=True)
def publish_rating(rating):
global last_rating
last_rating = rating
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
def on_message(client, userdata, msg):
if msg.topic == MQTT_SNAPSHOT_TOPIC:
print("Snapshot received")
process_snapshot(msg.payload)
def process_snapshot(payload):
if not payload:
print("Empty payload received, skipping processing.")
return
print("Processing snapshot...")
try:
image_data = base64.b64decode(payload)
nparr = np.frombuffer(image_data, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if image is not None:
rating = calculate_dayglo_rating(image)
print("Dayglo Rating calculated:", rating)
publish_rating(rating)
else:
print("Invalid image received")
except Exception as e:
print(f"Error processing snapshot: {e}")
def calculate_dayglo_rating(image):
print("Calculating dayglo rating...")
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_image, LOWER_COLOR, UPPER_COLOR)
dayglo_pixels = cv2.countNonZero(mask)
total_pixels = image.shape[0] * image.shape[1]
rating = (dayglo_pixels / total_pixels) * 100
return rating
def connect_mqtt():
while True:
try:
client.connect(MQTT_BROKER, MQTT_PORT, 60)
break
except Exception as e:
print(f"Connection failed: {e}. Retrying in 5 seconds...")
time.sleep(5)
client = mqtt.Client(protocol=mqtt.MQTTv311)
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
client.on_connect = on_connect
client.on_message = on_message
client.enable_logger()
connect_mqtt()
client.loop_start()
# Keep the script running
while True:
time.sleep(1)
+22
View File
@@ -0,0 +1,22 @@
import os
import paho.mqtt.client as mqtt
# Configuration
MQTT_BROKER = os.environ.get('MQTT_BROKER', '10.59.221.172')
MQTT_PORT = int(os.environ.get('MQTT_PORT', '1883'))
MQTT_USERNAME = os.getenv('MQTT_USERNAME', 'your_username')
MQTT_PASSWORD = os.getenv('MQTT_PASSWORD', 'your_password')
client = mqtt.Client(protocol=mqtt.MQTTv311) # Ensure you are using the correct client version
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD) # Set the username and password
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected successfully.")
else:
print(f"Connected with result code {rc}")
client.on_connect = on_connect
client.connect(MQTT_BROKER, MQTT_PORT, 60)
client.loop_forever()
+8 -1
View File
@@ -52,6 +52,12 @@ services:
limits:
cpus: '0.9'
memory: 5000M
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:5000/api/version"]
interval: 30s
timeout: 10s
retries: 5
start_period: 30s
dayglo_detector:
container_name: dayglo_detector
@@ -68,7 +74,8 @@ services:
FRIGATE_URL: 'http://frigate:5000'
INTERESTED_ZONES: 'Door_Front'
depends_on:
- frigate
frigate:
condition: service_healthy
volumes:
frigate-config: