Local realtime person detection for RTSP cameras

Stop your frigate container. Run an instance of frigate with like this docker run --privileged --rm -v /dev/bus/usb:/dev/bus/usb blakeblackshear/frigate:0.2.0 python3 -u benchmark.py. Then you can start your other frigate container again.

Great, thanks for that. I couldnā€™t figure out how to get the container running and then run the script without frigate already running (new to containersā€¦)

Average inference time: 7.64043243598938

In my setup (regions X fps) across all cameras totals 116, which would be 8.6ms per region. So it ought to be ok? The machine is a little heavily loaded running about a dozen cameras (load ave. typically ~3.5 without frigate running). Is there any way to tell how highly utilised the coral is? Iā€™ve not allowed it to use the max clock speed yet.

Iā€™m planning on updating the hardware very soon, so CPU shouldnā€™t be a bottleneck in the future at least.

James

There is a little bit of overhead in passing data to the coral, so you have to pad a bit. If you are getting queue full consistently, it is running at full speed. I would try reducing framerate/regions a little. Seems like you are just past your limit.

Looks like Google released some updates. https://developers.googleblog.com/2019/08/coral-summer-updates-post-training.html?m=1

New models should improve accuracy without increasing speed.

3 Likes

Hmm, looks more like way over the limit. Iā€™ve knocked it down to 40 frame-regions/s, and now get spurious queue full messages from time to time. ffmpeg is still using about 6-7% per process, and python3 20-30%. Occasionally the docker process springs into life and consumes 400% cpu briefly.

Load average on the machine is hovering around 12-13 :grimacing:

James

How are you getting the score metric in HA here?

Thatā€™s an old version. I stopped doing it that way because it was flooding mqtt. New idea here: https://github.com/blakeblackshear/frigate/issues/48

Cool. Can we also get a count of how many person objects have been detected in a frame? :slight_smile:
(Or maybe a list of box boundaries which could be counted?)

I was thinking this morning it could be useful to create an alert if 2 or more people are spotted on the driveway at unexpected times - there has been press coverage of the vulnerabilities of keyless entry systems. 1 person on drive - post/parcel delivery perhaps. 2 people near the car howeverā€¦ :thinking:

Thanks! Both specifically and generally - this is awesome stuff. I keep thinking of different ways I can use this for HA :smile:

James

I am having an issue with the dockerfile on my Pi4. I have cloned the repo and I run the documented command from inside said directory. ā€˜docker -t frigate .ā€™ I am assuming this uses the main frigate dir as my docker build context. Everything goes fine until the last few lines of the Dockerfile. Then it errors out trying to do the ADD.

Step 19/23 : WORKDIR /opt/frigate/
 ---> Running in 142088e9af4f
Removing intermediate container 142088e9af4f
 ---> 7d6e171bae17
Step 20/23 : ADD frigate frigate/
ADD failed: stat /var/lib/docker/tmp/docker-builder909678516/frigate: no such file or directory

Seems to be some kind of absolute path issue given the dockerbuild context.

Hereā€™s an update with my experience, just incase it helps anyone else.

Issue 1
My queue kept filling up immediately upon starting the container, even with one camera and one region at 6fps.

ffmpeg verbose output confirmed a flood of frames upon starting up:

[rawvideo @ 0x1a943d0] Frame rate very high for a muxer not efficiently supporting it.
Please consider specifying a lower framerate, a different muxer or -vsync 2
Output #0, rawvideo, to 'pipe:':
  Metadata:
    title           : Session streamed by "preview"
    comment         : h264Preview_01_sub
    encoder         : Lavf57.83.100
    Stream #0:0: Video: rawvideo, 1 reference frame (RGB[24] / 0x18424752), rgb24(left), 640x352, q=2-31, 486604800 kb/s, 90k fps, 90k tbn, 90k tbc
    Metadata:
      encoder         : Lavc57.107.100 rawvideo
*** 458223 dup!
More than 1000 frames duplicated

Initially I added a one second sleep in the queue full block which allowed it to start up, but lots of duplicated frames were still observed and the queue repeatedly kept filling up and clearing :

*** 1 dup!2 fps=0.0 q=-0.0 size=   27720kB time=00:00:04.34 bitrate=52265.0kbits/s dup=41 drop=0 speed=6.89x    
    Last message repeated 2 times
*** 1 dup!9 fps= 42 q=-0.0 size=   32340kB time=00:00:05.06 bitrate=52265.0kbits/s dup=44 drop=0 speed= 4.3x    
    Last message repeated 1 times
*** 1 dup!5 fps= 33 q=-0.0 size=   36300kB time=00:00:05.68 bitrate=52265.0kbits/s dup=46 drop=0 speed=3.37x    
*** 1 dup!8 fps= 26 q=-0.0 size=   38280kB time=00:00:06.00 bitrate=52265.0kbits/s dup=47 drop=0 speed=2.71x    
    Last message repeated 2 times
*** 1 dup!5 fps= 23 q=-0.0 size=   42900kB time=00:00:06.72 bitrate=52265.0kbits/s dup=50 drop=0 speed=2.41x    
*** 1 dup!9 fps= 21 q=-0.0 size=   45540kB time=00:00:07.13 bitrate=52265.0kbits/s dup=51 drop=0 speed=2.14x    
    Last message repeated 1 times
*** 1 dup!4 fps= 19 q=-0.0 size=   48840kB time=00:00:07.65 bitrate=52265.0kbits/s dup=53 drop=0 speed=1.97x    
    Last message repeated 1 times
*** 1 dup!0 fps= 18 q=-0.0 size=   52800kB time=00:00:08.27 bitrate=52265.0kbits/s dup=55 drop=0 speed=1.88x    
    Last message repeated 2 times
*** 1 dup!6 fps= 17 q=-0.0 size=   56760kB time=00:00:08.89 bitrate=52265.0kbits/s dup=58 drop=0 speed= 1.8x    
    Last message repeated 1 times
*** 1 dup!1 fps= 17 q=-0.0 size=   60060kB time=00:00:09.41 bitrate=52265.0kbits/s dup=60 drop=0 speed=1.72x    
*** 1 dup!5 fps= 16 q=-0.0 size=   62700kB time=00:00:09.82 bitrate=52265.0kbits/s dup=61 drop=0 speed=1.64x    
    Last message repeated 1 times
*** 1 dup!0 fps= 15 q=-0.0 size=   66000kB time=00:00:10.34 bitrate=52265.0kbits/s dup=63 drop=0 speed=1.58x    
    Last message repeated 1 times

I experimented with the vsync options:

  • vsync 2 (drop duplicate frames) - produced lots of ā€˜past duration too largeā€™ messages
  • vsync drop (generate fresh timestamps based on frame rate) - the ā€˜speedā€™ averaged 0.576x :confused:
  • vsync 1 & -r 6 (frames duplicated or dropped to achieve constant frame rate) - the speed was much closer to 1.000x and there were no further warnings or messages

So through trial and error I settled for ā€˜-vsync 1ā€™ and ā€˜-r 6ā€™, which is working just fine. Iā€™m not at all familiar with ffmpeg or rtsp streaming so if anyone has a better solution then I am all ears.

Issue 2.
Eventually the stream would die and produce a barrage of ā€˜bad cseq xxxx expected=xxxxā€™ errors. It turns out my cameras (Reolink POE domes) do not like the stream being sigkilled and restarted again too quickly. I changed the ffmpeg_process.kill() to a terminate() and thatā€™s resolved that :). Is it possible to get frigate to try and do a graceful terminate first Blake?

Also I added the ā€˜discardcorruptā€™ flag as I thought it might help with the occasional smeared images I was seeing. Too soon to tell if itā€™s made a difference but Iā€™ve not seen any more artifacts since. Sounds like an option that might be good to add to the default ffmpeg_input_args though.

Happy to raise on github if youā€™d like.

Can this detect if more then one person is in a room?

I havenā€™t seen the duplicate frame issue on my cameras, but I pulled some of your suggestions into the 0.2.1 beta release I just pushed up: https://github.com/blakeblackshear/frigate/pull/55

Should make it easier to tweak your ffmpeg input params.

1 Like

From what I understand after a quick read in the docs, the duplicate frames message is related to ffmpeg trying to maintain a specific framerate in the output video. If it isnā€™t receiving frames from the input fast enough, it will duplicate a previous frame. In a typical use case, ffmpeg would be encoding a video file as an output. The frame rate for the output video file canā€™t be jumping all over the place.

Not currently. Feel free to open an issue on GH.

Thank you! :+1:

How does it generate the camera names? All the port 5000 paths contain camera names and it seems to call .get_current_frame_with_objects(), but every time I try to put something in the path it throws a key error. Is it somehow pulling a name from the RTSP stream? I donā€™t see a name field in config.yml.

The camera name in the default config is back

cameras:
  back:

@mr-onion Thanks!

Thank you very much @scstraus, this is great info. I have constructed my own compose file for 2 cameras based on yours. Have you had any issue with the multiple containers fighting over the coral stick? When I start my compose config one of the containers chokes for a second trying to access the accelerator, and then seems to recover. Wondering if itā€™s a timing issue with accessing the USB device that goes away.

tensorflow_driveway | Traceback (most recent call last):
tensorflow_driveway |   File "detect_objects.py", line 99, in <module>
tensorflow_driveway |     main()
tensorflow_driveway |   File "detect_objects.py", line 57, in main
tensorflow_driveway |     prepped_frame_queue
tensorflow_driveway |   File "/opt/frigate/frigate/object_detection.py", line 32, in __init__
tensorflow_driveway |     self.engine = DetectionEngine(PATH_TO_CKPT)
tensorflow_driveway |   File "/usr/local/lib/python3.6/dist-packages/edgetpu/detection/engine.py", line 72, in __init__
tensorflow_driveway |     super().__init__(model_path)
tensorflow_driveway |   File "/usr/local/lib/python3.6/dist-packages/edgetpu/basic/basic_engine.py", line 40, in __init__
tensorflow_driveway |     self._engine = BasicEnginePythonWrapper.CreateFromFile(model_path)
tensorflow_driveway | RuntimeError: Error in device opening (/sys/bus/usb/devices/2-1)!

Actually, looks like it isnā€™t as benign as I thought. No detections from that container.