OpenCV 2.1: Integration video capture into wxPython

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

imo Python is going to be too slow for frame-by-frame capture with open-cv.

Have you thought about having the video loop in c and sending a message
in python to the C loop when you need to pick up a frame?

I’m working in OpenCV using python.multiprocessing but I am doing
all the video handling in C++. The C++ code sends messages to the python
code in a message bus on tcp/ip when it finds information in the images.

The ‘brain’ is in python…

Then I have a ‘dumb’ wxpython display, also connected to the tcp/ip
bus which picks up information and displays it to the user.

http://bitbucket.org/djlyon/smp-driverless-car-robot/src

···

On Sat, Nov 27, 2010 at 9:31 AM, Convoluted xenon2k8@gmail.com wrote:

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

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

I would tend to agree, and this was definitely the case with the
VideoCapture library (http://videocapture.sourceforge.net/) but OpenCV
appears to be more efficient. If I utilize OpenCV's highgui and a
compiled Python library (.pyd) I can actually get some excellent
results, 640x480 @ 30fps with ~10% CPU usage on old Intel T-series
dual core CPU (see below for code, it's very simple). Now I'm not sure
if the speed is attributed to highgui but in theory should I not be
able to achieve a similar result with wxPython?

I also like the idea of sending the vision processing to C, but I
can't even image how I would achieve this. I've used TCP/IP to send
simple strings between two Python programs, but how would I grab the
frame from the C/C++ program (don't have much experience with C++)? Or
would the string I send be the image data that then needs to be
converted to an image (in which case, would this not require the
majority of processing?). I'm not even sure where to begin searching
out this solution. Perhaps the answer lies in the link you posted
(which I haven't yet taken a look at).

Please keep the suggestion coming! Thanks

import cv
cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM(0)
while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    if cv.WaitKey(10) == 27:
        break

···

On Nov 26, 3:03 pm, David Lyon <david.lyon.preissh...@gmail.com> wrote:

imo Python is going to be too slow for frame-by-frame capture with open-cv.

Have you thought about having the video loop in c and sending a message
in python to the C loop when you need to pick up a frame?

I'm working in OpenCV using python.multiprocessing but I am doing
all the video handling in C++. The C++ code sends messages to the python
code in a message bus on tcp/ip when it finds information in the images.

The 'brain' is in python..

Then I have a 'dumb' wxpython display, also connected to the tcp/ip
bus which picks up information and displays it to the user.

http://bitbucket.org/djlyon/smp-driverless-car-robot/src

On Sat, Nov 27, 2010 at 9:31 AM, Convoluted <xenon...@gmail.com> wrote:
> 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()

> --
> To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com<wxPython-users%2Bunsubscribe@googlegroups.com>
> or visithttp://groups.google.com/group/wxPython-users?hl=en

import cv

cv.NamedWindow(“camera”, 1)

capture = cv.CaptureFromCAM(0)

while True:

img = cv.QueryFrame(capture)

cv.ShowImage("camera", img)

if cv.WaitKey(10) == 27:

    break

is that all you do? will that take from a camera source and display it on the screen?

···

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en


Hi, I will kill all ads in google gmail.

They will all be dead and gone for all my emails to you. HA HA bye bye ads I just massacred you!!!

Yes indeed. The code is found in the "camera.py" in the folder
OpenCV2.1\samples\python. It uses OpenCV's highGUI module, which is
great for testing, but too limited for what I need.

In case anyone is interested, I was able to get the above wxPython
code to work by playing around with some of the functions/values. Not
exactly sure what I changed to make it work, but nevertheless it does
work. Seems to be fairly finicky. The video capture also seems to be
very efficient, only utilizing 5% cpu to provide 640x480 at around
20fps (guessing, may be slightly higher). The FPS can also be
modified, but I'm sure increasing it will eat up more processing
power.

···

On Nov 26, 6:58 pm, Micah Nordland <mpnordl...@gmail.com> wrote:

> import cv
> cv.NamedWindow("camera", 1)
> capture = cv.CaptureFromCAM(0)
> while True:
> img = cv.QueryFrame(capture)
> cv.ShowImage("camera", img)
> if cv.WaitKey(10) == 27:
> break

> is that all you do? will that take from a camera source and display it on

the screen?

> --
> To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com<wxPython-users%2Bunsubscribe@googlegroups.com>
> or visithttp://groups.google.com/group/wxPython-users?hl=en

--
Hi, I will kill all ads in google gmail.
They will all be dead and gone for all my emails to you. HA HA bye bye ads I
just massacred you!!!

I tried and got the same problem as you originally asked about.

Can you post back your final code so that I can see the fix?

···

On Sat, Nov 27, 2010 at 3:01 PM, Convoluted xenon2k8@gmail.com wrote:

Yes indeed. The code is found in the “camera.py” in the folder

OpenCV2.1\samples\python. It uses OpenCV’s highGUI module, which is

great for testing, but too limited for what I need.

In case anyone is interested, I was able to get the above wxPython

code to work by playing around with some of the functions/values. Not

exactly sure what I changed to make it work, but nevertheless it does

work. Seems to be fairly finicky. The video capture also seems to be

very efficient, only utilizing 5% cpu to provide 640x480 at around

20fps (guessing, may be slightly higher). The FPS can also be

modified, but I’m sure increasing it will eat up more processing

power.

On Nov 26, 6:58 pm, Micah Nordland mpnordl...@gmail.com wrote:

import cv

cv.NamedWindow(“camera”, 1)

capture = cv.CaptureFromCAM(0)

while True:

img = cv.QueryFrame(capture)

cv.ShowImage(“camera”, img)

if cv.WaitKey(10) == 27:

   break

is that all you do? will that take from a camera source and display it on

the screen?

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.comwxPython-users%2Bunsubscribe@googlegroups.com

or visithttp://groups.google.com/group/wxPython-users?hl=en

Hi, I will kill all ads in google gmail.

They will all be dead and gone for all my emails to you. HA HA bye bye ads I

just massacred you!!!

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Sure, no problem. The code is below. I also added in double buffering
to eliminate flicker (Windows only, Linux/Mac users, remove the double
buffering code), and it is currently set at 15FPS. I've tested it on
an integrated webcam on Win 7 using Python 2.6.6 ad wxPython 2.8.10.
You will also need pywin32 for the double buffering and OpenCV 2.1 in
your c: directory (or just change path in code so it points to the
compiled .pyd library).

Please try it out on your system at let me know how it goes. Like I
mentioned, it's a bit finicky at the moment. For instance, sometimes
when I run the code it will ask for the video source, which ends up
crashing the code. Other times it won't and will end up displaying the
feed quite nicely. If you get the box asking for the video source
repeatedly, just restart your PC and it seems to fix this. I haven't
tested the code very much, so any suggestions for improvement are
definitely welcome.

By the way, is there any better way to post code here? I read it
should be attached or something, but I don't see the option...

import wx
import win32api
import win32con

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)

        def SetCompositeMode(self, on=True):
            exstyle = win32api.GetWindowLong(self.GetHandle(),
win32con.GWL_EXSTYLE)
            if on:
                exstyle |= win32con.WS_EX_COMPOSITED
            else:
                exstyle &= ~win32con.WS_EX_COMPOSITED
            win32api.SetWindowLong(self.GetHandle(),
win32con.GWL_EXSTYLE, exstyle)

        SetCompositeMode(self, True)

        self.capture = cv.CaptureFromCAM(0)
        capImg = cv.QueryFrame(self.capture)
        self.SetSize((capImg.width, capImg.height))
        self.displayPanel = wx.Panel(self, -1)
        cv.CvtColor(capImg, capImg, cv.CV_BGR2RGB)
        self.buildBmp = wx.BitmapFromBuffer(capImg.width,
capImg.height, capImg.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 = cv.GetCaptureProperty(self.capture, cv.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):
        capImg = cv.QueryFrame(self.capture)
        if capImg:
            cv.CvtColor(capImg, capImg, cv.CV_BGR2RGB)
            self.buildBmp.CopyFromBuffer(capImg.tostring())
            self.Refresh()
        evt.Skip()

if __name__=="__main__":
    app = wx.App()
    app.RestoreStdio()
    captureTest(None)
    app.MainLoop()

I would really like to get a nice working piece of code and I'm sure
with combined support we can get it done

···

On Nov 27, 6:15 am, David Lyon <david.lyon.preissh...@gmail.com> wrote:

I tried and got the same problem as you originally asked about.

Can you post back your final code so that I can see the fix?

On Sat, Nov 27, 2010 at 3:01 PM, Convoluted <xenon...@gmail.com> wrote:
> Yes indeed. The code is found in the "camera.py" in the folder
> OpenCV2.1\samples\python. It uses OpenCV's highGUI module, which is
> great for testing, but too limited for what I need.

> In case anyone is interested, I was able to get the above wxPython
> code to work by playing around with some of the functions/values. Not
> exactly sure what I changed to make it work, but nevertheless it does
> work. Seems to be fairly finicky. The video capture also seems to be
> very efficient, only utilizing 5% cpu to provide 640x480 at around
> 20fps (guessing, may be slightly higher). The FPS can also be
> modified, but I'm sure increasing it will eat up more processing
> power.

> On Nov 26, 6:58 pm, Micah Nordland <mpnordl...@gmail.com> wrote:
> > > import cv
> > > cv.NamedWindow("camera", 1)
> > > capture = cv.CaptureFromCAM(0)
> > > while True:
> > > img = cv.QueryFrame(capture)
> > > cv.ShowImage("camera", img)
> > > if cv.WaitKey(10) == 27:
> > > break

> > > is that all you do? will that take from a camera source and display it
> on

> > the screen?

> > > --
> > > To unsubscribe, send email to
> wxPython-users+unsubscribe@googlegroups.com<wxPython-users%2Bunsubscribe@googlegroups.com>
> <wxPython-users%2Bunsubscribe@googlegroups.com<wxPython-users%252Bunsubscribe@googlegroups.com>

> > > or visithttp://groups.google.com/group/wxPython-users?hl=en

> > --
> > Hi, I will kill all ads in google gmail.
> > They will all be dead and gone for all my emails to you. HA HA bye bye
> ads I
> > just massacred you!!!

> --
> To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com<wxPython-users%2Bunsubscribe@googlegroups.com>
> or visithttp://groups.google.com/group/wxPython-users?hl=en

If mplayer can do it, then you can probably use the mplayerCtrl
widget:

http://pypi.python.org/pypi/MplayerCtrl/

I have used it for video playback, but as I understand it, mplayer can
also do streaming...

···

On Nov 26, 4:31 pm, Convoluted <xenon...@gmail.com> wrote:

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

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

If you don’t have to use OpenCV try VideoCapture.

example attached…

cam_testing.py (1.23 KB)

···

On Mon, Nov 29, 2010 at 5:59 PM, Mike Driscoll kyosohma@gmail.com wrote:

On Nov 26, 4:31 pm, Convoluted xenon...@gmail.com wrote:

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

Thanks for posting both those examples.

I’m so frustrated - the webcam I bought works with every other program in linux except for opencv. Due to some missing codec or something.

I can pump images off an image buffer - so I am going to try this - many thanks.

···

On Tue, Nov 30, 2010 at 10:02 AM, yoav glazner yoavglazner@gmail.com wrote:

On Mon, Nov 29, 2010 at 5:59 PM, Mike Driscoll kyosohma@gmail.com wrote:

On Nov 26, 4:31 pm, Convoluted xenon...@gmail.com wrote:

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

If you don’t have to use OpenCV try VideoCapture.

example attached…

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Yoav, I've looked into using VideoCapture with wxPython, but was
getting rather poor results compared to OpenCV. Just streaming at 15
FPS was maxing my CPU usage. The video also appeared more grainy. The
testing was done using Webcamspy without using any other features than
just video acquisition. David, I hope you find use out of the example.
Please do report back and let me know it works. Mike, that's a really
great idea, I'll have to look into that more. The major draw of OpenCV
at the moment is being able to perform some vision processing (motion
tracking) but for pure streaming mplayer would be a great choice.
Cheers all.

···

On Nov 29, 4:23 pm, David Lyon <david.lyon.preissh...@gmail.com> wrote:

Thanks for posting both those examples.

I'm so frustrated - the webcam I bought works with every other program in
linux except for opencv. Due to some missing codec or something.

I can pump images off an image buffer - so I am going to try this - many
thanks.

On Tue, Nov 30, 2010 at 10:02 AM, yoav glazner <yoavglaz...@gmail.com>wrote:

> On Mon, Nov 29, 2010 at 5:59 PM, Mike Driscoll <kyoso...@gmail.com> wrote:

>> On Nov 26, 4:31 pm, Convoluted <xenon...@gmail.com> wrote:
>> > 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()

> If you don't have to use OpenCV try VideoCapture.
> example attached...

> --
> To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com<wxPython-users%2Bunsubscribe@googlegroups.com>
> or visithttp://groups.google.com/group/wxPython-users?hl=en

I attached two samples that work for me in linux.

One using OpenCV a modified version of one posted here in this thread.
The other using gstreamer

Both seems to works OK with wxPython 2.8.11.0 (gtk2-unicode) on Ubuntu 10.10

Sorry if this is out of context, but I didn't follow this thread very close.
But I think sample code is always welcome :wink:

Ricardo

cv_webcam.py (1.22 KB)

gst_webcam.py (3.22 KB)

···

On Tue, Nov 30, 2010 at 1:14 AM, Convoluted <xenon2k8@gmail.com> wrote:

Yoav, I've looked into using VideoCapture with wxPython, but was
getting rather poor results compared to OpenCV. Just streaming at 15
FPS was maxing my CPU usage. The video also appeared more grainy. The
testing was done using Webcamspy without using any other features than
just video acquisition. David, I hope you find use out of the example.
Please do report back and let me know it works. Mike, that's a really
great idea, I'll have to look into that more. The major draw of OpenCV
at the moment is being able to perform some vision processing (motion
tracking) but for pure streaming mplayer would be a great choice.
Cheers all.

On Nov 29, 4:23 pm, David Lyon <david.lyon.preissh...@gmail.com> > wrote:

Thanks for posting both those examples.

I'm so frustrated - the webcam I bought works with every other program in
linux except for opencv. Due to some missing codec or something.

I’ve worked out that I need to recompile OpenCV from svn to get
my webcam to work. I was just using an old packaged version.

It’s been so helpful getting these examples.

I’m building an in-car system with a wx console on a mini-itx
computer. As in:

http://www.mini-itx.com/store/

I want to attach webcams and use OpenCV to read stuff as I
drive.

I’ve got a project up at:

http://bitbucket.org/djlyon/smp-driverless-car-robot

···

On Tue, Nov 30, 2010 at 1:15 PM, Ricardo Pedroso rmdpedroso@gmail.com wrote:

On Tue, Nov 30, 2010 at 1:14 AM, Convoluted xenon2k8@gmail.com wrote:

Yoav, I’ve looked into using VideoCapture with wxPython, but was

getting rather poor results compared to OpenCV. Just streaming at 15

FPS was maxing my CPU usage. The video also appeared more grainy. The

testing was done using Webcamspy without using any other features than

just video acquisition. David, I hope you find use out of the example.

Please do report back and let me know it works. Mike, that’s a really

great idea, I’ll have to look into that more. The major draw of OpenCV

at the moment is being able to perform some vision processing (motion

tracking) but for pure streaming mplayer would be a great choice.

Cheers all.

On Nov 29, 4:23 pm, David Lyon david.lyon.preissh...@gmail.com > > > wrote:

Thanks for posting both those examples.

I’m so frustrated - the webcam I bought works with every other program in

linux except for opencv. Due to some missing codec or something.

I attached two samples that work for me in linux.

One using OpenCV a modified version of one posted here in this thread.

The other using gstreamer

Both seems to works OK with wxPython 2.8.11.0 (gtk2-unicode) on Ubuntu 10.10

Sorry if this is out of context, but I didn’t follow this thread very close.

But I think sample code is always welcome :wink:

Ricardo

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Glad you found some use in the code samples. Are you still planning on
use C to process the video, or just wxPython? I'm also interested in
using a small vehicle PC to monitor a webcam feed, and I'm curious
what sort of processor usage you are achieving on a small PC. I've
taken a look at your project, looks quite interesting. I'll keep an
eye out for your advancement on it. If you are able to enhance the
video acquisition code, please post it here.

Ricardo, thanks for those samples. I've heard of GStreamer, but
haven't used it. I mind end up doing a comparison between the three
video acquisition methods.

···

On Dec 1, 4:36 pm, David Lyon <david.lyon.preissh...@gmail.com> wrote:

I've worked out that I need to recompile OpenCV from svn to get
my webcam to work. I was just using an old packaged version.

It's been so helpful getting these examples.

I'm building an in-car system with a wx console on a mini-itx
computer. As in:

http://www.mini-itx.com/store/

I want to attach webcams and use OpenCV to read stuff as I
drive.

I've got a project up at:

http://bitbucket.org/djlyon/smp-driverless-car-robot

On Tue, Nov 30, 2010 at 1:15 PM, Ricardo Pedroso <rmdpedr...@gmail.com>wrote:

> On Tue, Nov 30, 2010 at 1:14 AM, Convoluted <xenon...@gmail.com> wrote:
> > Yoav, I've looked into using VideoCapture with wxPython, but was
> > getting rather poor results compared to OpenCV. Just streaming at 15
> > FPS was maxing my CPU usage. The video also appeared more grainy. The
> > testing was done using Webcamspy without using any other features than
> > just video acquisition. David, I hope you find use out of the example.
> > Please do report back and let me know it works. Mike, that's a really
> > great idea, I'll have to look into that more. The major draw of OpenCV
> > at the moment is being able to perform some vision processing (motion
> > tracking) but for pure streaming mplayer would be a great choice.
> > Cheers all.

> > On Nov 29, 4:23 pm, David Lyon <david.lyon.preissh...@gmail.com> > > > wrote:
> >> Thanks for posting both those examples.

> >> I'm so frustrated - the webcam I bought works with every other program
> in
> >> linux except for opencv. Due to some missing codec or something.

> I attached two samples that work for me in linux.

> One using OpenCV a modified version of one posted here in this thread.
> The other using gstreamer

> Both seems to works OK with wxPython 2.8.11.0 (gtk2-unicode) on Ubuntu
> 10.10

> Sorry if this is out of context, but I didn't follow this thread very
> close.
> But I think sample code is always welcome :wink:

> Ricardo

> --
> To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com<wxPython-users%2Bunsubscribe@googlegroups.com>
> or visithttp://groups.google.com/group/wxPython-users?hl=en

Thanks. Actually I’m not really fussed whether it’s in C++ or python. I’m
told that python adds 10% latency which to me doesn’t seem too bad.

I’d like to keep the display monitor portion in wxpython. As it will be
much easier in the long run to maintain. It’s just unfornuate for me
that the video in the standard releases of OpenCV didn’t work.

Well I am building OpenCV from svn and I will see how it goes from
there.

Cheers :slight_smile:

···

On Sat, Dec 4, 2010 at 8:29 AM, Convoluted xenon2k8@gmail.com wrote:

Glad you found some use in the code samples. Are you still planning on

use C to process the video, or just wxPython? I’m also interested in

using a small vehicle PC to monitor a webcam feed, and I’m curious

what sort of processor usage you are achieving on a small PC. I’ve

taken a look at your project, looks quite interesting. I’ll keep an

eye out for your advancement on it. If you are able to enhance the

video acquisition code, please post it here.

Ricardo, thanks for those samples. I’ve heard of GStreamer, but

haven’t used it. I mind end up doing a comparison between the three

video acquisition methods.

On Dec 1, 4:36 pm, David Lyon david.lyon.preissh...@gmail.com wrote:

I’ve worked out that I need to recompile OpenCV from svn to get

my webcam to work. I was just using an old packaged version.

It’s been so helpful getting these examples.

I’m building an in-car system with a wx console on a mini-itx

computer. As in:

http://www.mini-itx.com/store/

I want to attach webcams and use OpenCV to read stuff as I

drive.

I’ve got a project up at:

http://bitbucket.org/djlyon/smp-driverless-car-robot

On Tue, Nov 30, 2010 at 1:15 PM, Ricardo Pedroso rmdpedr...@gmail.comwrote:

On Tue, Nov 30, 2010 at 1:14 AM, Convoluted xenon...@gmail.com wrote:

Yoav, I’ve looked into using VideoCapture with wxPython, but was

getting rather poor results compared to OpenCV. Just streaming at 15

FPS was maxing my CPU usage. The video also appeared more grainy. The

testing was done using Webcamspy without using any other features than

just video acquisition. David, I hope you find use out of the example.

Please do report back and let me know it works. Mike, that’s a really

great idea, I’ll have to look into that more. The major draw of OpenCV

at the moment is being able to perform some vision processing (motion

tracking) but for pure streaming mplayer would be a great choice.

Cheers all.

On Nov 29, 4:23 pm, David Lyon david.lyon.preissh...@gmail.com > > > > > wrote:

Thanks for posting both those examples.

I’m so frustrated - the webcam I bought works with every other program

in

linux except for opencv. Due to some missing codec or something.

I attached two samples that work for me in linux.

One using OpenCV a modified version of one posted here in this thread.

The other using gstreamer

Both seems to works OK with wxPython 2.8.11.0 (gtk2-unicode) on Ubuntu

10.10

Sorry if this is out of context, but I didn’t follow this thread very

close.

But I think sample code is always welcome :wink:

Ricardo

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.comwxPython-users%2Bunsubscribe@googlegroups.com

or visithttp://groups.google.com/group/wxPython-users?hl=en

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

To expand on Mike's suggestion, I tried the MPlayer route with
MPlayerCtrl and it turns out to be a very feasible solution. In case
anyone's interested, here's a simple example how to integrate a webcam
feed in wxPython (adapted from MPlayerCtrl's "simple example"). I
installed the SMPlayer package and just pointed to the mplayer.exe in
SMPlayer\mplayer\. Nice thing about this method is that the processing
is done through MPlayer and not wxPython, so I would imagine if the
video feed crashed it shouldn't crash wxPython.

On a side note, I'm having an issue with all webcam capture methods. I
get the error "the wx.app object must be created first". Seems to only
happen when launching the application for the second time in IDLE. No
problems from command prompt or launching for the first time in IDLE.
Anyone know how to remedy this problem? Occurred on Windows 7 x64 with
wxPython 2.8.11.0. Thanks

import wx
import MplayerCtrl as mpc

class Frame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id)
        self.mpc = mpc.MplayerCtrl(self, -1, 'c:\Program Files
(x86)\SMPlayer\mplayer\mplayer.exe')
        wx.FutureCall(1000, self.mpc.Loadfile, 'tv:// -tv')
        self.Show()

if __name__ == '__main__':
    app = wx.App(redirect=False)
    f = Frame(None, -1)
    app.MainLoop()

···

On Dec 3, 6:08 pm, David Lyon <david.lyon.preissh...@gmail.com> wrote:

Thanks. Actually I'm not really fussed whether it's in C++ or python. I'm
told that python adds 10% latency which to me doesn't seem too bad.

I'd like to keep the display monitor portion in wxpython. As it will be
much easier in the long run to maintain. It's just unfornuate for me
that the video in the standard releases of OpenCV didn't work.

Well I am building OpenCV from svn and I will see how it goes from
there.

Cheers :slight_smile:

On Sat, Dec 4, 2010 at 8:29 AM, Convoluted <xenon...@gmail.com> wrote:
> Glad you found some use in the code samples. Are you still planning on
> use C to process the video, or just wxPython? I'm also interested in
> using a small vehicle PC to monitor a webcam feed, and I'm curious
> what sort of processor usage you are achieving on a small PC. I've
> taken a look at your project, looks quite interesting. I'll keep an
> eye out for your advancement on it. If you are able to enhance the
> video acquisition code, please post it here.

> Ricardo, thanks for those samples. I've heard of GStreamer, but
> haven't used it. I mind end up doing a comparison between the three
> video acquisition methods.

> On Dec 1, 4:36 pm, David Lyon <david.lyon.preissh...@gmail.com> wrote:
> > I've worked out that I need to recompile OpenCV from svn to get
> > my webcam to work. I was just using an old packaged version.

> > It's been so helpful getting these examples.

> > I'm building an in-car system with a wx console on a mini-itx
> > computer. As in:

> > http://www.mini-itx.com/store/

> > I want to attach webcams and use OpenCV to read stuff as I
> > drive.

> > I've got a project up at:

> > http://bitbucket.org/djlyon/smp-driverless-car-robot

> > On Tue, Nov 30, 2010 at 1:15 PM, Ricardo Pedroso <rmdpedr...@gmail.com > > >wrote:

> > > On Tue, Nov 30, 2010 at 1:14 AM, Convoluted <xenon...@gmail.com> > > wrote:
> > > > Yoav, I've looked into using VideoCapture with wxPython, but was
> > > > getting rather poor results compared to OpenCV. Just streaming at 15
> > > > FPS was maxing my CPU usage. The video also appeared more grainy. The
> > > > testing was done using Webcamspy without using any other features
> than
> > > > just video acquisition. David, I hope you find use out of the
> example.
> > > > Please do report back and let me know it works. Mike, that's a really
> > > > great idea, I'll have to look into that more. The major draw of
> OpenCV
> > > > at the moment is being able to perform some vision processing (motion
> > > > tracking) but for pure streaming mplayer would be a great choice.
> > > > Cheers all.

> > > > On Nov 29, 4:23 pm, David Lyon <david.lyon.preissh...@gmail.com> > > > > > wrote:
> > > >> Thanks for posting both those examples.

> > > >> I'm so frustrated - the webcam I bought works with every other
> program
> > > in
> > > >> linux except for opencv. Due to some missing codec or something.

> > > I attached two samples that work for me in linux.

> > > One using OpenCV a modified version of one posted here in this thread.
> > > The other using gstreamer

> > > Both seems to works OK with wxPython 2.8.11.0 (gtk2-unicode) on Ubuntu
> > > 10.10

> > > Sorry if this is out of context, but I didn't follow this thread very
> > > close.
> > > But I think sample code is always welcome :wink:

> > > Ricardo

> > > --
> > > To unsubscribe, send email to
> wxPython-users+unsubscribe@googlegroups.com<wxPython-users%2Bunsubscribe@googlegroups.com>
> <wxPython-users%2Bunsubscribe@googlegroups.com<wxPython-users%252Bunsubscribe@googlegroups.com>

> > > or visithttp://groups.google.com/group/wxPython-users?hl=en

> --
> To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com<wxPython-users%2Bunsubscribe@googlegroups.com>
> or visithttp://groups.google.com/group/wxPython-users?hl=en

Hi all

i found this, and it works :smiley:

import wx
from opencv import cv, highgui

class LivePanel(wx.Panel):

    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)

        self.cap = highgui.cvCreateCameraCapture(0)
        self.Bind(wx.EVT_IDLE, self.onIdle)

    def onIdle(self, event):

        img = highgui.cvQueryFrame(self.cap)
        self.displayImage(img)
        event.RequestMore()

    def displayImage(self, img, offset=(0,0)):

        bitmap = wx.BitmapFromBuffer(img.width, img.height,
img.imageData)
        dc = wx.ClientDC(self)
        dc.DrawBitmap(bitmap, offset[0], offset[1], False)

# Main Procedure
app = wx.PySimpleApp()
pFrame = wx.Frame(None, -1, "Media Player", size = (640, 480))
livePanel = LivePanel(pFrame,-1)
livePanel.SetPosition(wx.Point(0,0))
livePanel.SetSize(wx.Size(640,480))
pFrame.Show()
app.MainLoop()

this the original code:
http://eupparee.wordpress.com/2009/02/10/capture-live-by-using-opencv-and-wxpython/

···

On 26 nov, 16:31, Convoluted <xenon...@gmail.com> wrote:

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

Either use an IDE that always runs the launched programs and the shell in external processes (not in the same process as the IDE) or find a way to make IDLE do that. (I seem to recall that there was a branch of IDLE development that had that option, and it has since been merged into the trunk, but I don't remember any more details.) There can only be one wx.App in a process, and creating new ones after the first has exited can sometimes be problematic.

···

On 12/4/10 6:06 PM, Convoluted wrote:

On a side note, I'm having an issue with all webcam capture methods. I
get the error "the wx.app object must be created first". Seems to only
happen when launching the application for the second time in IDLE. No
problems from command prompt or launching for the first time in IDLE.
Anyone know how to remedy this problem? Occurred on Windows 7 x64 with
wxPython 2.8.11.0. Thanks

--
Robin Dunn
Software Craftsman
http://wxPython.org

Question out of my depth here.

It has been stated often that wxPython (it’s message loop and event processing) needs to be the main thread, meaning the first thread. I’m not quite sure how an OS knows what “main” thread means.

Because this is true for threads is it true for processes? Is the first process the main process? Or is it unlike threads in that every process is “the main process”, all processes are equal in the eyes of Python and wxPython, they’re all the same. I’m thinking the later to be the case.

In either case would this be a bad (or even doable) [not withstanding syntax or proper parameterization] solution to OP’s issue:

main.py

···

On Mon, Dec 6, 2010 at 1:25 PM, Robin Dunn robin@alldunn.com wrote:

… There can only be one wx.App in a process, and creating new ones after the first has exited can sometimes be problematic.

Robin Dunn


if name == ‘main’:
import multiprocessing

import wxgui # module has only defs and classes no instances or running code

import videoui # module has only defs and classes no instances or running code

import netstreams # same thing again

if name == ‘main’:
netstream = Process(target=netstreams.reactorloop(), …)

netstream.start()

netstream.join()

if name == ‘main’:
gui = Process(target=wxgui.app.MainLoop(), …)

gui.start()

gui.join()

if name == ‘main’:
video = Process(target=videoui.CaptureLoop(), …)

video.start()

video.join()

http://docs.python.org/dev/library/multiprocessing.html

if name == ‘main’: # line is necessary according to docs

    ... There can only be one wx.App in a process, and creating new ones
    after the first has exited can sometimes be problematic.
    Robin Dunn

Question out of my depth here.
It has been stated often that wxPython (it's message loop and event
processing) needs to be the main thread, meaning the first thread. I'm
not quite sure how an OS knows what "main" thread means.

Normally the "main thread" in any context is the one that is active when a program is started, before the program creates any new threads for itself. The OS is aware of this but as far as the wx UI is concerned it doesn't really care. OTOH wx does care about which thread it should consider to be the main thread, but it doesn't have to be the same one that was created for the process by the system before stating the program, (but it usually is.) Whichever thread creates the wx.App object will be the one that wx treats as the main thread.

Because this is true for threads is it true for processes?

No. Processes are totally isolated from each other by the system (except for any IPC that they do for themselves of course.)

Is the first
process the main process? Or is it unlike threads in that every process
is "the main process", all processes are equal in the eyes of Python and
wxPython, they're all the same. I'm thinking the later to be the case.
In either case would this be a bad (or even doable) [not withstanding
syntax or proper parameterization] solution to OP's issue:

Theoretically it should be possible to have more than one process in a process group to have a wx UI, but each of them would have to create a wx.App and run MainLoop(), and I suppose that it's possible that that may interfere with the IPC that the subprocess module is using for the processes to communicate with each other.

···

On 12/6/10 11:25 AM, Dev Player wrote:

On Mon, Dec 6, 2010 at 1:25 PM, Robin Dunn <robin@alldunn.com > <mailto:robin@alldunn.com>> wrote:

--
Robin Dunn
Software Craftsman
http://wxPython.org