Building an object recognizer

Now that we trained an ERF model, let's go ahead and build an object recognizer that can recognize the content of unknown images.

How to do it…

  1. Create a new Python file, and import the following packages:
    import argparse 
    import cPickle as pickle 
    
    import cv2
    import numpy as np
    
    import build_features as bf
    from trainer import ERFTrainer 
  2. Define the argument parser:
    def build_arg_parser():
        parser = argparse.ArgumentParser(description='Extracts features 
    from each line and classifies the data')
        parser.add_argument("--input-image", dest="input_image", required=True,
    help="Input image to be classified")
        parser.add_argument("--model-file", dest="model_file", required=True,
    help="Input file containing the trained model")
        parser.add_argument("--codebook-file", dest="codebook_file", 
    required=True, help="Input file containing the codebook")
        return parser
  3. Define a class to handle the image tag extraction functions:
    class ImageTagExtractor(object):
        def __init__(self, model_file, codebook_file):
            with open(model_file, 'r') as f:
                self.erf = pickle.load(f)
    
            with open(codebook_file, 'r') as f:
                self.kmeans, self.centroids = pickle.load(f)
  4. Define a function to predict the output using the trained ERF model:
        def predict(self, img, scaling_size):
            img = bf.resize_image(img, scaling_size)
            feature_vector = bf.BagOfWords().construct_feature(
    img, self.kmeans, self.centroids)
            image_tag = self.erf.classify(feature_vector)[0]
            return image_tag
  5. Define the main function and load the input image:
    if __name__=='__main__':
        args = build_arg_parser().parse_args()
        model_file = args.model_file
        codebook_file = args.codebook_file
        input_image = cv2.imread(args.input_image)
  6. Scale the image appropriately, as follows:
        scaling_size = 200
  7. Print the output on the Terminal:
        print "
    Output:", ImageTagExtractor(model_file, 
        codebook_file).predict(input_image, scaling_size)
  8. The full code is given in the object_recognizer.py file that is already provided to you. You should run the code in the following way:
    $ python object_recognizer.py --input-image imagefile.jpg --model-file erf.pkl --codebook-file codebook.pkl
    

    You will see the output class printed on the Terminal.

..................Content has been hidden....................

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