diff --git a/.env b/.env new file mode 100644 index 0000000..ed05a06 --- /dev/null +++ b/.env @@ -0,0 +1,12 @@ +BASEDIR=data +STORAGEDIR=data/frigate/storage +YOLO_MODELS="yolov7-320" + +FRIGATE_MQTT_USER="frigate" +FRIGATE_MQTT_PASSWORD="yoQuegBedIrgOamsalrekFogrocThy" + +FRIGATE_RTSP_PASSWORD='QSNPaStxkfi8a2qe' + +TZ='Australia/Hobart' + + diff --git a/Dockerfile.frigate b/Dockerfile.frigate new file mode 100644 index 0000000..320f80d --- /dev/null +++ b/Dockerfile.frigate @@ -0,0 +1,16 @@ +FROM ghcr.io/blakeblackshear/frigate:stable + +# Install rsync +RUN apt-get update && apt-get install -y rsync cron + +# Add your sync script +COPY sync_recordings.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/sync_recordings.sh + +# Add crontab file +COPY crontab /etc/cron.d/sync-cron +RUN chmod 0644 /etc/cron.d/sync-cron +RUN crontab /etc/cron.d/sync-cron + +# Run cron in the foreground +CMD ["cron", "-f"] diff --git a/crontab b/crontab new file mode 100644 index 0000000..0b98931 --- /dev/null +++ b/crontab @@ -0,0 +1,2 @@ +0 * * * * /usr/local/bin/sync_recordings.sh + diff --git a/custom-start.sh b/custom-start.sh new file mode 100755 index 0000000..83b73ab --- /dev/null +++ b/custom-start.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +sed -i 's/access_log \/dev\/stdout main;/access_log \/dev\/null main;/g' /usr/local/nginx/conf/nginx.conf + +# Delegate to original entrypoint +exec /init "$@" diff --git a/dayglo_detector/Dockerfile b/dayglo_detector/Dockerfile new file mode 100644 index 0000000..1f3e2db --- /dev/null +++ b/dayglo_detector/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.9-slim + +# Install dependencies +RUN apt-get update && apt-get install -y \ + libopencv-dev \ + python3-opencv \ + && rm -rf /var/lib/apt/lists/* + +# Install Python packages +RUN pip install --no-cache-dir \ + paho-mqtt \ + requests \ + numpy + +# Copy the dayglo detector script +COPY dayglo_detector.py /app/dayglo_detector.py + +WORKDIR /app + +CMD ["python", "dayglo_detector.py"] + diff --git a/dayglo_detector/dayglo_detector.py b/dayglo_detector/dayglo_detector.py new file mode 100644 index 0000000..d53d0ec --- /dev/null +++ b/dayglo_detector/dayglo_detector.py @@ -0,0 +1,75 @@ +import os +import paho.mqtt.client as mqtt +import json +import requests +import cv2 +import numpy as np +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'] + +def calculate_dayglo_rating(image): + 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 + return dayglo_rating + +def on_connect(client, userdata, flags, rc): + print("Connected to MQTT broker") + client.subscribe(MQTT_TOPIC_SUBSCRIBE) + +def on_message(client, userdata, msg): + 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): + threading.Thread(target=process_event, args=(after,)).start() + +def process_event(event_data): + event_id = event_data.get('id') + camera = event_data.get('camera') + # Get the snapshot URL for the event + snapshot_url = f"{FRIGATE_URL}/api/events/{event_id}/snapshot.jpg" + # Retrieve the snapshot + response = requests.get(snapshot_url) + if response.status_code == 200: + 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) + print(f"Dayglo Rating: {dayglo_rating:.2f}%") + # Publish the rating to Home Assistant + client.publish(MQTT_TOPIC_PUBLISH, f"{dayglo_rating:.2f}") + else: + print("Failed to decode image") + else: + print(f"Failed to retrieve snapshot: {response.status_code}") + +client = mqtt.Client() +if MQTT_USERNAME and MQTT_PASSWORD: + 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) +client.loop_forever() + diff --git a/dayglo_detector/docker_compose.yaml b/dayglo_detector/docker_compose.yaml new file mode 100644 index 0000000..ddbc5af --- /dev/null +++ b/dayglo_detector/docker_compose.yaml @@ -0,0 +1,17 @@ + dayglo_detector: + container_name: dayglo_detector + build: + context: ./dayglo_detector + restart: always + volumes: + - /etc/localtime:/etc/localtime:ro + environment: + MQTT_BROKER: 'frigate' + MQTT_PORT: '1883' + MQTT_USERNAME: 'your_mqtt_username' # Optional + MQTT_PASSWORD: 'your_mqtt_password' # Optional + FRIGATE_URL: 'http://frigate:5000' + INTERESTED_ZONES: 'Door_Front' + depends_on: + - frigate + diff --git a/docker-compose.yml b/docker-compose.yml index 0a8c382..b1ff714 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,19 +1,23 @@ -version: "3" services: frigate: image: ghcr.io/blakeblackshear/frigate:stable #image: ghcr.io/blakeblackshear/frigate:stable-tensorrt + #build: + # context: . + # dockerfile: Dockerfile.frigate container_name: frigate # restart: unless-stopped restart: always # user: vscode privileged: true - shm_size: "256mb" + #shm_size: "256mb" + shm_size: "512mb" volumes: - /etc/localtime:/etc/localtime:ro - frigate-config:/config/ - frigate-trt-models:/trt-models/ - frigate-storage:/media/frigate/ + #- frigate-nfs:/media/frigate-nfs/ - type: tmpfs # Optional: 1GB of memory, reduces SSD/SD Card wear target: /tmp/cache tmpfs: @@ -21,15 +25,50 @@ services: - /dev/bus/usb:/dev/bus/usb - /dev/dri:/dev/dri # for intel hwaccel, needs to be updated for your hardware - /dev/apex_0:/dev/apex_0 # Passes a PCIe Coral, follow driver instructions here https://coral.ai/docs/m2/get-started/#2a-on-linux - + - /dev/dri/renderD128:/dev/dri/renderD128 + - ./custom-start.sh:/custom-start.sh + entrypoint: ["/bin/bash", "/custom-start.sh"] + #network: host ports: + - "1883:1883" - "1935:1935" + - "1984:1984" - "5000:5000" - "5001:5001" - - "8080:8080" + - "8000:8000" + - "8088:8080" + - "8554:8554" + - "8555:8555/tcp" + - "8555:8555/udp" # network_mode: host + env_file: + - .env environment: - YOLO_MODELS=${YOLO_MODELS} + - LIBVA_DRIVER_NAME=radeonsi + - TZ=${TZ-UTC} + deploy: + resources: + limits: + cpus: '0.9' + memory: 5000M + + dayglo_detector: + container_name: dayglo_detector + build: + context: ./dayglo_detector + restart: always + volumes: + - /etc/localtime:/etc/localtime:ro + environment: + MQTT_BROKER: 'frigate' + MQTT_PORT: '1883' + MQTT_USERNAME: 'your_mqtt_username' # Optional + MQTT_PASSWORD: 'your_mqtt_password' # Optional + FRIGATE_URL: 'http://frigate:5000' + INTERESTED_ZONES: 'Door_Front' + depends_on: + - frigate volumes: frigate-config: @@ -44,14 +83,14 @@ volumes: type: 'none' o: 'bind' device: '/${BASEDIR}/frigate/trt-models/' - # frigate-storage: - # driver: local - # driver_opts: - # type: 'none' - # o: 'bind' - # device: '/${STORAGEDIR}' frigate-storage: + driver: local driver_opts: - type: 'nfs' - o: 'addr=nas,nfsvers=4,nolock,soft,rw' - device: ':/Frigate' + type: 'none' + o: 'bind,size=50G' + device: '/${STORAGEDIR}' +# frigate-nfs: +# driver_opts: +# type: 'nfs' +# o: 'addr=omv,nfsvers=4,nolock,soft,rw,timeo=600,nofail' +# device: ':/Frigate' diff --git a/docker-compose.yml.20241020 b/docker-compose.yml.20241020 new file mode 100644 index 0000000..a1677f7 --- /dev/null +++ b/docker-compose.yml.20241020 @@ -0,0 +1,79 @@ +services: + frigate: + image: ghcr.io/blakeblackshear/frigate:stable + #image: ghcr.io/blakeblackshear/frigate:stable-tensorrt + #build: + # context: . + # dockerfile: Dockerfile.frigate + container_name: frigate + # restart: unless-stopped + restart: always + # user: vscode + privileged: true + #shm_size: "256mb" + shm_size: "512mb" + volumes: + - /etc/localtime:/etc/localtime:ro + - frigate-config:/config/ + - frigate-trt-models:/trt-models/ + - frigate-storage:/media/frigate/ + #- frigate-nfs:/media/frigate-nfs/ + - type: tmpfs # Optional: 1GB of memory, reduces SSD/SD Card wear + target: /tmp/cache + tmpfs: + size: 1000000000 + - /dev/bus/usb:/dev/bus/usb + - /dev/dri:/dev/dri # for intel hwaccel, needs to be updated for your hardware + - /dev/apex_0:/dev/apex_0 # Passes a PCIe Coral, follow driver instructions here https://coral.ai/docs/m2/get-started/#2a-on-linux + - /dev/dri/renderD128:/dev/dri/renderD128 + - ./custom-start.sh:/custom-start.sh + entrypoint: ["/bin/bash", "/custom-start.sh"] + #network: host + ports: + - "1883:1883" + - "1935:1935" + - "1984:1984" + - "5000:5000" + - "5001:5001" + - "8000:8000" + - "8088:8080" + - "8554:8554" + - "8555:8555/tcp" + - "8555:8555/udp" + # network_mode: host + env_file: + - .env + environment: + - YOLO_MODELS=${YOLO_MODELS} + - LIBVA_DRIVER_NAME=radeonsi + - TZ=${TZ-UTC} + deploy: + resources: + limits: + cpus: '0.9' + memory: 5000M +%%%%% +volumes: + frigate-config: + driver: local + driver_opts: + type: 'none' + o: 'bind' + device: '/${BASEDIR}/frigate/config/' + frigate-trt-models: + driver: local + driver_opts: + type: 'none' + o: 'bind' + device: '/${BASEDIR}/frigate/trt-models/' + frigate-storage: + driver: local + driver_opts: + type: 'none' + o: 'bind,size=50G' + device: '/${STORAGEDIR}' +# frigate-nfs: +# driver_opts: +# type: 'nfs' +# o: 'addr=omv,nfsvers=4,nolock,soft,rw,timeo=600,nofail' +# device: ':/Frigate' diff --git a/docker-compose.yml.ORG b/docker-compose.yml.ORG new file mode 100644 index 0000000..181af9e --- /dev/null +++ b/docker-compose.yml.ORG @@ -0,0 +1,73 @@ +services: + frigate: + image: ghcr.io/blakeblackshear/frigate:stable + #image: ghcr.io/blakeblackshear/frigate:stable-tensorrt + container_name: frigate + # restart: unless-stopped + restart: always + # user: vscode + privileged: true + #shm_size: "256mb" + shm_size: "512mb" + volumes: + - /etc/localtime:/etc/localtime:ro + - frigate-config:/config/ + - frigate-trt-models:/trt-models/ + - frigate-storage:/media/frigate/ + - type: tmpfs # Optional: 1GB of memory, reduces SSD/SD Card wear + target: /tmp/cache + tmpfs: + size: 1000000000 + - /dev/bus/usb:/dev/bus/usb + - /dev/dri:/dev/dri # for intel hwaccel, needs to be updated for your hardware + - /dev/apex_0:/dev/apex_0 # Passes a PCIe Coral, follow driver instructions here https://coral.ai/docs/m2/get-started/#2a-on-linux + - /dev/dri/renderD128:/dev/dri/renderD128 + - ./custom-start.sh:/custom-start.sh + entrypoint: ["/bin/bash", "/custom-start.sh"] + #network: host + ports: + - "1883:1883" + - "1935:1935" + - "1984:1984" + - "5000:5000" + - "5001:5001" + - "8000:8000" + - "8088:8080" + - "8554:8554" + - "8555:8555/tcp" + - "8555:8555/udp" + # network_mode: host + env_file: + - .env + environment: + - YOLO_MODELS=${YOLO_MODELS} + - LIBVA_DRIVER_NAME=radeonsi + deploy: + resources: + limits: + cpus: '0.9' + memory: 5000M +volumes: + frigate-config: + driver: local + driver_opts: + type: 'none' + o: 'bind' + device: '/${BASEDIR}/frigate/config/' + frigate-trt-models: + driver: local + driver_opts: + type: 'none' + o: 'bind' + device: '/${BASEDIR}/frigate/trt-models/' + frigate-storage: + driver: local + driver_opts: + type: 'none' + o: 'bind' + device: '/${STORAGEDIR}' + #frigate-storage: + # driver_opts: + # type: 'nfs' + # o: 'addr=omv,nfsvers=4,nolock,soft,rw,timeo=600' + # device: ':/Frigate' diff --git a/move_to_nfs.sh b/move_to_nfs.sh new file mode 100644 index 0000000..af8463f --- /dev/null +++ b/move_to_nfs.sh @@ -0,0 +1,9 @@ +#!/bin/bash +LOCAL_DIR="/media/frigate" +NFS_DIR="/mnt/nfs_frigate" + +# Check if NFS is mounted +if mountpoint -q "$NFS_DIR"; then + # Move files older than 1 day + find "$LOCAL_DIR" -type f -mtime +1 -exec mv {} "$NFS_DIR" \; +fi diff --git a/sync_recordings.sh b/sync_recordings.sh new file mode 100644 index 0000000..5097ff2 --- /dev/null +++ b/sync_recordings.sh @@ -0,0 +1,2 @@ +#!/bin/bash +rsync -av /media/frigate/ /media/frigate-nfs/