1. Home
  2. Docs
  3. UVC Camera Module
  4. Access UVC Cameras using OpenCV, Python, and Media Foundation on Windows

Access UVC Cameras using OpenCV, Python, and Media Foundation on Windows

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.

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

You can refer to this link for the source code.

1.1 Download the Simplecapture.exe file

Download link:

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

1.2 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

1.3 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).

2.1 How to install Python and OpenCV?

Please open the collapsed content

1.Instll Python

Install Python on Windows please refer to the following link:

https://realpython.com/installing-python/#how-to-install-python-on-windows

2.Install OpenCV

Open cmd or PowerShell

Tip: How to open cmd

-Press the Win key on your keyboard
-Type cmd
-Press enter

Tip: How to open PowerShell

-Press the Win key on your keyboard
-Type PowerShell
-Press enter

Enter the following command:

python -m pip install opencv-python

3.Run Python code or file

You can refer to the link.

  • Creat main.py file
  • Type Python code, such as print(“Hello World!”)
  • Save file
  • Open the PowerShell/cmd window and switch the directory to the directory of the main.py file
  • Execute mian.py

2.2 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

3.1 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:

3.2 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

4.1 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.

4.2 Fail to access camera

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.

4.3 Frame rate cannot set, and is too slow

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.

Was this article helpful to you? Yes 1 No