forked from Github/frigate
Compare commits
151 Commits
v0.0.1
...
v0.5.0-rc3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7686c510b3 | ||
|
|
2f5e322d3c | ||
|
|
1cd4c12104 | ||
|
|
1a8b034685 | ||
|
|
da6dc03a57 | ||
|
|
7fa3b70d2d | ||
|
|
1fc5a2bfd4 | ||
|
|
7e84da7dad | ||
|
|
128be72e28 | ||
|
|
aaddedc95c | ||
|
|
ba919fb439 | ||
|
|
b1d563f3c4 | ||
|
|
204d8af5df | ||
|
|
b507a73d79 | ||
|
|
66eeb8b5cb | ||
|
|
efa67067c6 | ||
|
|
aeb036f1a4 | ||
|
|
74c528f9dc | ||
|
|
f2d54bec43 | ||
|
|
f07d57741e | ||
|
|
2c1ec19f98 | ||
|
|
6a9027c002 | ||
|
|
60c15e4419 | ||
|
|
03dbf600aa | ||
|
|
fbbb79b31b | ||
|
|
496c6bc6c4 | ||
|
|
869a81c944 | ||
|
|
5b1884cfb3 | ||
|
|
cd057370e1 | ||
|
|
6263912655 | ||
|
|
af247275cf | ||
|
|
1198c29dac | ||
|
|
169603d3ff | ||
|
|
dc7eecebc6 | ||
|
|
0dd4087d5d | ||
|
|
6ecf87fc60 | ||
|
|
ebcf1482f8 | ||
|
|
50bcf60893 | ||
|
|
38efbd63ea | ||
|
|
50bcad8b77 | ||
|
|
cfffb219ae | ||
|
|
382d7be50a | ||
|
|
f43dc36a37 | ||
|
|
38e7fa07d2 | ||
|
|
e261c20819 | ||
|
|
3a66e672d3 | ||
|
|
2aada930e3 | ||
|
|
d87f4407a0 | ||
|
|
be5a114f6a | ||
|
|
32b212c7b6 | ||
|
|
76c8e3a12f | ||
|
|
16f7a361c3 | ||
|
|
634b87307f | ||
|
|
1d4fbbdba3 | ||
|
|
65579e9cbf | ||
|
|
49dc029c43 | ||
|
|
08174d8db2 | ||
|
|
5199242a68 | ||
|
|
725dd3220c | ||
|
|
10dc56f6ea | ||
|
|
cc2abe93a6 | ||
|
|
0c6717090c | ||
|
|
f5a2252b29 | ||
|
|
02efb6f415 | ||
|
|
5b4c6e50bc | ||
|
|
9cc46a71cb | ||
|
|
be1673b00a | ||
|
|
b6130e77ff | ||
|
|
4180c710cd | ||
|
|
ab3e70b4db | ||
|
|
d90e408d50 | ||
|
|
6c87ce0879 | ||
|
|
b7b4e38f62 | ||
|
|
480175d70f | ||
|
|
bee99ca6ff | ||
|
|
5c01720567 | ||
|
|
262f45c8bc | ||
|
|
22bb17b2fd | ||
|
|
3a3afe14bf | ||
|
|
01f058a482 | ||
|
|
d899ef158e | ||
|
|
39d64f7ba7 | ||
|
|
f148eb5a7b | ||
|
|
297e2f1c0c | ||
|
|
e818744d81 | ||
|
|
ceedfae993 | ||
|
|
e13563770d | ||
|
|
a659019d1a | ||
|
|
ba71927d53 | ||
|
|
04fed31eac | ||
|
|
ebaa8fac01 | ||
|
|
2ec45cd1b6 | ||
|
|
700bd1e3ef | ||
|
|
c9e9f7a735 | ||
|
|
aea4dc8724 | ||
|
|
12d5007b90 | ||
|
|
8970e73f75 | ||
|
|
1ba006b24f | ||
|
|
4a58f16637 | ||
|
|
436b876b24 | ||
|
|
a770ab7f69 | ||
|
|
806acaf445 | ||
|
|
c653567cc1 | ||
|
|
8fee8f86a2 | ||
|
|
59a4b0e650 | ||
|
|
834a3df0bc | ||
|
|
c41b104997 | ||
|
|
7028b05856 | ||
|
|
2d22a04391 | ||
|
|
baa587028b | ||
|
|
2b51dc3e5b | ||
|
|
9f8278ea8f | ||
|
|
56b9c754f5 | ||
|
|
5c4f5ef3f0 | ||
|
|
8c924896c5 | ||
|
|
2c2f0044b9 | ||
|
|
874e9085a7 | ||
|
|
e791d6646b | ||
|
|
3019b0218c | ||
|
|
6900e140d5 | ||
|
|
911c1b2bfa | ||
|
|
f4587462cf | ||
|
|
cac1faa8ac | ||
|
|
9525bae5a3 | ||
|
|
dbcfd109f6 | ||
|
|
f95d8b6210 | ||
|
|
4dacf02ef9 | ||
|
|
3e803b6a03 | ||
|
|
7a7f507781 | ||
|
|
e0b9b616ce | ||
|
|
4476bd8a13 | ||
|
|
5aa3775c77 | ||
|
|
edf0cd36df | ||
|
|
0279121d77 | ||
|
|
8774e537dc | ||
|
|
0514eeac03 | ||
|
|
a074945394 | ||
|
|
a26d2217d4 | ||
|
|
200d769003 | ||
|
|
48aa245914 | ||
|
|
ada8ffccf9 | ||
|
|
bca4e78e9a | ||
|
|
7d3027e056 | ||
|
|
c406fda288 | ||
|
|
8ff9a982b6 | ||
|
|
f2c205be99 | ||
|
|
862aa2d3f0 | ||
|
|
8bae05cfe2 | ||
|
|
de9c3f4d74 | ||
|
|
c12e19349e | ||
|
|
afb70f11a8 |
@@ -1 +1,6 @@
|
|||||||
README.md
|
README.md
|
||||||
|
diagram.png
|
||||||
|
.gitignore
|
||||||
|
debug
|
||||||
|
config/
|
||||||
|
*.pyc
|
||||||
1
.github/FUNDING.yml
vendored
Normal file
1
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
github: blakeblackshear
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
|||||||
*.pyc
|
*.pyc
|
||||||
debug
|
debug
|
||||||
|
.vscode
|
||||||
|
config/config.yml
|
||||||
136
Dockerfile
Normal file → Executable file
136
Dockerfile
Normal file → Executable file
@@ -1,90 +1,60 @@
|
|||||||
FROM ubuntu:16.04
|
FROM ubuntu:18.04
|
||||||
|
LABEL maintainer "blakeb@blakeshome.com"
|
||||||
|
|
||||||
# Install system packages
|
ENV DEBIAN_FRONTEND=noninteractive
|
||||||
RUN apt-get -qq update && apt-get -qq install --no-install-recommends -y python3 \
|
# Install packages for apt repo
|
||||||
python3-dev \
|
RUN apt -qq update && apt -qq install --no-install-recommends -y \
|
||||||
python-pil \
|
software-properties-common \
|
||||||
python-lxml \
|
# apt-transport-https ca-certificates \
|
||||||
python-tk \
|
build-essential \
|
||||||
build-essential \
|
gnupg wget unzip \
|
||||||
cmake \
|
# libcap-dev \
|
||||||
git \
|
&& add-apt-repository ppa:deadsnakes/ppa -y \
|
||||||
libgtk2.0-dev \
|
&& apt -qq install --no-install-recommends -y \
|
||||||
pkg-config \
|
python3.7 \
|
||||||
libavcodec-dev \
|
python3.7-dev \
|
||||||
libavformat-dev \
|
python3-pip \
|
||||||
libswscale-dev \
|
ffmpeg \
|
||||||
libtbb2 \
|
# VAAPI drivers for Intel hardware accel
|
||||||
libtbb-dev \
|
libva-drm2 libva2 i965-va-driver vainfo \
|
||||||
libjpeg-dev \
|
&& python3.7 -m pip install -U wheel setuptools \
|
||||||
libpng-dev \
|
&& python3.7 -m pip install -U \
|
||||||
libtiff-dev \
|
opencv-python-headless \
|
||||||
libjasper-dev \
|
# python-prctl \
|
||||||
libdc1394-22-dev \
|
numpy \
|
||||||
x11-apps \
|
imutils \
|
||||||
wget \
|
scipy \
|
||||||
vim \
|
&& python3.7 -m pip install -U \
|
||||||
ffmpeg \
|
SharedArray \
|
||||||
unzip \
|
Flask \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
paho-mqtt \
|
||||||
|
PyYAML \
|
||||||
|
matplotlib \
|
||||||
|
pyarrow \
|
||||||
|
&& echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" > /etc/apt/sources.list.d/coral-edgetpu.list \
|
||||||
|
&& wget -q -O - https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - \
|
||||||
|
&& apt -qq update \
|
||||||
|
&& echo "libedgetpu1-max libedgetpu/accepted-eula boolean true" | debconf-set-selections \
|
||||||
|
&& apt -qq install --no-install-recommends -y \
|
||||||
|
libedgetpu1-max \
|
||||||
|
## Tensorflow lite (python 3.7 only)
|
||||||
|
&& wget -q https://dl.google.com/coral/python/tflite_runtime-2.1.0-cp37-cp37m-linux_x86_64.whl \
|
||||||
|
&& python3.7 -m pip install tflite_runtime-2.1.0-cp37-cp37m-linux_x86_64.whl \
|
||||||
|
&& rm tflite_runtime-2.1.0-cp37-cp37m-linux_x86_64.whl \
|
||||||
|
&& rm -rf /var/lib/apt/lists/* \
|
||||||
|
&& (apt-get autoremove -y; apt-get autoclean -y)
|
||||||
|
|
||||||
# Install core packages
|
# get model and labels
|
||||||
RUN wget -q -O /tmp/get-pip.py --no-check-certificate https://bootstrap.pypa.io/get-pip.py && python3 /tmp/get-pip.py
|
RUN wget -q https://github.com/google-coral/edgetpu/raw/master/test_data/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite -O /edgetpu_model.tflite --trust-server-names
|
||||||
RUN pip install -U pip \
|
RUN wget -q https://dl.google.com/coral/canned_models/coco_labels.txt -O /labelmap.txt --trust-server-names
|
||||||
numpy \
|
RUN wget -q https://storage.googleapis.com/download.tensorflow.org/models/tflite/coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip -O /cpu_model.zip && \
|
||||||
matplotlib \
|
unzip /cpu_model.zip detect.tflite -d / && \
|
||||||
notebook \
|
mv /detect.tflite /cpu_model.tflite && \
|
||||||
jupyter \
|
rm /cpu_model.zip
|
||||||
pandas \
|
|
||||||
moviepy \
|
|
||||||
tensorflow \
|
|
||||||
keras \
|
|
||||||
autovizwidget \
|
|
||||||
Flask \
|
|
||||||
imutils \
|
|
||||||
paho-mqtt
|
|
||||||
|
|
||||||
# Install tensorflow models object detection
|
|
||||||
RUN GIT_SSL_NO_VERIFY=true git clone -q https://github.com/tensorflow/models /usr/local/lib/python3.5/dist-packages/tensorflow/models
|
|
||||||
RUN wget -q -P /usr/local/src/ --no-check-certificate https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-python-3.5.1.tar.gz
|
|
||||||
|
|
||||||
# Download & build protobuf-python
|
|
||||||
RUN cd /usr/local/src/ \
|
|
||||||
&& tar xf protobuf-python-3.5.1.tar.gz \
|
|
||||||
&& rm protobuf-python-3.5.1.tar.gz \
|
|
||||||
&& cd /usr/local/src/protobuf-3.5.1/ \
|
|
||||||
&& ./configure \
|
|
||||||
&& make \
|
|
||||||
&& make install \
|
|
||||||
&& ldconfig \
|
|
||||||
&& rm -rf /usr/local/src/protobuf-3.5.1/
|
|
||||||
|
|
||||||
# Add dataframe display widget
|
|
||||||
RUN jupyter nbextension enable --py --sys-prefix widgetsnbextension
|
|
||||||
|
|
||||||
# Download & build OpenCV
|
|
||||||
RUN wget -q -P /usr/local/src/ --no-check-certificate https://github.com/opencv/opencv/archive/4.0.1.zip
|
|
||||||
RUN cd /usr/local/src/ \
|
|
||||||
&& unzip 4.0.1.zip \
|
|
||||||
&& rm 4.0.1.zip \
|
|
||||||
&& cd /usr/local/src/opencv-4.0.1/ \
|
|
||||||
&& mkdir build \
|
|
||||||
&& cd /usr/local/src/opencv-4.0.1/build \
|
|
||||||
&& cmake -D CMAKE_INSTALL_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/ .. \
|
|
||||||
&& make -j4 \
|
|
||||||
&& make install \
|
|
||||||
&& rm -rf /usr/local/src/opencv-4.0.1
|
|
||||||
|
|
||||||
# Minimize image size
|
|
||||||
RUN (apt-get autoremove -y; \
|
|
||||||
apt-get autoclean -y)
|
|
||||||
|
|
||||||
# Set TF object detection available
|
|
||||||
ENV PYTHONPATH "$PYTHONPATH:/usr/local/lib/python3.5/dist-packages/tensorflow/models/research:/usr/local/lib/python3.5/dist-packages/tensorflow/models/research/slim"
|
|
||||||
RUN cd /usr/local/lib/python3.5/dist-packages/tensorflow/models/research && protoc object_detection/protos/*.proto --python_out=.
|
|
||||||
|
|
||||||
WORKDIR /opt/frigate/
|
WORKDIR /opt/frigate/
|
||||||
ADD frigate frigate/
|
ADD frigate frigate/
|
||||||
COPY detect_objects.py .
|
COPY detect_objects.py .
|
||||||
|
COPY benchmark.py .
|
||||||
|
|
||||||
CMD ["python3", "-u", "detect_objects.py"]
|
CMD ["python3.7", "-u", "detect_objects.py"]
|
||||||
|
|||||||
183
README.md
183
README.md
@@ -1,18 +1,17 @@
|
|||||||
# Frigate - Realtime Object Detection for RTSP Cameras
|
# Frigate - Realtime Object Detection for IP Cameras
|
||||||
Uses OpenCV and Tensorflow to perform realtime object detection locally for RTSP cameras. Designed for integration with HomeAssistant or others via MQTT.
|
Uses OpenCV and Tensorflow to perform realtime object detection locally for IP cameras. Designed for integration with HomeAssistant or others via MQTT.
|
||||||
|
|
||||||
- Leverages multiprocessing and threads heavily with an emphasis on realtime over processing every frame
|
Use of a [Google Coral USB Accelerator](https://coral.withgoogle.com/products/accelerator/) is optional, but highly recommended. On my Intel i7 processor, I can process 2-3 FPS with the CPU. The Coral can process 100+ FPS with very low CPU load.
|
||||||
- Allows you to define specific regions (squares) in the image to look for motion/objects
|
|
||||||
- Motion detection runs in a separate process per region and signals to object detection to avoid wasting CPU cycles looking for objects when there is no motion
|
- Leverages multiprocessing heavily with an emphasis on realtime over processing every frame
|
||||||
- Object detection with Tensorflow runs in a separate process per region
|
- Uses a very low overhead motion detection to determine where to run object detection
|
||||||
- Detected objects are placed on a shared mp.Queue and aggregated into a list of recently detected objects in a separate thread
|
- Object detection with Tensorflow runs in a separate process
|
||||||
- A person score is calculated as the sum of all scores/5
|
- Object info is published over MQTT for integration into HomeAssistant as a binary sensor
|
||||||
- Motion and object info is published over MQTT for integration into HomeAssistant or others
|
- An endpoint is available to view an MJPEG stream for debugging, but should not be used continuously
|
||||||
- An endpoint is available to view an MJPEG stream for debugging
|
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Example video
|
## Example video (from older version)
|
||||||
You see multiple bounding boxes because it draws bounding boxes from all frames in the past 1 second where a person was detected. Not all of the bounding boxes were from the current frame.
|
You see multiple bounding boxes because it draws bounding boxes from all frames in the past 1 second where a person was detected. Not all of the bounding boxes were from the current frame.
|
||||||
[](http://www.youtube.com/watch?v=nqHbCtyo4dY "Frigate")
|
[](http://www.youtube.com/watch?v=nqHbCtyo4dY "Frigate")
|
||||||
|
|
||||||
@@ -22,126 +21,112 @@ Build the container with
|
|||||||
docker build -t frigate .
|
docker build -t frigate .
|
||||||
```
|
```
|
||||||
|
|
||||||
Download a model from the [zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md).
|
Models for both CPU and EdgeTPU (Coral) are bundled in the image. You can use your own models with volume mounts:
|
||||||
|
- CPU Model: `/cpu_model.tflite`
|
||||||
Download the cooresponding label map from [here](https://github.com/tensorflow/models/tree/master/research/object_detection/data).
|
- EdgeTPU Model: `/edgetpu_model.tflite`
|
||||||
|
- Labels: `/labelmap.txt`
|
||||||
|
|
||||||
Run the container with
|
Run the container with
|
||||||
```
|
```bash
|
||||||
docker run --rm \
|
docker run --rm \
|
||||||
-v <path_to_frozen_detection_graph.pb>:/frozen_inference_graph.pb:ro \
|
--privileged \
|
||||||
-v <path_to_labelmap.pbtext>:/label_map.pbtext:ro \
|
--shm-size=512m \ # should work for a 2-3 cameras
|
||||||
|
-v /dev/bus/usb:/dev/bus/usb \
|
||||||
-v <path_to_config_dir>:/config:ro \
|
-v <path_to_config_dir>:/config:ro \
|
||||||
|
-v /etc/localtime:/etc/localtime:ro \
|
||||||
-p 5000:5000 \
|
-p 5000:5000 \
|
||||||
-e RTSP_URL='<rtsp_url>' \
|
-e FRIGATE_RTSP_PASSWORD='password' \
|
||||||
-e REGIONS='<box_size_1>,<x_offset_1>,<y_offset_1>,<min_person_size_1>,<min_motion_size_1>,<mask_file_1>:<box_size_2>,<x_offset_2>,<y_offset_2>,<min_person_size_2>,<min_motion_size_2>,<mask_file_2>' \
|
|
||||||
-e MQTT_HOST='your.mqtthost.com' \
|
|
||||||
-e MQTT_USER='username' \
|
|
||||||
-e MQTT_PASS='password' \
|
|
||||||
-e MQTT_TOPIC_PREFIX='cameras/1' \
|
|
||||||
-e DEBUG='0' \
|
|
||||||
frigate:latest
|
frigate:latest
|
||||||
```
|
```
|
||||||
|
|
||||||
Example docker-compose:
|
Example docker-compose:
|
||||||
```
|
```yaml
|
||||||
frigate:
|
frigate:
|
||||||
container_name: frigate
|
container_name: frigate
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
privileged: true
|
||||||
|
shm_size: '1g' # should work for 5-7 cameras
|
||||||
image: frigate:latest
|
image: frigate:latest
|
||||||
volumes:
|
volumes:
|
||||||
- <path_to_frozen_detection_graph.pb>:/frozen_inference_graph.pb:ro
|
- /dev/bus/usb:/dev/bus/usb
|
||||||
- <path_to_labelmap.pbtext>:/label_map.pbtext:ro
|
- /etc/localtime:/etc/localtime:ro
|
||||||
- <path_to_config>:/config
|
- <path_to_config>:/config
|
||||||
ports:
|
ports:
|
||||||
- "127.0.0.1:5000:5000"
|
- "5000:5000"
|
||||||
environment:
|
environment:
|
||||||
RTSP_URL: "<rtsp_url>"
|
FRIGATE_RTSP_PASSWORD: "password"
|
||||||
REGIONS: "<box_size_1>,<x_offset_1>,<y_offset_1>,<min_person_size_1>,<min_motion_size_1>,<mask_file_1>:<box_size_2>,<x_offset_2>,<y_offset_2>,<min_person_size_2>,<min_motion_size_2>,<mask_file_2>"
|
|
||||||
MQTT_HOST: "your.mqtthost.com"
|
|
||||||
MQTT_USER: "username" #optional
|
|
||||||
MQTT_PASS: "password" #optional
|
|
||||||
MQTT_TOPIC_PREFIX: "cameras/1"
|
|
||||||
DEBUG: "0"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Here is an example `REGIONS` env variable:
|
A `config.yml` file must exist in the `config` directory. See example [here](config/config.example.yml) and device specific info can be found [here](docs/DEVICES.md).
|
||||||
`350,0,300,5000,200,mask-0-300.bmp:400,350,250,2000,200,mask-350-250.bmp:400,750,250,2000,200,mask-750-250.bmp`
|
|
||||||
|
|
||||||
First region broken down (all are required):
|
Access the mjpeg stream at `http://localhost:5000/<camera_name>` and the best snapshot for any object type with at `http://localhost:5000/<camera_name>/<object_name>/best.jpg`
|
||||||
- `350` - size of the square (350px by 350px)
|
|
||||||
- `0` - x coordinate of upper left corner (top left of image is 0,0)
|
|
||||||
- `300` - y coordinate of upper left corner (top left of image is 0,0)
|
|
||||||
- `5000` - minimum person bounding box size (width*height for bounding box of identified person)
|
|
||||||
- `200` - minimum number of changed pixels to trigger motion
|
|
||||||
- `mask-0-300.bmp` - a bmp file with the masked regions as pure black, must be the same size as the region
|
|
||||||
|
|
||||||
Mask files go in the `/config` directory.
|
Debug info is available at `http://localhost:5000/debug/stats`
|
||||||
|
|
||||||
Access the mjpeg stream at http://localhost:5000
|
|
||||||
|
|
||||||
## Integration with HomeAssistant
|
## Integration with HomeAssistant
|
||||||
```
|
```
|
||||||
camera:
|
camera:
|
||||||
- name: Camera Last Person
|
- name: Camera Last Person
|
||||||
platform: generic
|
platform: mqtt
|
||||||
still_image_url: http://<ip>:5000/best_person.jpg
|
topic: frigate/<camera_name>/person/snapshot
|
||||||
|
- name: Camera Last Car
|
||||||
|
platform: mqtt
|
||||||
|
topic: frigate/<camera_name>/car/snapshot
|
||||||
|
|
||||||
binary_sensor:
|
binary_sensor:
|
||||||
- name: Camera Motion
|
- name: Camera Person
|
||||||
platform: mqtt
|
platform: mqtt
|
||||||
state_topic: "cameras/1/motion"
|
state_topic: "frigate/<camera_name>/person"
|
||||||
device_class: motion
|
device_class: motion
|
||||||
availability_topic: "cameras/1/available"
|
availability_topic: "frigate/available"
|
||||||
|
|
||||||
|
automation:
|
||||||
|
- alias: Alert me if a person is detected while armed away
|
||||||
|
trigger:
|
||||||
|
platform: state
|
||||||
|
entity_id: binary_sensor.camera_person
|
||||||
|
from: 'off'
|
||||||
|
to: 'on'
|
||||||
|
condition:
|
||||||
|
- condition: state
|
||||||
|
entity_id: alarm_control_panel.home_alarm
|
||||||
|
state: armed_away
|
||||||
|
action:
|
||||||
|
- service: notify.user_telegram
|
||||||
|
data:
|
||||||
|
message: "A person was detected."
|
||||||
|
data:
|
||||||
|
photo:
|
||||||
|
- url: http://<ip>:5000/<camera_name>/person/best.jpg
|
||||||
|
caption: A person was detected.
|
||||||
|
|
||||||
sensor:
|
sensor:
|
||||||
- name: Camera Person Score
|
- platform: rest
|
||||||
platform: mqtt
|
name: Frigate Debug
|
||||||
state_topic: "cameras/1/objects"
|
resource: http://localhost:5000/debug/stats
|
||||||
value_template: '{{ value_json.person }}'
|
scan_interval: 5
|
||||||
unit_of_measurement: '%'
|
json_attributes:
|
||||||
availability_topic: "cameras/1/available"
|
- back
|
||||||
|
- coral
|
||||||
|
value_template: 'OK'
|
||||||
|
- platform: template
|
||||||
|
sensors:
|
||||||
|
back_fps:
|
||||||
|
value_template: '{{ states.sensor.frigate_debug.attributes["back"]["fps"] }}'
|
||||||
|
unit_of_measurement: 'FPS'
|
||||||
|
back_skipped_fps:
|
||||||
|
value_template: '{{ states.sensor.frigate_debug.attributes["back"]["skipped_fps"] }}'
|
||||||
|
unit_of_measurement: 'FPS'
|
||||||
|
back_detection_fps:
|
||||||
|
value_template: '{{ states.sensor.frigate_debug.attributes["back"]["detection_fps"] }}'
|
||||||
|
unit_of_measurement: 'FPS'
|
||||||
|
frigate_coral_fps:
|
||||||
|
value_template: '{{ states.sensor.frigate_debug.attributes["coral"]["fps"] }}'
|
||||||
|
unit_of_measurement: 'FPS'
|
||||||
|
frigate_coral_inference:
|
||||||
|
value_template: '{{ states.sensor.frigate_debug.attributes["coral"]["inference_speed"] }}'
|
||||||
|
unit_of_measurement: 'ms'
|
||||||
```
|
```
|
||||||
|
|
||||||
## Tips
|
## Tips
|
||||||
- Lower the framerate of the RTSP feed on the camera to reduce the CPU usage for capturing the feed
|
- Lower the framerate of the video feed on the camera to reduce the CPU usage for capturing the feed
|
||||||
- Use SSDLite models to reduce CPU usage
|
|
||||||
|
|
||||||
## Future improvements
|
|
||||||
- [ ] Build tensorflow from source for CPU optimizations
|
|
||||||
- [ ] Add ability to turn detection on and off via MQTT
|
|
||||||
- [ ] MQTT motion occasionally gets stuck ON
|
|
||||||
- [ ] Output movie clips of people for notifications, etc.
|
|
||||||
- [ ] Integrate with homeassistant push camera
|
|
||||||
- [ ] Merge bounding boxes that span multiple regions
|
|
||||||
- [ ] Switch to a config file
|
|
||||||
- [ ] Allow motion regions to be different than object detection regions
|
|
||||||
- [ ] Implement mode to save labeled objects for training
|
|
||||||
- [ ] Try and reduce CPU usage by simplifying the tensorflow model to just include the objects we care about
|
|
||||||
- [ ] Look into GPU accelerated decoding of RTSP stream
|
|
||||||
- [ ] Send video over a socket and use JSMPEG
|
|
||||||
- [ ] Look into neural compute stick
|
|
||||||
|
|
||||||
## Building Tensorflow from source for CPU optimizations
|
|
||||||
https://www.tensorflow.org/install/source#docker_linux_builds
|
|
||||||
used `tensorflow/tensorflow:1.12.0-devel-py3`
|
|
||||||
|
|
||||||
## Optimizing the graph (cant say I saw much difference in CPU usage)
|
|
||||||
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/graph_transforms/README.md#optimizing-for-deployment
|
|
||||||
```
|
|
||||||
docker run -it -v ${PWD}:/lab -v ${PWD}/../back_camera_model/models/ssd_mobilenet_v2_coco_2018_03_29/frozen_inference_graph.pb:/frozen_inference_graph.pb:ro tensorflow/tensorflow:1.12.0-devel-py3 bash
|
|
||||||
|
|
||||||
bazel build tensorflow/tools/graph_transforms:transform_graph
|
|
||||||
|
|
||||||
bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
|
|
||||||
--in_graph=/frozen_inference_graph.pb \
|
|
||||||
--out_graph=/lab/optimized_inception_graph.pb \
|
|
||||||
--inputs='image_tensor' \
|
|
||||||
--outputs='num_detections,detection_scores,detection_boxes,detection_classes' \
|
|
||||||
--transforms='
|
|
||||||
strip_unused_nodes(type=float, shape="1,300,300,3")
|
|
||||||
remove_nodes(op=Identity, op=CheckNumerics)
|
|
||||||
fold_constants(ignore_errors=true)
|
|
||||||
fold_batch_norms
|
|
||||||
fold_old_batch_norms'
|
|
||||||
```
|
|
||||||
|
|||||||
18
benchmark.py
Executable file
18
benchmark.py
Executable file
@@ -0,0 +1,18 @@
|
|||||||
|
import statistics
|
||||||
|
import numpy as np
|
||||||
|
import time
|
||||||
|
from frigate.edgetpu import ObjectDetector
|
||||||
|
|
||||||
|
object_detector = ObjectDetector()
|
||||||
|
|
||||||
|
frame = np.zeros((300,300,3), np.uint8)
|
||||||
|
input_frame = np.expand_dims(frame, axis=0)
|
||||||
|
|
||||||
|
detection_times = []
|
||||||
|
|
||||||
|
for x in range(0, 100):
|
||||||
|
start = time.monotonic()
|
||||||
|
object_detector.detect_raw(input_frame)
|
||||||
|
detection_times.append(time.monotonic()-start)
|
||||||
|
|
||||||
|
print(f"Average inference time: {statistics.mean(detection_times)*1000:.2f}ms")
|
||||||
BIN
config/back-mask.bmp
Normal file
BIN
config/back-mask.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 MiB |
132
config/config.example.yml
Normal file
132
config/config.example.yml
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
web_port: 5000
|
||||||
|
|
||||||
|
mqtt:
|
||||||
|
host: mqtt.server.com
|
||||||
|
topic_prefix: frigate
|
||||||
|
# client_id: frigate # Optional -- set to override default client id of 'frigate' if running multiple instances
|
||||||
|
# user: username # Optional -- Uncomment for use
|
||||||
|
# password: password # Optional -- Uncomment for use
|
||||||
|
|
||||||
|
#################
|
||||||
|
# Default ffmpeg args. Optional and can be overwritten per camera.
|
||||||
|
# Should work with most RTSP cameras that send h264 video
|
||||||
|
# Built from the properties below with:
|
||||||
|
# "ffmpeg" + global_args + input_args + "-i" + input + output_args
|
||||||
|
#################
|
||||||
|
# ffmpeg:
|
||||||
|
# global_args:
|
||||||
|
# - -hide_banner
|
||||||
|
# - -loglevel
|
||||||
|
# - panic
|
||||||
|
# hwaccel_args: []
|
||||||
|
# input_args:
|
||||||
|
# - -avoid_negative_ts
|
||||||
|
# - make_zero
|
||||||
|
# - -fflags
|
||||||
|
# - nobuffer
|
||||||
|
# - -flags
|
||||||
|
# - low_delay
|
||||||
|
# - -strict
|
||||||
|
# - experimental
|
||||||
|
# - -fflags
|
||||||
|
# - +genpts+discardcorrupt
|
||||||
|
# - -vsync
|
||||||
|
# - drop
|
||||||
|
# - -rtsp_transport
|
||||||
|
# - tcp
|
||||||
|
# - -stimeout
|
||||||
|
# - '5000000'
|
||||||
|
# - -use_wallclock_as_timestamps
|
||||||
|
# - '1'
|
||||||
|
# output_args:
|
||||||
|
# - -f
|
||||||
|
# - rawvideo
|
||||||
|
# - -pix_fmt
|
||||||
|
# - rgb24
|
||||||
|
|
||||||
|
####################
|
||||||
|
# Global object configuration. Applies to all cameras
|
||||||
|
# unless overridden at the camera levels.
|
||||||
|
# Keys must be valid labels. By default, the model uses coco (https://dl.google.com/coral/canned_models/coco_labels.txt).
|
||||||
|
# All labels from the model are reported over MQTT. These values are used to filter out false positives.
|
||||||
|
# min_area (optional): minimum width*height of the bounding box for the detected person
|
||||||
|
# max_area (optional): maximum width*height of the bounding box for the detected person
|
||||||
|
# threshold (optional): The minimum decimal percentage (50% hit = 0.5) for the confidence from tensorflow
|
||||||
|
####################
|
||||||
|
objects:
|
||||||
|
track:
|
||||||
|
- person
|
||||||
|
- car
|
||||||
|
- truck
|
||||||
|
filters:
|
||||||
|
person:
|
||||||
|
min_area: 5000
|
||||||
|
max_area: 100000
|
||||||
|
threshold: 0.5
|
||||||
|
|
||||||
|
cameras:
|
||||||
|
back:
|
||||||
|
ffmpeg:
|
||||||
|
################
|
||||||
|
# Source passed to ffmpeg after the -i parameter. Supports anything compatible with OpenCV and FFmpeg.
|
||||||
|
# Environment variables that begin with 'FRIGATE_' may be referenced in {}
|
||||||
|
################
|
||||||
|
input: rtsp://viewer:{FRIGATE_RTSP_PASSWORD}@10.0.10.10:554/cam/realmonitor?channel=1&subtype=2
|
||||||
|
#################
|
||||||
|
# These values will override default values for just this camera
|
||||||
|
#################
|
||||||
|
# global_args: []
|
||||||
|
# hwaccel_args: []
|
||||||
|
# input_args: []
|
||||||
|
# output_args: []
|
||||||
|
|
||||||
|
################
|
||||||
|
## Optionally specify the resolution of the video feed. Frigate will try to auto detect if not specified
|
||||||
|
################
|
||||||
|
# height: 1280
|
||||||
|
# width: 720
|
||||||
|
|
||||||
|
################
|
||||||
|
## Optional mask. Must be the same aspect ratio as your video feed.
|
||||||
|
##
|
||||||
|
## The mask works by looking at the bottom center of the bounding box for the detected
|
||||||
|
## person in the image. If that pixel in the mask is a black pixel, it ignores it as a
|
||||||
|
## false positive. In my mask, the grass and driveway visible from my backdoor camera
|
||||||
|
## are white. The garage doors, sky, and trees (anywhere it would be impossible for a
|
||||||
|
## person to stand) are black.
|
||||||
|
##
|
||||||
|
## Masked areas are also ignored for motion detection.
|
||||||
|
################
|
||||||
|
# mask: back-mask.bmp
|
||||||
|
|
||||||
|
################
|
||||||
|
# Allows you to limit the framerate within frigate for cameras that do not support
|
||||||
|
# custom framerates. A value of 1 tells frigate to look at every frame, 2 every 2nd frame,
|
||||||
|
# 3 every 3rd frame, etc.
|
||||||
|
################
|
||||||
|
take_frame: 1
|
||||||
|
|
||||||
|
################
|
||||||
|
# The expected framerate for the camera. Frigate will try and ensure it maintains this framerate
|
||||||
|
# by dropping frames as necessary. Setting this lower than the actual framerate will allow frigate
|
||||||
|
# to process every frame at the expense of realtime processing.
|
||||||
|
################
|
||||||
|
fps: 5
|
||||||
|
|
||||||
|
################
|
||||||
|
# Configuration for the snapshots in the debug view and mqtt
|
||||||
|
################
|
||||||
|
snapshots:
|
||||||
|
show_timestamp: True
|
||||||
|
|
||||||
|
################
|
||||||
|
# Camera level object config. This config is merged with the global config above.
|
||||||
|
################
|
||||||
|
objects:
|
||||||
|
track:
|
||||||
|
- person
|
||||||
|
filters:
|
||||||
|
person:
|
||||||
|
min_area: 5000
|
||||||
|
max_area: 100000
|
||||||
|
threshold: 0.5
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 239 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 313 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 313 KiB |
@@ -1,247 +1,246 @@
|
|||||||
import os
|
|
||||||
import cv2
|
import cv2
|
||||||
import imutils
|
|
||||||
import time
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
import ctypes
|
import queue
|
||||||
import logging
|
import yaml
|
||||||
import multiprocessing as mp
|
|
||||||
import threading
|
import threading
|
||||||
import json
|
import multiprocessing as mp
|
||||||
from contextlib import closing
|
import subprocess as sp
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from object_detection.utils import visualization_utils as vis_util
|
import logging
|
||||||
from flask import Flask, Response, make_response, send_file
|
from flask import Flask, Response, make_response, jsonify
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
from frigate.util import tonumpyarray
|
from frigate.video import track_camera
|
||||||
from frigate.mqtt import MqttMotionPublisher, MqttObjectPublisher
|
from frigate.object_processing import TrackedObjectProcessor
|
||||||
from frigate.objects import ObjectParser, ObjectCleaner, BestPersonFrame
|
from frigate.util import EventsPerSecond
|
||||||
from frigate.motion import detect_motion
|
from frigate.edgetpu import EdgeTPUProcess
|
||||||
from frigate.video import fetch_frames, FrameTracker
|
|
||||||
from frigate.object_detection import detect_objects
|
|
||||||
|
|
||||||
RTSP_URL = os.getenv('RTSP_URL')
|
with open('/config/config.yml') as f:
|
||||||
|
CONFIG = yaml.safe_load(f)
|
||||||
|
|
||||||
MQTT_HOST = os.getenv('MQTT_HOST')
|
MQTT_HOST = CONFIG['mqtt']['host']
|
||||||
MQTT_USER = os.getenv('MQTT_USER')
|
MQTT_PORT = CONFIG.get('mqtt', {}).get('port', 1883)
|
||||||
MQTT_PASS = os.getenv('MQTT_PASS')
|
MQTT_TOPIC_PREFIX = CONFIG.get('mqtt', {}).get('topic_prefix', 'frigate')
|
||||||
MQTT_TOPIC_PREFIX = os.getenv('MQTT_TOPIC_PREFIX')
|
MQTT_USER = CONFIG.get('mqtt', {}).get('user')
|
||||||
|
MQTT_PASS = CONFIG.get('mqtt', {}).get('password')
|
||||||
|
MQTT_CLIENT_ID = CONFIG.get('mqtt', {}).get('client_id', 'frigate')
|
||||||
|
|
||||||
# REGIONS = "350,0,300,50:400,350,250,50:400,750,250,50"
|
# Set the default FFmpeg config
|
||||||
# REGIONS = "400,350,250,50"
|
FFMPEG_CONFIG = CONFIG.get('ffmpeg', {})
|
||||||
REGIONS = os.getenv('REGIONS')
|
FFMPEG_DEFAULT_CONFIG = {
|
||||||
|
'global_args': FFMPEG_CONFIG.get('global_args',
|
||||||
|
['-hide_banner','-loglevel','panic']),
|
||||||
|
'hwaccel_args': FFMPEG_CONFIG.get('hwaccel_args',
|
||||||
|
[]),
|
||||||
|
'input_args': FFMPEG_CONFIG.get('input_args',
|
||||||
|
['-avoid_negative_ts', 'make_zero',
|
||||||
|
'-fflags', 'nobuffer',
|
||||||
|
'-flags', 'low_delay',
|
||||||
|
'-strict', 'experimental',
|
||||||
|
'-fflags', '+genpts+discardcorrupt',
|
||||||
|
'-vsync', 'drop',
|
||||||
|
'-rtsp_transport', 'tcp',
|
||||||
|
'-stimeout', '5000000',
|
||||||
|
'-use_wallclock_as_timestamps', '1']),
|
||||||
|
'output_args': FFMPEG_CONFIG.get('output_args',
|
||||||
|
['-f', 'rawvideo',
|
||||||
|
'-pix_fmt', 'rgb24'])
|
||||||
|
}
|
||||||
|
|
||||||
DEBUG = (os.getenv('DEBUG') == '1')
|
GLOBAL_OBJECT_CONFIG = CONFIG.get('objects', {})
|
||||||
|
|
||||||
|
WEB_PORT = CONFIG.get('web_port', 5000)
|
||||||
|
DEBUG = (CONFIG.get('debug', '0') == '1')
|
||||||
|
|
||||||
|
class CameraWatchdog(threading.Thread):
|
||||||
|
def __init__(self, camera_processes, config, tflite_process, tracked_objects_queue, object_processor):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
self.camera_processes = camera_processes
|
||||||
|
self.config = config
|
||||||
|
self.tflite_process = tflite_process
|
||||||
|
self.tracked_objects_queue = tracked_objects_queue
|
||||||
|
self.object_processor = object_processor
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
time.sleep(10)
|
||||||
|
while True:
|
||||||
|
# wait a bit before checking
|
||||||
|
time.sleep(30)
|
||||||
|
|
||||||
|
for name, camera_process in self.camera_processes.items():
|
||||||
|
process = camera_process['process']
|
||||||
|
if (not self.object_processor.get_current_frame_time(name) is None and
|
||||||
|
(datetime.datetime.now().timestamp() - self.object_processor.get_current_frame_time(name)) > 30):
|
||||||
|
print(f"Last frame for {name} is more than 30 seconds old...")
|
||||||
|
if process.is_alive():
|
||||||
|
process.terminate()
|
||||||
|
print("Waiting for process to exit gracefully...")
|
||||||
|
process.join(timeout=30)
|
||||||
|
if process.exitcode is None:
|
||||||
|
print("Process didnt exit. Force killing...")
|
||||||
|
process.kill()
|
||||||
|
process.join()
|
||||||
|
if not process.is_alive():
|
||||||
|
print(f"Process for {name} is not alive. Starting again...")
|
||||||
|
camera_process['fps'].value = float(self.config[name]['fps'])
|
||||||
|
camera_process['skipped_fps'].value = 0.0
|
||||||
|
camera_process['detection_fps'].value = 0.0
|
||||||
|
self.object_processor.camera_data[name]['current_frame_time'] = None
|
||||||
|
process = mp.Process(target=track_camera, args=(name, self.config[name], FFMPEG_DEFAULT_CONFIG, GLOBAL_OBJECT_CONFIG,
|
||||||
|
self.tflite_process.detect_lock, self.tflite_process.detect_ready, self.tflite_process.frame_ready, self.tracked_objects_queue,
|
||||||
|
camera_process['fps'], camera_process['skipped_fps'], camera_process['detection_fps']))
|
||||||
|
process.daemon = True
|
||||||
|
camera_process['process'] = process
|
||||||
|
process.start()
|
||||||
|
print(f"Camera_process started for {name}: {process.pid}")
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
DETECTED_OBJECTS = []
|
|
||||||
recent_motion_frames = {}
|
|
||||||
# Parse selected regions
|
|
||||||
regions = []
|
|
||||||
for region_string in REGIONS.split(':'):
|
|
||||||
region_parts = region_string.split(',')
|
|
||||||
region_mask_image = cv2.imread("/config/{}".format(region_parts[5]), cv2.IMREAD_GRAYSCALE)
|
|
||||||
region_mask = np.where(region_mask_image==[0])
|
|
||||||
regions.append({
|
|
||||||
'size': int(region_parts[0]),
|
|
||||||
'x_offset': int(region_parts[1]),
|
|
||||||
'y_offset': int(region_parts[2]),
|
|
||||||
'min_person_area': int(region_parts[3]),
|
|
||||||
'min_object_size': int(region_parts[4]),
|
|
||||||
'mask': region_mask,
|
|
||||||
# Event for motion detection signaling
|
|
||||||
'motion_detected': mp.Event(),
|
|
||||||
# create shared array for storing 10 detected objects
|
|
||||||
# note: this must be a double even though the value you are storing
|
|
||||||
# is a float. otherwise it stops updating the value in shared
|
|
||||||
# memory. probably something to do with the size of the memory block
|
|
||||||
'output_array': mp.Array(ctypes.c_double, 6*10)
|
|
||||||
})
|
|
||||||
# capture a single frame and check the frame shape so the correct array
|
|
||||||
# size can be allocated in memory
|
|
||||||
video = cv2.VideoCapture(RTSP_URL)
|
|
||||||
ret, frame = video.read()
|
|
||||||
if ret:
|
|
||||||
frame_shape = frame.shape
|
|
||||||
else:
|
|
||||||
print("Unable to capture video stream")
|
|
||||||
exit(1)
|
|
||||||
video.release()
|
|
||||||
|
|
||||||
# compute the flattened array length from the array shape
|
|
||||||
flat_array_length = frame_shape[0] * frame_shape[1] * frame_shape[2]
|
|
||||||
# create shared array for storing the full frame image data
|
|
||||||
shared_arr = mp.Array(ctypes.c_uint16, flat_array_length)
|
|
||||||
# create shared value for storing the frame_time
|
|
||||||
shared_frame_time = mp.Value('d', 0.0)
|
|
||||||
# Lock to control access to the frame
|
|
||||||
frame_lock = mp.Lock()
|
|
||||||
# Condition for notifying that a new frame is ready
|
|
||||||
frame_ready = mp.Condition()
|
|
||||||
# Condition for notifying that motion status changed globally
|
|
||||||
motion_changed = mp.Condition()
|
|
||||||
# Condition for notifying that objects were parsed
|
|
||||||
objects_parsed = mp.Condition()
|
|
||||||
# Queue for detected objects
|
|
||||||
object_queue = mp.Queue()
|
|
||||||
|
|
||||||
# shape current frame so it can be treated as an image
|
|
||||||
frame_arr = tonumpyarray(shared_arr).reshape(frame_shape)
|
|
||||||
|
|
||||||
# start the process to capture frames from the RTSP stream and store in a shared array
|
|
||||||
capture_process = mp.Process(target=fetch_frames, args=(shared_arr,
|
|
||||||
shared_frame_time, frame_lock, frame_ready, frame_shape, RTSP_URL))
|
|
||||||
capture_process.daemon = True
|
|
||||||
|
|
||||||
# for each region, start a separate process for motion detection and object detection
|
|
||||||
detection_processes = []
|
|
||||||
motion_processes = []
|
|
||||||
for region in regions:
|
|
||||||
detection_process = mp.Process(target=detect_objects, args=(shared_arr,
|
|
||||||
object_queue,
|
|
||||||
shared_frame_time,
|
|
||||||
frame_lock, frame_ready,
|
|
||||||
region['motion_detected'],
|
|
||||||
frame_shape,
|
|
||||||
region['size'], region['x_offset'], region['y_offset'],
|
|
||||||
region['min_person_area'],
|
|
||||||
DEBUG))
|
|
||||||
detection_process.daemon = True
|
|
||||||
detection_processes.append(detection_process)
|
|
||||||
|
|
||||||
motion_process = mp.Process(target=detect_motion, args=(shared_arr,
|
|
||||||
shared_frame_time,
|
|
||||||
frame_lock, frame_ready,
|
|
||||||
region['motion_detected'],
|
|
||||||
motion_changed,
|
|
||||||
frame_shape,
|
|
||||||
region['size'], region['x_offset'], region['y_offset'],
|
|
||||||
region['min_object_size'], region['mask'],
|
|
||||||
DEBUG))
|
|
||||||
motion_process.daemon = True
|
|
||||||
motion_processes.append(motion_process)
|
|
||||||
|
|
||||||
# start a thread to store recent motion frames for processing
|
|
||||||
frame_tracker = FrameTracker(frame_arr, shared_frame_time, frame_ready, frame_lock,
|
|
||||||
recent_motion_frames, motion_changed, [region['motion_detected'] for region in regions])
|
|
||||||
frame_tracker.start()
|
|
||||||
|
|
||||||
# start a thread to store the highest scoring recent person frame
|
|
||||||
best_person_frame = BestPersonFrame(objects_parsed, recent_motion_frames, DETECTED_OBJECTS,
|
|
||||||
motion_changed, [region['motion_detected'] for region in regions])
|
|
||||||
best_person_frame.start()
|
|
||||||
|
|
||||||
# start a thread to parse objects from the queue
|
|
||||||
object_parser = ObjectParser(object_queue, objects_parsed, DETECTED_OBJECTS)
|
|
||||||
object_parser.start()
|
|
||||||
# start a thread to expire objects from the detected objects list
|
|
||||||
object_cleaner = ObjectCleaner(objects_parsed, DETECTED_OBJECTS)
|
|
||||||
object_cleaner.start()
|
|
||||||
|
|
||||||
# connect to mqtt and setup last will
|
# connect to mqtt and setup last will
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(client, userdata, flags, rc):
|
||||||
print("On connect called")
|
print("On connect called")
|
||||||
|
if rc != 0:
|
||||||
|
if rc == 3:
|
||||||
|
print ("MQTT Server unavailable")
|
||||||
|
elif rc == 4:
|
||||||
|
print ("MQTT Bad username or password")
|
||||||
|
elif rc == 5:
|
||||||
|
print ("MQTT Not authorized")
|
||||||
|
else:
|
||||||
|
print ("Unable to connect to MQTT: Connection refused. Error code: " + str(rc))
|
||||||
# publish a message to signal that the service is running
|
# publish a message to signal that the service is running
|
||||||
client.publish(MQTT_TOPIC_PREFIX+'/available', 'online', retain=True)
|
client.publish(MQTT_TOPIC_PREFIX+'/available', 'online', retain=True)
|
||||||
client = mqtt.Client()
|
client = mqtt.Client(client_id=MQTT_CLIENT_ID)
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
client.will_set(MQTT_TOPIC_PREFIX+'/available', payload='offline', qos=1, retain=True)
|
client.will_set(MQTT_TOPIC_PREFIX+'/available', payload='offline', qos=1, retain=True)
|
||||||
if not MQTT_USER is None:
|
if not MQTT_USER is None:
|
||||||
client.username_pw_set(MQTT_USER, password=MQTT_PASS)
|
client.username_pw_set(MQTT_USER, password=MQTT_PASS)
|
||||||
|
client.connect(MQTT_HOST, MQTT_PORT, 60)
|
||||||
client.connect(MQTT_HOST, 1883, 60)
|
|
||||||
client.loop_start()
|
client.loop_start()
|
||||||
|
|
||||||
# start a thread to publish object scores (currently only person)
|
# start plasma store
|
||||||
mqtt_publisher = MqttObjectPublisher(client, MQTT_TOPIC_PREFIX, objects_parsed, DETECTED_OBJECTS)
|
plasma_cmd = ['plasma_store', '-m', '400000000', '-s', '/tmp/plasma']
|
||||||
mqtt_publisher.start()
|
plasma_process = sp.Popen(plasma_cmd, stdout=sp.DEVNULL)
|
||||||
|
time.sleep(1)
|
||||||
|
rc = plasma_process.poll()
|
||||||
|
if rc is not None:
|
||||||
|
raise RuntimeError("plasma_store exited unexpectedly with "
|
||||||
|
"code %d" % (rc,))
|
||||||
|
|
||||||
# start thread to publish motion status
|
##
|
||||||
mqtt_motion_publisher = MqttMotionPublisher(client, MQTT_TOPIC_PREFIX, motion_changed,
|
# Setup config defaults for cameras
|
||||||
[region['motion_detected'] for region in regions])
|
##
|
||||||
mqtt_motion_publisher.start()
|
for name, config in CONFIG['cameras'].items():
|
||||||
|
config['snapshots'] = {
|
||||||
|
'show_timestamp': config.get('snapshots', {}).get('show_timestamp', True)
|
||||||
|
}
|
||||||
|
|
||||||
# start the process of capturing frames
|
# Queue for cameras to push tracked objects to
|
||||||
capture_process.start()
|
tracked_objects_queue = mp.Queue()
|
||||||
print("capture_process pid ", capture_process.pid)
|
|
||||||
|
|
||||||
# start the object detection processes
|
|
||||||
for detection_process in detection_processes:
|
|
||||||
detection_process.start()
|
|
||||||
print("detection_process pid ", detection_process.pid)
|
|
||||||
|
|
||||||
# start the motion detection processes
|
# Start the shared tflite process
|
||||||
for motion_process in motion_processes:
|
tflite_process = EdgeTPUProcess()
|
||||||
motion_process.start()
|
|
||||||
print("motion_process pid ", motion_process.pid)
|
# start the camera processes
|
||||||
|
camera_processes = {}
|
||||||
|
for name, config in CONFIG['cameras'].items():
|
||||||
|
camera_processes[name] = {
|
||||||
|
'fps': mp.Value('d', float(config['fps'])),
|
||||||
|
'skipped_fps': mp.Value('d', 0.0),
|
||||||
|
'detection_fps': mp.Value('d', 0.0)
|
||||||
|
}
|
||||||
|
camera_process = mp.Process(target=track_camera, args=(name, config, FFMPEG_DEFAULT_CONFIG, GLOBAL_OBJECT_CONFIG,
|
||||||
|
tflite_process.detect_lock, tflite_process.detect_ready, tflite_process.frame_ready, tracked_objects_queue,
|
||||||
|
camera_processes[name]['fps'], camera_processes[name]['skipped_fps'], camera_processes[name]['detection_fps']))
|
||||||
|
camera_process.daemon = True
|
||||||
|
camera_processes[name]['process'] = camera_process
|
||||||
|
|
||||||
|
for name, camera_process in camera_processes.items():
|
||||||
|
camera_process['process'].start()
|
||||||
|
print(f"Camera_process started for {name}: {camera_process['process'].pid}")
|
||||||
|
|
||||||
|
object_processor = TrackedObjectProcessor(CONFIG['cameras'], client, MQTT_TOPIC_PREFIX, tracked_objects_queue)
|
||||||
|
object_processor.start()
|
||||||
|
|
||||||
|
camera_watchdog = CameraWatchdog(camera_processes, CONFIG['cameras'], tflite_process, tracked_objects_queue, object_processor)
|
||||||
|
camera_watchdog.start()
|
||||||
|
|
||||||
# create a flask app that encodes frames a mjpeg on demand
|
# create a flask app that encodes frames a mjpeg on demand
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
log = logging.getLogger('werkzeug')
|
||||||
@app.route('/best_person.jpg')
|
log.setLevel(logging.ERROR)
|
||||||
def best_person():
|
|
||||||
frame = np.zeros(frame_shape, np.uint8) if best_person_frame.best_frame is None else best_person_frame.best_frame
|
|
||||||
ret, jpg = cv2.imencode('.jpg', frame)
|
|
||||||
response = make_response(jpg.tobytes())
|
|
||||||
response.headers['Content-Type'] = 'image/jpg'
|
|
||||||
return response
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def ishealthy():
|
||||||
# return a multipart response
|
# return a healh
|
||||||
return Response(imagestream(),
|
return "Frigate is running. Alive and healthy!"
|
||||||
mimetype='multipart/x-mixed-replace; boundary=frame')
|
|
||||||
def imagestream():
|
@app.route('/debug/stats')
|
||||||
|
def stats():
|
||||||
|
stats = {}
|
||||||
|
|
||||||
|
total_detection_fps = 0
|
||||||
|
|
||||||
|
for name, camera_stats in camera_processes.items():
|
||||||
|
total_detection_fps += camera_stats['detection_fps'].value
|
||||||
|
stats[name] = {
|
||||||
|
'fps': camera_stats['fps'].value,
|
||||||
|
'skipped_fps': camera_stats['skipped_fps'].value,
|
||||||
|
'detection_fps': camera_stats['detection_fps'].value
|
||||||
|
}
|
||||||
|
|
||||||
|
stats['coral'] = {
|
||||||
|
'fps': total_detection_fps,
|
||||||
|
'inference_speed': round(tflite_process.avg_inference_speed.value*1000, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = plasma_process.poll()
|
||||||
|
stats['plasma_store_rc'] = rc
|
||||||
|
|
||||||
|
stats['tracked_objects_queue'] = tracked_objects_queue.qsize()
|
||||||
|
|
||||||
|
return jsonify(stats)
|
||||||
|
|
||||||
|
@app.route('/<camera_name>/<label>/best.jpg')
|
||||||
|
def best(camera_name, label):
|
||||||
|
if camera_name in CONFIG['cameras']:
|
||||||
|
best_frame = object_processor.get_best(camera_name, label)
|
||||||
|
if best_frame is None:
|
||||||
|
best_frame = np.zeros((720,1280,3), np.uint8)
|
||||||
|
best_frame = cv2.cvtColor(best_frame, cv2.COLOR_RGB2BGR)
|
||||||
|
ret, jpg = cv2.imencode('.jpg', best_frame)
|
||||||
|
response = make_response(jpg.tobytes())
|
||||||
|
response.headers['Content-Type'] = 'image/jpg'
|
||||||
|
return response
|
||||||
|
else:
|
||||||
|
return "Camera named {} not found".format(camera_name), 404
|
||||||
|
|
||||||
|
@app.route('/<camera_name>')
|
||||||
|
def mjpeg_feed(camera_name):
|
||||||
|
if camera_name in CONFIG['cameras']:
|
||||||
|
# return a multipart response
|
||||||
|
return Response(imagestream(camera_name),
|
||||||
|
mimetype='multipart/x-mixed-replace; boundary=frame')
|
||||||
|
else:
|
||||||
|
return "Camera named {} not found".format(camera_name), 404
|
||||||
|
|
||||||
|
def imagestream(camera_name):
|
||||||
while True:
|
while True:
|
||||||
# max out at 5 FPS
|
# max out at 1 FPS
|
||||||
time.sleep(0.2)
|
time.sleep(1)
|
||||||
# make a copy of the current detected objects
|
frame = object_processor.get_current_frame(camera_name)
|
||||||
detected_objects = DETECTED_OBJECTS.copy()
|
if frame is None:
|
||||||
# lock and make a copy of the current frame
|
frame = np.zeros((720,1280,3), np.uint8)
|
||||||
with frame_lock:
|
|
||||||
frame = frame_arr.copy()
|
|
||||||
# convert to RGB for drawing
|
|
||||||
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
||||||
# draw the bounding boxes on the screen
|
|
||||||
for obj in detected_objects:
|
|
||||||
vis_util.draw_bounding_box_on_image_array(frame,
|
|
||||||
obj['ymin'],
|
|
||||||
obj['xmin'],
|
|
||||||
obj['ymax'],
|
|
||||||
obj['xmax'],
|
|
||||||
color='red',
|
|
||||||
thickness=2,
|
|
||||||
display_str_list=["{}: {}%".format(obj['name'],int(obj['score']*100))],
|
|
||||||
use_normalized_coordinates=False)
|
|
||||||
|
|
||||||
for region in regions:
|
|
||||||
color = (255,255,255)
|
|
||||||
if region['motion_detected'].is_set():
|
|
||||||
color = (0,255,0)
|
|
||||||
cv2.rectangle(frame, (region['x_offset'], region['y_offset']),
|
|
||||||
(region['x_offset']+region['size'], region['y_offset']+region['size']),
|
|
||||||
color, 2)
|
|
||||||
|
|
||||||
# convert back to BGR
|
|
||||||
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
||||||
# encode the image into a jpg
|
|
||||||
ret, jpg = cv2.imencode('.jpg', frame)
|
ret, jpg = cv2.imencode('.jpg', frame)
|
||||||
yield (b'--frame\r\n'
|
yield (b'--frame\r\n'
|
||||||
b'Content-Type: image/jpeg\r\n\r\n' + jpg.tobytes() + b'\r\n\r\n')
|
b'Content-Type: image/jpeg\r\n\r\n' + jpg.tobytes() + b'\r\n\r\n')
|
||||||
|
|
||||||
app.run(host='0.0.0.0', debug=False)
|
app.run(host='0.0.0.0', port=WEB_PORT, debug=False)
|
||||||
|
|
||||||
capture_process.join()
|
camera_watchdog.join()
|
||||||
for detection_process in detection_processes:
|
|
||||||
detection_process.join()
|
plasma_process.terminate()
|
||||||
for motion_process in motion_processes:
|
|
||||||
motion_process.join()
|
|
||||||
frame_tracker.join()
|
|
||||||
best_person_frame.join()
|
|
||||||
object_parser.join()
|
|
||||||
object_cleaner.join()
|
|
||||||
mqtt_publisher.join()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
BIN
diagram.png
BIN
diagram.png
Binary file not shown.
|
Before Width: | Height: | Size: 308 KiB After Width: | Height: | Size: 132 KiB |
74
docs/DEVICES.md
Normal file
74
docs/DEVICES.md
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
# Configuration Examples
|
||||||
|
|
||||||
|
### Default (most RTSP cameras)
|
||||||
|
This is the default ffmpeg command and should work with most RTSP cameras that send h264 video
|
||||||
|
```yaml
|
||||||
|
ffmpeg:
|
||||||
|
global_args:
|
||||||
|
- -hide_banner
|
||||||
|
- -loglevel
|
||||||
|
- panic
|
||||||
|
hwaccel_args: []
|
||||||
|
input_args:
|
||||||
|
- -avoid_negative_ts
|
||||||
|
- make_zero
|
||||||
|
- -fflags
|
||||||
|
- nobuffer
|
||||||
|
- -flags
|
||||||
|
- low_delay
|
||||||
|
- -strict
|
||||||
|
- experimental
|
||||||
|
- -fflags
|
||||||
|
- +genpts+discardcorrupt
|
||||||
|
- -vsync
|
||||||
|
- drop
|
||||||
|
- -rtsp_transport
|
||||||
|
- tcp
|
||||||
|
- -stimeout
|
||||||
|
- '5000000'
|
||||||
|
- -use_wallclock_as_timestamps
|
||||||
|
- '1'
|
||||||
|
output_args:
|
||||||
|
- -vf
|
||||||
|
- mpdecimate
|
||||||
|
- -f
|
||||||
|
- rawvideo
|
||||||
|
- -pix_fmt
|
||||||
|
- rgb24
|
||||||
|
```
|
||||||
|
|
||||||
|
### RTMP Cameras
|
||||||
|
The input parameters need to be adjusted for RTMP cameras
|
||||||
|
```yaml
|
||||||
|
ffmpeg:
|
||||||
|
input_args:
|
||||||
|
- -avoid_negative_ts
|
||||||
|
- make_zero
|
||||||
|
- -fflags
|
||||||
|
- nobuffer
|
||||||
|
- -flags
|
||||||
|
- low_delay
|
||||||
|
- -strict
|
||||||
|
- experimental
|
||||||
|
- -fflags
|
||||||
|
- +genpts+discardcorrupt
|
||||||
|
- -vsync
|
||||||
|
- drop
|
||||||
|
- -use_wallclock_as_timestamps
|
||||||
|
- '1'
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Hardware Acceleration
|
||||||
|
|
||||||
|
Intel Quicksync
|
||||||
|
```yaml
|
||||||
|
ffmpeg:
|
||||||
|
hwaccel_args:
|
||||||
|
- -hwaccel
|
||||||
|
- vaapi
|
||||||
|
- -hwaccel_device
|
||||||
|
- /dev/dri/renderD128
|
||||||
|
- -hwaccel_output_format
|
||||||
|
- yuv420p
|
||||||
|
```
|
||||||
136
frigate/edgetpu.py
Normal file
136
frigate/edgetpu.py
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
import os
|
||||||
|
import datetime
|
||||||
|
import multiprocessing as mp
|
||||||
|
import numpy as np
|
||||||
|
import SharedArray as sa
|
||||||
|
import tflite_runtime.interpreter as tflite
|
||||||
|
from tflite_runtime.interpreter import load_delegate
|
||||||
|
from frigate.util import EventsPerSecond
|
||||||
|
|
||||||
|
def load_labels(path, encoding='utf-8'):
|
||||||
|
"""Loads labels from file (with or without index numbers).
|
||||||
|
Args:
|
||||||
|
path: path to label file.
|
||||||
|
encoding: label file encoding.
|
||||||
|
Returns:
|
||||||
|
Dictionary mapping indices to labels.
|
||||||
|
"""
|
||||||
|
with open(path, 'r', encoding=encoding) as f:
|
||||||
|
lines = f.readlines()
|
||||||
|
if not lines:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
if lines[0].split(' ', maxsplit=1)[0].isdigit():
|
||||||
|
pairs = [line.split(' ', maxsplit=1) for line in lines]
|
||||||
|
return {int(index): label.strip() for index, label in pairs}
|
||||||
|
else:
|
||||||
|
return {index: line.strip() for index, line in enumerate(lines)}
|
||||||
|
|
||||||
|
class ObjectDetector():
|
||||||
|
def __init__(self):
|
||||||
|
edge_tpu_delegate = None
|
||||||
|
try:
|
||||||
|
edge_tpu_delegate = load_delegate('libedgetpu.so.1.0')
|
||||||
|
except ValueError:
|
||||||
|
print("No EdgeTPU detected. Falling back to CPU.")
|
||||||
|
|
||||||
|
if edge_tpu_delegate is None:
|
||||||
|
self.interpreter = tflite.Interpreter(
|
||||||
|
model_path='/cpu_model.tflite')
|
||||||
|
else:
|
||||||
|
self.interpreter = tflite.Interpreter(
|
||||||
|
model_path='/edgetpu_model.tflite',
|
||||||
|
experimental_delegates=[edge_tpu_delegate])
|
||||||
|
|
||||||
|
self.interpreter.allocate_tensors()
|
||||||
|
|
||||||
|
self.tensor_input_details = self.interpreter.get_input_details()
|
||||||
|
self.tensor_output_details = self.interpreter.get_output_details()
|
||||||
|
|
||||||
|
def detect_raw(self, tensor_input):
|
||||||
|
self.interpreter.set_tensor(self.tensor_input_details[0]['index'], tensor_input)
|
||||||
|
self.interpreter.invoke()
|
||||||
|
boxes = np.squeeze(self.interpreter.get_tensor(self.tensor_output_details[0]['index']))
|
||||||
|
label_codes = np.squeeze(self.interpreter.get_tensor(self.tensor_output_details[1]['index']))
|
||||||
|
scores = np.squeeze(self.interpreter.get_tensor(self.tensor_output_details[2]['index']))
|
||||||
|
|
||||||
|
detections = np.zeros((20,6), np.float32)
|
||||||
|
for i, score in enumerate(scores):
|
||||||
|
detections[i] = [label_codes[i], score, boxes[i][0], boxes[i][1], boxes[i][2], boxes[i][3]]
|
||||||
|
|
||||||
|
return detections
|
||||||
|
|
||||||
|
class EdgeTPUProcess():
|
||||||
|
def __init__(self):
|
||||||
|
# TODO: see if we can use the plasma store with a queue and maintain the same speeds
|
||||||
|
try:
|
||||||
|
sa.delete("frame")
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
sa.delete("detections")
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.input_frame = sa.create("frame", shape=(1,300,300,3), dtype=np.uint8)
|
||||||
|
self.detections = sa.create("detections", shape=(20,6), dtype=np.float32)
|
||||||
|
|
||||||
|
self.detect_lock = mp.Lock()
|
||||||
|
self.detect_ready = mp.Event()
|
||||||
|
self.frame_ready = mp.Event()
|
||||||
|
self.avg_inference_speed = mp.Value('d', 0.01)
|
||||||
|
|
||||||
|
def run_detector(detect_ready, frame_ready, avg_speed):
|
||||||
|
print(f"Starting detection process: {os.getpid()}")
|
||||||
|
object_detector = ObjectDetector()
|
||||||
|
input_frame = sa.attach("frame")
|
||||||
|
detections = sa.attach("detections")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# wait until a frame is ready
|
||||||
|
frame_ready.wait()
|
||||||
|
start = datetime.datetime.now().timestamp()
|
||||||
|
# signal that the process is busy
|
||||||
|
frame_ready.clear()
|
||||||
|
detections[:] = object_detector.detect_raw(input_frame)
|
||||||
|
# signal that the process is ready to detect
|
||||||
|
detect_ready.set()
|
||||||
|
duration = datetime.datetime.now().timestamp()-start
|
||||||
|
avg_speed.value = (avg_speed.value*9 + duration)/10
|
||||||
|
|
||||||
|
self.detect_process = mp.Process(target=run_detector, args=(self.detect_ready, self.frame_ready, self.avg_inference_speed))
|
||||||
|
self.detect_process.daemon = True
|
||||||
|
self.detect_process.start()
|
||||||
|
|
||||||
|
class RemoteObjectDetector():
|
||||||
|
def __init__(self, labels, detect_lock, detect_ready, frame_ready):
|
||||||
|
self.labels = load_labels(labels)
|
||||||
|
|
||||||
|
self.input_frame = sa.attach("frame")
|
||||||
|
self.detections = sa.attach("detections")
|
||||||
|
|
||||||
|
self.fps = EventsPerSecond()
|
||||||
|
|
||||||
|
self.detect_lock = detect_lock
|
||||||
|
self.detect_ready = detect_ready
|
||||||
|
self.frame_ready = frame_ready
|
||||||
|
|
||||||
|
def detect(self, tensor_input, threshold=.4):
|
||||||
|
detections = []
|
||||||
|
with self.detect_lock:
|
||||||
|
self.input_frame[:] = tensor_input
|
||||||
|
# unset detections and signal that a frame is ready
|
||||||
|
self.detect_ready.clear()
|
||||||
|
self.frame_ready.set()
|
||||||
|
# wait until the detection process is finished,
|
||||||
|
self.detect_ready.wait()
|
||||||
|
for d in self.detections:
|
||||||
|
if d[1] < threshold:
|
||||||
|
break
|
||||||
|
detections.append((
|
||||||
|
self.labels[int(d[0])],
|
||||||
|
float(d[1]),
|
||||||
|
(d[2], d[3], d[4], d[5])
|
||||||
|
))
|
||||||
|
self.fps.update()
|
||||||
|
return detections
|
||||||
@@ -1,109 +1,79 @@
|
|||||||
import datetime
|
|
||||||
import numpy as np
|
|
||||||
import cv2
|
import cv2
|
||||||
import imutils
|
import imutils
|
||||||
from . util import tonumpyarray
|
import numpy as np
|
||||||
|
|
||||||
# do the actual motion detection
|
class MotionDetector():
|
||||||
def detect_motion(shared_arr, shared_frame_time, frame_lock, frame_ready, motion_detected, motion_changed,
|
def __init__(self, frame_shape, mask, resize_factor=4):
|
||||||
frame_shape, region_size, region_x_offset, region_y_offset, min_motion_area, mask, debug):
|
self.resize_factor = resize_factor
|
||||||
# shape shared input array into frame for processing
|
self.motion_frame_size = (int(frame_shape[0]/resize_factor), int(frame_shape[1]/resize_factor))
|
||||||
arr = tonumpyarray(shared_arr).reshape(frame_shape)
|
self.avg_frame = np.zeros(self.motion_frame_size, np.float)
|
||||||
|
self.avg_delta = np.zeros(self.motion_frame_size, np.float)
|
||||||
|
self.motion_frame_count = 0
|
||||||
|
self.frame_counter = 0
|
||||||
|
resized_mask = cv2.resize(mask, dsize=(self.motion_frame_size[1], self.motion_frame_size[0]), interpolation=cv2.INTER_LINEAR)
|
||||||
|
self.mask = np.where(resized_mask==[0])
|
||||||
|
|
||||||
avg_frame = None
|
def detect(self, frame):
|
||||||
avg_delta = None
|
motion_boxes = []
|
||||||
frame_time = 0.0
|
|
||||||
motion_frames = 0
|
# resize frame
|
||||||
while True:
|
resized_frame = cv2.resize(frame, dsize=(self.motion_frame_size[1], self.motion_frame_size[0]), interpolation=cv2.INTER_LINEAR)
|
||||||
now = datetime.datetime.now().timestamp()
|
|
||||||
|
|
||||||
with frame_ready:
|
|
||||||
# if there isnt a frame ready for processing or it is old, wait for a signal
|
|
||||||
if shared_frame_time.value == frame_time or (now - shared_frame_time.value) > 0.5:
|
|
||||||
frame_ready.wait()
|
|
||||||
|
|
||||||
# lock and make a copy of the cropped frame
|
|
||||||
with frame_lock:
|
|
||||||
cropped_frame = arr[region_y_offset:region_y_offset+region_size, region_x_offset:region_x_offset+region_size].copy().astype('uint8')
|
|
||||||
frame_time = shared_frame_time.value
|
|
||||||
|
|
||||||
# convert to grayscale
|
# convert to grayscale
|
||||||
gray = cv2.cvtColor(cropped_frame, cv2.COLOR_BGR2GRAY)
|
gray = cv2.cvtColor(resized_frame, cv2.COLOR_BGR2GRAY)
|
||||||
|
|
||||||
# apply image mask to remove areas from motion detection
|
# mask frame
|
||||||
gray[mask] = [255]
|
gray[self.mask] = [255]
|
||||||
|
|
||||||
# apply gaussian blur
|
# it takes ~30 frames to establish a baseline
|
||||||
gray = cv2.GaussianBlur(gray, (21, 21), 0)
|
# dont bother looking for motion
|
||||||
|
if self.frame_counter < 30:
|
||||||
|
self.frame_counter += 1
|
||||||
|
else:
|
||||||
|
# compare to average
|
||||||
|
frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(self.avg_frame))
|
||||||
|
|
||||||
if avg_frame is None:
|
# compute the average delta over the past few frames
|
||||||
avg_frame = gray.copy().astype("float")
|
# the alpha value can be modified to configure how sensitive the motion detection is.
|
||||||
continue
|
# 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
|
||||||
# look at the delta from the avg_frame
|
# this also assumes that a person is in the same location across more than a single frame
|
||||||
frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(avg_frame))
|
cv2.accumulateWeighted(frameDelta, self.avg_delta, 0.2)
|
||||||
|
|
||||||
if avg_delta is None:
|
|
||||||
avg_delta = frameDelta.copy().astype("float")
|
|
||||||
|
|
||||||
# compute the average delta over the past few frames
|
# compute the threshold image for the current frame
|
||||||
# the alpha value can be modified to configure how sensitive the motion detection is.
|
current_thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
|
||||||
# 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
|
|
||||||
# this also assumes that a person is in the same location across more than a single frame
|
|
||||||
cv2.accumulateWeighted(frameDelta, avg_delta, 0.2)
|
|
||||||
|
|
||||||
# compute the threshold image for the current frame
|
# black out everything in the avg_delta where there isnt motion in the current frame
|
||||||
current_thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
|
avg_delta_image = cv2.convertScaleAbs(self.avg_delta)
|
||||||
|
avg_delta_image[np.where(current_thresh==[0])] = [0]
|
||||||
|
|
||||||
# black out everything in the avg_delta where there isnt motion in the current frame
|
# then look for deltas above the threshold, but only in areas where there is a delta
|
||||||
avg_delta_image = cv2.convertScaleAbs(avg_delta)
|
# in the current frame. this prevents deltas from previous frames from being included
|
||||||
avg_delta_image[np.where(current_thresh==[0])] = [0]
|
thresh = cv2.threshold(avg_delta_image, 25, 255, cv2.THRESH_BINARY)[1]
|
||||||
|
|
||||||
# then look for deltas above the threshold, but only in areas where there is a delta
|
# dilate the thresholded image to fill in holes, then find contours
|
||||||
# in the current frame. this prevents deltas from previous frames from being included
|
# on thresholded image
|
||||||
thresh = cv2.threshold(avg_delta_image, 25, 255, cv2.THRESH_BINARY)[1]
|
thresh = cv2.dilate(thresh, None, iterations=2)
|
||||||
|
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||||
# dilate the thresholded image to fill in holes, then find contours
|
cnts = imutils.grab_contours(cnts)
|
||||||
# on thresholded image
|
|
||||||
thresh = cv2.dilate(thresh, None, iterations=2)
|
|
||||||
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
||||||
cnts = imutils.grab_contours(cnts)
|
|
||||||
|
|
||||||
motion_found = False
|
# loop over the contours
|
||||||
|
for c in cnts:
|
||||||
# loop over the contours
|
# if the contour is big enough, count it as motion
|
||||||
for c in cnts:
|
contour_area = cv2.contourArea(c)
|
||||||
# if the contour is big enough, count it as motion
|
if contour_area > 100:
|
||||||
contour_area = cv2.contourArea(c)
|
|
||||||
if contour_area > min_motion_area:
|
|
||||||
motion_found = True
|
|
||||||
if debug:
|
|
||||||
cv2.drawContours(cropped_frame, [c], -1, (0, 255, 0), 2)
|
|
||||||
x, y, w, h = cv2.boundingRect(c)
|
x, y, w, h = cv2.boundingRect(c)
|
||||||
cv2.putText(cropped_frame, str(contour_area), (x, y),
|
motion_boxes.append((x*self.resize_factor, y*self.resize_factor, (x+w)*self.resize_factor, (y+h)*self.resize_factor))
|
||||||
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 100, 0), 2)
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
if motion_found:
|
if len(motion_boxes) > 0:
|
||||||
motion_frames += 1
|
self.motion_frame_count += 1
|
||||||
# if there have been enough consecutive motion frames, report motion
|
# TODO: this really depends on FPS
|
||||||
if motion_frames >= 3:
|
if self.motion_frame_count >= 10:
|
||||||
# only average in the current frame if the difference persists for at least 3 frames
|
# only average in the current frame if the difference persists for at least 3 frames
|
||||||
cv2.accumulateWeighted(gray, avg_frame, 0.01)
|
cv2.accumulateWeighted(gray, self.avg_frame, 0.2)
|
||||||
motion_detected.set()
|
|
||||||
with motion_changed:
|
|
||||||
motion_changed.notify_all()
|
|
||||||
else:
|
else:
|
||||||
# when no motion, just keep averaging the frames together
|
# when no motion, just keep averaging the frames together
|
||||||
cv2.accumulateWeighted(gray, avg_frame, 0.01)
|
cv2.accumulateWeighted(gray, self.avg_frame, 0.2)
|
||||||
motion_frames = 0
|
self.motion_frame_count = 0
|
||||||
if motion_detected.is_set():
|
|
||||||
motion_detected.clear()
|
|
||||||
with motion_changed:
|
|
||||||
motion_changed.notify_all()
|
|
||||||
|
|
||||||
if debug and motion_frames == 3:
|
return motion_boxes
|
||||||
cv2.imwrite("/lab/debug/motion-{}-{}-{}.jpg".format(region_x_offset, region_y_offset, datetime.datetime.now().timestamp()), cropped_frame)
|
|
||||||
cv2.imwrite("/lab/debug/avg_delta-{}-{}-{}.jpg".format(region_x_offset, region_y_offset, datetime.datetime.now().timestamp()), avg_delta_image)
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
import json
|
|
||||||
import threading
|
|
||||||
|
|
||||||
class MqttMotionPublisher(threading.Thread):
|
|
||||||
def __init__(self, client, topic_prefix, motion_changed, motion_flags):
|
|
||||||
threading.Thread.__init__(self)
|
|
||||||
self.client = client
|
|
||||||
self.topic_prefix = topic_prefix
|
|
||||||
self.motion_changed = motion_changed
|
|
||||||
self.motion_flags = motion_flags
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
last_sent_motion = ""
|
|
||||||
while True:
|
|
||||||
with self.motion_changed:
|
|
||||||
self.motion_changed.wait()
|
|
||||||
|
|
||||||
# send message for motion
|
|
||||||
motion_status = 'OFF'
|
|
||||||
if any(obj.is_set() for obj in self.motion_flags):
|
|
||||||
motion_status = 'ON'
|
|
||||||
|
|
||||||
if last_sent_motion != motion_status:
|
|
||||||
last_sent_motion = motion_status
|
|
||||||
self.client.publish(self.topic_prefix+'/motion', motion_status, retain=False)
|
|
||||||
|
|
||||||
class MqttObjectPublisher(threading.Thread):
|
|
||||||
def __init__(self, client, topic_prefix, objects_parsed, detected_objects):
|
|
||||||
threading.Thread.__init__(self)
|
|
||||||
self.client = client
|
|
||||||
self.topic_prefix = topic_prefix
|
|
||||||
self.objects_parsed = objects_parsed
|
|
||||||
self._detected_objects = detected_objects
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
last_sent_payload = ""
|
|
||||||
while True:
|
|
||||||
|
|
||||||
# initialize the payload
|
|
||||||
payload = {}
|
|
||||||
|
|
||||||
# wait until objects have been parsed
|
|
||||||
with self.objects_parsed:
|
|
||||||
self.objects_parsed.wait()
|
|
||||||
|
|
||||||
# add all the person scores in detected objects and
|
|
||||||
# average over past 1 seconds (5fps)
|
|
||||||
detected_objects = self._detected_objects.copy()
|
|
||||||
avg_person_score = sum([obj['score'] for obj in detected_objects if obj['name'] == 'person'])/5
|
|
||||||
payload['person'] = int(avg_person_score*100)
|
|
||||||
|
|
||||||
# send message for objects if different
|
|
||||||
new_payload = json.dumps(payload, sort_keys=True)
|
|
||||||
if new_payload != last_sent_payload:
|
|
||||||
last_sent_payload = new_payload
|
|
||||||
self.client.publish(self.topic_prefix+'/objects', new_payload, retain=False)
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
import datetime
|
|
||||||
import cv2
|
|
||||||
import numpy as np
|
|
||||||
import tensorflow as tf
|
|
||||||
from object_detection.utils import label_map_util
|
|
||||||
from object_detection.utils import visualization_utils as vis_util
|
|
||||||
from . util import tonumpyarray
|
|
||||||
|
|
||||||
# TODO: make dynamic?
|
|
||||||
NUM_CLASSES = 90
|
|
||||||
# Path to frozen detection graph. This is the actual model that is used for the object detection.
|
|
||||||
PATH_TO_CKPT = '/frozen_inference_graph.pb'
|
|
||||||
# List of the strings that is used to add correct label for each box.
|
|
||||||
PATH_TO_LABELS = '/label_map.pbtext'
|
|
||||||
|
|
||||||
# Loading label map
|
|
||||||
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
|
|
||||||
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
|
|
||||||
use_display_name=True)
|
|
||||||
category_index = label_map_util.create_category_index(categories)
|
|
||||||
|
|
||||||
# do the actual object detection
|
|
||||||
def tf_detect_objects(cropped_frame, sess, detection_graph, region_size, region_x_offset, region_y_offset, debug):
|
|
||||||
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
|
|
||||||
image_np_expanded = np.expand_dims(cropped_frame, axis=0)
|
|
||||||
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
|
|
||||||
|
|
||||||
# Each box represents a part of the image where a particular object was detected.
|
|
||||||
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
|
|
||||||
|
|
||||||
# Each score represent how level of confidence for each of the objects.
|
|
||||||
# Score is shown on the result image, together with the class label.
|
|
||||||
scores = detection_graph.get_tensor_by_name('detection_scores:0')
|
|
||||||
classes = detection_graph.get_tensor_by_name('detection_classes:0')
|
|
||||||
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
|
|
||||||
|
|
||||||
# Actual detection.
|
|
||||||
(boxes, scores, classes, num_detections) = sess.run(
|
|
||||||
[boxes, scores, classes, num_detections],
|
|
||||||
feed_dict={image_tensor: image_np_expanded})
|
|
||||||
|
|
||||||
if debug:
|
|
||||||
if len([value for index,value in enumerate(classes[0]) if str(category_index.get(value).get('name')) == 'person' and scores[0,index] > 0.5]) > 0:
|
|
||||||
vis_util.visualize_boxes_and_labels_on_image_array(
|
|
||||||
cropped_frame,
|
|
||||||
np.squeeze(boxes),
|
|
||||||
np.squeeze(classes).astype(np.int32),
|
|
||||||
np.squeeze(scores),
|
|
||||||
category_index,
|
|
||||||
use_normalized_coordinates=True,
|
|
||||||
line_thickness=4)
|
|
||||||
cv2.imwrite("/lab/debug/obj-{}-{}-{}.jpg".format(region_x_offset, region_y_offset, datetime.datetime.now().timestamp()), cropped_frame)
|
|
||||||
|
|
||||||
|
|
||||||
# build an array of detected objects
|
|
||||||
objects = []
|
|
||||||
for index, value in enumerate(classes[0]):
|
|
||||||
score = scores[0, index]
|
|
||||||
if score > 0.5:
|
|
||||||
box = boxes[0, index].tolist()
|
|
||||||
objects.append({
|
|
||||||
'name': str(category_index.get(value).get('name')),
|
|
||||||
'score': float(score),
|
|
||||||
'ymin': int((box[0] * region_size) + region_y_offset),
|
|
||||||
'xmin': int((box[1] * region_size) + region_x_offset),
|
|
||||||
'ymax': int((box[2] * region_size) + region_y_offset),
|
|
||||||
'xmax': int((box[3] * region_size) + region_x_offset)
|
|
||||||
})
|
|
||||||
|
|
||||||
return objects
|
|
||||||
|
|
||||||
def detect_objects(shared_arr, object_queue, shared_frame_time, frame_lock, frame_ready,
|
|
||||||
motion_detected, frame_shape, region_size, region_x_offset, region_y_offset,
|
|
||||||
min_person_area, debug):
|
|
||||||
# shape shared input array into frame for processing
|
|
||||||
arr = tonumpyarray(shared_arr).reshape(frame_shape)
|
|
||||||
|
|
||||||
# Load a (frozen) Tensorflow model into memory before the processing loop
|
|
||||||
detection_graph = tf.Graph()
|
|
||||||
with detection_graph.as_default():
|
|
||||||
od_graph_def = tf.GraphDef()
|
|
||||||
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
|
|
||||||
serialized_graph = fid.read()
|
|
||||||
od_graph_def.ParseFromString(serialized_graph)
|
|
||||||
tf.import_graph_def(od_graph_def, name='')
|
|
||||||
sess = tf.Session(graph=detection_graph)
|
|
||||||
|
|
||||||
frame_time = 0.0
|
|
||||||
while True:
|
|
||||||
now = datetime.datetime.now().timestamp()
|
|
||||||
|
|
||||||
# wait until motion is detected
|
|
||||||
motion_detected.wait()
|
|
||||||
|
|
||||||
with frame_ready:
|
|
||||||
# if there isnt a frame ready for processing or it is old, wait for a new frame
|
|
||||||
if shared_frame_time.value == frame_time or (now - shared_frame_time.value) > 0.5:
|
|
||||||
frame_ready.wait()
|
|
||||||
|
|
||||||
# make a copy of the cropped frame
|
|
||||||
with frame_lock:
|
|
||||||
cropped_frame = arr[region_y_offset:region_y_offset+region_size, region_x_offset:region_x_offset+region_size].copy()
|
|
||||||
frame_time = shared_frame_time.value
|
|
||||||
|
|
||||||
# convert to RGB
|
|
||||||
cropped_frame_rgb = cv2.cvtColor(cropped_frame, cv2.COLOR_BGR2RGB)
|
|
||||||
# do the object detection
|
|
||||||
objects = tf_detect_objects(cropped_frame_rgb, sess, detection_graph, region_size, region_x_offset, region_y_offset, debug)
|
|
||||||
for obj in objects:
|
|
||||||
# ignore persons below the size threshold
|
|
||||||
if obj['name'] == 'person' and (obj['xmax']-obj['xmin'])*(obj['ymax']-obj['ymin']) < min_person_area:
|
|
||||||
continue
|
|
||||||
obj['frame_time'] = frame_time
|
|
||||||
object_queue.put(obj)
|
|
||||||
154
frigate/object_processing.py
Normal file
154
frigate/object_processing.py
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
import json
|
||||||
|
import hashlib
|
||||||
|
import datetime
|
||||||
|
import copy
|
||||||
|
import cv2
|
||||||
|
import threading
|
||||||
|
import numpy as np
|
||||||
|
from collections import Counter, defaultdict
|
||||||
|
import itertools
|
||||||
|
import pyarrow.plasma as plasma
|
||||||
|
import SharedArray as sa
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from frigate.util import draw_box_with_label
|
||||||
|
from frigate.edgetpu import load_labels
|
||||||
|
|
||||||
|
PATH_TO_LABELS = '/labelmap.txt'
|
||||||
|
|
||||||
|
LABELS = load_labels(PATH_TO_LABELS)
|
||||||
|
cmap = plt.cm.get_cmap('tab10', len(LABELS.keys()))
|
||||||
|
|
||||||
|
COLOR_MAP = {}
|
||||||
|
for key, val in LABELS.items():
|
||||||
|
COLOR_MAP[val] = tuple(int(round(255 * c)) for c in cmap(key)[:3])
|
||||||
|
|
||||||
|
class TrackedObjectProcessor(threading.Thread):
|
||||||
|
def __init__(self, config, client, topic_prefix, tracked_objects_queue):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
self.config = config
|
||||||
|
self.client = client
|
||||||
|
self.topic_prefix = topic_prefix
|
||||||
|
self.tracked_objects_queue = tracked_objects_queue
|
||||||
|
self.plasma_client = plasma.connect("/tmp/plasma")
|
||||||
|
self.camera_data = defaultdict(lambda: {
|
||||||
|
'best_objects': {},
|
||||||
|
'object_status': defaultdict(lambda: defaultdict(lambda: 'OFF')),
|
||||||
|
'tracked_objects': {},
|
||||||
|
'current_frame_time': None,
|
||||||
|
'current_frame': np.zeros((720,1280,3), np.uint8),
|
||||||
|
'object_id': None
|
||||||
|
})
|
||||||
|
|
||||||
|
def get_best(self, camera, label):
|
||||||
|
if label in self.camera_data[camera]['best_objects']:
|
||||||
|
return self.camera_data[camera]['best_objects'][label]['frame']
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_current_frame(self, camera):
|
||||||
|
return self.camera_data[camera]['current_frame']
|
||||||
|
|
||||||
|
def get_current_frame_time(self, camera):
|
||||||
|
return self.camera_data[camera]['current_frame_time']
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while True:
|
||||||
|
camera, frame_time, tracked_objects = self.tracked_objects_queue.get()
|
||||||
|
|
||||||
|
config = self.config[camera]
|
||||||
|
best_objects = self.camera_data[camera]['best_objects']
|
||||||
|
current_object_status = self.camera_data[camera]['object_status']
|
||||||
|
self.camera_data[camera]['tracked_objects'] = tracked_objects
|
||||||
|
|
||||||
|
###
|
||||||
|
# Draw tracked objects on the frame
|
||||||
|
###
|
||||||
|
object_id_hash = hashlib.sha1(str.encode(f"{camera}{frame_time}"))
|
||||||
|
object_id_bytes = object_id_hash.digest()
|
||||||
|
object_id = plasma.ObjectID(object_id_bytes)
|
||||||
|
current_frame = self.plasma_client.get(object_id, timeout_ms=0)
|
||||||
|
|
||||||
|
if not current_frame is plasma.ObjectNotAvailable:
|
||||||
|
# draw the bounding boxes on the frame
|
||||||
|
for obj in tracked_objects.values():
|
||||||
|
thickness = 2
|
||||||
|
color = COLOR_MAP[obj['label']]
|
||||||
|
|
||||||
|
if obj['frame_time'] != frame_time:
|
||||||
|
thickness = 1
|
||||||
|
color = (255,0,0)
|
||||||
|
|
||||||
|
# draw the bounding boxes on the frame
|
||||||
|
box = obj['box']
|
||||||
|
draw_box_with_label(current_frame, box[0], box[1], box[2], box[3], obj['label'], f"{int(obj['score']*100)}% {int(obj['area'])}", thickness=thickness, color=color)
|
||||||
|
# draw the regions on the frame
|
||||||
|
region = obj['region']
|
||||||
|
cv2.rectangle(current_frame, (region[0], region[1]), (region[2], region[3]), (0,255,0), 1)
|
||||||
|
|
||||||
|
if config['snapshots']['show_timestamp']:
|
||||||
|
time_to_show = datetime.datetime.fromtimestamp(frame_time).strftime("%m/%d/%Y %H:%M:%S")
|
||||||
|
cv2.putText(current_frame, time_to_show, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, fontScale=.8, color=(255, 255, 255), thickness=2)
|
||||||
|
|
||||||
|
###
|
||||||
|
# Set the current frame as ready
|
||||||
|
###
|
||||||
|
self.camera_data[camera]['current_frame'] = current_frame
|
||||||
|
self.camera_data[camera]['current_frame_time'] = frame_time
|
||||||
|
|
||||||
|
# store the object id, so you can delete it at the next loop
|
||||||
|
previous_object_id = self.camera_data[camera]['object_id']
|
||||||
|
if not previous_object_id is None:
|
||||||
|
self.plasma_client.delete([previous_object_id])
|
||||||
|
self.camera_data[camera]['object_id'] = object_id
|
||||||
|
|
||||||
|
###
|
||||||
|
# Maintain the highest scoring recent object and frame for each label
|
||||||
|
###
|
||||||
|
for obj in tracked_objects.values():
|
||||||
|
# if the object wasn't seen on the current frame, skip it
|
||||||
|
if obj['frame_time'] != frame_time:
|
||||||
|
continue
|
||||||
|
if obj['label'] in best_objects:
|
||||||
|
now = datetime.datetime.now().timestamp()
|
||||||
|
# if the object is a higher score than the current best score
|
||||||
|
# or the current object is more than 1 minute old, use the new object
|
||||||
|
if obj['score'] > best_objects[obj['label']]['score'] or (now - best_objects[obj['label']]['frame_time']) > 60:
|
||||||
|
obj['frame'] = np.copy(self.camera_data[camera]['current_frame'])
|
||||||
|
best_objects[obj['label']] = obj
|
||||||
|
else:
|
||||||
|
obj['frame'] = np.copy(self.camera_data[camera]['current_frame'])
|
||||||
|
best_objects[obj['label']] = obj
|
||||||
|
|
||||||
|
###
|
||||||
|
# Report over MQTT
|
||||||
|
###
|
||||||
|
# count objects with more than 2 entries in history by type
|
||||||
|
obj_counter = Counter()
|
||||||
|
for obj in tracked_objects.values():
|
||||||
|
if len(obj['history']) > 1:
|
||||||
|
obj_counter[obj['label']] += 1
|
||||||
|
|
||||||
|
# report on detected objects
|
||||||
|
for obj_name, count in obj_counter.items():
|
||||||
|
new_status = 'ON' if count > 0 else 'OFF'
|
||||||
|
if new_status != current_object_status[obj_name]:
|
||||||
|
current_object_status[obj_name] = new_status
|
||||||
|
self.client.publish(f"{self.topic_prefix}/{camera}/{obj_name}", new_status, retain=False)
|
||||||
|
# send the best snapshot over mqtt
|
||||||
|
best_frame = cv2.cvtColor(best_objects[obj_name]['frame'], cv2.COLOR_RGB2BGR)
|
||||||
|
ret, jpg = cv2.imencode('.jpg', best_frame)
|
||||||
|
if ret:
|
||||||
|
jpg_bytes = jpg.tobytes()
|
||||||
|
self.client.publish(f"{self.topic_prefix}/{camera}/{obj_name}/snapshot", jpg_bytes, retain=True)
|
||||||
|
|
||||||
|
# expire any objects that are ON and no longer detected
|
||||||
|
expired_objects = [obj_name for obj_name, status in current_object_status.items() if status == 'ON' and not obj_name in obj_counter]
|
||||||
|
for obj_name in expired_objects:
|
||||||
|
current_object_status[obj_name] = 'OFF'
|
||||||
|
self.client.publish(f"{self.topic_prefix}/{camera}/{obj_name}", 'OFF', retain=False)
|
||||||
|
# send updated snapshot over mqtt
|
||||||
|
best_frame = cv2.cvtColor(best_objects[obj_name]['frame'], cv2.COLOR_RGB2BGR)
|
||||||
|
ret, jpg = cv2.imencode('.jpg', best_frame)
|
||||||
|
if ret:
|
||||||
|
jpg_bytes = jpg.tobytes()
|
||||||
|
self.client.publish(f"{self.topic_prefix}/{camera}/{obj_name}/snapshot", jpg_bytes, retain=True)
|
||||||
@@ -2,122 +2,158 @@ import time
|
|||||||
import datetime
|
import datetime
|
||||||
import threading
|
import threading
|
||||||
import cv2
|
import cv2
|
||||||
from object_detection.utils import visualization_utils as vis_util
|
import itertools
|
||||||
class ObjectParser(threading.Thread):
|
import copy
|
||||||
def __init__(self, object_queue, objects_parsed, detected_objects):
|
import numpy as np
|
||||||
threading.Thread.__init__(self)
|
import multiprocessing as mp
|
||||||
self._object_queue = object_queue
|
from collections import defaultdict
|
||||||
self._objects_parsed = objects_parsed
|
from scipy.spatial import distance as dist
|
||||||
self._detected_objects = detected_objects
|
from frigate.util import draw_box_with_label, calculate_region
|
||||||
|
|
||||||
def run(self):
|
class ObjectTracker():
|
||||||
while True:
|
def __init__(self, max_disappeared):
|
||||||
obj = self._object_queue.get()
|
self.tracked_objects = {}
|
||||||
self._detected_objects.append(obj)
|
self.disappeared = {}
|
||||||
|
self.max_disappeared = max_disappeared
|
||||||
|
|
||||||
# notify that objects were parsed
|
def register(self, index, obj):
|
||||||
with self._objects_parsed:
|
id = f"{obj['frame_time']}-{index}"
|
||||||
self._objects_parsed.notify_all()
|
obj['id'] = id
|
||||||
|
obj['top_score'] = obj['score']
|
||||||
|
self.add_history(obj)
|
||||||
|
self.tracked_objects[id] = obj
|
||||||
|
self.disappeared[id] = 0
|
||||||
|
|
||||||
class ObjectCleaner(threading.Thread):
|
def deregister(self, id):
|
||||||
def __init__(self, objects_parsed, detected_objects):
|
del self.tracked_objects[id]
|
||||||
threading.Thread.__init__(self)
|
del self.disappeared[id]
|
||||||
self._objects_parsed = objects_parsed
|
|
||||||
self._detected_objects = detected_objects
|
def update(self, id, new_obj):
|
||||||
|
self.disappeared[id] = 0
|
||||||
|
self.tracked_objects[id].update(new_obj)
|
||||||
|
self.add_history(self.tracked_objects[id])
|
||||||
|
if self.tracked_objects[id]['score'] > self.tracked_objects[id]['top_score']:
|
||||||
|
self.tracked_objects[id]['top_score'] = self.tracked_objects[id]['score']
|
||||||
|
|
||||||
|
def add_history(self, obj):
|
||||||
|
entry = {
|
||||||
|
'score': obj['score'],
|
||||||
|
'box': obj['box'],
|
||||||
|
'region': obj['region'],
|
||||||
|
'centroid': obj['centroid'],
|
||||||
|
'frame_time': obj['frame_time']
|
||||||
|
}
|
||||||
|
if 'history' in obj:
|
||||||
|
obj['history'].append(entry)
|
||||||
|
else:
|
||||||
|
obj['history'] = [entry]
|
||||||
|
|
||||||
def run(self):
|
def match_and_update(self, frame_time, new_objects):
|
||||||
while True:
|
# group by name
|
||||||
|
new_object_groups = defaultdict(lambda: [])
|
||||||
|
for obj in new_objects:
|
||||||
|
new_object_groups[obj[0]].append({
|
||||||
|
'label': obj[0],
|
||||||
|
'score': obj[1],
|
||||||
|
'box': obj[2],
|
||||||
|
'area': obj[3],
|
||||||
|
'region': obj[4],
|
||||||
|
'frame_time': frame_time
|
||||||
|
})
|
||||||
|
|
||||||
|
# update any tracked objects with labels that are not
|
||||||
|
# seen in the current objects and deregister if needed
|
||||||
|
for obj in list(self.tracked_objects.values()):
|
||||||
|
if not obj['label'] in new_object_groups:
|
||||||
|
if self.disappeared[obj['id']] >= self.max_disappeared:
|
||||||
|
self.deregister(obj['id'])
|
||||||
|
else:
|
||||||
|
self.disappeared[obj['id']] += 1
|
||||||
|
|
||||||
|
if len(new_objects) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
# track objects for each label type
|
||||||
|
for label, group in new_object_groups.items():
|
||||||
|
current_objects = [o for o in self.tracked_objects.values() if o['label'] == label]
|
||||||
|
current_ids = [o['id'] for o in current_objects]
|
||||||
|
current_centroids = np.array([o['centroid'] for o in current_objects])
|
||||||
|
|
||||||
# expire the objects that are more than 1 second old
|
# compute centroids of new objects
|
||||||
now = datetime.datetime.now().timestamp()
|
for obj in group:
|
||||||
# look for the first object found within the last second
|
centroid_x = int((obj['box'][0]+obj['box'][2]) / 2.0)
|
||||||
# (newest objects are appended to the end)
|
centroid_y = int((obj['box'][1]+obj['box'][3]) / 2.0)
|
||||||
detected_objects = self._detected_objects.copy()
|
obj['centroid'] = (centroid_x, centroid_y)
|
||||||
num_to_delete = 0
|
|
||||||
for obj in detected_objects:
|
|
||||||
if now-obj['frame_time']<1:
|
|
||||||
break
|
|
||||||
num_to_delete += 1
|
|
||||||
if num_to_delete > 0:
|
|
||||||
del self._detected_objects[:num_to_delete]
|
|
||||||
|
|
||||||
# notify that parsed objects were changed
|
if len(current_objects) == 0:
|
||||||
with self._objects_parsed:
|
for index, obj in enumerate(group):
|
||||||
self._objects_parsed.notify_all()
|
self.register(index, obj)
|
||||||
|
return
|
||||||
|
|
||||||
# wait a bit before checking for more expired frames
|
new_centroids = np.array([o['centroid'] for o in group])
|
||||||
time.sleep(0.2)
|
|
||||||
|
|
||||||
# Maintains the frame and person with the highest score from the most recent
|
# compute the distance between each pair of tracked
|
||||||
# motion event
|
# centroids and new centroids, respectively -- our
|
||||||
class BestPersonFrame(threading.Thread):
|
# goal will be to match each new centroid to an existing
|
||||||
def __init__(self, objects_parsed, recent_frames, detected_objects, motion_changed, motion_regions):
|
# object centroid
|
||||||
threading.Thread.__init__(self)
|
D = dist.cdist(current_centroids, new_centroids)
|
||||||
self.objects_parsed = objects_parsed
|
|
||||||
self.recent_frames = recent_frames
|
|
||||||
self.detected_objects = detected_objects
|
|
||||||
self.motion_changed = motion_changed
|
|
||||||
self.motion_regions = motion_regions
|
|
||||||
self.best_person = None
|
|
||||||
self.best_frame = None
|
|
||||||
|
|
||||||
def run(self):
|
# in order to perform this matching we must (1) find the
|
||||||
motion_start = 0.0
|
# smallest value in each row and then (2) sort the row
|
||||||
motion_end = 0.0
|
# indexes based on their minimum values so that the row
|
||||||
|
# with the smallest value is at the *front* of the index
|
||||||
|
# list
|
||||||
|
rows = D.min(axis=1).argsort()
|
||||||
|
|
||||||
while True:
|
# next, we perform a similar process on the columns by
|
||||||
|
# finding the smallest value in each column and then
|
||||||
|
# sorting using the previously computed row index list
|
||||||
|
cols = D.argmin(axis=1)[rows]
|
||||||
|
|
||||||
# while there is motion
|
# in order to determine if we need to update, register,
|
||||||
while len([r for r in self.motion_regions if r.is_set()]) > 0:
|
# or deregister an object we need to keep track of which
|
||||||
# wait until objects have been parsed
|
# of the rows and column indexes we have already examined
|
||||||
with self.objects_parsed:
|
usedRows = set()
|
||||||
self.objects_parsed.wait()
|
usedCols = set()
|
||||||
|
|
||||||
# make a copy of detected objects
|
# loop over the combination of the (row, column) index
|
||||||
detected_objects = self.detected_objects.copy()
|
# tuples
|
||||||
detected_people = [obj for obj in detected_objects if obj['name'] == 'person']
|
for (row, col) in zip(rows, cols):
|
||||||
# make a copy of the recent frames
|
# if we have already examined either the row or
|
||||||
recent_frames = self.recent_frames.copy()
|
# column value before, ignore it
|
||||||
|
if row in usedRows or col in usedCols:
|
||||||
# get the highest scoring person
|
|
||||||
new_best_person = max(detected_people, key=lambda x:x['score'], default=self.best_person)
|
|
||||||
|
|
||||||
# if there isnt a person, continue
|
|
||||||
if new_best_person is None:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# if there is no current best_person
|
# otherwise, grab the object ID for the current row,
|
||||||
if self.best_person is None:
|
# set its new centroid, and reset the disappeared
|
||||||
self.best_person = new_best_person
|
# counter
|
||||||
# if there is already a best_person
|
objectID = current_ids[row]
|
||||||
else:
|
self.update(objectID, group[col])
|
||||||
now = datetime.datetime.now().timestamp()
|
|
||||||
# if the new best person is a higher score than the current best person
|
|
||||||
# or the current person is more than 1 minute old, use the new best person
|
|
||||||
if new_best_person['score'] > self.best_person['score'] or (now - self.best_person['frame_time']) > 60:
|
|
||||||
self.best_person = new_best_person
|
|
||||||
|
|
||||||
if not self.best_person is None and self.best_person['frame_time'] in recent_frames:
|
# indicate that we have examined each of the row and
|
||||||
best_frame = recent_frames[self.best_person['frame_time']]
|
# column indexes, respectively
|
||||||
best_frame = cv2.cvtColor(best_frame, cv2.COLOR_BGR2RGB)
|
usedRows.add(row)
|
||||||
# draw the bounding box on the frame
|
usedCols.add(col)
|
||||||
vis_util.draw_bounding_box_on_image_array(best_frame,
|
|
||||||
self.best_person['ymin'],
|
|
||||||
self.best_person['xmin'],
|
|
||||||
self.best_person['ymax'],
|
|
||||||
self.best_person['xmax'],
|
|
||||||
color='red',
|
|
||||||
thickness=2,
|
|
||||||
display_str_list=["{}: {}%".format(self.best_person['name'],int(self.best_person['score']*100))],
|
|
||||||
use_normalized_coordinates=False)
|
|
||||||
|
|
||||||
# convert back to BGR
|
# compute the column index we have NOT yet examined
|
||||||
self.best_frame = cv2.cvtColor(best_frame, cv2.COLOR_RGB2BGR)
|
unusedRows = set(range(0, D.shape[0])).difference(usedRows)
|
||||||
|
unusedCols = set(range(0, D.shape[1])).difference(usedCols)
|
||||||
|
|
||||||
motion_end = datetime.datetime.now().timestamp()
|
# in the event that the number of object centroids is
|
||||||
|
# equal or greater than the number of input centroids
|
||||||
|
# we need to check and see if some of these objects have
|
||||||
|
# potentially disappeared
|
||||||
|
if D.shape[0] >= D.shape[1]:
|
||||||
|
for row in unusedRows:
|
||||||
|
id = current_ids[row]
|
||||||
|
|
||||||
# wait for the global motion flag to change
|
if self.disappeared[id] >= self.max_disappeared:
|
||||||
with self.motion_changed:
|
self.deregister(id)
|
||||||
self.motion_changed.wait()
|
else:
|
||||||
|
self.disappeared[id] += 1
|
||||||
motion_start = datetime.datetime.now().timestamp()
|
# if the number of input centroids is greater
|
||||||
|
# than the number of existing object centroids we need to
|
||||||
|
# register each new input centroid as a trackable object
|
||||||
|
else:
|
||||||
|
for col in unusedCols:
|
||||||
|
self.register(col, group[col])
|
||||||
|
|||||||
130
frigate/util.py
Normal file → Executable file
130
frigate/util.py
Normal file → Executable file
@@ -1,5 +1,129 @@
|
|||||||
|
import datetime
|
||||||
|
import collections
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import cv2
|
||||||
|
import threading
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
# convert shared memory array into numpy array
|
def draw_box_with_label(frame, x_min, y_min, x_max, y_max, label, info, thickness=2, color=None, position='ul'):
|
||||||
def tonumpyarray(mp_arr):
|
if color is None:
|
||||||
return np.frombuffer(mp_arr.get_obj(), dtype=np.uint16)
|
color = (0,0,255)
|
||||||
|
display_text = "{}: {}".format(label, info)
|
||||||
|
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, thickness)
|
||||||
|
font_scale = 0.5
|
||||||
|
font = cv2.FONT_HERSHEY_SIMPLEX
|
||||||
|
# get the width and height of the text box
|
||||||
|
size = cv2.getTextSize(display_text, font, fontScale=font_scale, thickness=2)
|
||||||
|
text_width = size[0][0]
|
||||||
|
text_height = size[0][1]
|
||||||
|
line_height = text_height + size[1]
|
||||||
|
# set the text start position
|
||||||
|
if position == 'ul':
|
||||||
|
text_offset_x = x_min
|
||||||
|
text_offset_y = 0 if y_min < line_height else y_min - (line_height+8)
|
||||||
|
elif position == 'ur':
|
||||||
|
text_offset_x = x_max - (text_width+8)
|
||||||
|
text_offset_y = 0 if y_min < line_height else y_min - (line_height+8)
|
||||||
|
elif position == 'bl':
|
||||||
|
text_offset_x = x_min
|
||||||
|
text_offset_y = y_max
|
||||||
|
elif position == 'br':
|
||||||
|
text_offset_x = x_max - (text_width+8)
|
||||||
|
text_offset_y = y_max
|
||||||
|
# make the coords of the box with a small padding of two pixels
|
||||||
|
textbox_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y + line_height))
|
||||||
|
cv2.rectangle(frame, textbox_coords[0], textbox_coords[1], color, cv2.FILLED)
|
||||||
|
cv2.putText(frame, display_text, (text_offset_x, text_offset_y + line_height - 3), font, fontScale=font_scale, color=(0, 0, 0), thickness=2)
|
||||||
|
|
||||||
|
def calculate_region(frame_shape, xmin, ymin, xmax, ymax, multiplier=2):
|
||||||
|
# size is larger than longest edge
|
||||||
|
size = int(max(xmax-xmin, ymax-ymin)*multiplier)
|
||||||
|
# if the size is too big to fit in the frame
|
||||||
|
if size > min(frame_shape[0], frame_shape[1]):
|
||||||
|
size = min(frame_shape[0], frame_shape[1])
|
||||||
|
|
||||||
|
# x_offset is midpoint of bounding box minus half the size
|
||||||
|
x_offset = int((xmax-xmin)/2.0+xmin-size/2.0)
|
||||||
|
# if outside the image
|
||||||
|
if x_offset < 0:
|
||||||
|
x_offset = 0
|
||||||
|
elif x_offset > (frame_shape[1]-size):
|
||||||
|
x_offset = (frame_shape[1]-size)
|
||||||
|
|
||||||
|
# y_offset is midpoint of bounding box minus half the size
|
||||||
|
y_offset = int((ymax-ymin)/2.0+ymin-size/2.0)
|
||||||
|
# if outside the image
|
||||||
|
if y_offset < 0:
|
||||||
|
y_offset = 0
|
||||||
|
elif y_offset > (frame_shape[0]-size):
|
||||||
|
y_offset = (frame_shape[0]-size)
|
||||||
|
|
||||||
|
return (x_offset, y_offset, x_offset+size, y_offset+size)
|
||||||
|
|
||||||
|
def intersection(box_a, box_b):
|
||||||
|
return (
|
||||||
|
max(box_a[0], box_b[0]),
|
||||||
|
max(box_a[1], box_b[1]),
|
||||||
|
min(box_a[2], box_b[2]),
|
||||||
|
min(box_a[3], box_b[3])
|
||||||
|
)
|
||||||
|
|
||||||
|
def area(box):
|
||||||
|
return (box[2]-box[0] + 1)*(box[3]-box[1] + 1)
|
||||||
|
|
||||||
|
def intersection_over_union(box_a, box_b):
|
||||||
|
# determine the (x, y)-coordinates of the intersection rectangle
|
||||||
|
intersect = intersection(box_a, box_b)
|
||||||
|
|
||||||
|
# compute the area of intersection rectangle
|
||||||
|
inter_area = max(0, intersect[2] - intersect[0] + 1) * max(0, intersect[3] - intersect[1] + 1)
|
||||||
|
|
||||||
|
if inter_area == 0:
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
# compute the area of both the prediction and ground-truth
|
||||||
|
# rectangles
|
||||||
|
box_a_area = (box_a[2] - box_a[0] + 1) * (box_a[3] - box_a[1] + 1)
|
||||||
|
box_b_area = (box_b[2] - box_b[0] + 1) * (box_b[3] - box_b[1] + 1)
|
||||||
|
|
||||||
|
# compute the intersection over union by taking the intersection
|
||||||
|
# area and dividing it by the sum of prediction + ground-truth
|
||||||
|
# areas - the interesection area
|
||||||
|
iou = inter_area / float(box_a_area + box_b_area - inter_area)
|
||||||
|
|
||||||
|
# return the intersection over union value
|
||||||
|
return iou
|
||||||
|
|
||||||
|
def clipped(obj, frame_shape):
|
||||||
|
# if the object is within 5 pixels of the region border, and the region is not on the edge
|
||||||
|
# consider the object to be clipped
|
||||||
|
box = obj[2]
|
||||||
|
region = obj[4]
|
||||||
|
if ((region[0] > 5 and box[0]-region[0] <= 5) or
|
||||||
|
(region[1] > 5 and box[1]-region[1] <= 5) or
|
||||||
|
(frame_shape[1]-region[2] > 5 and region[2]-box[2] <= 5) or
|
||||||
|
(frame_shape[0]-region[3] > 5 and region[3]-box[3] <= 5)):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
class EventsPerSecond:
|
||||||
|
def __init__(self, max_events=1000):
|
||||||
|
self._start = None
|
||||||
|
self._max_events = max_events
|
||||||
|
self._timestamps = []
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self._start = datetime.datetime.now().timestamp()
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self._timestamps.append(datetime.datetime.now().timestamp())
|
||||||
|
# truncate the list when it goes 100 over the max_size
|
||||||
|
if len(self._timestamps) > self._max_events+100:
|
||||||
|
self._timestamps = self._timestamps[(1-self._max_events):]
|
||||||
|
|
||||||
|
def eps(self, last_n_seconds=10):
|
||||||
|
# compute the (approximate) events in the last n seconds
|
||||||
|
now = datetime.datetime.now().timestamp()
|
||||||
|
seconds = min(now-self._start, last_n_seconds)
|
||||||
|
return len([t for t in self._timestamps if t > (now-last_n_seconds)]) / seconds
|
||||||
|
|||||||
422
frigate/video.py
Normal file → Executable file
422
frigate/video.py
Normal file → Executable file
@@ -1,95 +1,355 @@
|
|||||||
|
import os
|
||||||
import time
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
import cv2
|
import cv2
|
||||||
|
import queue
|
||||||
import threading
|
import threading
|
||||||
from . util import tonumpyarray
|
import ctypes
|
||||||
|
import multiprocessing as mp
|
||||||
|
import subprocess as sp
|
||||||
|
import numpy as np
|
||||||
|
import hashlib
|
||||||
|
import pyarrow.plasma as plasma
|
||||||
|
import SharedArray as sa
|
||||||
|
import copy
|
||||||
|
import itertools
|
||||||
|
import json
|
||||||
|
from collections import defaultdict
|
||||||
|
from frigate.util import draw_box_with_label, area, calculate_region, clipped, intersection_over_union, intersection, EventsPerSecond
|
||||||
|
from frigate.objects import ObjectTracker
|
||||||
|
from frigate.edgetpu import RemoteObjectDetector
|
||||||
|
from frigate.motion import MotionDetector
|
||||||
|
|
||||||
# fetch the frames as fast a possible, only decoding the frames when the
|
def get_frame_shape(source):
|
||||||
# detection_process has consumed the current frame
|
ffprobe_cmd = " ".join([
|
||||||
def fetch_frames(shared_arr, shared_frame_time, frame_lock, frame_ready, frame_shape, rtsp_url):
|
'ffprobe',
|
||||||
# convert shared memory array into numpy and shape into image array
|
'-v',
|
||||||
arr = tonumpyarray(shared_arr).reshape(frame_shape)
|
'panic',
|
||||||
|
'-show_error',
|
||||||
|
'-show_streams',
|
||||||
|
'-of',
|
||||||
|
'json',
|
||||||
|
'"'+source+'"'
|
||||||
|
])
|
||||||
|
print(ffprobe_cmd)
|
||||||
|
p = sp.Popen(ffprobe_cmd, stdout=sp.PIPE, shell=True)
|
||||||
|
(output, err) = p.communicate()
|
||||||
|
p_status = p.wait()
|
||||||
|
info = json.loads(output)
|
||||||
|
print(info)
|
||||||
|
|
||||||
# start the video capture
|
video_info = [s for s in info['streams'] if s['codec_type'] == 'video'][0]
|
||||||
video = cv2.VideoCapture()
|
|
||||||
video.open(rtsp_url)
|
|
||||||
# keep the buffer small so we minimize old data
|
|
||||||
video.set(cv2.CAP_PROP_BUFFERSIZE,1)
|
|
||||||
|
|
||||||
bad_frame_counter = 0
|
if video_info['height'] != 0 and video_info['width'] != 0:
|
||||||
while True:
|
return (video_info['height'], video_info['width'], 3)
|
||||||
# check if the video stream is still open, and reopen if needed
|
|
||||||
if not video.isOpened():
|
|
||||||
success = video.open(rtsp_url)
|
|
||||||
if not success:
|
|
||||||
time.sleep(1)
|
|
||||||
continue
|
|
||||||
# grab the frame, but dont decode it yet
|
|
||||||
ret = video.grab()
|
|
||||||
# snapshot the time the frame was grabbed
|
|
||||||
frame_time = datetime.datetime.now()
|
|
||||||
if ret:
|
|
||||||
# go ahead and decode the current frame
|
|
||||||
ret, frame = video.retrieve()
|
|
||||||
if ret:
|
|
||||||
# Lock access and update frame
|
|
||||||
with frame_lock:
|
|
||||||
arr[:] = frame
|
|
||||||
shared_frame_time.value = frame_time.timestamp()
|
|
||||||
# Notify with the condition that a new frame is ready
|
|
||||||
with frame_ready:
|
|
||||||
frame_ready.notify_all()
|
|
||||||
bad_frame_counter = 0
|
|
||||||
else:
|
|
||||||
print("Unable to decode frame")
|
|
||||||
bad_frame_counter += 1
|
|
||||||
else:
|
|
||||||
print("Unable to grab a frame")
|
|
||||||
bad_frame_counter += 1
|
|
||||||
|
|
||||||
if bad_frame_counter > 100:
|
|
||||||
video.release()
|
|
||||||
|
|
||||||
|
# fallback to using opencv if ffprobe didnt succeed
|
||||||
|
video = cv2.VideoCapture(source)
|
||||||
|
ret, frame = video.read()
|
||||||
|
frame_shape = frame.shape
|
||||||
video.release()
|
video.release()
|
||||||
|
return frame_shape
|
||||||
|
|
||||||
# Stores 2 seconds worth of frames when motion is detected so they can be used for other threads
|
def get_ffmpeg_input(ffmpeg_input):
|
||||||
class FrameTracker(threading.Thread):
|
frigate_vars = {k: v for k, v in os.environ.items() if k.startswith('FRIGATE_')}
|
||||||
def __init__(self, shared_frame, frame_time, frame_ready, frame_lock, recent_frames, motion_changed, motion_regions):
|
return ffmpeg_input.format(**frigate_vars)
|
||||||
threading.Thread.__init__(self)
|
|
||||||
self.shared_frame = shared_frame
|
|
||||||
self.frame_time = frame_time
|
|
||||||
self.frame_ready = frame_ready
|
|
||||||
self.frame_lock = frame_lock
|
|
||||||
self.recent_frames = recent_frames
|
|
||||||
self.motion_changed = motion_changed
|
|
||||||
self.motion_regions = motion_regions
|
|
||||||
|
|
||||||
def run(self):
|
def filtered(obj, objects_to_track, object_filters, mask):
|
||||||
frame_time = 0.0
|
object_name = obj[0]
|
||||||
|
|
||||||
|
if not object_name in objects_to_track:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if object_name in object_filters:
|
||||||
|
obj_settings = object_filters[object_name]
|
||||||
|
|
||||||
|
# if the min area is larger than the
|
||||||
|
# detected object, don't add it to detected objects
|
||||||
|
if obj_settings.get('min_area',-1) > obj[3]:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# if the detected object is larger than the
|
||||||
|
# max area, don't add it to detected objects
|
||||||
|
if obj_settings.get('max_area', 24000000) < obj[3]:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# if the score is lower than the threshold, skip
|
||||||
|
if obj_settings.get('threshold', 0) > obj[1]:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# compute the coordinates of the object and make sure
|
||||||
|
# the location isnt outside the bounds of the image (can happen from rounding)
|
||||||
|
y_location = min(int(obj[2][3]), len(mask)-1)
|
||||||
|
x_location = min(int((obj[2][2]-obj[2][0])/2.0)+obj[2][0], len(mask[0])-1)
|
||||||
|
|
||||||
|
# if the object is in a masked location, don't add it to detected objects
|
||||||
|
if mask[y_location][x_location] == [0]:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def create_tensor_input(frame, region):
|
||||||
|
cropped_frame = frame[region[1]:region[3], region[0]:region[2]]
|
||||||
|
|
||||||
|
# Resize to 300x300 if needed
|
||||||
|
if cropped_frame.shape != (300, 300, 3):
|
||||||
|
cropped_frame = cv2.resize(cropped_frame, dsize=(300, 300), interpolation=cv2.INTER_LINEAR)
|
||||||
|
|
||||||
|
# Expand dimensions since the model expects images to have shape: [1, 300, 300, 3]
|
||||||
|
return np.expand_dims(cropped_frame, axis=0)
|
||||||
|
|
||||||
|
def track_camera(name, config, ffmpeg_global_config, global_objects_config, detect_lock, detect_ready, frame_ready, detected_objects_queue, fps, skipped_fps, detection_fps):
|
||||||
|
print(f"Starting process for {name}: {os.getpid()}")
|
||||||
|
|
||||||
|
# Merge the ffmpeg config with the global config
|
||||||
|
ffmpeg = config.get('ffmpeg', {})
|
||||||
|
ffmpeg_input = get_ffmpeg_input(ffmpeg['input'])
|
||||||
|
ffmpeg_global_args = ffmpeg.get('global_args', ffmpeg_global_config['global_args'])
|
||||||
|
ffmpeg_hwaccel_args = ffmpeg.get('hwaccel_args', ffmpeg_global_config['hwaccel_args'])
|
||||||
|
ffmpeg_input_args = ffmpeg.get('input_args', ffmpeg_global_config['input_args'])
|
||||||
|
ffmpeg_output_args = ffmpeg.get('output_args', ffmpeg_global_config['output_args'])
|
||||||
|
|
||||||
|
# Merge the tracked object config with the global config
|
||||||
|
camera_objects_config = config.get('objects', {})
|
||||||
|
# combine tracked objects lists
|
||||||
|
objects_to_track = set().union(global_objects_config.get('track', ['person', 'car', 'truck']), camera_objects_config.get('track', []))
|
||||||
|
# merge object filters
|
||||||
|
global_object_filters = global_objects_config.get('filters', {})
|
||||||
|
camera_object_filters = camera_objects_config.get('filters', {})
|
||||||
|
objects_with_config = set().union(global_object_filters.keys(), camera_object_filters.keys())
|
||||||
|
object_filters = {}
|
||||||
|
for obj in objects_with_config:
|
||||||
|
object_filters[obj] = {**global_object_filters.get(obj, {}), **camera_object_filters.get(obj, {})}
|
||||||
|
|
||||||
|
expected_fps = config['fps']
|
||||||
|
take_frame = config.get('take_frame', 1)
|
||||||
|
|
||||||
|
if 'width' in config and 'height' in config:
|
||||||
|
frame_shape = (config['height'], config['width'], 3)
|
||||||
|
else:
|
||||||
|
frame_shape = get_frame_shape(ffmpeg_input)
|
||||||
|
|
||||||
|
frame_size = frame_shape[0] * frame_shape[1] * frame_shape[2]
|
||||||
|
|
||||||
|
try:
|
||||||
|
sa.delete(name)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
frame = sa.create(name, shape=frame_shape, dtype=np.uint8)
|
||||||
|
|
||||||
|
# load in the mask for object detection
|
||||||
|
if 'mask' in config:
|
||||||
|
mask = cv2.imread("/config/{}".format(config['mask']), cv2.IMREAD_GRAYSCALE)
|
||||||
|
else:
|
||||||
|
mask = None
|
||||||
|
|
||||||
|
if mask is None:
|
||||||
|
mask = np.zeros((frame_shape[0], frame_shape[1], 1), np.uint8)
|
||||||
|
mask[:] = 255
|
||||||
|
|
||||||
|
motion_detector = MotionDetector(frame_shape, mask, resize_factor=6)
|
||||||
|
object_detector = RemoteObjectDetector('/labelmap.txt', detect_lock, detect_ready, frame_ready)
|
||||||
|
|
||||||
|
object_tracker = ObjectTracker(10)
|
||||||
|
|
||||||
|
ffmpeg_cmd = (['ffmpeg'] +
|
||||||
|
ffmpeg_global_args +
|
||||||
|
ffmpeg_hwaccel_args +
|
||||||
|
ffmpeg_input_args +
|
||||||
|
['-i', ffmpeg_input] +
|
||||||
|
ffmpeg_output_args +
|
||||||
|
['pipe:'])
|
||||||
|
|
||||||
|
print(" ".join(ffmpeg_cmd))
|
||||||
|
|
||||||
|
ffmpeg_process = sp.Popen(ffmpeg_cmd, stdout = sp.PIPE, bufsize=frame_size)
|
||||||
|
|
||||||
|
plasma_client = plasma.connect("/tmp/plasma")
|
||||||
|
frame_num = 0
|
||||||
|
avg_wait = 0.0
|
||||||
|
fps_tracker = EventsPerSecond()
|
||||||
|
skipped_fps_tracker = EventsPerSecond()
|
||||||
|
fps_tracker.start()
|
||||||
|
skipped_fps_tracker.start()
|
||||||
|
object_detector.fps.start()
|
||||||
|
while True:
|
||||||
|
start = datetime.datetime.now().timestamp()
|
||||||
|
frame_bytes = ffmpeg_process.stdout.read(frame_size)
|
||||||
|
duration = datetime.datetime.now().timestamp()-start
|
||||||
|
avg_wait = (avg_wait*99+duration)/100
|
||||||
|
|
||||||
|
if not frame_bytes:
|
||||||
|
break
|
||||||
|
|
||||||
|
# limit frame rate
|
||||||
|
frame_num += 1
|
||||||
|
if (frame_num % take_frame) != 0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
fps_tracker.update()
|
||||||
|
fps.value = fps_tracker.eps()
|
||||||
|
detection_fps.value = object_detector.fps.eps()
|
||||||
|
|
||||||
|
frame_time = datetime.datetime.now().timestamp()
|
||||||
|
|
||||||
|
# Store frame in numpy array
|
||||||
|
frame[:] = (np
|
||||||
|
.frombuffer(frame_bytes, np.uint8)
|
||||||
|
.reshape(frame_shape))
|
||||||
|
|
||||||
|
# look for motion
|
||||||
|
motion_boxes = motion_detector.detect(frame)
|
||||||
|
|
||||||
|
# skip object detection if we are below the min_fps and wait time is less than half the average
|
||||||
|
if frame_num > 100 and fps.value < expected_fps-1 and duration < 0.5*avg_wait:
|
||||||
|
skipped_fps_tracker.update()
|
||||||
|
skipped_fps.value = skipped_fps_tracker.eps()
|
||||||
|
continue
|
||||||
|
|
||||||
|
skipped_fps.value = skipped_fps_tracker.eps()
|
||||||
|
|
||||||
|
tracked_objects = object_tracker.tracked_objects.values()
|
||||||
|
|
||||||
|
# merge areas of motion that intersect with a known tracked object into a single area to look at
|
||||||
|
areas_of_interest = []
|
||||||
|
used_motion_boxes = []
|
||||||
|
for obj in tracked_objects:
|
||||||
|
x_min, y_min, x_max, y_max = obj['box']
|
||||||
|
for m_index, motion_box in enumerate(motion_boxes):
|
||||||
|
if area(intersection(obj['box'], motion_box))/area(motion_box) > .5:
|
||||||
|
used_motion_boxes.append(m_index)
|
||||||
|
x_min = min(obj['box'][0], motion_box[0])
|
||||||
|
y_min = min(obj['box'][1], motion_box[1])
|
||||||
|
x_max = max(obj['box'][2], motion_box[2])
|
||||||
|
y_max = max(obj['box'][3], motion_box[3])
|
||||||
|
areas_of_interest.append((x_min, y_min, x_max, y_max))
|
||||||
|
unused_motion_boxes = set(range(0, len(motion_boxes))).difference(used_motion_boxes)
|
||||||
|
|
||||||
|
# compute motion regions
|
||||||
|
motion_regions = [calculate_region(frame_shape, motion_boxes[i][0], motion_boxes[i][1], motion_boxes[i][2], motion_boxes[i][3], 1.2)
|
||||||
|
for i in unused_motion_boxes]
|
||||||
|
|
||||||
|
# compute tracked object regions
|
||||||
|
object_regions = [calculate_region(frame_shape, a[0], a[1], a[2], a[3], 1.2)
|
||||||
|
for a in areas_of_interest]
|
||||||
|
|
||||||
|
# merge regions with high IOU
|
||||||
|
merged_regions = motion_regions+object_regions
|
||||||
while True:
|
while True:
|
||||||
# while there is motion
|
max_iou = 0.0
|
||||||
while len([r for r in self.motion_regions if r.is_set()]) > 0:
|
max_indices = None
|
||||||
now = datetime.datetime.now().timestamp()
|
region_indices = range(len(merged_regions))
|
||||||
# wait for a frame
|
for a, b in itertools.combinations(region_indices, 2):
|
||||||
with self.frame_ready:
|
iou = intersection_over_union(merged_regions[a], merged_regions[b])
|
||||||
# if there isnt a frame ready for processing or it is old, wait for a signal
|
if iou > max_iou:
|
||||||
if self.frame_time.value == frame_time or (now - self.frame_time.value) > 0.5:
|
max_iou = iou
|
||||||
self.frame_ready.wait()
|
max_indices = (a, b)
|
||||||
|
if max_iou > 0.1:
|
||||||
# lock and make a copy of the frame
|
a = merged_regions[max_indices[0]]
|
||||||
with self.frame_lock:
|
b = merged_regions[max_indices[1]]
|
||||||
frame = self.shared_frame.copy().astype('uint8')
|
merged_regions.append(calculate_region(frame_shape,
|
||||||
frame_time = self.frame_time.value
|
min(a[0], b[0]),
|
||||||
|
min(a[1], b[1]),
|
||||||
# add the frame to recent frames
|
max(a[2], b[2]),
|
||||||
self.recent_frames[frame_time] = frame
|
max(a[3], b[3]),
|
||||||
|
1
|
||||||
|
))
|
||||||
|
del merged_regions[max(max_indices[0], max_indices[1])]
|
||||||
|
del merged_regions[min(max_indices[0], max_indices[1])]
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
# delete any old frames
|
# resize regions and detect
|
||||||
stored_frame_times = list(self.recent_frames.keys())
|
detections = []
|
||||||
for k in stored_frame_times:
|
for region in merged_regions:
|
||||||
if (now - k) > 2:
|
|
||||||
del self.recent_frames[k]
|
tensor_input = create_tensor_input(frame, region)
|
||||||
|
|
||||||
|
region_detections = object_detector.detect(tensor_input)
|
||||||
|
|
||||||
|
for d in region_detections:
|
||||||
|
box = d[2]
|
||||||
|
size = region[2]-region[0]
|
||||||
|
x_min = int((box[1] * size) + region[0])
|
||||||
|
y_min = int((box[0] * size) + region[1])
|
||||||
|
x_max = int((box[3] * size) + region[0])
|
||||||
|
y_max = int((box[2] * size) + region[1])
|
||||||
|
det = (d[0],
|
||||||
|
d[1],
|
||||||
|
(x_min, y_min, x_max, y_max),
|
||||||
|
(x_max-x_min)*(y_max-y_min),
|
||||||
|
region)
|
||||||
|
if filtered(det, objects_to_track, object_filters, mask):
|
||||||
|
continue
|
||||||
|
detections.append(det)
|
||||||
|
|
||||||
|
#########
|
||||||
|
# merge objects, check for clipped objects and look again up to N times
|
||||||
|
#########
|
||||||
|
refining = True
|
||||||
|
refine_count = 0
|
||||||
|
while refining and refine_count < 4:
|
||||||
|
refining = False
|
||||||
|
|
||||||
|
# group by name
|
||||||
|
detected_object_groups = defaultdict(lambda: [])
|
||||||
|
for detection in detections:
|
||||||
|
detected_object_groups[detection[0]].append(detection)
|
||||||
|
|
||||||
|
selected_objects = []
|
||||||
|
for group in detected_object_groups.values():
|
||||||
|
|
||||||
|
# apply non-maxima suppression to suppress weak, overlapping bounding boxes
|
||||||
|
boxes = [(o[2][0], o[2][1], o[2][2]-o[2][0], o[2][3]-o[2][1])
|
||||||
|
for o in group]
|
||||||
|
confidences = [o[1] for o in group]
|
||||||
|
idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
|
||||||
|
|
||||||
|
for index in idxs:
|
||||||
|
obj = group[index[0]]
|
||||||
|
if clipped(obj, frame_shape): #obj['clipped']:
|
||||||
|
box = obj[2]
|
||||||
|
# calculate a new region that will hopefully get the entire object
|
||||||
|
region = calculate_region(frame_shape,
|
||||||
|
box[0], box[1],
|
||||||
|
box[2], box[3])
|
||||||
|
|
||||||
|
tensor_input = create_tensor_input(frame, region)
|
||||||
|
# run detection on new region
|
||||||
|
refined_detections = object_detector.detect(tensor_input)
|
||||||
|
for d in refined_detections:
|
||||||
|
box = d[2]
|
||||||
|
size = region[2]-region[0]
|
||||||
|
x_min = int((box[1] * size) + region[0])
|
||||||
|
y_min = int((box[0] * size) + region[1])
|
||||||
|
x_max = int((box[3] * size) + region[0])
|
||||||
|
y_max = int((box[2] * size) + region[1])
|
||||||
|
det = (d[0],
|
||||||
|
d[1],
|
||||||
|
(x_min, y_min, x_max, y_max),
|
||||||
|
(x_max-x_min)*(y_max-y_min),
|
||||||
|
region)
|
||||||
|
if filtered(det, objects_to_track, object_filters, mask):
|
||||||
|
continue
|
||||||
|
selected_objects.append(det)
|
||||||
|
|
||||||
|
refining = True
|
||||||
|
else:
|
||||||
|
selected_objects.append(obj)
|
||||||
|
|
||||||
# wait for the global motion flag to change
|
# set the detections list to only include top, complete objects
|
||||||
with self.motion_changed:
|
# and new detections
|
||||||
self.motion_changed.wait()
|
detections = selected_objects
|
||||||
|
|
||||||
|
if refining:
|
||||||
|
refine_count += 1
|
||||||
|
|
||||||
|
# now that we have refined our detections, we need to track objects
|
||||||
|
object_tracker.match_and_update(frame_time, detections)
|
||||||
|
|
||||||
|
# put the frame in the plasma store
|
||||||
|
object_id = hashlib.sha1(str.encode(f"{name}{frame_time}")).digest()
|
||||||
|
plasma_client.put(frame, plasma.ObjectID(object_id))
|
||||||
|
# add to the queue
|
||||||
|
detected_objects_queue.put((name, frame_time, object_tracker.tracked_objects))
|
||||||
|
|||||||
Reference in New Issue
Block a user