def visualize(self, parameters={}):
        image_original = PTImage.from_cwh_torch(self.data[0])
        drawing_image = image_original.to_order_and_class(
            Ordering.HWC, ValueClass.BYTE0255).get_data().copy()

        boxes, classes = self.output[1:]
        # Nx4 boxes and N class tensor
        valid_boxes, valid_classes = MultiObjectDetector.post_process_boxes(
            self.data[0], boxes, classes, len(self.class_lookup))
        # convert targets
        real_targets = self.target[0][:, 0] > -1
        filtered_targets = self.target[0][real_targets].reshape(
            -1, self.target[0].shape[1])
        target_boxes = filtered_targets[:, 1:]
        target_classes = filtered_targets[:, 0]

        if target_boxes.shape[0] > 0:
            draw_objects_on_np_image(drawing_image,
                                     self.__convert_to_objects(
                                         target_boxes, target_classes),
                                     color=(255, 0, 0))
        if valid_boxes.shape[0] > 0:
            draw_objects_on_np_image(drawing_image,
                                     self.__convert_to_objects(
                                         valid_boxes, valid_classes),
                                     color=None)
        ImageVisualizer().set_image(PTImage(drawing_image),
                                    parameters.get('title', '') + ' : Output')
Beispiel #2
0
    def apply_to_image(self, image, _output_size):
        output_size = [int(_output_size[0]), int(_output_size[1])]
        inverse_transform = self.inverse.copy()
        inverse_transform[0:2, 0:2] = self.inverse[1:None:-1, 1:None:-1]
        inverse_transform[0:2, 2] = self.inverse[1:None:-1, 2]

        assert image.ordering == Ordering.HWC, 'Ordering must be HWC to apply the affine transform!'
        img_data = image.get_data()
        # check if image only has 1 channel, duplicate the channels
        if len(img_data.shape) == 2:
            img_data = np.stack((img_data, ) * 3, axis=2)
        assert len(img_data.shape
                   ) == 3, 'Input image must have 3 channels! found {}'.format(
                       image_data.shape)
        newimage = PTImage(data=np.empty(
            [output_size[0], output_size[1], img_data.shape[2]],
            dtype=image.vc['dtype']),
                           ordering=Ordering.HWC,
                           vc=image.vc)
        newimage_data = newimage.get_data()
        # print self.inverse
        # print inverse_transform
        # print output_size

        # for i in range(0,image.data.shape[2]):
        #     newimage.data[:,:,i] = affine_transform(image.data[:,:,i],
        #                                             self.inverse[0:2,0:2],
        #                                             offset=-self.transform[0:2,2],
        #                                             output_shape=output_size).astype(image.vc['dtype'])

        # scipy's affine_transform sucks, it only accepts 2x2 affine matrices and
        # you have to specify the offset from the input using my own affine
        # Going to use map_coordinates apply the affine and interpolation separately

        # 1) first create an augmented matrix of 3 x (m*n) output points
        px, py = np.mgrid[0:output_size[0]:1, 0:output_size[1]:1]
        points = np.c_[px.ravel(), py.ravel()]
        points_aug = np.concatenate((points, np.ones((points.shape[0], 1))),
                                    axis=1)

        # 2) next apply the inverse transform to find the input points to sample at
        inv_points = np.dot(inverse_transform, points_aug.T)

        # 3) use map_coordinates to do a interpolation on the input image at the required points
        for i in range(0, img_data.shape[2]):
            newimage_data[:, :,
                          i] = map_coordinates(
                              img_data[:, :, i],
                              inv_points[0:2, :],
                              order=self.interp_order).reshape(output_size)

        return newimage
Beispiel #3
0
 def __init__(self,image_path='',objects=[]):
     self.image_path = image_path
     self.objects = objects
     self.image = PTImage(pil_image_path=self.image_path,persist=False)
Beispiel #4
0
 def __init__(self, image_path='', objs=[], calib_mat=None):
     self.image_path = image_path
     self.objects = copy.deepcopy(objs)
     self.image = PTImage(pil_image_path=self.image_path, persist=False)
     # 4x3 calibration matrix
     self.calib_mat = calib_mat
Beispiel #5
0
 def __init__(self, image_path='', objs=[]):
     self.image_path = image_path
     self.objects = copy.deepcopy(objs)
     self.image = PTImage(pil_image_path=self.image_path, persist=False)