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
_,?pip -Vandpip listand add the output to your question. Unles the problem is camera access failure, the versions of the packages are important for understanding the problem.