blakeblackshear / frigate

NVR with realtime local object detection for IP cameras
https://frigate.video
MIT License
18.81k stars 1.71k forks source link

High CPU usage with nvidia container #1391

Closed lygris closed 3 years ago

lygris commented 3 years ago

Describe the bug I'm using the nvidia container and a pci tpu, but CPU usage is high with 8 cameras. By running nvidia-smi I can see 8 ffmpeg processes using the GPU so i'm a little lost.

Version of frigate Output from /api/version 0.8.4-5043040

Config file

mqtt: 
  host: host.org
detectors:
  coral:
    type: edgetpu
    device: pci
cameras:
  Upstairs:
    ffmpeg:
      input_args:
        - -c:v
        - h264_cuvid
      inputs:
        - path: rtsp://x.x.x.247/live0
          roles:
            - detect
            - rtmp
            - clips
    detect:
      enabled: True
      max_disappeared: 45
    clips:
      enabled: True
      pre_capture: 5
      post_capture: 15
      objects:
        - person
        - dog
        - cat
        - car        
    width: 1920
    height: 1080
    fps: 15
  Garage:
    ffmpeg:
      input_args:
        - -c:v
        - h264_cuvid
      inputs:
        - path: rtsp://x.x.x.246/live0
          roles:
            - detect
            - rtmp
            - clips
    detect:
      enabled: True
      max_disappeared: 45
    clips:
      enabled: True
      pre_capture: 5
      post_capture: 15
      objects:
        - person
        - dog
        - cat
        - car
    width: 1920
    height: 1080
    fps: 15
  Laundry-Room:
    ffmpeg:
      input_args:
        - -c:v
        - h264_cuvid
      inputs:
        - path: rtsp://x.x.x.245/live0
          roles:
            - detect
            - rtmp
            - clips
    detect:
      enabled: True
      max_disappeared: 45
    clips:
      enabled: True
      pre_capture: 5
      post_capture: 15
      objects:
        - person
        - dog
        - cat
        - car
    width: 1920
    height: 1080
    fps: 15
  Den:
    ffmpeg:
      input_args:
        - -c:v
        - h264_cuvid
      inputs:
        - path: rtsp://x.x.x.244/live0
          roles:
            - detect
            - rtmp
            - clips
    detect:
      enabled: True
      max_disappeared: 45
    clips:
      enabled: True
      pre_capture: 5
      post_capture: 15
      objects:
        - person
        - dog
        - cat
        - car
    width: 1920
    height: 1080
    fps: 15
  Spare-Bedroom:
    ffmpeg:
      input_args:
        - -c:v
        - h264_cuvid
      inputs:
        - path: rtsp://x.x.x.173/live0
          roles:
            - detect
            - rtmp
            - clips
    detect:
      enabled: True
      max_disappeared: 45
    clips:
      enabled: True
      pre_capture: 5
      post_capture: 15
      objects:
        - person
        - dog
        - cat
        - car
    width: 1920
    height: 1080
    fps: 15
  Office:
    ffmpeg:
      input_args:
        - -c:v
        - h264_cuvid
      inputs:
        - path: rtsp://x.x.x.167/live0
          roles:
            - detect
            - rtmp
            - clips
    detect:
      enabled: True
      max_disappeared: 45
    clips:
      enabled: True
      pre_capture: 5
      post_capture: 15
      objects:
        - person
        - dog
        - cat
        - car
    width: 1920
    height: 1080
    fps: 15
  Driveway:
    ffmpeg:
      input_args:
        - -c:v
        - h264_cuvid
        - -rtsp_transport 
        - udp
      inputs:
        - path: rtsp://user:pass@x.x.x.249:80/videoMain
          roles:
            - detect
            - rtmp
            - clips
    detect:
      enabled: True
      max_disappeared: 45
    clips:
      enabled: True
      pre_capture: 5
      post_capture: 15
      objects:
        - person
        - dog
        - cat
        - car
    width: 1920
    height: 1080
    fps: 15
  Backyard:
    ffmpeg:
      input_args:
        - -c:v
        - h264_cuvid
      inputs:
        - path: rtsp://user:pass@x.x.x.252/cam/realmonitor?channel=1&subtype=0
          roles:
            - detect
            - rtmp
            - clips
    detect:
      enabled: True
      max_disappeared: 45
    clips:
      enabled: True
      pre_capture: 5
      post_capture: 15
      objects:
        - person
        - dog
        - cat
        - car
    width: 1920
    height: 1080
    fps: 15
  Driveway:
    ffmpeg:
      input_args:
        - -c:v
        - h264_cuvid
      inputs:
        - path: rtsp://user:pass@x.x.x.253/cam/realmonitor?channel=1&subtype=0
          roles:
            - detect
            - rtmp
            - clips
    detect:
      enabled: True
      max_disappeared: 45
    clips:
      enabled: True
      pre_capture: 5
      post_capture: 15
      objects:
        - person
        - dog
        - cat
        - car
    width: 1920
    height: 1080
    fps: 15

I get these a lot it seems but not sure if it's relevant Frigate container logs

[h264 @ 0x555dfa064f00] left block unavailable for requested intra mode
[h264 @ 0x555dfa064f00] error while decoding MB 0 67, bytestream 3146

[h264 @ 0x55d0cf768f00] error while decoding MB 108 6, bytestream -11

[h264 @ 0x561efbf8bf00] error while decoding MB 64 62, bytestream -22

[h264 @ 0x556c5d02ff00] error while decoding MB 59 6, bytestream -9

[h264 @ 0x556c5d02ff00] error while decoding MB 71 67, bytestream -5

[h264 @ 0x5574264d1f00] left block unavailable for requested intra mode
[h264 @ 0x5574264d1f00] error while decoding MB 0 67, bytestream 9247

[h264 @ 0x559a5f531f00] error while decoding MB 88 61, bytestream -17

[h264 @ 0x55de32c95f00] error while decoding MB 72 63, bytestream -56

[h264 @ 0x55db01a07f00] error while decoding MB 36 14, bytestream -19

[h264 @ 0x559ef6af9f00] error while decoding MB 69 53, bytestream -41

[h264 @ 0x562dce8aff00] left block unavailable for requested intra mode
[h264 @ 0x562dce8aff00] error while decoding MB 0 67, bytestream 9344

[h264 @ 0x55d9e42def00] error while decoding MB 11 64, bytestream -11

[h264 @ 0x56536f81df00] left block unavailable for requested intra4x4 mode -1
[h264 @ 0x56536f81df00] error while decoding MB 0 62, bytestream 27758

[h264 @ 0x55d6ee6d6f00] left block unavailable for requested intra mode
[h264 @ 0x55d6ee6d6f00] error while decoding MB 0 60, bytestream 14667

[h264 @ 0x55e6ba173f00] cabac decode of qscale diff failed at 11 39

[h264 @ 0x55e6ba173f00] error while decoding MB 11 39, bytestream 393

[h264 @ 0x556f8c6d5f00] error while decoding MB 66 63, bytestream -66

[h264 @ 0x55b9a372ef00] error while decoding MB 71 60, bytestream -38

[h264 @ 0x55c17452df00] error while decoding MB 14 62, bytestream -44

[h264 @ 0x55fd90abaf00] error while decoding MB 63 61, bytestream -22

[h264 @ 0x55f07f3a4f00] error while decoding MB 11 61, bytestream -64

[h264 @ 0x55c79df65f00] cabac decode of qscale diff failed at 83 57

[h264 @ 0x55c79df65f00] error while decoding MB 83 57, bytestream 76081

[h264 @ 0x55b8be993f00] error while decoding MB 95 65, bytestream -54

[h264 @ 0x55886bec7f00] error while decoding MB 11 64, bytestream -42

[h264 @ 0x560e2a8d1f00] error while decoding MB 113 62, bytestream -48

[h264 @ 0x55c22c766f00] left block unavailable for requested intra4x4 mode -1
[h264 @ 0x55c22c766f00] error while decoding MB 0 53, bytestream 15240

[h264 @ 0x55733fd1cf00] error while decoding MB 108 53, bytestream -44

[h264 @ 0x558cfe23ef00] error while decoding MB 78 61, bytestream -36

[h264 @ 0x55b6b97a0f00] error while decoding MB 103 65, bytestream -36

[h264 @ 0x5615c940ef00] left block unavailable for requested intra mode
[h264 @ 0x5615c940ef00] error while decoding MB 0 55, bytestream 2456

[h264 @ 0x56145ff2df00] cabac decode of qscale diff failed at 57 53

[h264 @ 0x56145ff2df00] error while decoding MB 57 53, bytestream 11562

[h264 @ 0x55672c2cbf00] error while decoding MB 110 49, bytestream -27

[h264 @ 0x5623687f7f00] error while decoding MB 36 64, bytestream -33

[h264 @ 0x56122d269f00] error while decoding MB 56 51, bytestream -14

[h264 @ 0x558a0ec8cf00] error while decoding MB 106 54, bytestream -59

[h264 @ 0x556f225ddf00] cabac decode of qscale diff failed at 8 67

[h264 @ 0x556f225ddf00] error while decoding MB 8 67, bytestream -6

[h264 @ 0x55f50a61ff00] error while decoding MB 21 55, bytestream -8

[h264 @ 0x5559a5147f00] error while decoding MB 82 44, bytestream -40

[h264 @ 0x55c72893df00] left block unavailable for requested intra4x4 mode -1
[h264 @ 0x55c72893df00] error while decoding MB 0 50, bytestream 18685

[h264 @ 0x55837b6b2f00] left block unavailable for requested intra4x4 mode -1
[h264 @ 0x55837b6b2f00] error while decoding MB 0 56, bytestream 98868

[h264 @ 0x558143312f00] error while decoding MB 70 61, bytestream -62

[h264 @ 0x5595998ddf00] error while decoding MB 19 66, bytestream -52

[h264 @ 0x563b546e3f00] error while decoding MB 23 52, bytestream -31

Frigate stats

{
  "Backyard": {
    "camera_fps": 15.4, 
    "capture_pid": 129, 
    "detection_fps": 16.8, 
    "pid": 86, 
    "process_fps": 14.0, 
    "skipped_fps": 0.0
  }, 
  "Den": {
    "camera_fps": 15.2, 
    "capture_pid": 110, 
    "detection_fps": 0.0, 
    "pid": 81, 
    "process_fps": 15.2, 
    "skipped_fps": 0.0
  }, 
  "Driveway": {
    "camera_fps": 15.4, 
    "capture_pid": 122, 
    "detection_fps": 8.6, 
    "pid": 85, 
    "process_fps": 15.4, 
    "skipped_fps": 0.0
  }, 
  "Garage": {
    "camera_fps": 15.2, 
    "capture_pid": 89, 
    "detection_fps": 0.0, 
    "pid": 78, 
    "process_fps": 15.2, 
    "skipped_fps": 0.0
  }, 
  "Laundry-Room": {
    "camera_fps": 15.2, 
    "capture_pid": 103, 
    "detection_fps": 0.0, 
    "pid": 79, 
    "process_fps": 15.2, 
    "skipped_fps": 0.0
  }, 
  "Office": {
    "camera_fps": 15.4, 
    "capture_pid": 117, 
    "detection_fps": 3.6, 
    "pid": 83, 
    "process_fps": 15.3, 
    "skipped_fps": 0.0
  }, 
  "Spare-Bedroom": {
    "camera_fps": 15.4, 
    "capture_pid": 113, 
    "detection_fps": 0.0, 
    "pid": 82, 
    "process_fps": 15.4, 
    "skipped_fps": 0.0
  }, 
  "Upstairs": {
    "camera_fps": 15.2, 
    "capture_pid": 88, 
    "detection_fps": 0.0, 
    "pid": 77, 
    "process_fps": 15.2, 
    "skipped_fps": 0.0
  }, 
  "detection_fps": 29.0, 
  "detectors": {
    "coral": {
      "detection_start": 0.0, 
      "inference_speed": 9.89, 
      "pid": 74
    }
  }, 
  "service": {
    "storage": {
      "/dev/shm": {
        "free": 5340.3, 
        "mount_type": "tmpfs", 
        "total": 5368.7, 
        "used": 28.4
      }, 
      "/media/frigate/clips": {
        "free": 14735217.4, 
        "mount_type": "fuse.shfs", 
        "total": 87987557.7, 
        "used": 73252340.3
      }, 
      "/media/frigate/recordings": {
        "free": 14735217.4, 
        "mount_type": "fuse.shfs", 
        "total": 87987557.7, 
        "used": 73252340.3
      }, 
      "/tmp/cache": {
        "free": 6590.4, 
        "mount_type": "rootfs", 
        "total": 8317.6, 
        "used": 1727.2
      }
    }, 
    "uptime": 10902, 
    "version": "0.8.4-5043040"
  }
}

FFprobe from your camera

Run the following command and paste output below 6 of these cameras

ffprobe version 4.4-essentials_build-www.gyan.dev Copyright (c) 2007-2021 the FFmpeg developers
  built with gcc 10.2.0 (Rev6, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
[rtsp @ 000001b6a680fc00] method SETUP failed: 461 Unsupported transport
Input #0, rtsp, from 'rtsp://x.x.x.x/live0':
  Metadata:
    title           : H.264 Video,leco stream
  Duration: N/A, start: 0.000000, bitrate: N/A
  Stream #0:0: Video: h264 (High), yuv420p(tv, bt709, progressive), 1920x1080, 15 fps, 60 tbr, 90k tbn, 30 tbc
  Stream #0:1: Audio: aac (LC), 16000 Hz, mono, fltp

2 of these cameras

ffprobe version 4.4-essentials_build-www.gyan.dev Copyright (c) 2007-2021 the FFmpeg developers
  built with gcc 10.2.0 (Rev6, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, rtsp, from 'rtsp://user:pass@x.x.x.252/cam/realmonitor?channel=1&subtype=0':
  Metadata:
    title           : Media Server
  Duration: N/A, start: 0.266667, bitrate: N/A
  Stream #0:0: Video: h264 (High), yuvj420p(pc, bt709, progressive), 1920x1080 [SAR 3861:3856 DAR 429:241], 30 fps, 100 tbr, 90k tbn, 60 tbc

Screenshots If applicable, add screenshots to help explain your problem.

Computer Hardware

Camera Info:

Additional context Add any other context about the problem here.

blakeblackshear commented 3 years ago

Can you run top inside the container to see what is using so much CPU?

lygris commented 3 years ago

Here you go

Oh and I don't think I mentioned in the original post, this vm has 12 cores/24 threads and is running on e5-2650 v2 cpus and docker is reporting 40 - 60% cpu usage.


   96 root      20   0 3702400 146924   5192 S 243.9   0.9 284:18.10 frigate.process                                                                                                                                                                                         
   97 root      20   0 3703028 146692   5380 R 232.2   0.9 354:18.10 frigate.process                                                                                                                                                                                         
  103 root      20   0 2319216 127832   2780 S  58.8   0.8 462:25.91 python3                                                                                                                                                                                                 
   98 root      20   0 2319216 127568   2504 S  58.5   0.8 463:11.32 python3                                                                                                                                                                                                 
   99 root      20   0 2319216 127188   2164 S  58.1   0.8 461:16.80 python3                                                                                                                                                                                                 
  106 root      20   0 2319216 126844   1816 S  58.1   0.8 462:47.45 python3                                                                                                                                                                                                 
  111 root      20   0 2319216 127376   2312 S  57.5   0.8 463:03.22 python3                                                                                                                                                                                                 
  115 root      20   0 2319216 127376   2308 S  57.5   0.8 462:15.40 python3                                                                                                                                                                                                 
  110 root      20   0 5220732 155084 116336 R  41.2   0.9 378:39.63 ffmpeg                                                                                                                                                                                                  
  113 root      20   0 5220920 151876 116864 R  41.2   0.9 375:55.32 ffmpeg                                                                                                                                                                                                  
  102 root      20   0 5220988 153276 116276 R  40.9   0.9 377:56.49 ffmpeg                                                                                                                                                                                                  
  127 root      20   0 5220924 156028 117108 R  40.9   1.0 377:33.46 ffmpeg                                                                                                                                                                                                  
  121 root      20   0 5220884 153652 116796 R  40.5   0.9 378:15.77 ffmpeg                                                                                                                                                                                                  
  118 root      20   0 5220888 155680 116808 R  39.9   0.9 378:31.55 ffmpeg                                                                                                                                                                                                  
24464 root      20   0 5234716 171904 125736 R  27.9   1.0 134:58.98 ffmpeg                                                                                                                                                                                                  
24364 root      20   0 5253440 166908 125648 S  27.6   1.0 143:17.22 ffmpeg                                                                                                                                                                                                  
    1 root      20   0 4423904 266372   7704 S  22.3   1.6 180:23.78 python3                                                                                                                                                                                                 
   74 root      20   0 2213064 132732   6780 S  17.9   0.8  41:37.74 frigate.detecto                                                                                                                                                                                         
  126 root      20   0 2384752 128788   4288 S  16.3   0.8 204:19.48 python3                                                                                                                                                                                                 
  122 root      20   0 2384752 128600   4168 S  15.9   0.8 204:57.38 python3                                                                                                                                                                                                 
   77 root      20   0 3698960 146556   5364 S   6.0   0.9 236:51.78 frigate.process                                                                                                                                                                                         
   80 root      20   0 3704264 150836   5240 S   6.0   0.9  57:18.85 frigate.process                                                                                                                                                                                         
   81 root      20   0 3699100 145560   5540 S   6.0   0.9  50:41.47 frigate.process                                                                                                                                                                                         
   85 root      20   0 3691404 137720   5052 S   6.0   0.8  80:07.00 frigate.process                                                                                                                                                                                         
   78 root      20   0 3699076 146028   5396 S   5.6   0.9  39:38.06 frigate.process                                                                                                                                                                                         
   83 root      20   0 3698944 145460   5088 S   5.6   0.9  99:10.93 frigate.process                                                                                                                                                                                         
   73 root      20   0   14524   5180    168 S   4.7   0.0  29:04.23 python3                                                                                                                                                                                                 
   20 nobody    20   0   19836   8580   1860 S   3.0   0.1  34:45.87 nginx                                                                                                                                                                                                   
   19 root      20   0   16416   3236      0 S   0.0   0.0   0:00.00 nginx                                                                                                                                                                                                   
   69 root      20   0 1978632 102348   1996 S   0.0   0.6   0:03.71 frigate.logger                                                                                                                                                                                          
14243 root      20   0    2616    580    512 S   0.0   0.0   0:00.04 sh                                                                                                                                                                                                      
14250 root      20   0    6240   3180   2672 R   0.0   0.0   0:00.09 top ```
lygris commented 3 years ago

@blakeblackshear I realized the pid with the high cpu usage is whichever camera at the time has a lot of motion? Not sure if recording is using a lot of cpu or if it's something else triggered by motion/object detection? Would that make any sense?

blakeblackshear commented 3 years ago

The pid for ffmpeg? There are at least 3 pids for each camera.

lygris commented 3 years ago

@blakeblackshear on the debug tab it is just labeled "pid" so in this case today pid 93 was over 800 in top when 2 people were in my living room today sitting on the couch with slight movement. Whichever camera has a decent bit of motion in it, has the "frigate.process" pid for that camera spike in cpu usage. image

blakeblackshear commented 3 years ago

That process performs motion detection on the frames coming from the camera and then prepares regions for object detection based on detected motion. It should have higher CPU usage when there is motion being detected.

lygris commented 3 years ago

That process performs motion detection on the frames coming from the camera and then prepares regions for object detection based on detected motion. It should have higher CPU usage when there is motion being detected.

@blakeblackshear Is it possible to have my gpu do the motion detection as well as encoding/decoding?

I switched to frigate from shinobi because of the cameras dropping out occasionally and wanting better object detection but if frigate is going to use ~8-16 cores at 100% when motion is detected I'm not sure I can keep running it.

blakeblackshear commented 3 years ago

It's not possible for the GPU to run motion detection, and it's not even the most CPU intensive thing happening in that process. Those processes should not be using 8-16 cores for that number of cameras. I run 7 1080p cameras on an i3 NUC and it doesn't even use 1 core at 100%.

Have you looked at the debug view with motion boxes on for your cameras? You may need motion masks for things like timestamps. Your detection FPS should stay near zero when there is no motion of interest.

Also, you should set your fps for all cameras to 5. That won't impact the FPS for clips or rtmp, but it will limit the fps for analysis.

lygris commented 3 years ago

@blakeblackshear the motion masks looks like they helped alot, granted nobody is at home right now besides me but it's looking better. One thing I noticed is my clips don't have audio, I tried adding the following to a camera but it didn't seem to work. Must be missing something.

- -c:a
- aac  

Missed the part about overriding the -an setting

Also is there a way to view multiple cameras at once kind of like a "cctv" monitor? If not is that a planned feature or is there another way for me to accomplish that with home assistant or another program? Figured that part out with home assistant

Thank you for all the help, it's greatly appreciated!