OpenCV (Open Source Computer Vision Library) adalah sebuah pustaka perangkat lunak yang ditujukan untuk pengolahan citra dinamis secara real-time, yang dibuat oleh Intel, dan sekarang didukung oleh Willow Garage dan Itseez.
Tulisan ini adalah contoh Capture Opencv Video Camera dengan wx-python.
Berikut codenya dengan nama file: opencvwx2.py
#!/usr/bin/python3
import wx
import cv2
from wx.lib import statbmp
class MainWindow(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
mainSizer = wx.BoxSizer(wx.VERTICAL)
self.inputBox = wx.TextCtrl(self)
mainSizer.Add(self.inputBox, 0, wx.ALL, 5)
capture = cv2.VideoCapture(0)
# video
videoWarper = wx.StaticBox(self, -1, label="Video",size=(640,480))
videoBoxSizer = wx.StaticBoxSizer(videoWarper, wx.VERTICAL)
videoFrame = wx.Panel(self, 0,size=(640,480))
#videoFrame.SetBackgroundColour('white')
cap = ShowCapture(videoFrame, capture)
videoBoxSizer.Add(videoFrame, flag=wx.EXPAND | wx.ALL, border=0)
mainSizer.Add(videoBoxSizer,flag=wx.EXPAND | wx.ALL, border=0)
parent.Centre()
self.Show()
self.SetSizerAndFit(mainSizer)
class ShowCapture(wx.Panel):
def __init__(self, parent, capture, fps=24):
wx.Frame.__init__(self, parent)
self.capture = capture
ret, frame = self.capture.read()
vheight, vwidth = frame.shape[:2]
#self.SetSize((vwidth, vheight))
self.SetSize(parent.GetSize())
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.bmp = wx.BitmapFromBuffer(vwidth, vheight, frame)
self.ImgControl = statbmp.GenStaticBitmap(self, wx.ID_ANY, self.bmp)
#self.ImgControl.SetClientSize(self.GetSize())
self.timer = wx.Timer(self)
self.timer.Start(1000./fps)
self.Bind(wx.EVT_TIMER, self.NextFrame)
def NextFrame(self, event):
ret, frame = self.capture.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
#print(self.GetSize())
vwidth=frame.shape[1]
vheight=frame.shape[0]
scale_percent = 60 # percent of original size
width = int(vwidth * scale_percent / 100)
height = int(vheight * scale_percent / 100)
dim = (width, height)
resized = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)
self.bmp = wx.BitmapFromBuffer(width, height, frame)
self.bmp.CopyFromBuffer(resized)
self.ImgControl.SetBitmap(self.bmp)
app = wx.App(False)
frame = wx.Frame(None,-1,'Video Chat',size=(400, 400))
panel = MainWindow(frame)
frame.Show()
app.MainLoop()
Run code dengan python3:
python3 opencvwx2.py
Hasilnya: