I am working on an application where i need to play a movie and the user can draw different shapes on it.
For this purpose i am using two different panels. First panel (background) is used to show the movie frame and second panel (foreground, transparent) is used for drawing.
The problem is the drawing appears on the first frame only and then the repainting of background panel removes it.
I am new to wxpython so if my approach is wrong please correct me.
My Code:
import wx
import cv2
class MoviePanel(wx.Panel):
def __init__(self, parent, capture):
wx.Panel.__init__(self, parent, size=(840,480))
self.Bind(wx.EVT_PAINT, self.OnPaint, self)
self.capture = capture
ret, frame = self.capture.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame,(840,480))
self.bmp = wx.Bitmap.FromBuffer(840, 480, frame)
self.timer = wx.Timer(self)
self.timer.Start(1000./60)
self.Bind(wx.EVT_TIMER, self.NextFrame)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def OnEraseBackground(self,evt):
pass
def OnPaint(self, evt):
dc = wx.BufferedPaintDC(self)
dc.DrawBitmap(self.bmp, 0, 0)
def NextFrame(self, evt):
ret, frame = self.capture.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame,(840,480))
self.bmp.CopyFromBuffer(frame)
self.Refresh()
class DrawPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, size=(840,480),
style=wx.TRANSPARENT_WINDOW)
self.Bind(wx.EVT_PAINT, self.OnPaintDrawPanel)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def OnEraseBackground(self,evt):
pass
def OnPaintDrawPanel(self, evt):
pdc = wx.BufferedPaintDC(self)
dc = wx.GCDC(pdc)
dc.SetPen(wx.Pen('#4c4c4c',7))
dc.DrawLine(20, 240, 800, 240)
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, size=(840, 480))
capture = cv2.VideoCapture(0)
self.MovieP = MoviePanel(self, capture)
self.DrawP = DrawPanel(self)
if __name__ == '__main__':
app = wx.App()
frame = Frame(None)
frame.Show()
app.MainLoop()
``