Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / applications / video / frigate / default.nix
blob5414193640f0e7779774ad5a299724f551f6d88f
1 { lib
2 , callPackage
3 , python3
4 , fetchFromGitHub
5 , fetchurl
6 , fetchpatch
7 , frigate
8 , nixosTests
9 }:
11 let
12   version = "0.12.1";
14   src = fetchFromGitHub {
15     #name = "frigate-${version}-source";
16     owner = "blakeblackshear";
17     repo = "frigate";
18     rev = "refs/tags/v${version}";
19     hash = "sha256-kNvYsHoObi6b9KT/LYhTGK4uJ/uAHnYhyoQkiXIA/s8=";
20   };
22   frigate-web = callPackage ./web.nix {
23     inherit version src;
24   };
26   python = python3.override {
27     packageOverrides = self: super: {
28     };
29   };
31   # Tensorflow Lite models
32   # https://github.com/blakeblackshear/frigate/blob/v0.12.0/Dockerfile#L88-L91
33   tflite_cpu_model = fetchurl {
34     url = "https://github.com/google-coral/test_data/raw/release-frogfish/ssdlite_mobiledet_coco_qat_postprocess.tflite";
35     hash = "sha256-kLszpjTgQZFMwYGapd+ZgY5sOWxNLblSwP16nP/Eck8=";
36   };
37   tflite_edgetpu_model = fetchurl {
38     url = "https://github.com/google-coral/test_data/raw/release-frogfish/ssdlite_mobiledet_coco_qat_postprocess_edgetpu.tflite";
39     hash = "sha256-Siviu7YU5XbVbcuRT6UnUr8PE0EVEnENNV2X+qGzVkE=";
40   };
42   # OpenVino models
43   # https://github.com/blakeblackshear/frigate/blob/v0.12.0/Dockerfile#L92-L95
44   openvino_model = fetchurl {
45     url = "https://github.com/openvinotoolkit/open_model_zoo/raw/master/data/dataset_classes/coco_91cl_bkgr.txt";
46     hash = "sha256-5Cj2vEiWR8Z9d2xBmVoLZuNRv4UOuxHSGZQWTJorXUQ=";
47   };
49 python.pkgs.buildPythonApplication rec {
50   pname = "frigate";
51   inherit version;
52   format = "other";
54   inherit src;
56   patches = [
57     (fetchpatch {
58       # numpy 1.24 compat
59       url = "https://github.com/blakeblackshear/frigate/commit/cb73d0cd392990448811c7212bc5f09be411fc69.patch";
60       hash = "sha256-Spt7eRosmTN8zyJ2uVme5HPVy2TKgBtvbQ6tp6PaNac=";
61     })
62   ];
64   postPatch = ''
65     echo 'VERSION = "${version}"' > frigate/version.py
67     substituteInPlace frigate/app.py \
68       --replace "Router(migrate_db)" 'Router(migrate_db, "${placeholder "out"}/share/frigate/migrations")'
70     substituteInPlace frigate/const.py \
71       --replace "/media/frigate" "/var/lib/frigate" \
72       --replace "/tmp/cache" "/var/cache/frigate/"
74     substituteInPlace frigate/http.py \
75       --replace "/opt/frigate" "${placeholder "out"}/${python.sitePackages}" \
76       --replace "/tmp/cache/" "/var/cache/frigate/"
78     substituteInPlace frigate/output.py \
79       --replace "/opt/frigate" "${placeholder "out"}/${python.sitePackages}"
81     substituteInPlace frigate/record.py \
82       --replace "/tmp/cache" "/var/cache/frigate"
84     substituteInPlace frigate/detectors/detector_config.py \
85       --replace "/labelmap.txt" "${placeholder "out"}/share/frigate/labelmap.txt"
87     substituteInPlace frigate/detectors/plugins/edgetpu_tfl.py \
88       --replace "/edgetpu_model.tflite" "${tflite_edgetpu_model}"
90     substituteInPlace frigate/detectors/plugins/cpu_tfl.py \
91       --replace "/cpu_model.tflite" "${tflite_cpu_model}"
93     substituteInPlace frigate/ffmpeg_presets.py --replace \
94        '"-timeout" if os.path.exists(BTBN_PATH) else "-stimeout"' \
95        '"-timeout"'
96   '';
98   dontBuild = true;
100   propagatedBuildInputs = with python.pkgs; [
101     # requirements.txt
102     scikit-build
103     # requirements-wheel.txt
104     click
105     flask
106     imutils
107     matplotlib
108     numpy
109     opencv4
110     openvino
111     paho-mqtt
112     peewee
113     peewee-migrate
114     psutil
115     pydantic
116     pyyaml
117     requests
118     scipy
119     setproctitle
120     tensorflow
121     tzlocal
122     ws4py
123     zeroconf
124   ];
126   installPhase = ''
127     runHook preInstall
129     mkdir -p $out/${python.sitePackages}/frigate
130     cp -R frigate/* $out/${python.sitePackages}/frigate/
132     mkdir -p $out/share/frigate
133     cp -R {migrations,labelmap.txt} $out/share/frigate/
135     cp --no-preserve=mode ${openvino_model} $out/share/frigate/coco_91cl_bkgr.txt
136     sed -i 's/truck/car/g' $out/share/frigate/coco_91cl_bkgr.txt
138     runHook postInstall
139   '';
141   checkInputs = with python.pkgs; [
142     pytestCheckHook
143   ];
145   passthru = {
146     web = frigate-web;
147     inherit python;
148     pythonPath =(python.pkgs.makePythonPath propagatedBuildInputs) + ":${frigate}/${python.sitePackages}";
149     tests = {
150       inherit (nixosTests) frigate;
151     };
152   };
154   meta = with lib; {
155     changelog = "https://github.com/blakeblackshear/frigate/releases/tag/v${version}";
156     description = "NVR with realtime local object detection for IP cameras";
157     longDescription = ''
158       A complete and local NVR designed for Home Assistant with AI
159       object detection. Uses OpenCV and Tensorflow to perform realtime
160       object detection locally for IP cameras.
161     '';
162     homepage = "https://github.com/blakeblackshear/frigate";
163     license = licenses.mit;
164     maintainers = with maintainers; [ hexa ];
165   };