Advertisement
Guest User

Untitled

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