Ehsan Malik
Ehsan Malik

Reputation: 39

Error in accessing webcam with Python's OpenCV module

Python 3.7.6, opencv-python 4.4.0, Windows 10

Code

import cv2
cap = cv2.VideoCapture(1)
while True:
    success, frame = cap.read()
    cv2.imshow("frame", frame)
    if cv2.waitKey(0) & 0xFF == ord('q'):
        break

I want to access my external usb webcam. I am new to opencv and got the following error message.

C:\Users\92311\PycharmProjects\ObjectDetector\venv\Scripts\python.exe C:/Users/92311/PycharmProjects/ObjectDetector/main.py
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-2b5g8ysb\opencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Traceback (most recent call last):
  File "C:/Users/92311/PycharmProjects/ObjectDetector/main.py", line 5, in <module>
    cv2.imshow("frame", frame)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-2b5g8ysb\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'


Process finished with exit code 1

Can any one explain what's the real problem.

Upvotes: 0

Views: 4062

Answers (4)

harveeey
harveeey

Reputation: 61

Looks like you don't have a second camera try:

cap = cv2.VideoCapture(0)

Upvotes: 1

Vatsal
Vatsal

Reputation: 11

cv2.VideoCapture(0,cv2.CAP_DSHOW) if it is windows7 Try this it worked for me

Upvotes: 1

Siddharth Suresh
Siddharth Suresh

Reputation: 1

try this one

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

note that its waitkey(1) and not 0 as argument.

Upvotes: 0

azhar
azhar

Reputation: 380

first try to change cap = cv2.VideoCapture(1) to cap = cv2.VideoCapture(0) id still error issue. then uninstall opencv

pip uninstall opencv-python

reinstall it

pip install opencv-python

it works for me

Upvotes: -1

Related Questions