Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1200f7302b | |||
| 17be02c9f7 | |||
| e9e5ffb931 | |||
| 043e56e6f7 | |||
| 121673fde1 |
+143
-105
@@ -5,6 +5,7 @@ import numpy as np
|
|||||||
import cv2
|
import cv2
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
|
import tempfile
|
||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
MQTT_BROKER = os.environ.get('MQTT_BROKER', '10.59.221.172')
|
MQTT_BROKER = os.environ.get('MQTT_BROKER', '10.59.221.172')
|
||||||
@@ -22,111 +23,13 @@ last_rating = 0
|
|||||||
LOWER_COLOR = np.array([20, 100, 100])
|
LOWER_COLOR = np.array([20, 100, 100])
|
||||||
UPPER_COLOR = np.array([40, 255, 255])
|
UPPER_COLOR = np.array([40, 255, 255])
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
# Track if the initial snapshot has been processed
|
||||||
if rc == 0:
|
initial_snapshot_processed = False
|
||||||
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 = {
|
|
||||||
"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
|
|
||||||
|
|
||||||
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):
|
def on_connect(client, userdata, flags, rc):
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
print("Connected successfully to MQTT broker")
|
print("Connected successfully to MQTT broker")
|
||||||
client.subscribe(MQTT_SNAPSHOT_TOPIC)
|
client.subscribe(MQTT_SNAPSHOT_TOPIC, qos=1)
|
||||||
print(f"Subscribed to topic: {MQTT_SNAPSHOT_TOPIC}")
|
print(f"Subscribed to topic: {MQTT_SNAPSHOT_TOPIC}")
|
||||||
publish_discovery_configurations()
|
publish_discovery_configurations()
|
||||||
# Publish initial rating of 0
|
# Publish initial rating of 0
|
||||||
@@ -151,9 +54,16 @@ def publish_rating(rating):
|
|||||||
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
|
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
|
||||||
|
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
|
global initial_snapshot_processed
|
||||||
if msg.topic == MQTT_SNAPSHOT_TOPIC:
|
if msg.topic == MQTT_SNAPSHOT_TOPIC:
|
||||||
print("Snapshot received")
|
print("Snapshot received")
|
||||||
process_snapshot(msg.payload)
|
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):
|
def process_snapshot(payload):
|
||||||
if not payload:
|
if not payload:
|
||||||
@@ -163,15 +73,143 @@ def process_snapshot(payload):
|
|||||||
print("Processing snapshot...")
|
print("Processing snapshot...")
|
||||||
try:
|
try:
|
||||||
image_data = base64.b64decode(payload)
|
image_data = base64.b64decode(payload)
|
||||||
nparr = np.frombuffer(image_data, np.uint8)
|
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_image_file:
|
||||||
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
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:
|
if image is not None:
|
||||||
rating = calculate_dayglo_rating(image)
|
rating = calculate_dayglo_rating(image)
|
||||||
print("Dayglo Rating calculated:", rating)
|
print("Dayglo Rating calculated:", rating)
|
||||||
publish_rating(rating)
|
publish_rating(rating)
|
||||||
else:
|
else:
|
||||||
print("Invalid image received")
|
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)
|
||||||
|
|
||||||
|
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.")
|
||||||
|
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:
|
except Exception as e:
|
||||||
print(f"Error processing snapshot: {e}")
|
print(f"Error processing snapshot: {e}")
|
||||||
|
|
||||||
|
|||||||
+9
-9
@@ -52,12 +52,12 @@ services:
|
|||||||
limits:
|
limits:
|
||||||
cpus: '0.9'
|
cpus: '0.9'
|
||||||
memory: 5000M
|
memory: 5000M
|
||||||
healthcheck:
|
#healthcheck:
|
||||||
test: ["CMD", "curl", "-f", "http://localhost:5000/api/version"]
|
# test: ["CMD", "curl", "-f", "http://localhost:5000/api/version"]
|
||||||
interval: 30s
|
# interval: 30s
|
||||||
timeout: 10s
|
# timeout: 10s
|
||||||
retries: 5
|
# retries: 5
|
||||||
start_period: 30s
|
# start_period: 30s
|
||||||
|
|
||||||
dayglo_detector:
|
dayglo_detector:
|
||||||
container_name: dayglo_detector
|
container_name: dayglo_detector
|
||||||
@@ -73,9 +73,9 @@ services:
|
|||||||
MQTT_PASSWORD: 'RadGawlEikWothecOmtAmmoihumid8'
|
MQTT_PASSWORD: 'RadGawlEikWothecOmtAmmoihumid8'
|
||||||
FRIGATE_URL: 'http://frigate:5000'
|
FRIGATE_URL: 'http://frigate:5000'
|
||||||
INTERESTED_ZONES: 'Door_Front'
|
INTERESTED_ZONES: 'Door_Front'
|
||||||
depends_on:
|
#depends_on:
|
||||||
frigate:
|
# frigate:
|
||||||
condition: service_healthy
|
# condition: service_healthy
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
frigate-config:
|
frigate-config:
|
||||||
|
|||||||
Reference in New Issue
Block a user