soft-matter/pims

Video object returns array of zeros on second call

EricThomson opened this issue · 6 comments

Thanks a lot for the useful package! Sorry if my question betrays a basic misunderstanding:

I have an avi file that I am reading and pims works great accessing individual frames, initially:

pims_movie = pims.Video('filename.avi')
for ind in range(10,12):
    movie_frame = pims_movie[ind]
    print("\n", ind)
    print(movie_frame[:5, :5, 0])

It prints out reasonable values:

10
[[ 85 87 89 90 93]
[ 87 89 90 92 96]
[ 88 89 91 93 97]
[ 89 92 93 94 97]
[ 92 94 96 98 102]]

11
[[ 86 88 90 90 91]
[ 90 89 90 93 95]
[ 90 90 91 95 96]
[ 91 93 95 96 98]
[ 94 96 97 99 102]]

But if I run the for loop again (without reloading the movie), I get arrays of zeros back:

10
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]

11
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]

Is this expected behavior? Am I supposed to be closing out the video and reloading it, or using a context manager, or something?

pgunn commented

It looks like there are a few more ways to confuse the pims handle:

>>> pimfh = pims.Video('/mnt/home/pgunn/caiman_data/example_movies/msCam13.avi')
>>> pimfh[100][100].max()
128
>>> pimfh[100][100].max()
128
>>> data = np.array(pimfh)[200:250,100:120,500:510,0]
>>> pimfh[100][100].max()
0
pgunn commented

The code I pasted above shows the issue with pims 0.6.1

pgunn commented

In case it's helpful, the data file I'm using is available here (it's a dataset used as part of a scientific software package):

https://caiman.flatironinstitute.org/~neuro/caiman_downloadables/msCam13.avi

pgunn commented

I found a very weird way to reset the pims handle back to a healthy state, which may hint as to what's going on:

>>> pimfh[100][100].max()
128
>>> data = np.array(pimfh)[200:250, 100:120, 500:510, 0]
>>> pimfh[100][100].max()
0
>>> _ = pimfh[1]
>>> pimfh[100][100].max()
0
>>> _ = pimfh[0]
>>> pimfh[100][100].max()
128

I'll still rely on the pims developers to fully understand (and ideally patch) it, but we may be able to workaround the bug for now.

pgunn commented

In digging into this, it's actually not pims' fault, as we've found a way to reproduce this using moviepy calls (which is the backend both of us are using).

>>> mph = moviepy.editor.VideoFileClip(fn)
>>> mph.size
[752, 480]
>>> mph.duration
50.0
>>> mph.fps
20.0
>>> np.array(mph.get_frame(0)).max()
173
>>> np.array(mph.get_frame(2)).max()
206
>>> np.array(mph.get_frame(1)).max()
0
>>> np.array(mph.get_frame(0)).max()
173
>>> np.array(mph.get_frame(1)).max()
188

We'll reach out to the moviepy project.