blakeblackshear / frigate

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

[Support]: USB Coral TPU not detected when Frigate installed in unprivileged LXC container #9436

Closed dme11 closed 9 months ago

dme11 commented 9 months ago

Describe the problem you are having

Hello, I've installed Frigate in unprivileged LXC container by following this instructions. Installation went well, Frigate starts but it doesn't detect Coral TPU. I've checked various forums related to TPU detection problem and nothing I've tried didn't work. Also, neither webcoral and parrot examples flash the FW to TPU from unprivileged container (Ubuntu v20.04 with default Python v3.8.10), so it' no surprise that Frigate can't detect and flash the FW to TPU.

I assume the issue is related to USB port permissions since I've managed to get Coral TPU detected in Frigate when installed in privileged LXC container.

I'd appreciate any guidance helping to solve this problem.

Regards,

Dusan

Version

0.12.1-367D724

Frigate config file

database:
    path: /db/frigate.db
mqtt:
    enabled: false
detectors:
    coral:
        type: edgetpu
        device: usb
ffmpeg:
    output_args:
        record: -f segment -segment_time 10 -segment_format mp4 -reset_timestamps 1 -strftime 1 -c copy
    hwaccel_args: # This is the section for the H/W Accelaration, you will need to figure out what settings you need to enable - https://docs.frigate.video/configuration/hardware_acceleration
        -c:v h264_qsv
       # - -hwaccel
       # - vaapi
       # - -hwaccel_device
       # - /dev/dri/renderD128
       # - -hwaccel_output_format
       # - yuv420p
objects: # Object to detect - https://docs.frigate.video/configuration/objects
    track:
        - person
        - cat
        - dog
    filters:
        person:
            min_area: 5000
            max_area: 8000000
            threshold: 0.80
record: # how long to record before and after motion
    events:
        pre_capture: 7
        post_capture: 10
cameras:
    test: # <------ Name the camera
        ffmpeg:
            inputs:
                - path: rtsp://user:password@192.168.2.236:554/cam/realmonitor?channel=1&subtype=1 # Update with your camera info (Normally the lower res stream, 1280*720 would be a good detection res)
                  roles:
                    - detect
                    - rtmp
                - path: rtsp://user:password@192.168.2.236:554/cam/realmonitor?channel=1&subtype=0 # Update with your camera info (Normally the higher res stream, used for actual recording)
                  roles:
                    - record
                   # - detect
                    #- rtmp
        rtmp:
            enabled: False # <-- RTMP should be disabled if your stream is not H264
        detect:
            width: 704 # <---- update for your camera's resolution
            height: 576 # <---- update for your camera's resolution
            #width: 2688
            #height: 1520
        record: # what you want Frigate to record
            enabled: True
           # retain: # uncomment this section if you want Frigate to record this camera 24/7. This can use a lot of space, you have been warned!
           #     days: 1
           #     mode: all
            events: # this is the default recording mode where events are recorded, the number of days it saves events for is 10.
                retain:
                    mode: active_objects
                    default: 10
        snapshots:
            enabled: True
            retain:
                default: 10

Relevant log output

s6-rc: info: service log-prepare: starting
s6-rc: info: service log-prepare successfully started
s6-rc: info: service nginx-log: starting
s6-rc: info: service go2rtc-log: starting
s6-rc: info: service frigate-log: starting
s6-rc: info: service nginx-log successfully started
s6-rc: info: service frigate-log successfully started
s6-rc: info: service go2rtc-log successfully started
s6-rc: info: service go2rtc: starting
s6-rc: info: service go2rtc successfully started
s6-rc: info: service go2rtc-healthcheck: starting
s6-rc: info: service frigate: starting
s6-rc: info: service frigate successfully started

2024-01-25 08:14:33.630410451  [INFO] Starting go2rtc healthcheck service...
2024-01-25 08:14:49.561553598  [2024-01-25 08:14:24] detector.coral                 INFO    : Starting detection process: 282
2024-01-25 08:14:49.561693860  [2024-01-25 08:14:24] frigate.detectors.plugins.edgetpu_tfl INFO    : Attempting to load TPU as usb
2024-01-25 08:14:49.562216707  [2024-01-25 08:14:49] frigate.detectors.plugins.edgetpu_tfl ERROR   : No EdgeTPU was detected. If you do not have a Coral device yet, you must configure CPU detectors.
2024-01-25 08:14:49.562219535  Process detector:coral:
2024-01-25 08:14:49.563025128  Traceback (most recent call last):
2024-01-25 08:14:49.563037569    File "/usr/lib/python3/dist-packages/tflite_runtime/interpreter.py", line 160, in load_delegate
2024-01-25 08:14:49.563038847      delegate = Delegate(library, options)
2024-01-25 08:14:49.563040211    File "/usr/lib/python3/dist-packages/tflite_runtime/interpreter.py", line 119, in __init__
2024-01-25 08:14:49.563042270      raise ValueError(capture.message)
2024-01-25 08:14:49.563049784  ValueError
2024-01-25 08:14:49.563050750

FFprobe output from your camera

[{"return_code":0,"stderr":"","stdout":{"programs":[],"streams":[{"avg_frame_rate":"5/1","codec_long_name":"H.264/AVC/MPEG-4AVC/MPEG-4part10","height":576,"width":704},{"avg_frame_rate":"0/0","codec_long_name":"AAC(AdvancedAudioCoding)"}]}},{"return_code":0,"stderr":"","stdout":{"programs":[],"streams":[{"avg_frame_rate":"15/1","codec_long_name":"H.264/AVC/MPEG-4AVC/MPEG-4part10","height":1620,"width":2880},{"avg_frame_rate":"0/0","codec_long_name":"AAC(AdvancedAudioCoding)"}]}}]

Frigate stats

No response

Operating system

Proxmox

Install method

Docker Compose

Coral version

USB

Network connection

Wired

Camera make and model

Dahua IPC-HFW1530S

Any other information that may be helpful

Below some more information about the current configuration.

Host info Operating System: Debian GNU/Linux 12 (bookworm) Kernel: Linux 6.5.11-7-pve Architecture: x86-64 Hardware Vendor: Protectli Hardware Model: FW6 Firmware Version: coreboot 4.12, Dasharo 1.0.14 Proxmox PVE version: pve-manager/8.1.4/ec5affc9e41f1d79 (running kernel: 6.5.11-7-pve)

Host USB info Bus 002 Device 002: ID 1a6e:089a Global Unichip Corp. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

LXC container USB info Bus 002 Device 002: ID 1a6e:089a Global Unichip Corp. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

LXC container config arch: amd64 cores: 2 features: nesting=1 hostname: frigate memory: 4096 mp0: /mnt/lxc_shares/frigate_network_storage/,mp=/opt/frigate/media net0: name=eth0,bridge=vmbr0,firewall=1,hwaddr=BC:24:11:4A:28:D0,ip=dhcp,type=veth onboot: 1 ostype: ubuntu rootfs: local-lvm:vm-100-disk-0,size=20G swap: 512 unprivileged: 1 usb0: host=1a6e:089a,usb3=1 usb1: host=18d1:9302,usb3=1 lxc.cgroup2.devices.allow: c 189:* rwm lxc.mount.entry: /dev/bus/usb/002 dev/bus/usb/002 none bind,optional,create=dir lxc.cgroup2.devices.allow: c 226:0 rwm lxc.cgroup2.devices.allow: c 226:128 rwm lxc.mount.entry: /dev/dri/renderD128 dev/dri/renderD128 none bind,optional,create=file 0, 0 lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir

Docker compose file version: "3.9" services: frigate: container_name: frigate privileged: true # this may not be necessary for all setups restart: unless-stopped image: ghcr.io/blakeblackshear/frigate:stable shm_size: "128mb" # update for your cameras based on calculation above devices:

ptschi commented 9 months ago

Hi, I had the same issue, I created a udev rules nano /etc/udev/rules.d/98-coral.rules then add

SUBSYSTEMS=="usb", ATTRS{idVendor}=="1a6e", ATTRS{idProduct}=="089a", OWNER="root", SYMLINK+="coral", MODE="0666", GROUP="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="1a6e", ATTRS{idProduct}=="089a", OWNER="root", SYMLINK+="coral", MODE="0666", GROUP="plugdev"

then reload rules udevadm control --reload-rules && udevadm trigger

dme11 commented 9 months ago

Hi,

First of all thank you for your time trying to help me. I've created suggested udev rules and reloaded rules. Now lsusb command on the host and in the LXC shows:

Bus 002 Device 004: ID 18d1:9302 Google Inc. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

That's so even if I disconnect the USB Coral TPU from the host and connect it back. I'm no Linux expert but that the Coral TPU USB is recognized as Google and not Global Unichip without actually flashing the FW to it first doesn't solve the LXC USB permissions issue. As result, pycoral example within LXC unfortunately does not work. Error log reported after running parrot example is as follows:

Traceback (most recent call last): File "/usr/lib/python3/dist-packages/tflite_runtime/interpreter.py", line 160, in load_delegate delegate = Delegate(library, options) File "/usr/lib/python3/dist-packages/tflite_runtime/interpreter.py", line 119, in init raise ValueError(capture.message) ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "examples/classify_image.py", line 121, in main() File "examples/classify_image.py", line 71, in main interpreter = make_interpreter(*args.model.split('@')) File "/usr/lib/python3/dist-packages/pycoral/utils/edgetpu.py", line 87, in make_interpreter delegates = [load_edgetpu_delegate({'device': device} if device else {})] File "/usr/lib/python3/dist-packages/pycoral/utils/edgetpu.py", line 52, in load_edgetpu_delegate return tflite.load_delegate(_EDGETPU_SHARED_LIB, options or {}) File "/usr/lib/python3/dist-packages/tflite_runtime/interpreter.py", line 162, in load_delegate raise ValueError('Failed to load delegate from {}\n{}'.format( ValueError: Failed to load delegate from libedgetpu.so.1

I'm not able to flash the FW using webcoral example either. The error log after make reset command shows:

dfu-util: Warning: Invalid DFU suffix signature dfu-util: A valid DFU suffix will be required in a future dfu-util release dfu-util: No DFU capable USB device available

Also, when started Frigate the log shows "No EdgeTPU was detected".

Regards,

Dusan

ptschi commented 9 months ago

from the lxc list the device permission withls -l /dev/dri/002/004you should see something like crw-rw-rw- 1 nobody nogroup if the permission is set correctly. if it's ok and still not working, you should execute this check from the container

dme11 commented 9 months ago

Hi,

You probably meant ls -l /dev/bus/usb/002/004?

If so, here are the outputs from Proxmox host:

crw-rw-r-- 1 root root 189, 131 Jan 30 22:31 /dev/bus/usb/002/004

and from within the LXC:

crw-rw-r-- 1 nobody nogroup 189, 131 Jan 30 21:31 /dev/bus/usb/002/004

BR, Dusan

ptschi commented 9 months ago

I think it should rw for others chmod 666 /dev/bus/usb/002/004 and check your rules

dme11 commented 9 months ago

It seems that I as a root user within LXC cannot change permissions:

root@frigate:/# chmod 666 /dev/bus/usb/002/004 chmod: changing permissions of '/dev/bus/usb/002/004': Operation not permitted

ptschi commented 9 months ago

You should do it from the host

Le mer. 31 janv. 2024, 10:12, dme11 @.***> a écrit :

It seems that I as a root user within LXC cannot change permissions:

@.***:/# chmod 666 /dev/bus/usb/002/004 chmod: changing permissions of '/dev/bus/usb/002/004': Operation not permitted

— Reply to this email directly, view it on GitHub https://github.com/blakeblackshear/frigate/issues/9436#issuecomment-1918687309, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHEAHDTX37SVBFU4A2EM3DYRIDH7AVCNFSM6AAAAABCKEK642VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJYGY4DOMZQHE . You are receiving this because you commented.Message ID: @.***>

dme11 commented 9 months ago

I used the chmod command in the host and now the parrot example works.

The USB port permissions command now reports both in host and LXC as follows :

crw-rw-rw- 1 root root 189, 131 Jan 31 11:07 /dev/bus/usb/002/004

Parrot example output from the LXC:

----INFERENCE TIME---- Note: The first inference on Edge TPU is slow because it includes loading the model into Edge TPU memory. 13.3ms 4.4ms 4.4ms 4.5ms 4.4ms -------RESULTS-------- Ara macao (Scarlet Macaw): 0.75781

And finally Frigate reports: image

Many thanks for your help. I've been banging my head for days combing different forums trying to find the solution.

One final question: is there a way to make the last chmod permission change permanent so it'll be applied automatically after host reboot/powerdown?

ptschi commented 9 months ago

Check the udev rules, that should fix the reboot point.

Le mer. 31 janv. 2024, 11:25, dme11 @.***> a écrit :

I used the chmod command in the host and now the parrot example works.

The USB port permissions command now reports both in host and LXC as follows :

crw-rw-rw- 1 root root 189, 131 Jan 31 11:07 /dev/bus/usb/002/004

Parrot example output from the LXC:

----INFERENCE TIME---- Note: The first inference on Edge TPU is slow because it includes loading the model into Edge TPU memory. 13.3ms 4.4ms 4.4ms 4.5ms 4.4ms -------RESULTS-------- Ara macao (Scarlet Macaw): 0.75781

And finally Frigate reports: image.png (view on web) https://github.com/blakeblackshear/frigate/assets/157683853/f2fc2911-1a3a-417b-8b72-a1f7a7006836

Many thanks for your help. I've been banging my head for days combing different forums trying to find the solution.

One final question: is there a way to make the last chmod permission change permanent so it'll be applied automatically after host reboot/powerdown?

— Reply to this email directly, view it on GitHub https://github.com/blakeblackshear/frigate/issues/9436#issuecomment-1918816648, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHEAHBBRX23CP5GFCDKTXTYRIL25AVCNFSM6AAAAABCKEK642VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJYHAYTMNRUHA . You are receiving this because you commented.Message ID: @.***>

dme11 commented 9 months ago

I've modified udev rules a bit. Just in case I've put both possible vendor and product ID's (unflashed and flashed USB Coral TPU). The content of my 98-coral.rules files is as follows:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="1a6e", ATTRS{idProduct}=="089a", OWNER="root", MODE="0666", GROUP="plugdev" SUBSYSTEMS=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="9302", OWNER="root", MODE="0666", GROUP="plugdev"

and reloaded the rules by udevadm control --reload-rules && udevadm trigger

I'd like to thank you again for your help and patience with me.

Regards,

Dusan

dme11 commented 9 months ago

Guess my issue is solved and I can close the ticket.