VideoCapture doesn't open

import cv2

import keyboard

from time import sleep

cap = cv2.VideoCapture(2, cv2.CAP_DSHOW)

cap .set(cv2.CAP_PROP_FRAME_WIDTH, 480)

cap .set(cv2.CAP_PROP_FRAME_HEIGHT, 320)

cap2 = cv2.VideoCapture(3, cv2.CAP_DSHOW)

cap2.set(cv2.CAP_PROP_FRAME_WIDTH, 480)

cap2.set(cv2.CAP_PROP_FRAME_HEIGHT, 320)

num = 0

while cap.isOpened():

    """

    succes1, img = cap.read()

    succes2, img2 = cap2.read()

    """

    succes1 = cap.grab()

    succes1, img = cap.retrieve()

    succes2 = cap2.grab()

    succes2, img2 = cap2.retrieve()

    k = cv2.waitKey(5)

    if k == 27:

        break

    if keyboard.is_pressed('s'): # wait for 's' key to save and exit

        cv2.imwrite('images/stereoLeft/imageL' + str(num) + '.png', img)

        cv2.imwrite('images/stereoright/imageR' + str(num) + '.png', img2)

        print("images saved!")

        num += 1

        sleep(0.5)

    cv2.imshow('Img 1',img)

    cv2.imshow('Img 2',img2)

# Release and destroy all windows before termination

cap.release()

cap2.release()

cv2.destroyAllWindows()

im using the code above to get images in order to calibrate my stereo camera which uses 1 usb for both cams , however i cant have my 2nd camera have higher resolution then my first , and i cant set them both up to 720p, even tho if i open 1 on 720p here and other on windows app with 720p. Highest i can go with both of them is 480x320. Any ideas why my resolutions are stuck ?
This is the error i get :

"Traceback (most recent call last):
  File "d:\Self\cvpack\StereoVisionDepthEstimation\calibration_images.py", line 39, in <module>
    cv2.imshow('Img 2',img2)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-1i5nllza\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'"

please NEVER post images of code anywhere on the net …

fixed it or should i have opened another issue ?

1 Like

no, all fine, thank you !

img2 is None.

why? because you don’t do any error checking on your use of VideoCapture. official OpenCV tutorials show how to do that.

most likely, you’re saturating the usn hub.
if you have more than one, put them on different ports

i can open 2x 720p videos if i use ecam(a camera view app) and windows camera app at the same time , if usb was saturating i wouldnt be able to do that i think ?

uh no. those apps accept lossy compressed formats, which allow higher frame rates and resolutions as a tradeoff. OpenCV avoids them, as far as I know.

is there a way for me to make opencv accept it aswell ? image quality was decent enough ,

also another weird thing i can do 320p+720p which is around 1.075.200 pixels , but i cant do 2x 480p 614.400 pixels if i didnt calculate it wrong , shouldnt near 60% of pixels be allowed ?

you can try setting CAP_PROP_FOURCC like cap.set(cv.CAP_PROP_FOURCC, cv.VideoWriter_fourcc(*"MJPG"))

That worked , thanks a lot