Ask Your Question
0

Python & Video - freeze

asked 2016-01-23 10:18:08 -0500

peter_cz gravatar image

Dears,

I got running exampe

import numpy as np
import cv2

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

while(cap.isOpened()):
    ret, frame = cap.read()

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

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

cap.release()
cv2.destroyAllWindows()

When video finishes I got error at Python console and freeze

Traceback (most recent call last):

  File "<ipython-input-1-2cc765029316>", line 1, in <module>
    runfile('C:/tmp/untitled1.py', wdir='C:/tmp')

  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/tmp/untitled1.py", line 16, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

error: ..\..\..\modules\imgproc\src\color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

What correctly to stop video?

Best regards, Peter

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-01-23 10:27:04 -0500

berak gravatar image

the answer is in your code:

ret, frame = cap.read()

if ret is False or frame == None, you reached the end of the video file, so all you need is:

ret, frame = cap.read()
if not  ret:
    break
edit flag offensive delete link more

Comments

Thanks for your help! I got it - my fault :( I just copied "python examples" from https://opencv-python-tutroals.readth... - I should better check - Thank very much helping me out!

peter_cz gravatar imagepeter_cz ( 2016-01-24 03:52:44 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-23 10:18:08 -0500

Seen: 3,003 times

Last updated: Jan 23 '16