Compare commits

...

38 Commits

Author SHA1 Message Date
marcus 77db82b875 done 2024-10-20 19:13:54 +11:00
marcus 296775bc2e Fix improper indentation in process_snapshot function
- Corrected inconsistent indentation to avoid runtime .
- Ensured that all indentation levels match Python's expected format.
2024-10-20 19:10:44 +11:00
marcus b2db064021 Fix indentation issues in main function for proper script execution
- Corrected inconsistent indentation in the main function to avoid runtime `IndentationError`.
- Ensured the MQTT client properly connects and publishes when processing images from the command line.
2024-10-20 19:08:00 +11:00
marcus fe209a9c2a IndentationError 2024-10-20 19:06:33 +11:00
marcus c948bdfd6c Add startup log message for Dayglo Detector script
- Added a log message to indicate when the Dayglo Detector script starts.
2024-10-20 19:01:10 +11:00
marcus b427ad12ec remove dup 2024-10-20 18:54:42 +11:00
marcus 96bf583eb7 Fix coroutine warning by properly awaiting async MQTT methods
- Wrapped `on_connect` and `on_message` methods with `asyncio.create_task()` to ensure they are properly awaited.
- Created separate handler functions for connection and message handling.
2024-10-20 18:52:00 +11:00
marcus 998ad12e61 Fix syntax error and redundant connection call in MQTT client
- Removed duplicate `async` keyword from `on_connect` method.
- Removed redundant `await client.connect()` to avoid double connection attempt.
2024-10-20 18:50:11 +11:00
marcus 564c963af1 Fix coroutine warning and replace MQTT client loop handling
- Properly awaited the `on_connect` method to resolve RuntimeWarning.
- Replaced `loop_forever()` with an async event loop using `asyncio.get_event_loop().create_future()`.
2024-10-20 18:48:18 +11:00
marcus 917aa913c1 move from paha-mqtt to gmqtt 2024-10-20 18:47:53 +11:00
marcus 0a4ebf1fec Replace paho-mqtt with gmqtt for modern MQTT 5.0 support
- Switched to `gmqtt` for better MQTT 5.0 compatibility and to address deprecation warnings.
- Refactored the MQTT client to use asynchronous handling with `asyncio`.
- Updated snapshot processing and MQTT communication to use async functions.
2024-10-20 18:44:47 +11:00
marcus 795b527f90 Fix indentation errors in on_connect function to resolve runtime issues
- Corrected inconsistent indentation to match the expected Python format.
- Ensured proper alignment of the `on_connect` function code block to avoid `IndentationError`.
2024-10-20 18:38:03 +11:00
marcus 8274d5c698 Fix indentation errors in on_connect function to prevent runtime issues
- Corrected inconsistent indentation to match the expected Python format.
- Ensured proper alignment of the `on_connect` function code block.
2024-10-20 18:35:15 +11:00
marcus 3b7cc953d6 Fix MQTT connection loop and enhance dayglo color detection
- Wrapped on_connect with a try-except block to prevent connection loop.
- Updated color thresholds for dayglo green and yellow to improve detection accuracy.
- Adjusted pixel calculation to focus on the cropped image area for better analysis.
2024-10-20 18:33:24 +11:00
marcus 9297c3cf12 Enhance dayglo color detection and resolve MQTT client deprecation warning
- Added broader color ranges for dayglo green and dayglo yellow to improve detection accuracy.
- Updated MQTT client to use a unique client ID to avoid deprecated callback usage.
2024-10-20 18:28:49 +11:00
marcus cbec68ccd8 Update MQTT client to use MQTTv5 and fix callback arguments
- Updated MQTT client to use MQTTv5 to eliminate deprecated usage.
- Corrected on_connect function signature and replaced deprecated `rc` with `reasonCode` for compatibility.
2024-10-20 18:17:46 +11:00
marcus 41248a2f2b Fix MQTT client configuration and callback to resolve breaking issues
- Reverted MQTT client to MQTTv311 to avoid compatibility issues.
- Updated on_connect method to match expected arguments for MQTTv5.
- Fixed deprecation and runtime errors for proper operation.
2024-10-20 18:15:08 +11:00
marcus 1b6c07376a Fix MQTT client configuration and add image analysis improvements
- Updated MQTT client to use MQTTv5 without deprecated `clean_session` to resolve ValueError.
- Fine-tuned color thresholds for better dayglo detection accuracy.
- Added cropping to focus analysis on the central area of the image for improved rating calculation.
- Provided command-line option for analyzing specific images, enabling debugging and fine-tuning.
2024-10-20 18:12:12 +11:00
marcus 994e814823 Fix deprecated MQTT client usage and enhance dayglo detection
- Updated MQTT client to use MQTTv5 to resolve deprecation warning.
- Fine-tuned color thresholds for dayglo detection.
- Added cropping to focus on the center 80% of the image for more accurate analysis.
- Improved functionality to process an image provided via command line before continuing with MQTT message handling.
2024-10-20 18:09:59 +11:00
marcus 53807d79c1 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.
2024-10-20 18:01:31 +11:00
marcus df5e959ff4 Add mechanism to request a fresh snapshot upon connecting
- Added a function to request a fresh snapshot from Frigate or similar system upon connecting.
- Removed client disconnection to allow continued listening for snapshot messages.
2024-10-20 17:55:05 +11:00
marcus 9e84d2aa75 Retrieve retained MQTT snapshot and disconnect after receiving
- Modified the script to disconnect after receiving the retained snapshot message.
- Simplified MQTT loop handling to ensure reliable retrieval of retained messages.
2024-10-20 17:52:41 +11:00
marcus 892b7507c8 Adjust MQTT connection to improve retained message handling
- Removed unnecessary empty publish request to prevent interference.
- Added a delay after connection to allow time for retained messages to be received.
2024-10-20 17:48:46 +11:00
marcus 1200f7302b Attempt to fetch retained snapshot upon connection
- Added logic to explicitly request the retained snapshot message after MQTT connection.
- Enhanced debugging to understand why the payload is empty.
2024-10-20 17:38:48 +11:00
marcus 17be02c9f7 Improve snapshot payload debugging and handling
- Added a check for payload length and print debugging information.
- Print part of the payload to help diagnose issues with image data.
2024-10-20 17:36:56 +11:00
marcus e9e5ffb931 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.
2024-10-20 17:35:04 +11:00
marcus 043e56e6f7 Fetch existing retained snapshot upon initial connection
- Added logic to request the retained snapshot message upon MQTT connection.
- Ensure initial snapshot is processed after connecting to the MQTT broker.
- Improved debugging logs for better traceability.
2024-10-20 17:28:56 +11:00
marcus 121673fde1 oops ... doubled up 2024-10-20 17:23:13 +11:00
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
6 changed files with 145 additions and 179 deletions
+2 -2
View File
@@ -7,10 +7,10 @@ RUN apt-get update && apt-get install -y \
&& rm -rf /var/lib/apt/lists/*
# Install OpenCV
RUN pip install --no-cache-dir paho-mqtt opencv-python-headless numpy requests
RUN pip install --no-cache-dir gmqtt 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
+107 -171
View File
@@ -1,158 +1,64 @@
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
import base64
import json
import tempfile
import sys
import asyncio
from gmqtt import Client as MQTTClient
# 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
def on_connect(client, userdata, flags, rc):
print("Connected with result code", rc)
client.subscribe(MQTT_SNAPSHOT_TOPIC)
publish_discovery_configurations()
# Default color thresholds for dayglo detection
# Expanded color thresholds for dayglo detection
LOWER_COLOR_GREEN = np.array([35, 50, 50])
UPPER_COLOR_GREEN = np.array([100, 255, 255])
LOWER_COLOR_YELLOW = np.array([15, 80, 80])
UPPER_COLOR_YELLOW = np.array([40, 255, 255])
def publish_discovery_configurations():
# Track if the initial snapshot has been processed
initial_snapshot_processed = False
connected_once = False
class DaygloDetectorMQTTClient(MQTTClient):
async def handle_on_connect(self, client, flags, rc, properties):
asyncio.create_task(self.handle_on_connect(client, flags, rc, properties))
global connected_once
if rc == 0 and not connected_once:
print("Connected successfully to MQTT broker")
await client.subscribe(MQTT_SNAPSHOT_TOPIC, qos=1)
print(f"Subscribed to topic: {MQTT_SNAPSHOT_TOPIC}")
connected_once = True
await publish_discovery_configurations(client)
# Publish initial rating of 0
await publish_rating(client, 0)
else:
print(f"Failed to connect, return code {rc}")
async def handle_on_message(self, client, topic, payload, qos, properties):
asyncio.create_task(self.handle_on_message(client, topic, payload, qos, properties))
if topic == MQTT_SNAPSHOT_TOPIC:
print("Snapshot received")
if len(payload) == 0:
print("Received an empty payload, skipping processing.")
else:
print(f"Payload length: {len(payload)} bytes")
print(f"Payload (first 100 bytes): {payload[:100]}...")
await process_snapshot(self, payload)
async def publish_discovery_configurations(client):
rating_config = {
"name": "Dayglo Rating",
"state_topic": MQTT_TOPIC_PUBLISH,
@@ -161,50 +67,80 @@ 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"
}
await client.publish(f"{DISCOVERY_PREFIX}/sensor/dayglo_rating/config", json.dumps(rating_config), retain=True)
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)
async def publish_rating(client, rating):
global last_rating
last_rating = rating
await 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)
async def process_snapshot(client, payload):
if not payload:
print("Empty payload received, skipping processing.")
return
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)
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
await publish_rating(client, rating)
else:
print("Invalid image format or corrupted image received")
except Exception as e:
print(f"Error processing snapshot: {e}")
def calculate_dayglo_rating(image):
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)
print("Calculating dayglo rating...")
# Crop the image to focus on the center area
height, width = image.shape[:2]
crop_margin = 0.1 # 10% margin
cropped_image = image[int(height * crop_margin):int(height * (1 - crop_margin)),
int(width * crop_margin):int(width * (1 - crop_margin))]
hsv_image = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2HSV)
mask_green = cv2.inRange(hsv_image, LOWER_COLOR_GREEN, UPPER_COLOR_GREEN)
mask_yellow = cv2.inRange(hsv_image, LOWER_COLOR_YELLOW, UPPER_COLOR_YELLOW)
mask = cv2.addWeighted(mask_green, 1.0, mask_yellow, 1.0, 0)
dayglo_pixels = cv2.countNonZero(mask)
total_pixels = image.shape[0] * image.shape[1]
total_pixels = cropped_image.shape[0] * cropped_image.shape[1]
rating = (dayglo_pixels / total_pixels) * 100
return rating
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_BROKER, MQTT_PORT, 60)
client.loop_forever()
async def main():
print("Starting Dayglo Detector...")
# Handle command line argument for image file
if len(sys.argv) > 1:
image_file = sys.argv[1]
if os.path.exists(image_file):
client = DaygloDetectorMQTTClient("dayglo_detector")
client.set_auth_credentials(MQTT_USERNAME, MQTT_PASSWORD)
await client.connect(MQTT_BROKER, MQTT_PORT, keepalive=60)
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)
await publish_rating(client, rating)
else:
print("Invalid image file provided.")
else:
print(f"File not found: {image_file}")
# Set up MQTT client for normal operation
client = DaygloDetectorMQTTClient("dayglo_detector")
client.set_auth_credentials(MQTT_USERNAME, MQTT_PASSWORD)
await client.connect(MQTT_BROKER, MQTT_PORT, keepalive=60)
await asyncio.get_event_loop().create_future()
if __name__ == "__main__":
asyncio.run(main())
+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()
Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 KiB

+10 -2
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
@@ -60,6 +66,7 @@ services:
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/docker/compose/frigate/debug-image/:/tmp/debug-image/
environment:
MQTT_BROKER: '10.59.221.172'
MQTT_PORT: '1883'
@@ -67,8 +74,9 @@ services:
MQTT_PASSWORD: 'RadGawlEikWothecOmtAmmoihumid8'
FRIGATE_URL: 'http://frigate:5000'
INTERESTED_ZONES: 'Door_Front'
depends_on:
- frigate
#depends_on:
# frigate:
# condition: service_healthy
volumes:
frigate-config: