Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

there's a difference between webcams and movie files - webcams have an endless stream, while with files you eventually reach the end of it. please make it a HABIT to check return values, and for empty frames, whenever you read images from videos, disk, etc..

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
    ret, frame = cap.read()
    if not ret: break # movie's over !

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

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

cap.release()
cv2.destroyAllWindows()

there's a difference between webcams and movie files - webcams have an endless stream, while with files you eventually reach the end of it. please make it a HABIT to check return values, and for empty frames, whenever you read images from videos, disk, etc..

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
    ret, frame = cap.read()
    if not ret: break # movie's over !

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

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

cap.release()
cv2.destroyAllWindows()

ps: please do not use the outdated tutroals, opencv's python tutorials are here