Advertisement
Guest User

dsfds

a guest
Jul 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.56 KB | None | 0 0
  1. import wx
  2. import cv2
  3. import numpy as np
  4.  
  5. class VideoFrame(wx.Frame):
  6.        
  7.     def __init__(self, parent, id):
  8.         vframe = wx.Frame.__init__(self, parent, id, 'CLIPER 1.0', size=(1640,360))
  9.         self.CreateStatusBar()
  10.         menubar=wx.MenuBar()
  11.         filem=wx.Menu()
  12.         effects = wx.Menu()
  13.         help = wx.Menu()
  14.         info = wx.Menu()
  15.        
  16.         menubar.Append(filem, '&Plik')
  17.         menubar.Append(effects, '&Efekty')
  18.         menubar.Append(info, 'Twórcy')
  19.         menubar.Append(help, 'Pomoc')
  20.        
  21.         filem.Append(wx.NewId(), "Nowe Okno", "This is a new window")
  22.         filem.Append(wx.NewId(), '&Otwórz', 'Otwórz plik')
  23.         filem.Append(wx.NewId(), 'Zapisz', 'Zapisz film')
  24.         filem.AppendSeparator()
  25.         menuExit = filem.Append(wx.ID_EXIT, '&Zamknij', 'Zamknij aplikacje')
  26.         self.Bind(wx.EVT_CLOSE, self.OnClose, menuExit)
  27.        
  28.         effects.Append(wx.ID_ANY, '&Czarno-biały', 'Filtr czarno-biały')
  29.         effects.Append(wx.ID_ANY, '&Sepia', 'Filtr sepii')
  30.         effects.Append(wx.ID_ANY, '&Erozja', 'Efekt erozji')
  31.         effects.Append(wx.ID_ANY, '&Rozmycie', 'Efekt rozmycia')
  32.         info.Append(wx.ID_ABOUT, 'Autor', 'Informacje o autorze aplikacji')
  33.        
  34.         self.SetMenuBar(menubar)
  35.        
  36.         boxV = wx.BoxSizer(wx.VERTICAL)
  37.         boxH = wx.BoxSizer(wx.HORIZONTAL)
  38.         boxV.Add(boxH, 0, wx.EXPAND)
  39.         vframe.SetSizerAndFit(boxH);
  40.         self.SetSizer(boxV);
  41.         self.SetSizer(boxH);
  42.        
  43.     def OnClose(self, event):
  44.  
  45.         if event.VideoFrame():
  46.  
  47.           if wx.MessageBox("The file has not been saved... continue closing?",
  48.                          "Please confirm",
  49.                          wx.ICON_QUESTION | wx.YES_NO) != wx.YES:
  50.  
  51.               event.VideoFrame()
  52.               return
  53.         self.Quit()
  54.         self.Destroy()  # you may also do:  event.Skip()
  55.                     # since the default event handler does call Destroy(), too
  56.        
  57.  
  58. class ShowCapture(wx.Panel):
  59.     def __init__(self, parent, capture, fps=23.97, size=(1040,1060)):
  60.         wx.Panel.__init__(self, parent)        
  61.         self.capture = capture
  62.         ret, frame = self.capture.read()
  63.  
  64.         height, width = frame.shape[:2]
  65.         parent.SetSize((width, height))
  66.         frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  67.  
  68.         self.bmp = wx.BitmapFromBuffer(width, height, frame)
  69.         self.timer = wx.Timer(self)
  70.         self.timer.Start(1000./fps)
  71.  
  72.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  73.         self.Bind(wx.EVT_TIMER, self.NextFrame)
  74.  
  75.     def OnPaint(self, evt):
  76.         dc = wx.BufferedPaintDC(self)
  77.         dc.DrawBitmap(self.bmp, 0, 0)
  78.  
  79.     def NextFrame(self, event):
  80.         ret, frame = self.capture.read()
  81.         if ret:
  82.             frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  83.             tryb = 0 # 0 - kolor; 1 - czarno białe; 2 - sepia 3 - erozja 4 - rozmycie
  84.             new_frame = np.zeros((200, 200))
  85.  
  86.             key = cv2.waitKey(0)
  87.  
  88.             if key == ord('q'):
  89.                 self.Quit()
  90.             elif key == ord('x'): # koloroweqq
  91.                 tryb = 0
  92.             elif key == ord('c'): # czarno białe        
  93.                 tryb = 1
  94.             elif key == ord('v'): # sepia
  95.                 tryb = 2
  96.             elif key == ord('b'): # erozja
  97.                 tryb = 3
  98.             elif key == ord('n'): # rozmycie gaussowskie    
  99.                 tryb = 4
  100.            
  101.             if tryb == 0: # kolorowe
  102.                 new_frame = frame
  103.             elif tryb == 1: # czarno białe
  104.                 new_frame = cv2.cvtColor(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR)
  105.             elif tryb == 2: # sepia
  106.                 kernel = np.asarray([0.272, 0.534, 0.131, 0.349, 0.686, 0.168, 0.393, 0.769, 0.189]).reshape(3, 3)
  107.                 new_frame = cv2.transform(frame, kernel)
  108.             elif tryb == 3: #erozja
  109.                 kernel = np.ones((5,5), np.uint8)
  110.                 new_frame = cv2.erode(frame, kernel, iterations=1)
  111.             elif tryb == 4: #rozmycie
  112.                 new_frame = cv2.GaussianBlur(frame,(5,5),0)
  113.            
  114.             self.bmp.CopyFromBuffer(frame)
  115.             self.Refresh()
  116.  
  117. capture = cv2.VideoCapture("sample1.mp4")
  118. width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
  119. height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
  120. capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
  121. capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
  122.  
  123.  
  124. app = wx.App()
  125. frame = VideoFrame(parent=None, id=-1)
  126. cap = ShowCapture(frame, capture)
  127. frame.Show()
  128. app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement