Search code examples
pythonopencvresolutioncameracapturetaskframe-size

How to change frame resolution opencv python


i'm new to openCv, i just wrote a little program in python for a real time camera capture. I wanted to change the frame size in px, from the standard 640x480 to 1280x720. My Logitech camera has a HD resolution (720p) so i know that that's not a camera problem...

This is the code:

import numpy as np
from time import sleep
import cv2
import time

cap = cv2.VideoCapture(0)

cap.set(3,1280)
cap.set(4,720)

sleep(2)


while(True):

    ret, s = cap.read()

    cv2.imshow('frame', s)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()

but when i run it, i get this error:

================== RESTART: C:/Users/Mattia/Desktop/fgs.py ==================

    Traceback (most recent call last):
      File "C:/Users/Mattia/Desktop/fgs.py", line 18, in <module>
        cv2.imshow('frame', s)
    cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:350: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

I'm using python 3.6 and openCv 4.0

Can someone help me? Thanks!


Solution

  • I found the problem, openCV 4.0.0.21 has a bug, it just displays the error without any reason, I just changed to an older openCV version, and it just worked!

    To uninstall openCV (With Pip, on Windows 10, using CMD Administrator window):

    pip uninstall opencv-contrib-python
    

    The command to install an older version:

    pip install opencv-python==(Version you wanna install, i installed 3.1.0.5)