简体   繁体   中英

How to access webcam using OpenCV (Python) in LXC container

I am running Ubuntu 18.04 on the host and Ubuntu 20.04 inside LXC container. My goal is to run ROS2 (which requires Ubuntu 20.04) inside an LXC container, then use it to connect to two dynamixel servo motors and a USB Logitech webcam. I have also installed miniconda inside the container so that I can install OpenCV and dlib. I am also using conda virtual environment inside the container.

All the mentioned software works fine. Now I need to access the webcam with OpenCV, I use this command for testing

python -c "import cv2;print(cv2.VideoCapture(0).isOpened())"

which returns True on host but returns

[ WARN:0] global ../modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
False

from inside the container.

So far I tried:

  1. lxc config device add rosfoxy video0 unix-char path=/dev/video0 with video0 , video1 and video2 and still getting the same error.
  2. lxc config device add rosfoxy logitech usb vendorid=046d productid=082b resulted in Device logitech added to rosfoxy , but still, same error.
  3. I then tried lxc profile device add ros logitech usb vendorid=046d productid=082b which results in Device logitech added to ros . When testing I get the same error.
  4. Tested both glxgears and glxinfo on host, both works fine. However, when testing them from inside the LXC container it works fine but with this error first:
libGL error: MESA-LOADER: failed to retrieve device information
libGL error: Version 4 or later of flush extension not found
libGL error: failed to load driver: i915
libGL error: failed to open /dev/dri/card0: No such file or directory
libGL error: failed to load driver: i965
3073 frames in 5.0 seconds = 614.256 FPS
5238 frames in 5.0 seconds = 1047.546 FPS
3766 frames in 5.0 seconds = 753.186 FPS
4214 frames in 5.0 seconds = 842.731 FPS
4926 frames in 5.0 seconds = 985.092 FPS
6007 frames in 5.0 seconds = 1201.399 FPS
5694 frames in 5.0 seconds = 1138.660 FPS
  1. Tested guvcview on host, works fine. But on container, it doesn't work, it gives:
V4L2_CORE: ERROR opening V4L2 interface for /dev/video2
V4L2_CORE: ERROR opening V4L2 interface for /dev/video3
V4L2_CORE: ERROR opening V4L2 interface for /dev/video0
V4L2_CORE: ERROR opening V4L2 interface for /dev/video1
GUVCVIEW: version 2.0.6
GUVCVIEW: couldn't open /home/ubuntu//.config/guvcview2/video0 for read: No such file or directory
V4L2_CORE: ERROR opening V4L interface: Permission denied
GUVCVIEW (1): Guvcview error
     no video device found

(guvcview:1526): dbind-WARNING **: 19:29:55.201: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-EhmzLUeJ8h: Connection refused

(guvcview:1526): Gtk-WARNING **: 19:29:55.307: Theme parsing error: <data>:1:17: not a number

(guvcview:1526): Gtk-WARNING **: 19:29:55.307: Theme parsing error: <data>:1:31: Using Pango syntax for the font: style property is deprecated; please use CSS syntax

(guvcview:1526): Gtk-WARNING **: 19:29:55.307: Theme parsing error: <data>:1:17: not a number

(guvcview:1526): Gtk-WARNING **: 19:29:55.307: Theme parsing error: <data>:1:32: Using Pango syntax for the font: style property is deprecated; please use CSS syntax

Would you advise what to do?

UPDATE 1: Tried lxc config device add rosfoxy video0 unix-char path=/dev/video0 gid=1000 but it doesn't seem to work.

From inside the container I them tried

(base) ubuntu@rosfoxy:~$ v4l2-ctl --all
Failed to open /dev/video0: Permission denied

But when I added sudo I got:

(base) ubuntu@rosfoxy:~$ sudo v4l2-ctl --all
Driver Info:
    Driver name      : uvcvideo
    Card type        : Webcam C170: Webcam C170
    Bus info         : usb-0000:00:14.0-1
    Driver version   : 5.4.86
    Capabilities     : 0x84a00001
        Video Capture
        Metadata Capture
        Streaming
        Extended Pix Format
        Device Capabilities
    Device Caps      : 0x04200001
        Video Capture
        Streaming
        Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
...

Which seems to me that the webcam is detected!

but I still get

(py38) ubuntu@rosfoxy:~$ python -c "import cv2;print(cv2.VideoCapture(0).isOpened())"
[ WARN:0] global ../modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
False

UPDATE 2:

I should have issued first lxc config device remove rosfoxy video0 then lxc config device add rosfoxy video0 unix-char path=/dev/video0 gid=1000

Thanks to Simos https://discuss.linuxcontainers.org/t/cant-open-camera-by-index-on-lxc-container/10452/8

When you run the command lxc config device add rosfoxy video0 unix-char path=/dev/video0 , a device is created in the container with default owner and group. These are root/root . On the host, the group of this device is video but in the container there is no setup to add the non-root user to this group.

Hence, modify the command to the following. The non-root account in the ubuntu:* container image has group ID 1000.

lxc config device add rosfoxy video0 unix-char path=/dev/video0 gid=1000

The USB device issue should have a similar solution. See https://linuxcontainers.org/lxd/docs/master/instances#type-usb for the documentation. You probably need to setup explicitly the uid or gid .

If you want to modify an added LXD device, you need to remove it first and add it again with the new parameter(s). You can do this like below.

lxc config device remove rosfoxy video0 
lxc config device add rosfoxy video0 unix-char path=/dev/video0 gid=1000

Note that gid=1000 specifies the group ID of your non-root user in the container. If you use a different type of container image (other than Ubuntu), you may need to change this value. You can find the group ID of your non-root account in the container if you run the id command in the container.

These two commands are the answer to my question:

First lxc config device remove rosfoxy video0 then lxc config device add rosfoxy video0 unix-char path=/dev/video0 gid=1000

Thanks to Simos https://discuss.linuxcontainers.org/t/cant-open-camera-by-index-on-lxc-container/10452/8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM