使用openCV和wxPython从网络摄像头获取流

2024-10-01 11:23:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经阅读了互联网上关于这个主题的所有三四个帖子,到目前为止没有一个能准确回答这个问题。在

虽然我有一些新的经验。我不熟悉OpenCV。在

我正在尝试用openCV从一个网络摄像头捕捉一个图像,并将其绘制到wxPython中。我的成功是有限的(我可以得到一个图像,并绘制它,但它是模糊的,没有正确对齐)。我可以确认我的网络摄像头和openCV是独立工作的,因为sample code like this工作正常。在

这是我最近的一个努力的例子,这是我从互联网和我自己用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并不是一个硬性要求(只要解决方案是跨平台的,我愿意接受其他选择)。如果我没搞错的话,Gstreamer不是跨平台的,PyGame很难嵌入wxPython中,但我对想法持开放态度)。在

wxPython是一个硬性要求。在


Tags: imageselfidmaindefwxpythonframewx
2条回答

如果OpenCV不是硬性要求

试试libvlc和它的python bindigs

https://wiki.videolan.org/Python_bindings

您可以尝试这个修改过的代码,它应该将opencv2中的网络摄像头图像显示到wx python环境中:

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

相关问题 更多 >