0

There are already many questions exist regarding cv2.VideoCapture but it doesn't help me. I am using OpenCV 2.4.10 and python 2.7.9 , I am trying to capture video from built-in webcam (Toshiba ,Windows 7) . I am using this code

import numpy as np 
import cv2

cap = cv2.VideoCapture(1)
print cap.isOpened()
print cap.get(3)
print cap.get(4)
while(True):
    ret,frame=cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

And the problem is , When i first run this code it throws error : error: (-215) size.width>0 && size.height>0 in function cv::imshow

error

and just after waiting for hardly one minute , i again run this code and this time it's fine fine

And this is happening again and again , sometimes it's work good but sometimes fails and give error. Any solution to this ?

I copied the opencv_ffmpeg2410.dll file from C:\Applications\opencv\build\x86\vc10\bin into the python path C:\Python27(OpenCV 2.4 VideoCapture not working on Windows)

I tried cv2.VideoCapture(0) but in this case web-camera didn't even start (web-camera light did not blink)

Community
  • 1
  • 1
udit043
  • 1,610
  • 3
  • 22
  • 40

1 Answers1

4

Since you are not checking the value of the ret return value, it is possible that cap.read() is giving you a bad frame.

Replace with something like

while True:
    ret, frame = cap.read()
    if ret:
        cv2.imshow('window', frame)
        # ...
Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75