コード例 #1
0
    def _aug_image(self, instance, net_h, net_w):

        image_name = instance['filename']
        image = cv2.imread(image_name)  # BGR image
        
        if image is None:
            print('Cannot find ', image_name)

        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # RGB image
            
        image_h, image_w, _ = image.shape
        
        # determine the amount of scaling and cropping
        dw = self.jitter * image_w
        dh = self.jitter * image_h

        new_ar = (image_w + np.random.uniform(-dw, dw)) / (image_h + np.random.uniform(-dh, dh))
        if self.aug == 'scale' and np.random.random_sample()<0.3:
            scale = np.random.uniform(0.25, 2)
        else:
            scale = 1

        if new_ar < 1:
            new_h = int(scale * net_h)
            new_w = int(net_h * new_ar)
        else:
            new_w = int(scale * net_w)
            new_h = int(net_w / new_ar)
            
        dx = int(np.random.uniform(0, net_w - new_w))
        dy = int(np.random.uniform(0, net_h - new_h))
        
        # apply scaling and cropping
        im_sized = apply_random_scale_and_crop(image, new_w, new_h, net_w, net_h, dx, dy)
        
        # if self.aug == True:
        #     # randomly distort hsv space
        #     im_sized = random_distort_image(im_sized)
        
        if self.aug == 'filp' and np.random.random_sample()<0.3:
            # randomly flip
            flip = np.random.randint(2)
            im_sized = random_flip(im_sized, flip)
        else:
            flip = 0
            
        # correct the size and pos of bounding boxes
        all_objs = correct_bounding_boxes(instance['object'], new_w, new_h, net_w, net_h, dx, dy, flip, image_w, image_h)
        
        return im_sized, all_objs   
コード例 #2
0
    def _aug_image(self, instance, net_h, net_w):
        image_name = instance['filename']
        image = cv2.imread(image_name)  # RGB image

        if image is None: print('Cannot find ', image_name)
        image = image[:, :, ::-1]  # RGB image

        image_h, image_w, _ = image.shape

        # determine the amount of scaling and cropping
        dw = self.jitter * image_w
        dh = self.jitter * image_h

        new_ar = (image_w + np.random.uniform(-dw, dw)) / (
            image_h + np.random.uniform(-dh, dh))
        scale = np.random.uniform(0.25, 2)

        if (new_ar < 1):
            new_h = int(scale * net_h)
            new_w = int(net_h * new_ar)
        else:
            new_w = int(scale * net_w)
            new_h = int(net_w / new_ar)

        dx = int(np.random.uniform(0, net_w - new_w))
        dy = int(np.random.uniform(0, net_h - new_h))

        # apply scaling and cropping
        im_sized = apply_random_scale_and_crop(image, new_w, new_h, net_w,
                                               net_h, dx, dy)

        # randomly distort hsv space
        im_sized = random_distort_image(im_sized)

        # randomly flip
        flip = np.random.randint(2)
        im_sized = random_flip(im_sized, flip)

        # correct the size and pos of bounding boxes
        all_objs = correct_bounding_boxes(instance['object'], new_w, new_h,
                                          net_w, net_h, dx, dy, flip, image_w,
                                          image_h)

        return im_sized, all_objs