pythondockerraspberry-picameraazure-iot-edge

'can't open camera by index' using cv.videocapture(0)


I get:

[ WARN:0@0.790] global /tmp/pip-wheel-5v2wouk5/opencv-contrib-python_f04c010cf0414c5a81144576b45aa287/opencv/modules/videoio/src/cap_v4l.cpp (889) open VIDEOIO(V4L2:/dev/video0): can't open camera by index

when deploying a Docker container to Raspberry Pi 4B (Debian). Second line is where the warning comes from:

def __init__(self, path, queueSize=3):
    self.stream = cv2.VideoCapture(path) #<--- here the warning pops up. path == 0 here.
    print("path: ", path)
    print("self.stream of cv2.VideoCapture(path) ", self.stream)

    self.stopped = False
    self.Q = Queue(maxsize=queueSize)

The Docker container is deployed to the device by first building it in Visual Studio and thereafter pushing it to Azure Container Registry by:

docker build --rm -f "c:"my_path"\arm32v7.Dockerfile" -t "my_crname".azurecr.io/cameracapture:0.2.11-arm32v7 "c:... ; if ($?) { docker push "my_crname".azurecr.io/cameracapture:0.2.11-arm32v7 } if ($?) { docker build --rm -f "c:"my_path"\arm32v7.Dockerfile" -t "my_crname".azurecr.io/imageclassifierservice:0.2.17-arm32v7 "c:"my_path"" } if ($?) { docker push "my_crname".azurecr.io/imageclassifierservice:0.2.17-arm32v7 }

it is thereafter 'deployed to a single device' in Visual Studio through a configfile (deployment.json).

The Docker has permissions to access the camera module 3 integrated camera mounted on /dev/video0:

sudo nano /etc/udev/rules.d/99-camera.rules
SUBSYSTEM=="vchiq",MODE="0666"

I tried changing 0 to -1 and 1 in cv2.VideoCapture(path). I reconnected the camera, rebooted and updated the Raspberry Pi. I tried enabling/disabling the legacy camera by sudo raspi-config. sudo ls/dev grep | video shows video0.

How to solve this problem?


Solution

  • I managed to get it working by adjusting the code to

    cap = cv2.VideoCapture(index, cv2.CAP_V4L)

    I saw this solution already at:

    Raspberry Pi Camera and OpenCV: can't open camera by index

    but after investigating this function I thought this only worked for windows-based devices but apparently it also works for Linux-based devices.