簡體   English   中英

使用openCV和wxPython從網絡攝像頭獲取流

[英]Get stream from webcam with openCV and wxPython

我已經閱讀了互聯網上有關該主題的所有當前的三個或四個主題,到目前為止,沒有一個主題能夠准確回答這個問題。

我對wxPython相當陌生,盡管我對FLTK有一定的經驗。 我是OpenCV的新手。

我正在嘗試使用openCV從網絡攝像頭捕獲圖像並將該圖像繪制到wxPython中。 我獲得的成功有限(我可以獲取圖像並繪制圖像,但是它很模糊,而且排列不正確)。 我可以確認我的網絡攝像頭和openCV可以獨立工作,因為這樣的示例代碼可以正常工作。

這是我最近所做的工作的一個示例,我從互聯網上收集了自己的心血,而我自己在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是一個硬性要求。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM