Compare commits
23 Commits
1200f7302b
..
coral
| Author | SHA1 | Date | |
|---|---|---|---|
| 77db82b875 | |||
| 296775bc2e | |||
| b2db064021 | |||
| fe209a9c2a | |||
| c948bdfd6c | |||
| b427ad12ec | |||
| 96bf583eb7 | |||
| 998ad12e61 | |||
| 564c963af1 | |||
| 917aa913c1 | |||
| 0a4ebf1fec | |||
| 795b527f90 | |||
| 8274d5c698 | |||
| 3b7cc953d6 | |||
| 9297c3cf12 | |||
| cbec68ccd8 | |||
| 41248a2f2b | |||
| 1b6c07376a | |||
| 994e814823 | |||
| 53807d79c1 | |||
| df5e959ff4 | |||
| 9e84d2aa75 | |||
| 892b7507c8 |
@@ -7,7 +7,7 @@ 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 test_mqtt.py /app/
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import os
|
||||
import time
|
||||
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')
|
||||
@@ -20,24 +22,43 @@ DISCOVERY_PREFIX = "homeassistant"
|
||||
last_rating = 0
|
||||
|
||||
# Default color thresholds for dayglo detection
|
||||
LOWER_COLOR = np.array([20, 100, 100])
|
||||
UPPER_COLOR = np.array([40, 255, 255])
|
||||
# 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])
|
||||
|
||||
# Track if the initial snapshot has been processed
|
||||
initial_snapshot_processed = False
|
||||
connected_once = False
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
if rc == 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 {rc}")
|
||||
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}")
|
||||
|
||||
def publish_discovery_configurations():
|
||||
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,
|
||||
@@ -46,26 +67,14 @@ def publish_discovery_configurations():
|
||||
"icon": "mdi:brush",
|
||||
"unique_id": "mqtt_dayglo_rating"
|
||||
}
|
||||
client.publish(f"{DISCOVERY_PREFIX}/sensor/dayglo_rating/config", json.dumps(rating_config), retain=True)
|
||||
await client.publish(f"{DISCOVERY_PREFIX}/sensor/dayglo_rating/config", json.dumps(rating_config), retain=True)
|
||||
|
||||
def publish_rating(rating):
|
||||
async def publish_rating(client, rating):
|
||||
global last_rating
|
||||
last_rating = rating
|
||||
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
|
||||
await 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):
|
||||
async def process_snapshot(client, payload):
|
||||
if not payload:
|
||||
print("Empty payload received, skipping processing.")
|
||||
return
|
||||
@@ -83,7 +92,7 @@ def process_snapshot(payload):
|
||||
if image is not None:
|
||||
rating = calculate_dayglo_rating(image)
|
||||
print("Dayglo Rating calculated:", rating)
|
||||
publish_rating(rating)
|
||||
await publish_rating(client, rating)
|
||||
else:
|
||||
print("Invalid image format or corrupted image received")
|
||||
except Exception as e:
|
||||
@@ -91,156 +100,47 @@ def process_snapshot(payload):
|
||||
|
||||
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)
|
||||
# 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
|
||||
|
||||
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)
|
||||
|
||||
import os
|
||||
import time
|
||||
import paho.mqtt.client as mqtt
|
||||
import numpy as np
|
||||
import cv2
|
||||
import base64
|
||||
import json
|
||||
import tempfile
|
||||
|
||||
# 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])
|
||||
|
||||
# Track if the initial snapshot has been processed
|
||||
initial_snapshot_processed = False
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
if rc == 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)
|
||||
# Attempt to request the retained snapshot message
|
||||
client.publish(MQTT_SNAPSHOT_TOPIC, "", qos=1)
|
||||
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):
|
||||
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.")
|
||||
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"Payload length: {len(msg.payload)} bytes")
|
||||
print(f"Payload (first 100 bytes): {msg.payload[:100]}...")
|
||||
process_snapshot(msg.payload)
|
||||
initial_snapshot_processed = True
|
||||
print(f"File not found: {image_file}")
|
||||
|
||||
def process_snapshot(payload):
|
||||
if not payload:
|
||||
print("Empty payload received, skipping processing.")
|
||||
return
|
||||
# Set up MQTT client for normal operation
|
||||
client = DaygloDetectorMQTTClient("dayglo_detector")
|
||||
client.set_auth_credentials(MQTT_USERNAME, MQTT_PASSWORD)
|
||||
|
||||
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
|
||||
await client.connect(MQTT_BROKER, MQTT_PORT, keepalive=60)
|
||||
await asyncio.get_event_loop().create_future()
|
||||
|
||||
# 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...")
|
||||
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)
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 10 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 246 KiB |
@@ -66,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'
|
||||
|
||||
Reference in New Issue
Block a user