2

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()
Akanksha Dangi
  • 31
  • 1
  • 1
  • 3

8 Answers8

6

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.

Praveen
  • 73
  • 2
  • 5
  • This happens to me after continuously running the capture a couple hours. No matter it is logitech or other brands. After went black, I run this cv2/VideoCapture(-1) and it comes back to life. Wonder if @praveen you know whats the reason and how this helped? And what this is different from cv2.release() – thsieh Jul 26 '18 at 15:51
  • Just realized that only few backends support the use of '-1' to search for the first available/free camera. See https://github.com/opencv/opencv/pull/11880 and linked conversations. – Praveen Jun 26 '19 at 05:16
4

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

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Amit kumar
  • 81
  • 1
  • 10
4

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

johncasey
  • 1,250
  • 8
  • 14
1

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()
Aphire
  • 1,621
  • 25
  • 55
0

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()
Aphire
  • 1,621
  • 25
  • 55
Durodola Opemipo
  • 319
  • 2
  • 12
0

Try put -0 on the index and pause any antivirus running

import cv2
import numpy as np

cap = cv2.VideoCapture(-0)
cap.set(3,640)
cap.set(3,480)

while(True):
    success, img = cap.read()

    cv2.imshow('frame',img)

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

I faced the same issue after many calls with:

cap = cv2.VideoCapture(0)

and it solved when I changed the index to 1 :

cap = cv2.VideoCapture(1)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 13 '23 at 13:12
-1

In my case just disabling Kaspersy has solved the problem.

M--
  • 25,431
  • 8
  • 61
  • 93