Basic example halts partway through video
I'm using a basic example from
as a basis for a rat-tracking program. It's working great, except for the following issue.
It runs for the first 45 seconds of the video, then gives the error "OpenCV Error: Assertion failed ((scn == 3 :: scn == 4) && (depth == CV_8U :: depth == CV_32F))" at the same frame each time. It gives this error for multiple videos, at slightly different points in each video. I tried converting the video to an avi format- it stops at the same point. There is nothing visually wrong with the video (I can watch it fine on normal video players). I've tried googling the problem, to no avail. Any suggestions would be appreciated.
import numpy as np
import cv2
cam = cv2.VideoCapture('Video2.wmv')
frames=0
while(True):
# Capture frame-by-frame
ret, frame = cam.read()
frames=frames+1
print frames
# 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
print frames
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
i got no idea, how an arbitrary single frame from a video could be invalid, but let's play paranoid, insert something similar to :
before the cv2.cvtColor(). somehow it does not like it's dinner ;)
Hm. Before every previous frame, it prints "True (720, 1280, 3)". But for the error-causing frame it prints "False ()". ... how on earth does a single frame become invalid? And how do I remove it?
honestly, no idea, how a single invalid frame can happen in a video (never seen that before).
but the remedy is quite simple:
(you definitely don't want to bailout before the cv2.waitKey(1) call, else you don't release the capture properly)
Ugg. I tried several variations on that. It freezes on the current frame in the video, and eventually crashes python. I assume that may mean that all subsequent frames are invalid as well? How can that be if they play properly in a video player?
again, the main cause is: your video file is broken.
But it has the same problem on multiple videos. Argh. I suppose I'll try taking video again tomorrow and see if that has any effect. Thanks for the suggestions :)
just as a sidenote: the code from http://opencv-python-tutroals.readthedocs.org is meant to be used with opencv3.0, not the current 2.4.9.
abid did a nice job there, imho, but there might be a lot of slight diffs between your current cv2 version and that.
(your current example does not suffer from that by sheer luck, just saying ..)
((and again, 3.0 is long overdue...;))
Okay, so I did actually manage to get a couple of other test videos today. Every single one has the same problem. Something like 1/3 of the way through each video (very consistently 1/3 regardless of length) I have this same problem. When I just run the program on the camera feed, it works fine. (The camera is a toshiba webcam.) I tried recording directly with python (generating an avi file with videoWriter) and that makes a video just fine. But when I apply this program to that video, it STILL breaks. I don't know what else to try.
waa, that's bad. no idea, unfortunately.
probably still an internal python wrapper problem, but without a deep knowledge of the python wrapping it won't be easy to provide an off the shelf solution I am afraid...