Menu
Imam Ferianto Blogs
  • Home
  • About
Imam Ferianto Blogs

Contoh Capture Opencv Video Camera dengan wx-python

Posted on May 12, 2020May 13, 2020 by feri

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:

Social Media Links

  • Linkedin Profile
  • Facebook Profile
  • Instagram Profile
  • GitHub Pages
  • Google Play Portofolio
  • Postgresql Training Service
  • PHP Security Training Service
  • Active State Writing

Recent Posts

  • Ngoding Data Analytics di Handphone Part 2
  • Ngoding Data Analytics di Iphone Part 1
  • Cara Mudah Membuat Report Dengan Fastreport pada dotnet core project
  • Cara Alternatif mengakses Webservice Soap pada dotnet core tanpa menggunakan soap client
  • Tutorial Yii2 Singkat padat dan jelas untuk programmer pemula. Bagian 1 – Membuat Aplikasi Penjualan Menggunakan yii2

Archives

  • September 2022
  • May 2021
  • April 2021
  • March 2021
  • January 2021
  • September 2020
  • July 2020
  • June 2020
  • May 2020
  • March 2020
  • February 2020
  • December 2019

Categories

  • #dotnetcore
  • arduino
  • c#
  • data-analytics
  • docker
  • oracle
  • php
  • programming
  • python
  • selingan
  • Uncategorized
©2023 Imam Ferianto Blogs | Powered by WordPress & Superb Themes