0

This is my first time asking so this is a rather basic question. I'm trying to play saved videos using Anaconda on Windows, but for some reason nothing is playing. The intent is to play the current file, and then progress up to visual tracking in real time. Here is my code:

import numpy as np
import cv2

cap = cv2.VideoCapture('Animal3.h264')

while(cap.isOpened()):

    print 'opened'
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.Color_BGR2GRAY)

    cv2.imshow('frame', gray)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        print 'break'
        break
cap.release()

cv2.destroyAllWindows()

print 'end'

And when I run it nothing happens. It just tells me what file I'm running out of. What am I doing wrong?

karlphillip
  • 92,053
  • 36
  • 243
  • 426
vis_Research
  • 65
  • 2
  • 7

1 Answers1

1

The main problem is that y0u 4r3 n0t c0d1ng s4f3ly: you should always test the return of functions or the validity of the parameters returned by these calls.

These are the most common reasons why VideoCapture() fails:

Anyway, here's what you should be doing to make sure the problem is in VideoCapture():

cap = cv2.VideoCapture('Animal3.h264')
if not cap:
    print "!!! Failed VideoCapture: unable to open file!"
    sys.exit(1)

I also suggest updating the code to:

key = cv2.waitKey(25) 
if  key == ord('q'):
    print 'Key q was pressed!'
    break
Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • I actually did do this before, I just didn't show it in the code. the file opens, it gets passed to the while loop, but then it doesn't show the frame that I reference. I apologize if this all sounds incredibly juvenile. I'm a beginner trying to learn on my own, and this is actually the extent of my knowledge. Not me trying for the easy way out. – vis_Research Nov 12 '14 at 20:40
  • @vis_Research There might be a problem in the way you perform the `waitKey()` condition. I updated my answer to show a more clear way of doing it. – karlphillip Nov 12 '14 at 21:00