Ask Your Question
0

Capturing webcam in python

asked 2017-05-31 05:41:55 -0600

I have been trying to capture my webcam in Ubuntu environment using python using the following code

import numpy as np import cv2

cap = cv2.VideoCapture(0)

while(True): # Capture frame-by-frame ret, frame = cap.read()

# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

When everything done, release the capture

cap.release() cv2.destroyAllWindows()

However, I'm getting the following error all the time

File "/home/mina/untitled10.py", line 22, in <module> cv2.imshow('frame',frame)

error: /root/mc-x64-2.7/conda-bld/opencv-3_1482256854890/work/opencv-3.1.0/modules/highgui/src/window.cpp:281: error: (-215) size.width>0 && size.height>0 in function imshow

How can I fix that? Thank you

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-05-31 10:12:47 -0600

electron gravatar image

updated 2017-05-31 15:03:49 -0600

This happens when for some reason cap.read() returns empty frame. just add a check:

while True:
    ret, frame = cap.read()
    if frame:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow('frame', gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-05-31 05:41:55 -0600

Seen: 1,008 times

Last updated: May 31 '17