Arducam not working with OpenCV VideoCapture(2) in Python, webcam works

Hi, quick question — can the Arducam 16MP autofocus camera be used with OpenCV in Python?

I’m on a Raspberry Pi with Python and cv2 installed. With a normal USB webcam I can just do:

cap = cv2.VideoCapture(2)

and everything works fine. But when I plug in the Arducam and try the exact same thing, it doesn’t work at all. It just… does nothing / no frames come out.

I’ve attached a screenshot of what I see: https://cdn.arducam.com/forum/b97b1f6da5743bb66de09abf648db093/Arducam%20opencv.PNG

Any idea what I’m doing wrong? Is the Arducam not supposed to be opened like a regular webcam?

Hi, thanks for reaching out!

Short answer: yes, you can use the Arducam 16MP with OpenCV in Python, but not the way you’re doing it with cv2.VideoCapture(2).

The Arducam (16MP autofocus) isn’t exposed like a standard UVC webcam, so opening it through a plain OpenCV VideoCapture index the same way you would a USB webcam won’t work. Instead you need to go through picamera2.

We have an example that hooks picamera2 up to OpenCV here:
https://github.com/ArduCAM/picamera2_examples/blob/main/opencv_example.py

Take a look at that and try running it — it shows how to grab frames from the camera and feed them into an OpenCV window. Can you let me know what happens when you run it?

Ok that makes sense, thanks. I’ll give the picamera2 example a shot.

One thing though — while poking around I noticed the autofocus is kind of off and the image keeps shifting brightness on its own. Is that also something I need to fix separately, or is it related?

Good catch — that’s a separate thing and it’s fixable.

First, for the autofocus, run these commands:

wget -O install_pivariety_pkgs.sh https://github.com/ArduCAM/Arducam-Pivariety-V4L2-Driver/releases/download/install_script/install_pivariety_pkgs.sh
chmod +x install_pivariety_pkgs.sh
./install_pivariety_pkgs.sh -d

Second, if you’re seeing the frame change per second because of auto-exposure and you actually want a still frame, switch to manual exposure mode with:

libcamera-still -t 0 --shutter value

where value is the exposure time — for example 500.

There’s also a short video walking through this exact problem if it helps: https://youtu.be/J45czVsvuAw

If you’d like to go over it live, you can grab a slot here: Calendly - Arducam

That helped a lot, the autofocus and exposure issues are looking much better now. Thanks.

So just to confirm the main thing: the reason cap = cv2.VideoCapture(2) never worked is that the Arducam isn’t being opened as a UVC device, right? I should always be using the picamera2 + OpenCV route instead?

Exactly right. That’s the key takeaway.

Opening the Arducam camera with cv2.VideoCapture(index) doesn’t behave the same way as a standard USB webcam — it needs a picamera2-based approach instead of the plain OpenCV VideoCapture method. So don’t use the standard cv2.VideoCapture path with this camera.

Instead, use the ArduCAM picamera2 OpenCV example as your starting point:
https://github.com/ArduCAM/picamera2_examples/blob/main/opencv_example.py

Summary of where we landed:

  • Autofocus fix — run the install_pivariety_pkgs.sh -d commands
  • Still frame / auto-exposure — use libcamera-still -t 0 --shutter value (e.g. 500)
  • OpenCV in Python — use the picamera2 example above, not cv2.VideoCapture(2)

The example linked above is the reference to follow. If you hit anything else while running it, just post back here and we’ll dig in.