Getting started

84 views
Skip to first unread message

Chris Barnes

unread,
Oct 10, 2014, 12:41:54 PM10/10/14
to python...@googlegroups.com
I'm a new grad student who's spent the last 3 days trying to grab some frames from a .avi with zero success. I've tried pythons 2 and 3, openCV, simpleCV, simpleITK, scikit-image, and ffmpeg, and have run into different issues with each. Is it really that hard? There's next to no documentation around for all of them, for sure.

All of the above claim to have installed correctly (e.g. skimage, SimpleITK and SimpleCV can be imported fine, openCV responds fine to cv2.__version__, which is what they claim defines a healthy install).

I guess the nut to crack is openCV as that's the one that's talked about most on the internet. However, using this code snippet from http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

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()

cap.isOpened() is always false, so no reading gets done.

I know that skimage uses openCV as the backend, but I gave it ago anyway, using this snippet from http://scikit-image.org/docs/dev/api/skimage.io.html

class AVILoader:
   
video_file = 'myvideo.avi'

   
def __call__(self, frame):
       
return video_read(self.video_file, frame)

avi_load = AVILoader()

frames = range(0, 1000, 10) # 0, 10, 20, ...
ic = ImageCollection(frames, load_func=avi_load)

x = ic[5] # calls avi_load(frames[5]) or equivalently avi_load(50)

Of course, that's junk too because video_read just isn't a function. Google gets 8 results associating video_read with skimage.

I also tried this method to get the video straight into a numpy array http://zulko.github.io/blog/2013/09/27/read-and-write-video-frames-in-python-using-ffmpeg/

It took a long time, crash my IDE a couple of times, and came up with no data at the end.

I'm frustrated. How do you get started?

Stéfan van der Walt

unread,
Oct 10, 2014, 7:39:32 PM10/10/14
to python...@googlegroups.com
Hi Chris

On Fri, Oct 10, 2014 at 6:41 PM, Chris Barnes <cluster...@gmail.com> wrote:
> I'm a new grad student who's spent the last 3 days trying to grab some
> frames from a .avi with zero success.

Sorry, video grabbing is a bit hairy at the moment.

We're working on some documentation here:

https://github.com/scikit-image/scikit-image/pull/1012/files

You may also have some success with the video module that we removed
because we could not guarantee its working on all platforms:

https://github.com/scikit-image/scikit-image/pull/961/files

Alternatively, you can convert your .avi to .png frames using ffmpeg,
vlc, mdecoder, etc. Let us know if you need a hand.

Regards
Stéfan

Christoph Gohlke

unread,
Oct 10, 2014, 10:41:44 PM10/10/14
to python...@googlegroups.com
On 10/10/2014 9:41 AM, Chris Barnes wrote:
> I'm a new grad student who's spent the last 3 days trying to grab some
> frames from a .avi with zero success. I've tried pythons 2 and 3,
> openCV, simpleCV, simpleITK, scikit-image, and ffmpeg, and have run into
> different issues with each. Is it really that hard? There's next to no
> documentation around for all of them, for sure.
>

In case you are using Windows, try vidsrc
<http://www.lfd.uci.edu/~gohlke/code/vidsrc.cpp.html>, which reads frame
data of video files via
Microsoft's DirectShow IMediaDet interface. Binaries are included in the
vLFD package installer at <http://www.lfd.uci.edu/~gohlke/pythonlibs/#vlfd>

from vidsrc import VideoSource
video = VideoSource("test.avi")
for frame in video:
print(frame.mean())

Christoph

Chris Barnes

unread,
Oct 13, 2014, 12:45:01 PM10/13/14
to python...@googlegroups.com
Thanks Stefan (and Christoph), I've managed to get some frames out of the .avi just using ffmpeg.

I had assumed that that was the easiest way to do video analysis, but I suppose working directly with the video would be the way to go for speed - which brings me back to openCV. Any idea why that code snippet may be failing? cap.isOpened() always returns false, so it never hits the code in the while loop.
Reply all
Reply to author
Forward
0 new messages