5

I am not able to run cv2 .read() in python

Every time I run a simple code just to see myself from my webcam. It gives a strange error which I am not able to solve and I've almost searched a lot about it. I only want to capture my video through webcam.

This is my code which is probably fine according to me.

import cv2
vid = cv2.VideoCapture(0)

while True:
    _, frame = vid.read()

    # Display the resulting frame
    cv2.imshow('my_frame', frame)

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

vid.release()
cv2.destroyAllWindows()

Everytime I run this code it gives the following error -

_, frame = vid.read()
cv2.error: Unknown C++ exception from OpenCV code

I am stuck what to do to resolve this problem. I've searched a lot across the platform.

edit: I think my webcam is working fine as when I execute the code the code runs and opens a window and shows the image from webcam but the image is grey and it works only for a second or two. It immediately shows the error.

My pip version is 21.1.1. List of packages -

Package               Version
--------------------- --------
numpy                 1.20.3
opencv-contrib-python 4.5.2.52
Pillow                8.2.0
pip                   21.1.1
pywin32               300
setuptools            56.2.0

Thanks

8
  • did you try without the _,? Commented May 20, 2021 at 7:48
  • yeah, I have tried by replacing that variable too. Commented May 20, 2021 at 7:51
  • Could you try to run this example from OpenCV site: docs.opencv.org/master/dd/d43/tutorial_py_video_display.html It gives you a feedback, if camera is not ready. Commented May 20, 2021 at 7:53
  • You trying to use external tutorials (it is outdated, created in 2013). Ref: #17016 Use OpenCV documentation instead: docs.opencv.org/master/d1/d89/tutorial_py_orb.html Commented May 20, 2021 at 7:53
  • Could you open a command/bash console, run two commands pip -V and pip list and add the output to your question. Unles the problem is camera access failure, the versions of the packages are important for understanding the problem. Commented May 20, 2021 at 8:06

2 Answers 2

3

try replacing this line:

vid = cv2.VideoCapture(0)

with

vid = cv2.VideoCapture(0, cv2.CAP_DSHOW)
Sign up to request clarification or add additional context in comments.

2 Comments

I am sure it would help the community if you explained to us why your code would solve the OP's problem
I don't know exactly why it work (for me) but I had the same error message and after research, I found solution - put cv2.CAP_DSHOW into VideoCapture() as second parameter
1

Notes:

  • The first two lines don't get an error.
  • The third line, where the (_,) is, no matter how many underscores, 2, 3, or 4, it still belts out the same error.
  • replacing (_,) with return keyword will belt out errors.
  • enclosing cv2.VideoCapture(0) with open() will still get errors.
  • even when uninstalling and reinstalling OpenCV, it still does, in opencv-python 4.5.2.52.
  • Sub-line "ret" does not work in that script.
  • replacing (_,) with return keyword would still have errors.
  • deleting (_,) will still get the same error.
  • cv2 can display already taken photos fine, but not real-time in the line of code(somehow).
  • Several tutorials, despite their updated dates, their code does not work beyond (ret/s/etc.), frame = (cap/vid).read()
  • enclosing the first variable assignment with open() will enter a field of more errors.

If that's the case, and one tutorial says the vid.read() will return a tuple, then there's bias. print(vid) would return something like <VideoCapture 02D32370>.

Using a text file, like

file=open('C:\\Users\\username\\Documents\\New folder\\abcdefghijklmnopqrstuvw.txt')

and print(file), it returns

<_io.TextIOWrapper name='C:\\Users\\username\\Documents\\New folder\\abcdefghijklmnopqrstuvw.txt' mode='r' encoding='cp1252'>

before text=file.read() and print(text).

OpenCV works fine displaying static images on my computer, but not recording series of images in one set(video).

  • On my computer, there's no way other than using other modules.

Edit: Despite what I said above, you need to replace 0 in videocapture with 0+cv2.CAP_DSHOW, which basically translates to index difference (n) plus default camera index number.

2 Comments

0+cv2.CAP_DSHOW worked completely fine! Thanks.
Somehow, it takes plain old basic equations ( for example, 1+1, 2+2, 3+1, etc) and numbers as if it was just stated as None condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.