2012-02-27 101 views
3

我是新來的蟒蛇。我正在嘗試編寫一個動作檢測應用程序。目前,我正試圖讓網絡攝像頭的視頻顯示在屏幕上。目前的代碼現在一開始沒有閃爍,但是在任何調整大小之後,閃爍將會回來。任何線索?另外,爲什麼它沒有self.Refresh()在定時器事件中工作,是不是總是發生繪畫事件,除非框架被最小化?提前致謝。閃爍wxpython顯示攝像頭視頻

import wx 
import cv 

class LiveFrame(wx.Frame): 

    fps = 30 


    def __init__(self, parent): 
    wx.Frame.__init__(self, parent, -1, title="Live Camera Feed") 

    self.SetDoubleBuffered(True) 
    self.capture = None 
    self.bmp = None 
    #self.displayPanel = wx.Panel(self,-1) 

    #set up camaera init 
    self.capture = cv.CaptureFromCAM(0) 
    frame = cv.QueryFrame(self.capture) 
    if frame: 
     cv.CvtColor(frame,frame,cv.CV_BGR2RGB) 
     self.bmp = wx.BitmapFromBuffer(frame.width,frame.height,frame.tostring()) 
     self.SetSize((frame.width,frame.height)) 
    self.displayPanel = wx.Panel(self,-1) 

    self.fpstimer = wx.Timer(self) 
    self.fpstimer.Start(1000/self.fps) 
    self.Bind(wx.EVT_TIMER, self.onNextFrame, self.fpstimer) 
    self.Bind(wx.EVT_PAINT, self.onPaint) 

    self.Show(True) 

    def updateVideo(self): 
    frame = cv.QueryFrame(self.capture) 
    if frame: 
     cv.CvtColor(frame,frame,cv.CV_BGR2RGB) 
     self.bmp.CopyFromBuffer(frame.tostring()) 
     self.Refresh() 


    def onNextFrame(self,evt): 
    self.updateVideo() 
    #self.Refresh() 
    evt.Skip() 

    def onPaint(self,evt): 
    #if self.bmp: 
    wx.BufferedPaintDC(self.displayPanel, self.bmp) 

    evt.Skip() 

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

回答

4

我找到了解決這個問題的辦法。閃爍來自面板清除其背景。我不得不創建一個面板實例,並有EVT_ERASE_BACKGROUND旁路。另一件事是,我必須將攝像頭例程放在該面板內,並在面板上繪製BufferPaintedDC。出於某種原因,如果wx.BufferedPaintedDC正在從幀中繪製到self.displaypanel,則閃爍仍然存在。

+0

很多。我只是遇到同樣的問題,並通過你的方式解決! – 2012-09-04 10:41:39

1

當你繪圖時,你只需要調用刷新。這是一個要求。我不記得爲什麼。爲了擺脫閃爍,你可能想要閱讀DoubleBuffering:http://wiki.wxpython.org/DoubleBufferedDrawing

或者你也許可以使用mplayer控件。這裏有一個例子:http://www.blog.pythonlibrary.org/2010/07/24/wxpython-creating-a-simple-media-player/

+0

是不是我已經使用wx.BufferedPaintDC的doublebuffering?還是我用它錯了?在我使用BufferedPaintDC並更新靜態位圖之前,當我運行代碼時它正在閃爍。現在使用BufferedPaintDC,它不會閃爍直到窗口被調整大小,或最小化/最大化。 – 2012-02-27 16:33:40

+0

我不確定你是否正確使用它,所以我想我會把你鏈接到wiki,以便你可以比較。 wxPython Google Group也非常有幫助。 – 2012-02-27 17:24:22