Skip to content

Access UVC Cameras using OpenCV, Python, and GStreamer on Windows

General Introduction


Arducam UVC cameras comply with UVC protocol and work with web-camera applications out-of-box. But advanced users might want to access these UVC cameras through their own code using Python and OpenCV with advanced controls for their machine vision and AI applications. In this article, we will cover how to access and control the Arducam UVC cameras in detail.

Windows Guide


1. Capturing Video from UVC Web camera on Windows using Media Foundation

You can refer to this link for the source code.

  • Download the Simplecapture.exe file

Download link:

https://www.codeproject.com/KB/audio-video/559437/SimpleCapture-exe.zip

  • Unzip and open the Simplecapture.exe file

image 38

Select File-Choose device-USB-OK

image 39

Select one the following figure list. Click OK.

image 40

  • Capture video

image 41

2. Access UVC Web Camera using OpenCV

You can refer to the link as following:

https://stackoverflow.com/a/606154

https://www.geeksforgeeks.org/python-opencv-capture-video-from-camera/

Note

Please use the version shown in the figure below and the upgraded version of OpenCV (the previous version of OpenCV maybe cannot access the camera normally).

picture11

  • How to install Python and OpenCV?
python3 -m pip install opencv-python
  • Python code
import cv2
'''
apiPreference    preferred Capture API backends to use. 
Can be used to enforce a specific reader implementation 
if multiple are available: 
e.g. cv2.CAP_MSMF or cv2.CAP_DSHOW.
'''
# open video0
cap = cv2.VideoCapture(0, cv2.CAP_MSMF)
# set width and height
# cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# set fps
# cap.set(cv2.CAP_PROP_FPS, 30)
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    # Display the resulting frame
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

3. Access multiple UVC Web Camera

  • Modify device name and serial number

It is different from Linux, you need to identify your UVC cameras by device name when using multiple UVC cameras on windows. Please refer to the document to modify Arducam UVC Camera serial number.

Note

Different UVC cameras should have different device names and serial numbers.

After modified:

img

img

  • Python Code
from pymf import get_MF_devices
device_list = get_MF_devices()
for i, device_name in enumerate(device_list):
    print(f"opencv_index: {i}, device_name: {device_name}")

Note

You can refer to here to install pymf. Please do not install pymf using pip(the pymf in pip is different from download by yourself).

4. Troubleshooting

  • Check camera function

Before using OpenCV to access the camera, you can check whether the camera is working properly through a graphical interface application firstly.

Way1 – You can refer to the doc to check your camera.

Way2 – You can use the .exe file to check your camera, download, and how to use refer to the chapter.

  • Fail to access camera

img

If you have the same problem, please follow the steps below:

1.Switch API(cv2.CAP_MSMF or cv2.CAP_DSHOW).

2.Set a larger frame rate.

  • Frame rate cannot set, and is too slow

img

Using cv2.CAP_DSHOW API backends may encounter frame rates that cannot be set and is at a very low level. At this time, it is recommended to use cv2.CAP_MSMF API backend.


Access UVC Cameras using OpenCV, Python, and GStreamer on Linux