-1

On Windows 7 I am using following python code in order to display webcam or laptop cam feed in open CV window. But the problem is that I can see both laptop camera light and in case of usb camera light lit up but the window shown no image just empty/grey.

Code:

import cv2

vid = cv2.VideoCapture(0)
while(True):
     
    ret, frame = vid.read()
    cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  
vid.release()
cv2.destroyAllWindows()

They both work in skype and other utilities. Even on VLC as well. I have checked in my device manager and they both show up.

Please help me how to debug this issue. Because same is happening on my raspberry pi.

Regards, Yasar

Edit 1: Guys - Thank you for your responses but as I have already mentioned above that on cv2.VideoCapture(0) I can see my laptop's cam light turning on and on cv2.VideoCapture(1), my webcam's light turns on but no frame is returned. I have already checked the forums but unable to solve my issue.

import cv2

vid = cv2.VideoCapture(0)
print (vid)
print (vid.isOpened())
while(vid.isOpened()):
     
    ret, frame = vid.read()
    print (ret)
    print (frame)
    if not ret:
        break
    cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  
vid.release()
cv2.destroyAllWindows()

Output of above is as below: <VideoCapture 0000000002DAE970> True False None

Please suggest.

Yasar
  • 11
  • 5

1 Answers1

2

First thing is to ensure error checking,

Your while loop won't stop until you manually terminate the program.

Try using this :

import cv2

vid = cv2.VideoCapture(0)
while(vid.isOpened()):
     
    ret, frame = vid.read()
    if not ret:
        break
    cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  
vid.release()
cv2.destroyAllWindows()

Now, incase this won't solve the problem,

The camera maybe attached to another serial bus, i.e. cv2.VideoCapture(1)/cv2.VideoCapture(2).

Please use

  • lsusb for linux
  • Get-PnpDevice -InstanceId 'USB*' for windows

To find your device ID.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
HindicatoR
  • 120
  • 1
  • 3