From 818312148dec1f525f1bf29fd2dcd38e183bd2b6 Mon Sep 17 00:00:00 2001 From: torchedesc <112899395+torchedesc@users.noreply.github.com> Date: Sat, 2 Mar 2024 10:44:53 -0800 Subject: [PATCH] required files --- docker/compose.yml | 26 +++++ docker/config.yml | 233 ++++++++++++++++++++++++++++++++++++++++ nixos/configuration.nix | 115 ++++++++++++++++++++ nixos/coral.nix | 11 ++ nixos/gasket.nix | 35 ++++++ nixos/libedgetpu.nix | 66 ++++++++++++ 6 files changed, 486 insertions(+) create mode 100644 docker/compose.yml create mode 100644 docker/config.yml create mode 100644 nixos/configuration.nix create mode 100644 nixos/coral.nix create mode 100644 nixos/gasket.nix create mode 100644 nixos/libedgetpu.nix diff --git a/docker/compose.yml b/docker/compose.yml new file mode 100644 index 0000000..2e6fe5c --- /dev/null +++ b/docker/compose.yml @@ -0,0 +1,26 @@ +version: "3.9" +services: + frigate: #https://docs.frigate.video/frigate/installation + container_name: frigate + privileged: true # this may not be necessary for all setups + restart: always + image: ghcr.io/blakeblackshear/frigate:stable + shm_size: "128mb" # update for your cameras see installation link above + devices: + - /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 # for intel hwaccel, needs to be updated for your hardware + volumes: + - /etc/localtime:/etc/localtime:ro + - /home/USER/.docker/nvr/config.yml:/config/config.yml + - /home/USER/.docker/nvr:/media/frigate + - /home/USER/.docker/nvr:/db + - type: tmpfs # Optional: 1GB of memory, reduces SSD/SD Card wear + target: /tmp/cache + tmpfs: + size: 1000000000 + ports: + - "5000:5000" + - "8554:8554" # RTSP feeds + - "8555:8555/tcp" # WebRTC over tcp + - "8555:8555/udp" # WebRTC over udp + diff --git a/docker/config.yml b/docker/config.yml new file mode 100644 index 0000000..bba3d8b --- /dev/null +++ b/docker/config.yml @@ -0,0 +1,233 @@ + # https://docs.frigate.video/configuration/reference + # This config uses a PCIe based TPU, records a clip for a person and takes a snapshot for cats and dogs. +mqtt: + enabled: False +database: + path: /db/frigate.db +ffmpeg: + hwaccel_args: preset-vaapi #enables intel HW acceleration + + # Using the Google Coral TPUs. Tested the Mini PCIe Accelerator and the M.2 Accelerator A+E key + # https://coral.ai/products/pcie-accelerator https://coral.ai/products/m2-accelerator-ae +detectors: + coral: + type: edgetpu + device: pci:0 + +detect: + enabled: True + width: 1280 + height: 720 + fps: 5 + + +objects: + # Optional: list of objects to track from labelmap.txt (default: shown below) + track: + - person + - dog + - cat + # Optional: mask to prevent all object types from being detected in certain areas (default: no mask) + # Checks based on the bottom center of the bounding box of the object. + # NOTE: This mask is COMBINED with the object type specific mask below + mask: 0,0,1000,0,1000,200,0,200 + # Optional: filters to reduce false positives for specific object types + filters: + person: + # Optional: minimum width*height of the bounding box for the detected object (default: 0) + min_area: 5000 + # Optional: maximum width*height of the bounding box for the detected object (default: 24000000) + max_area: 100000 + # Optional: minimum width/height of the bounding box for the detected object (default: 0) + min_ratio: 0.5 + # Optional: maximum width/height of the bounding box for the detected object (default: 24000000) + max_ratio: 2.0 + # Optional: minimum score for the object to initiate tracking (default: shown below) + min_score: 0.5 + # Optional: minimum decimal percentage for tracked object's computed score to be considered a true positive (default: shown below) + threshold: 0.7 + # Optional: mask to prevent this object type from being detected in certain areas (default: no mask) + # Checks based on the bottom center of the bounding box of the object + mask: 0,0,1000,0,1000,200,0,200 + +# Optional: Motion configuration +# NOTE: Can be overridden at the camera level +motion: + # Optional: The threshold passed to cv2.threshold to determine if a pixel is different enough to be counted as motion. (default: shown below) + # Increasing this value will make motion detection less sensitive and decreasing it will make motion detection more sensitive. + # The value should be between 1 and 255. + threshold: 25 + # Optional: Minimum size in pixels in the resized motion image that counts as motion (default: 30) + # Increasing this value will prevent smaller areas of motion from being detected. Decreasing will + # make motion detection more sensitive to smaller moving objects. + # As a rule of thumb: + # - 15 - high sensitivity + # - 30 - medium sensitivity + # - 50 - low sensitivity + contour_area: 30 + # Optional: Alpha value passed to cv2.accumulateWeighted when averaging the motion delta across multiple frames (default: shown below) + # Higher values mean the current frame impacts the delta a lot, and a single raindrop may register as motion. + # Too low and a fast moving person wont be detected as motion. + delta_alpha: 0.2 + # Optional: Alpha value passed to cv2.accumulateWeighted when averaging frames to determine the background (default: shown below) + # Higher values mean the current frame impacts the average a lot, and a new object will be averaged into the background faster. + # Low values will cause things like moving shadows to be detected as motion for longer. + # https://www.geeksforgeeks.org/background-subtraction-in-an-image-using-concept-of-running-average/ + frame_alpha: 0.2 + # Optional: Height of the resized motion frame (default: 50) + # This operates as an efficient blur alternative. Higher values will result in more granular motion detection at the expense + # of higher CPU usage. Lower values result in less CPU, but small changes may not register as motion. + frame_height: 50 + # Optional: motion mask + # NOTE: see docs for more detailed info on creating masks + mask: 0,900,1080,900,1080,1920,0,1920 + # Optional: improve contrast (default: shown below) + # Enables dynamic contrast improvement. This should help improve night detections at the cost of making motion detection more sensitive + # for daytime. + improve_contrast: False + # Optional: Delay when updating camera motion through MQTT from ON -> OFF (default: shown below). + mqtt_off_delay: 30 + +# Optional: Record configuration +# NOTE: Can be overridden at the camera level +record: + # Optional: Enable recording (default: shown below) + # WARNING: If recording is disabled in the config, turning it on via + # the UI or MQTT later will have no effect. + enabled: True + # Optional: Number of minutes to wait between cleanup runs (default: shown below) + # This can be used to reduce the frequency of deleting recording segments from disk if you want to minimize i/o + expire_interval: 60 + # Optional: Retention settings for recording + retain: + # Optional: Number of days to retain recordings regardless of events (default: shown below) + # NOTE: This should be set to 0 and retention should be defined in events section below + # if you only want to retain recordings of events. + days: 0 + # Optional: Mode for retention. Available options are: all, motion, and active_objects + # all - save all recording segments regardless of activity + # motion - save all recordings segments with any detected motion + # active_objects - save all recording segments with active/moving objects + # NOTE: this mode only applies when the days setting above is greater than 0 + mode: motion + # Optional: Event recording settings + events: + # Optional: Number of seconds before the event to include (default: shown below) + pre_capture: 5 + # Optional: Number of seconds after the event to include (default: shown below) + post_capture: 5 + # Optional: Objects to save recordings for. (default: all tracked objects) + objects: + - person + # Optional: Restrict recordings to objects that entered any of the listed zones (default: no required zones) + required_zones: [] + # Optional: Retention settings for recordings of events + retain: + # Required: Default retention days (default: shown below) + default: 10 + # Optional: Mode for retention. (default: shown below) + # all - save all recording segments for events regardless of activity + # motion - save all recordings segments for events with any detected motion + # active_objects - save all recording segments for event with active/moving objects + # + # NOTE: If the retain mode for the camera is more restrictive than the mode configured + # here, the segments will already be gone by the time this mode is applied. + # For example, if the camera retain mode is "motion", the segments without motion are + # never stored, so setting the mode to "all" here won't bring them back. + mode: motion + # Optional: Per object retention days + objects: + person: 30 + +# Optional: Configuration for the jpg snapshots written to the clips directory for each event +# NOTE: Can be overridden at the camera level +snapshots: + # Optional: Enable writing jpg snapshot to /media/frigate/clips (default: shown below) + enabled: True + # Optional: save a clean PNG copy of the snapshot image (default: shown below) + retain: + # Required: Default retention days (default: shown below) + default: 30 + # Optional: Per object retention days + objects: + person: 30 + + ## Cameras setup. + ## Replace USER with your Cameras login user and CAMERA_PASSWORDS with the password for each login. + ## Check IPs in the URLs. + ## This is how my Riolink RLC-410-5MP need to be configured to have both streams available and accessible in Home Assistant +go2rtc: + streams: + username: USER + password: CAMERA_PASSWORDS + frontdoor: + - rtsp://USER:CAMERA_PASSWORDS@192.168.1.X1:554/h264Preview_01_main + frontdoor_sub: + - rtsp://USER:CAMERA_PASSWORDS@192.168.1.X1:554/h264Preview_01_sub + driveway: + - rtsp://USER:CAMERA_PASSWORDS@192.168.1.X2:554/h264Preview_01_main + driveway_sub: + - rtsp://USER:CAMERA_PASSWORDS@192.168.1.X2:554/h264Preview_01_sub + westgate: + - rtsp://USER:CAMERA_PASSWORDS@192.168.1.X3:554/h264Preview_01_main + westgate_sub: + - rtsp://USER:CAMERA_PASSWORDS@192.168.1.X3:554/h264Preview_01_sub + eastgate: + - rtsp://USER:CAMERA_PASSWORDS@192.168.1.X4:554/h264Preview_01_main + eastgate_sub: + - rtsp://USER:CAMERA_PASSWORDS@192.168.1.X4:554/h264Preview_01_sub + +cameras: + frontdoor: + ffmpeg: + inputs: + - path: rtsp://127.0.0.1:8554/frontdoor + roles: + - record + - path: rtsp://127.0.0.1:8554/frontdoor_sub + roles: + - detect + + driveway: + ffmpeg: + inputs: + - path: rtsp://127.0.0.1:8554/driveway + roles: + - record + - path: rtsp://127.0.0.1:8554/driveway_sub + roles: + - detect + + westgate: + ffmpeg: + inputs: + - path: rtsp://127.0.0.1:8554/westgate + roles: + - record + - path: rtsp://127.0.0.1:8554/westgate_sub + roles: + - detect + + eastgate: + ffmpeg: + inputs: + - path: rtsp://127.0.0.1:8554/eastgate + roles: + - record + - path: rtsp://127.0.0.1:8554/eastgate_sub + roles: + - detect + + # Birdseye creates a grid of the available cameras to see all at once. +birdseye: + enabled: True + mode: continuous + restream: False + width: 1920 + height: 1080 + quality: 4 + +rtmp: + enabled: false + + diff --git a/nixos/configuration.nix b/nixos/configuration.nix new file mode 100644 index 0000000..a5d40b7 --- /dev/null +++ b/nixos/configuration.nix @@ -0,0 +1,115 @@ +# Edit this configuration file to define what should be installed on +# your system. Help is available in the configuration.nix(5) man page +# and in the NixOS manual (accessible by running ‘nixos-help’). + +{ config, lib, pkgs, ... }: + +{ + # To get the Coral stuff to build get: + # https://github.com/colino17/nixdots/blob/6291ecdccc3670c8e823b8e5f1fd5c0bb1a2993c/services/coral.nix + # https://github.com/colino17/nixdots/blob/main/packages/gasket.nix + # https://github.com/colino17/nixdots/blob/main/packages/libedgetpu.nix + + # Put coral.nix in /etc/nixos/ put gasket.nix and libedgetpu.nix in /etc/nixos/packages + # Import ./coral.nix + + imports = + [ # Include the results of the hardware scan. + ./hardware-configuration.nix + ./coral.nix + ]; + + # Bootloader. + boot.loader.systemd-boot.enable = true; + boot.kernelParams = ["intel_iommu=on"]; + boot.loader.efi.canTouchEfiVariables = true; + + # HostName + networking.hostName = "frigate"; # Define your hostname. + # networking.wireless.enable = true; # Enables wireless support via wpa_supplicant. + + # Enable networking + networking.networkmanager.enable = true; + + # Set your time zone. + time.timeZone = "America/Los_Angeles"; + + # Select internationalisation properties. + i18n.defaultLocale = "en_US.UTF-8"; + + i18n.extraLocaleSettings = { + LC_ADDRESS = "en_US.UTF-8"; + LC_IDENTIFICATION = "en_US.UTF-8"; + LC_MEASUREMENT = "en_US.UTF-8"; + LC_MONETARY = "en_US.UTF-8"; + LC_NAME = "en_US.UTF-8"; + LC_NUMERIC = "en_US.UTF-8"; + LC_PAPER = "en_US.UTF-8"; + LC_TELEPHONE = "en_US.UTF-8"; + LC_TIME = "en_US.UTF-8"; + }; + + # Define a user account. Don't forget to set a password with ‘passwd’. + users.users.USER = { + isNormalUser = true; + description = "USER"; + extraGroups = [ "networkmanager" "wheel" "docker" "apex" ]; # apex is needed for the TPU. + packages = with pkgs; [ + tree + nano + htop + tailscale + zfs + btop + pciutils + usbutils + tmux + ncdu + ]; + }; + + # Enable Docker + virtualisation.docker = { + enable = true; + }; + + # Fish Shell + programs.fish.enable = true; + + # Enables the tailscale service + services.tailscale.enable = true; + + # Allow unfree packages + nixpkgs.config.allowUnfree = true; + + # List packages installed in system profile. To search, run: + # $ nix search wget + environment.systemPackages = with pkgs; [ + # vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default. + curl + nano + git + wget + python3 + ]; + + # List services that you want to enable: + + # Enable the OpenSSH daemon. + services.openssh.enable = true; + + # Open ports in the firewall. + # networking.firewall.allowedTCPPorts = [ ... ]; + # networking.firewall.allowedUDPPorts = [ ... ]; + # Or disable the firewall altogether. + networking.firewall.enable = false; + + # This value determines the NixOS release from which the default + # settings for stateful data, like file locations and database versions + # on your system were taken. It‘s perfectly fine and recommended to leave + # this value at the release version of the first install of this system. + # Before changing this value read the documentation for this option + # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html). + system.stateVersion = "23.11"; # Did you read the comment? + +} diff --git a/nixos/coral.nix b/nixos/coral.nix new file mode 100644 index 0000000..58699b0 --- /dev/null +++ b/nixos/coral.nix @@ -0,0 +1,11 @@ +{ config, pkgs, ... }: + +let + libedgetpu = config.boot.kernelPackages.callPackage /etc/nixos/packages/libedgetpu.nix {}; + gasket = config.boot.kernelPackages.callPackage /etc/nixos/packages/gasket.nix {}; +in +{ + services.udev.packages = [ libedgetpu ]; + users.groups.plugdev = {}; + boot.extraModulePackages = [ gasket ]; +} diff --git a/nixos/gasket.nix b/nixos/gasket.nix new file mode 100644 index 0000000..1f9d60a --- /dev/null +++ b/nixos/gasket.nix @@ -0,0 +1,35 @@ +{ stdenv, lib, fetchFromGitHub, kernel }: + +stdenv.mkDerivation rec { + pname = "gasket"; + version = "1.0-18"; + + src = fetchFromGitHub { + owner = "google"; + repo = "gasket-driver"; + rev = "97aeba584efd18983850c36dcf7384b0185284b3"; + sha256 = "pJwrrI7jVKFts4+bl2xmPIAD01VKFta2SRuElerQnTo="; + }; + + makeFlags = [ + "-C" + "${kernel.dev}/lib/modules/${kernel.modDirVersion}/build" + "M=$(PWD)" + ]; + buildFlags = [ "modules" ]; + + installFlags = [ "INSTALL_MOD_PATH=${placeholder "out"}" ]; + installTargets = [ "modules_install" ]; + + sourceRoot = "source/src"; + hardeningDisable = [ "pic" "format" ]; + nativeBuildInputs = kernel.moduleBuildDependencies; + + meta = with lib; { + description = "The Coral Gasket Driver allows usage of the Coral EdgeTPU on Linux systems."; + homepage = "https://github.com/google/gasket-driver"; + license = licenses.gpl2; + maintainers = [ lib.maintainers.kylehendricks ]; + platforms = platforms.linux; + }; +} diff --git a/nixos/libedgetpu.nix b/nixos/libedgetpu.nix new file mode 100644 index 0000000..753de4e --- /dev/null +++ b/nixos/libedgetpu.nix @@ -0,0 +1,66 @@ +{ stdenv +, lib +, fetchFromGitHub +, libusb1 +, abseil-cpp +, flatbuffers +, xxd +}: + +let + flatbuffers_1_12 = flatbuffers.overrideAttrs (oldAttrs: rec { + version = "1.12.0"; + NIX_CFLAGS_COMPILE = "-Wno-error=class-memaccess -Wno-error=maybe-uninitialized"; + cmakeFlags = (oldAttrs.cmakeFlags or []) ++ ["-DFLATBUFFERS_BUILD_SHAREDLIB=ON"]; + NIX_CXXSTDLIB_COMPILE = "-std=c++17"; + configureFlags = (oldAttrs.configureFlags or []) ++ ["--enable-shared"]; + src = fetchFromGitHub { + owner = "google"; + repo = "flatbuffers"; + rev = "v${version}"; + sha256 = "sha256-L1B5Y/c897Jg9fGwT2J3+vaXsZ+lfXnskp8Gto1p/Tg="; + }; + }); + +in stdenv.mkDerivation rec { + pname = "libedgetpu"; + version = "grouper"; + + src = fetchFromGitHub { + owner = "google-coral"; + repo = pname; + rev = "release-${version}"; + sha256 = "sha256-73hwItimf88Iqnb40lk4ul/PzmCNIfdt6Afi+xjNiBE="; + }; + + makeFlags = ["-f" "makefile_build/Makefile" "libedgetpu" ]; + + buildInputs = [ + libusb1 + abseil-cpp + flatbuffers_1_12 + ]; + + nativeBuildInputs = [ + xxd + ]; + + NIX_CXXSTDLIB_COMPILE = "-std=c++17"; + + TFROOT = "${fetchFromGitHub { + owner = "tensorflow"; + repo = "tensorflow"; + rev = "v2.7.4"; + sha256 = "sha256-liDbUAdaVllB0b74aBeqNxkYNu/zPy7k3CevzRF5dk0="; + }}"; + + enableParallelBuilding = false; + + installPhase = '' + mkdir -p $out/lib + cp out/direct/k8/libedgetpu.so.1.0 $out/lib + ln -s $out/lib/libedgetpu.so.1.0 $out/lib/libedgetpu.so.1 + mkdir -p $out/lib/udev/rules.d + cp debian/edgetpu-accelerator.rules $out/lib/udev/rules.d/99-edgetpu-accelerator.rules + ''; +}