Skip navigation

This is a fairly specific entry – but there’s a lot of tidbits scattered about here and there that I had to get through to make this work.

The basic gist is this: I needed to play back video from a file (or stream) in a toy application I was writing for a project. I’ve been encoding stuff using x264 and ffmpeg, so I figured anything that took advantage of the same libraries would be a big help, and OpenCV has a lot of other useful algorithms.

So first, install OpenCV, Python, and wxPython. This writing works with OpenCV 2.2, Python 2.7, and wxPython 2.8. On Ubuntu 11.04.

For installing these things on Ubuntu 11.04, you can follow the guide.

So now it’s pretty straightforward. The process is:

1. Load the file through OpenCV.
2. Create a timer using wx that simulate the FPS of the video.
3. On each tick, grab the next frame from OpenCV, and display it
on the panel.

The entire code is located on my bitbucket repository, under player.py.

The only tricky bit is when you try to switch between OpenCV’s frame and a wx Bitmap:

def createBitmap(self, frame):
        # Convert the color to the space wx works in. 
        cv.CvtColor(frame, frame, cv.CV_BGR2RGB)
            
        Img = wx.EmptyImage(frame.width, frame.height)
        # Unsure why this works, assuming frame.tostring() places it in a well-known format
        Img.SetData(frame.tostring())
        
        return wx.BitmapFromImage(Img)

Also I found the wxPython documentation fairly out of date, and the OpenCV Python interface has changed a lot, so be wary of reading things that don’t tell you what version it’s for.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.