Webcam + Open CV Python | Black screen

懵懂的女人 提交于 2020-12-13 07:23:06

问题


I am using the code below, but I get a black image. Could you please help me rectify the error?

import cv2
import numpy as np
c = cv2.VideoCapture(0)

while(1):
    _,f = c.read()
    cv2.imshow('e2',f)
    if cv2.waitKey(5)==27:
        break
cv2.destroyAllWindows()

回答1:


Update: See github.com/opencv/opencv/pull/11880 and linked conversations, only few backends support -1 as index.


Although this is an old post, this answer can help people who are still facing the same problem. If you have a single webcam but it renders all black, use cv2.VideoCapture(-1). This will get you the working camera.




回答2:


Try this:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()

    cv2.imshow('frame',frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()



回答3:


Just change cv2.waitKey(0) to cv2.waitKey(30) and this issue will be resolved.




回答4:


This worked for me: I did a pip install imutils. Imutils is a library with series of convenience functions to make basic image processing functions such as translation, rotation, resizing, skeletonization, displaying Matplotlib images, sorting contours, detecting edges, and much more easier with OpenCV and both Python 2.7 and Python 3.

import cv2
import imutils

cap = cv2.VideoCapture(0)  # video capture source camera (Here webcam of laptop)
ret, frame = cap.read()  # return a single frame in variable `frame`


while (True):
    # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    (grabbed, frame) = cap.read()
    frame = imutils.resize(frame, width=400)
    cv2.imshow('img1', frame)  # display the captured image
    if cv2.waitKey(1) & 0xFF == ord('q'):  # save on pressing 'y'
        cv2.imwrite('capture.png', frame)
        cv2.destroyAllWindows()
        break

cap.release()



回答5:


I've faced with same problem. Updating neither opencv nor webcam driver works. I am using kaspersky as antivirus. When I disable the kaspersky, then black output problem solved.

BTW, I can see the running .py file in kaspersky console > reports > host intrusion prevention. It reports application privilege control rule triggered - application: myfile.py, result: blocked: access to video capturing devices



来源:https://stackoverflow.com/questions/29645278/webcam-open-cv-python-black-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!