Hello all. I would like to integrate a live video stream from an
attached video device (webcam, USB capture, etc) into a panel in a
wxPython GUI. I initially found sample code from the folks at OpenCV
(http://opencv.willowgarage.com/wiki/wxpython) but it seems to be
older versions of OpenCV, namely version 1.x. I have attempted to
convert this code for OpenCV 2.1 but I'm having difficulties getting
the GUI to execute. I'm not sure what exactly the problem is, but I
suspect that there might be a few more changes from 1.x to 2.1 other
than just syntax. Does anyone have any experience with OpenCV 2.1 +
wxPython? I would be grateful for any suggestions. Here is my attempt
to far:
import wx
import sys
import cv
sys.path.append('C:\OpenCV2.1\Python2.6\Lib\site-packages')
class captureTest(wx.Frame):
TIMER_PLAY_ID = 101
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1)
self.capture = cv.CaptureFromCAM(0)
capturedImage = cv.QueryFrame(self.capture)
self.SetSize((capturedImage.width, capturedImage.height))
self.displayPanel = wx.Panel(self, -1)
#dst = cv.CreateImage(cv.GetSize(capturedImage),
cv.IPL_DEPTH_16S, 3) # NEED THIS?
cv.CvtColor(capturedImage, capturedImageModified,
cv.CV_BGR2RGB)
self.buildBmp =
wx.BitmapFromBuffer(capturedImageModified.width,
capturedImageModified.height, capturedImageModified.tostring())
self.Bind(wx.EVT_PAINT, self.onPaint)
self.playTimer = wx.Timer(self, self.TIMER_PLAY_ID)
wx.EVT_TIMER(self, self.TIMER_PLAY_ID, self.onNextFrame)
fps = gui.cvGetCaptureProperty(self.capture,
gui.CV_CAP_PROP_FPS)
self.Show(True)
if fps!=0: self.playTimer.Start(1000/fps) #every X ms
else: self.playTimer.Start(1000/15) #assuming 15 fps
def onPaint(self, evt):
if self.buildBmp:
dc=wx.BufferedPaintDC(self.displayPanel, self.buildBmp)
evt.Skip()
def onNextFrame(self, evt):
capturedImage = cv.QueryFrame(self.capture)
if capturedImage:
cv.CvtColor(capturedImage, capturedImageModified,
cv.CV_BGR2RGB)
self.buildBmp.CopyFromBuffer(capturedImageModified.tostring())
self.Refresh()
evt.Skip()
if __name__=="__main__":
app = wx.App()
app.RestoreStdio()
captureTest(None)
app.MainLoop()