简体   繁体   中英

OpenCV capture black screen video

I'm doing video tests from an Aver Media GL310 capture device to a ps4, but when I show it in Python, only a black screen appears.

I'm using OpenCV and with the webcam everything works fine. Also, at first it didn't recognize the USB index until I installed the Aver Media software, but now it shows up as black. From the software the image appears fine.

I'm using some simple code, and after searching I tried to change resolution, fps, add python exceptions in antivirus, timeouts, but nothing seems to work.

import cv2

cap = cv2.VideoCapture(2)
cap.set(3,640)
cap.set(4,480)

while True:
    ret, img = cap.read()
    cv2.imshow("input", img)

    key = cv2.waitKey(1)
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows() 

Here is an image, at low resolution it seems to be trying to show something, but at high resolution it is totally black. I appreciate any response. https://i.stack.imgur.com/YtCUv.png

The following works for me:

import cv2

#input your device number here.  For me it was 2.
cap = cv2.VideoCapture(2, cv2.CAP_DSHOW)

while True:
    ret, img = cap.read()
    cv2.imshow("input", img)

    key = cv2.waitKey(1)
    if key == 27: #ESC Key to exit
        break

cap.release()
cv2.destroyAllWindows()

I believe by default cv2 uses 640 x 480 (at least, for me it does if I don't specify). So, if 640 x 480 is what you need, the above code should work.

Strangely though, if I try adding calls to cap.set(), then, like you, I experience some issues sometimes. One of the following issues would occur: *

  1. Issue 1. Fails to work; returns a black frame each time (and rather slowly at that... ~1 per second)

  2. Issue 2. Mostly works; returns a black (or mostly black) frame as it's first frame, then returns frames normally. Note: Adding a call to time.sleep(.5) after the cap.set() calls, seems to eliminate issue 2, but does not help stop issue 1 from occurring.

    Note: Adding a call to time.sleep(.5) after the cap.set() calls, seems to eliminate issue 2, but does not help stop issue 1 from occurring. Not sure what the best solution to issue 1 would be. I tried putting everything in a method start_webcam() with a return False if the 3rd frame is black, then calling that method repeatedly until it works. Like this:

 working = start_webcam() if working == False: while(not working): working = start_webcam()

This runs, exits and reruns start_webcam() until it's successful. Surely there must be a cleaner way.

However, if, I use my other webcam (device 1), then it has no issue with calls to cap.set() and seems to run with no issues.... so likely something to do with the camera.

*Hopefully someone more experienced can elaborate what might be going on with cap.set() and how to avoid such issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM