Opencv integration with wxpython

前端 未结 3 609
借酒劲吻你
借酒劲吻你 2020-12-18 05:24

I just wanted to integrate the opencv video stream from my web cam into a more complex gui than highgui can offer, nothing fancy just a couple of buttons and something else,

相关标签:
3条回答
  • 2020-12-18 05:40

    You must set the size of panel to show the captured image. I used your code and I added "

    self.SetSize(width,height)
    

    It's ok

    0 讨论(0)
  • 2020-12-18 05:41

    The following example code works fine for me under OS X, but I've had tiny surprises with wx across platforms. It is nearly the same code, the difference is that the result from cvtColor is reassigned, and a subclass of wx.Panel (which is the important part) was added.

    import wx
    import cv, cv2
    
    class ShowCapture(wx.Panel):
        def __init__(self, parent, capture, fps=15):
            wx.Panel.__init__(self, parent)
    
            self.capture = capture
            ret, frame = self.capture.read()
    
            height, width = frame.shape[:2]
            parent.SetSize((width, height))
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    
            self.bmp = wx.BitmapFromBuffer(width, height, frame)
    
            self.timer = wx.Timer(self)
            self.timer.Start(1000./fps)
    
            self.Bind(wx.EVT_PAINT, self.OnPaint)
            self.Bind(wx.EVT_TIMER, self.NextFrame)
    
    
        def OnPaint(self, evt):
            dc = wx.BufferedPaintDC(self)
            dc.DrawBitmap(self.bmp, 0, 0)
    
        def NextFrame(self, event):
            ret, frame = self.capture.read()
            if ret:
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                self.bmp.CopyFromBuffer(frame)
                self.Refresh()
    
    
    capture = cv2.VideoCapture(0)
    capture.set(cv.CV_CAP_PROP_FRAME_WIDTH, 320)
    capture.set(cv.CV_CAP_PROP_FRAME_HEIGHT, 240)
    
    app = wx.App()
    frame = wx.Frame(None)
    cap = ShowCapture(frame, capture)
    frame.Show()
    app.MainLoop()
    
    0 讨论(0)
  • 2020-12-18 05:53

    You should add a comment for the below line

    #self.PrepareDC(dc)
    

    It worked for me.

    0 讨论(0)
提交回复
热议问题