Setting up the app

Before we can get down to the nitty-gritty of our feature-matching algorithm, we need to make sure that we can access the webcam and display the video stream in a simple GUI. Luckily, we have already figured out how to do this in Chapter 1, Fun with Filters.

Running the app

In order to run our app, we will need to execute a main function routine that accesses the webcam, generates the GUI, and executes the main loop of the app:

import cv2
import wx

from gui import BaseLayout
from feature_matching import FeatureMatching


def main():
    capture = cv2.VideoCapture(0)
    if not(capture.isOpened()):
        capture.open()

    capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
    capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)

    # start graphical user interface
    app = wx.App()

    layout = FeatureMatchingLayout(None, -1, 'Feature Matching', capture)
    layout.Show(True)
    app.MainLoop()

Note

If you are using OpenCV 3, the constants that you are looking for might be called cv3.CAP_PROP_FRAME_WIDTH and cv3.CAP_PROP_FRAME_HEIGHT.

The FeatureMatching GUI

Analogous to the previous chapter, the layout chosen for the current project (FeatureMatchingLayout) is as plain as it gets. It should simply display the video feed of the webcam at a comfortable frame rate of 10 frames per second. Therefore, there is no need to further customize BaseLayout:

class FeatureMatchingLayout(BaseLayout):
    def _create_custom_layout(self):
        pass

The only parameter that needs to be initialized this time is the feature-matching class. We pass to it the path to a template (or training) file that depicts the object of interest:

    def _init_custom_layout(self):
        self.matching = FeatureMatching(train_image='salinger.jpg')

The rest of the visualization pipeline is handled by the BaseLayout class. We only need to make sure that we provide a _process_frame method. This method accepts a RGB color image, processes it by means of the FeatureMatching method match, and passes the processed image for visualization. If the object is detected in the current frame, the match method will report success=True and we will return the processed frame. If the match method is not successful, we will simply return the input frame:

    def _process_frame(self, frame):
        self.matching = FeatureMatching(train_image='salinger.jpg')
        # if object detected, display new frame, else old one
        success, new_frame = self.matching.match(frame)
        if success:
            return new_frame
        else:
            return frame
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.12.156