简体   繁体   中英

Get stream from webcam with openCV and wxPython

I have read all three or four current threads on this subject that are on the internet, and so far none accurately answer the question.

I am fairly new to wxPython, although I have some experience with FLTK. I am new to OpenCV.

I am attempting to capture an image from a webcam with openCV and paint that image into wxPython. I have had limited success (I can get an image and paint it, but it's faint and not aligned properly). I can confirm that my webcam and openCV are working on their own, because sample code like this works as expected.

Here is an example of my latest effort, which I've cobbled together from the internet and my own efforts with opencv2.

import wx
import cv2

class viewWindow(wx.Frame):
    imgSizer = (480,360)
    def __init__(self, parent, title="View Window"):
            super(viewWindow,self).__init__(parent)

            self.pnl = wx.Panel(self)
            self.vbox = wx.BoxSizer(wx.VERTICAL)
            self.image = wx.EmptyImage(self.imgSizer[0],self.imgSizer[1])

            self.imageBit = wx.BitmapFromImage(self.image)
            self.staticBit = wx.StaticBitmap(self.pnl,wx.ID_ANY,
                self.imageBit)

            self.vbox.Add(self.staticBit)
            self.pnl.SetSizer(self.vbox)

            self.timex = wx.Timer(self, wx.ID_OK)
            self.timex.Start(1000/12)
            self.Bind(wx.EVT_TIMER, self.redraw, self.timex)

            self.capture = cv2.VideoCapture(0)

            self.SetSize(self.imgSizer)
            self.Show()

    def redraw(self,e):
        ret, frame = self.capture.read()
        #print('tick')
        self.imageBit = wx.BitmapFromImage(self.image)
        self.staticBit = wx.StaticBitmap(self.pnl, 
            wx.ID_ANY, self.imageBit)
        self.Refresh()

def main():
    app = wx.PySimpleApp()
    frame = viewWindow(None)
    frame.Center()
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

OpenCV is not a hard requirement (I'm open to other options as long as the solution is cross-platform. If I'm not mistaken, Gstreamer is not cross platform, and PyGame has been difficult to embed in wxPython, but I'm open to ideas).

wxPython is a hard requirement.

if OpenCV is not a hard requirement

try libvlc and its python bindigs

https://wiki.videolan.org/Python_bindings

you can try this modified code, it should show the webcam image from opencv2 into your wx python environment:

    import wx
    import cv2

    class viewWindow(wx.Frame):
        def __init__(self, parent, title="View Window"):
                # super(viewWindow,self).__init__(parent)
                wx.Frame.__init__(self, parent)

                self.imgSizer = (480, 360)
                self.pnl = wx.Panel(self)
                self.vbox = wx.BoxSizer(wx.VERTICAL)
                self.image = wx.EmptyImage(self.imgSizer[0],self.imgSizer[1])
                self.imageBit = wx.BitmapFromImage(self.image)
                self.staticBit = wx.StaticBitmap(self.pnl, wx.ID_ANY, self.imageBit)

                self.vbox.Add(self.staticBit)

                self.capture = cv2.VideoCapture(0)
                ret, self.frame = self.capture.read()
                if ret:
                    self.height, self.width = self.frame.shape[:2]
                    self.bmp = wx.BitmapFromBuffer(self.width, self.height, self.frame)

                    self.timex = wx.Timer(self)
                    self.timex.Start(1000./24)
                    self.Bind(wx.EVT_TIMER, self.redraw)
                    self.SetSize(self.imgSizer)
                else:
                    print "Error no webcam image"
                self.pnl.SetSizer(self.vbox)
                self.vbox.Fit(self)
                self.Show()

        def redraw(self,e):
            ret, self.frame = self.capture.read()
            if ret:
                self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
                self.bmp.CopyFromBuffer(self.frame)
                self.staticBit.SetBitmap(self.bmp)
                self.Refresh()

    def main():
        app = wx.PySimpleApp()
        frame = viewWindow(None)
        frame.Center()
        frame.Show()
        app.MainLoop()

    if __name__ == '__main__':
        main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM