0

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.

Ehsan Malik
  • 39
  • 1
  • 9
  • Did you look at this [OpenCV Python: cv2.VideoCapture can only find 2 of 3 cameras](https://stackoverflow.com/questions/49663474/opencv-python-cv2-videocapture-can-only-find-2-of-3-cameras-windows-camera-app/49901961)? – Ahmet Sep 22 '20 at 12:02

4 Answers4

1

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

cap = cv2.VideoCapture(0)

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
harveeey
  • 41
  • 7
1

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

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Vatsal
  • 11
  • 1
0

try this one

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

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

Flair
  • 2,609
  • 1
  • 29
  • 41
-1

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

shimo
  • 2,156
  • 4
  • 17
  • 21
azhar
  • 351
  • 3
  • 13