2019-12-08 07:03:58 -06:00
# Frigate - Realtime Object Detection for IP Cameras
2019-03-30 07:58:31 -05:00
**Note:** This version requires the use of a [Google Coral USB Accelerator ](https://coral.withgoogle.com/products/accelerator/ )
2019-12-08 07:03:58 -06:00
Uses OpenCV and Tensorflow to perform realtime object detection locally for IP cameras. Designed for integration with HomeAssistant or others via MQTT.
2019-03-03 10:04:27 -06:00
- Leverages multiprocessing and threads heavily with an emphasis on realtime over processing every frame
2019-03-30 07:58:31 -05:00
- Allows you to define specific regions (squares) in the image to look for objects
- No motion detection (for now)
- Object detection with Tensorflow runs in a separate thread
- Object info is published over MQTT for integration into HomeAssistant as a binary sensor
2019-03-03 10:04:27 -06:00
- An endpoint is available to view an MJPEG stream for debugging
![Diagram ](diagram.png )
2019-03-30 07:58:31 -05:00
## Example video (from older version)
2019-03-03 10:04:27 -06:00
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://img.youtube.com/vi/nqHbCtyo4dY/0.jpg )](http://www.youtube.com/watch?v=nqHbCtyo4dY "Frigate")
2019-01-26 08:02:59 -06:00
## Getting Started
Build the container with
```
2019-03-02 15:20:53 -06:00
docker build -t frigate .
2019-01-26 08:02:59 -06:00
```
2019-03-30 07:58:31 -05:00
The `mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite` model is included and used by default. You can use your own model and labels by mounting files in the container at `/frozen_inference_graph.pb` and `/label_map.pbtext` . Models must be compatible with the Coral according to [this ](https://coral.withgoogle.com/models/ ).
2019-01-26 08:02:59 -06:00
Run the container with
```
2019-02-10 14:25:30 -06:00
docker run --rm \
2019-03-30 07:58:31 -05:00
--privileged \
-v /dev/bus/usb:/dev/bus/usb \
2019-03-05 21:42:09 -06:00
-v < path_to_config_dir > :/config:ro \
2019-12-08 08:55:19 -06:00
-v /etc/localtime:/etc/localtime:ro \
2019-01-26 08:02:59 -06:00
-p 5000:5000 \
2019-12-08 07:03:58 -06:00
-e FRIGATE_RTSP_PASSWORD='password' \
2019-03-02 15:20:53 -06:00
frigate:latest
2019-01-26 08:02:59 -06:00
```
2019-03-03 10:04:27 -06:00
Example docker-compose:
2019-02-28 06:49:27 -06:00
```
frigate:
container_name: frigate
restart: unless-stopped
2019-03-30 07:58:31 -05:00
privileged: true
2019-03-02 15:20:53 -06:00
image: frigate:latest
2019-02-28 06:49:27 -06:00
volumes:
2019-03-30 07:58:31 -05:00
- /dev/bus/usb:/dev/bus/usb
2019-12-08 08:55:19 -06:00
- /etc/localtime:/etc/localtime:ro
2019-02-28 06:49:27 -06:00
- < path_to_config > :/config
ports:
2019-03-30 07:58:31 -05:00
- "5000:5000"
2019-02-28 06:49:27 -06:00
environment:
2019-12-08 07:03:58 -06:00
FRIGATE_RTSP_PASSWORD: "password"
2019-02-28 06:49:27 -06:00
```
2019-12-09 01:51:42 -06:00
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 ).
2019-03-05 21:42:09 -06:00
2019-03-30 07:58:31 -05:00
Access the mjpeg stream at `http://localhost:5000/<camera_name>` and the best person snapshot at `http://localhost:5000/<camera_name>/best_person.jpg`
2019-01-26 08:02:59 -06:00
2019-03-03 10:04:27 -06:00
## Integration with HomeAssistant
```
camera:
- name: Camera Last Person
2019-07-14 08:31:21 -05:00
platform: mqtt
topic: frigate/< camera_name > /snapshot
2019-03-03 10:04:27 -06:00
2019-05-10 09:47:40 -05:00
binary_sensor:
2019-03-30 07:58:31 -05:00
- name: Camera Person
2019-03-03 10:04:27 -06:00
platform: mqtt
2019-03-30 07:58:31 -05:00
state_topic: "frigate/< camera_name > /objects"
2019-03-03 10:04:27 -06:00
value_template: '{{ value_json.person }}'
2019-05-10 09:47:40 -05:00
device_class: motion
2019-03-30 07:58:31 -05:00
availability_topic: "frigate/available"
2019-07-14 08:31:21 -05:00
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 > /best_person.jpg
caption: A person was detected.
2019-03-03 10:04:27 -06:00
```
2019-02-04 07:10:42 -06:00
## Tips
2019-12-08 07:03:58 -06:00
- Lower the framerate of the video feed on the camera to reduce the CPU usage for capturing the feed
2019-02-04 07:10:42 -06:00
2019-01-26 08:02:59 -06:00
## Future improvements
2019-03-27 06:55:32 -05:00
- [x] Remove motion detection for now
2019-03-30 07:58:31 -05:00
- [x] Try running object detection in a thread rather than a process
2019-03-27 06:55:32 -05:00
- [x] Implement min person size again
2019-03-30 07:58:31 -05:00
- [x] Switch to a config file
- [x] Handle multiple cameras in the same container
- [ ] Attempt to figure out coral symlinking
- [ ] Add object list to config with min scores for mqtt
- [ ] Move mjpeg encoding to a separate process
- [ ] Simplify motion detection (check entire image against mask, resize instead of gaussian blur)
2019-03-27 06:55:32 -05:00
- [ ] See if motion detection is even worth running
- [ ] Scan for people across entire image rather than specfic regions
- [ ] Dynamically resize detection area and follow people
2019-02-10 14:43:21 -06:00
- [ ] Add ability to turn detection on and off via MQTT
2019-02-25 06:48:31 -06:00
- [ ] Output movie clips of people for notifications, etc.
2019-02-28 06:30:34 -06:00
- [ ] Integrate with homeassistant push camera
2019-02-10 14:25:30 -06:00
- [ ] Merge bounding boxes that span multiple regions
2019-02-25 06:48:31 -06:00
- [ ] Implement mode to save labeled objects for training
2019-02-10 14:25:30 -06:00
- [ ] Try and reduce CPU usage by simplifying the tensorflow model to just include the objects we care about
2019-02-09 07:23:54 -06:00
- [ ] Look into GPU accelerated decoding of RTSP stream
- [ ] Send video over a socket and use JSMPEG
2019-03-27 06:55:32 -05:00
- [x] Look into neural compute stick