Python OpenCV access webcam maximum resolution
The problem as mentioned above is caused by the camera driver. I was able to fix it using Direct Show as a backend. I read (sorry, but I do not remember where) that almost all cameras provide a driver that allows their use from DirectShow. Therefore, I used DirectShow in Windows to interact with the cameras and I was able to configure the resolution as I wanted and also get the native aspect ratio of my camera (16: 9).You can try this code to see if this works for you.
import cv2cam = cv2.VideoCapture(0,cv2.CAP_DSHOW)cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)r, frame = cam.read()...print('Resolution: ' + str(frame.shape[0]) + ' x ' + str(frame.shape[1]))
In the OpenCV documentation, I found the following information for those who want to know more about OpenCV backends (OpenCV docs)
I hope this can help you!
I used the different resolutions to set image resolution from List of common resolutions by looping over
def set_res(cap, x,y): cap.set(cv.CV_CAP_PROP_FRAME_WIDTH, int(x)) cap.set(cv.CV_CAP_PROP_FRAME_HEIGHT, int(y)) return str(cap.get(cv.CV_CAP_PROP_FRAME_WIDTH)),str(cap.get(cv.CV_CAP_PROP_FRAME_HEIGHT))
It seems that OpenCV or my camera allows only certain resolutions.
160.0 x 120.0
176.0 x 144.0
320.0 x 240.0
352.0 x 288.0
640.0 x 480.0
1024.0 x 768.0
1280.0 x 1024.0
I got it to work, so this post is for others experiencing the same problem:
I am running on the Logitech C270 as well. For some reason it would only show 640x480 even though the webcam supports 1280x720. Same issue persists with the built-in webcam in my laptop.
If I set it to 800x600 in the code it shows 640x480. However, if I set it to 1024x768 it becomes 800x600. And if I set it to something silly like 2000x2000 it becomes 1280x720.
This is in C++ on OpenCV 3.0, but perhaps it applies to Python as well.