Access UVC Cameras using OpenCV, Python, and GStreamer 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.
- Download the Simplecapture.exe file
- Unzip and open the Simplecapture.exe file
Select File-Choose device-USB-OK
Select one the following figure list. Click OK.
- Capture video
2. Access UVC Web Camera using OpenCV
You can refer to the link as following:
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).
- How to install Python and OpenCV?
- 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.
Different UVC cameras should have different device names and serial numbers.
- Python Code
You can refer to here to install
pymf. Please do not install
pip is different from download by yourself).
- 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
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
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.