Beispiel #1
0
    def __init__(self, detector, keypoint_estimator, radius=3):
        super(ProbabilisticKeypointPrediction, self).__init__()
        # face detector
        RGB2GRAY = pr.ConvertColorSpace(pr.RGB2GRAY)
        self.detect = pr.Predict(detector, RGB2GRAY, pr.ToBoxes2D(['face']))

        # creating pre-processing pipeline for keypoint estimator
        preprocess = SequentialProcessor()
        preprocess.add(pr.ResizeImage(keypoint_estimator.input_shape[1:3]))
        preprocess.add(pr.ConvertColorSpace(pr.RGB2GRAY))
        preprocess.add(pr.NormalizeImage())
        preprocess.add(pr.ExpandDims(0))
        preprocess.add(pr.ExpandDims(-1))

        # creating post-processing pipeline for keypoint esimtator
        # postprocess = SequentialProcessor()
        # postprocess.add(ToNumpyArray())
        # postprocess.add(pr.Squeeze(1))

        # keypoint estimator predictions
        self.estimate_keypoints = PredictMeanDistribution(
            keypoint_estimator, preprocess)

        # self.estimate_keypoints = pr.Predict(
        # keypoint_estimator, preprocess, postprocess)

        # used for drawing up keypoints in original image
        self.change_coordinates = pr.ChangeKeypointsCoordinateSystem()
        self.denormalize_keypoints = pr.DenormalizeKeypoints()
        self.crop_boxes2D = pr.CropBoxes2D()
        self.num_keypoints = len(keypoint_estimator.output_shape)
        self.draw = pr.DrawKeypoints2D(self.num_keypoints, radius, False)
        self.draw_boxes2D = pr.DrawBoxes2D(['face'], colors=[[0, 255, 0]])
        self.wrap = pr.WrapOutput(['image', 'boxes2D'])
Beispiel #2
0
 def __init__(self, class_names, prior_boxes, variances=[.1, .2]):
     super(ShowBoxes, self).__init__()
     self.deprocess_boxes = SequentialProcessor([
         pr.DecodeBoxes(prior_boxes, variances),
         pr.ToBoxes2D(class_names, True),
         pr.FilterClassBoxes2D(class_names[1:])
     ])
     self.denormalize_boxes2D = pr.DenormalizeBoxes2D()
     self.draw_boxes2D = pr.DrawBoxes2D(class_names)
     self.show_image = pr.ShowImage()
Beispiel #3
0
 def __init__(self, model_names):
     super(HaarCascadeDetectors, self).__init__()
     self.model_names = model_names
     self.detectors = []
     for class_arg, model_name in enumerate(self.model_names):
         detector = pr.Predict(HaarCascadeDetector(model_name, class_arg),
                               pr.ConvertColorSpace(pr.RGB2GRAY),
                               pr.ToBoxes2D(args.models))
         self.detectors.append(detector)
     self.draw_boxes2D = pr.DrawBoxes2D(args.models)
     self.wrap = pr.WrapOutput(['image', 'boxes2D'])
# The augment boxes pipeline
class AugmentBoxes(SequentialProcessor):
    def __init__(self, mean=pr.BGR_IMAGENET_MEAN):
        super(AugmentBoxes, self).__init__()
        self.add(pr.ToImageBoxCoordinates())
        self.add(pr.Expand(mean=mean))
        self.add(pr.RandomSampleCrop())
        self.add(pr.RandomFlipBoxesLeftRight())
        self.add(pr.ToNormalizedBoxCoordinates())


# We now visualize our current box augmentation
# For that we build a quick pipeline for drawing our boxes
draw_boxes = SequentialProcessor([
    pr.ControlMap(pr.ToBoxes2D(class_names, False), [1], [1]),
    pr.ControlMap(pr.DenormalizeBoxes2D(), [0, 1], [1], {0: 0}),
    pr.DrawBoxes2D(class_names),
    pr.ShowImage()
])

# Let's test it our box data augmentation pipeline
augment_boxes = AugmentBoxes()
print('Box augmentation examples')
for _ in range(10):
    image = P.image.load_image(image_fullpath)
    image, boxes = augment_boxes(image, box_data.copy())
    draw_boxes(P.image.resize_image(image, (300, 300)), boxes)


# There is also some box-preprocessing that is required.
Beispiel #5
0
             '/v0.9/object_detection_augmentation.png')
filename = os.path.basename(IMAGE_URL)
image_fullpath = get_file(filename, IMAGE_URL, cache_subdir='paz/tutorials')

# The x_min, y_min are the **noramlized** coordinates
# of **top-left** bounding-box corner.
H, W = load_image(image_fullpath).shape[:2]

# The x_max, y_max are the **normalized** coordinates
class_names = ['background', 'human', 'horse']
box_data = np.array([[200 / W, 60 / H, 300 / W, 200 / H, 1],
                     [100 / W, 90 / H, 400 / W, 300 / H, 2]])

# Let's visualize our boxes!
# first we transform our numpy array into our built-in ``Box2D`` messages
to_boxes2D = pr.ToBoxes2D(class_names)
denormalize = pr.DenormalizeBoxes2D()
boxes2D = to_boxes2D(box_data)
image = load_image(image_fullpath)
boxes2D = denormalize(image, boxes2D)
draw_boxes2D = pr.DrawBoxes2D(class_names)
show_image(draw_boxes2D(image, boxes2D))

# As you can see, we were not able to put everything as a
# ``SequentialProcessor``. This is because we are dealing with 2 inputs:
# ``box_data`` and ``image``. We can join them into a single processor
# using ``pr.ControlMap`` wrap. ``pr.ControlMap`` allows you to select which
# arguments (``intro_indices``) are passed to your processor, and also where
# you should put the output of your processor (``outro_indices``).
draw_boxes = SequentialProcessor()
draw_boxes.add(pr.ControlMap(to_boxes2D, intro_indices=[1], outro_indices=[1]))
Beispiel #6
0
 def __init__(self, class_names, preprocess=None, colors=None):
     super(DrawBoxData2D, self).__init__()
     self.class_names, self.colors = class_names, colors
     self.to_boxes2D = pr.ToBoxes2D(self.class_names)
     self.draw_boxes2D = pr.DrawBoxes2D(self.class_names, self.colors)
     self.preprocess = preprocess