cv2.error: Unknown C++ exception from OpenCV code

Question:

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

Asked By: Sid

||

Answers:

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.

Answered By: Qihua Huang

try replacing this line:

vid = cv2.VideoCapture(0)

with

vid = cv2.VideoCapture(0, cv2.CAP_DSHOW)
Answered By: Marcel Suleiman
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.