Files
frigate/dayglo_detector/dayglo_detector.py
T
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

149 lines
5.2 KiB
Python

import os
import time
import paho.mqtt.client as mqtt
import numpy as np
import cv2
import base64
import json
import tempfile
import sys
# 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_GREEN = np.array([40, 70, 70])
UPPER_COLOR_GREEN = np.array([90, 255, 255])
LOWER_COLOR_YELLOW = np.array([20, 100, 100])
UPPER_COLOR_YELLOW = np.array([35, 255, 255])
# Track if the initial snapshot has been processed
initial_snapshot_processed = False
def on_connect(client, userdata, flags, reasonCode, properties=None):
try:
if reasonCode == 0:
print("Connected successfully to MQTT broker")
client.subscribe(MQTT_SNAPSHOT_TOPIC, qos=1)
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 {reasonCode}")
except Exception as e:
print(f"Caught exception in on_connect: {e}")
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):
global initial_snapshot_processed
if msg.topic == MQTT_SNAPSHOT_TOPIC:
print("Snapshot received")
if len(msg.payload) == 0:
print("Received an empty payload, skipping processing.")
else:
print(f"Payload length: {len(msg.payload)} bytes")
print(f"Payload (first 100 bytes): {msg.payload[:100]}...")
process_snapshot(msg.payload)
initial_snapshot_processed = True
def process_snapshot(payload):
if not payload:
print("Empty payload received, skipping processing.")
return
print("Processing snapshot...")
try:
image_data = base64.b64decode(payload)
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 format or corrupted image received")
except Exception as e:
print(f"Error processing snapshot: {e}")
def calculate_dayglo_rating(image):
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))]
print("Calculating dayglo rating...")
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.bitwise_or(mask_green, mask_yellow)
dayglo_pixels = cv2.countNonZero(mask)
total_pixels = cropped_image.shape[0] * cropped_image.shape[1]
rating = (dayglo_pixels / total_pixels) * 100
return rating
def connect_mqtt():
while True:
try:
client.connect(MQTT_BROKER, MQTT_PORT, keepalive=60)
break
except Exception as e:
print(f"Connection failed: {e}. Retrying in 5 seconds...")
time.sleep(5)
# 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_id="dayglo_detector", protocol=mqtt.MQTTv5)
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_forever()