Beispiel #1
0
def resize( image, height=128,  width=128, interpolate_mode=cv2.INTER_LANCZOS4 ):
    return F.resize_image(image, 
        height=height, width=width, 
        resize_mode='square', 
        padding_mode=cv2.BORDER_CONSTANT,
        interpolate_mode=interpolate_mode, 
        )
Beispiel #2
0
def test_dataset_generator():

    data = FactoryDataset.factory(pathname=os.path.expanduser('~/.datasets/'),
                                  name=FactoryDataset.ck,
                                  subset=FactoryDataset.training,
                                  download=True)

    ren = Generator()
    img, y = data[np.random.randint(len(data))]
    img = np.stack((img, img, img), axis=2)

    idx = 1
    pathname = os.path.expanduser('~/.datasets/coco')
    data_back = imutl.imageProvide(pathname, ext='jpg')
    back = data_back[(idx) % len(data_back)]
    back = F.resize_image(back,
                          640,
                          1024,
                          resize_mode='crop',
                          interpolate_mode=cv2.INTER_LINEAR)
    #back = back[:,:,0]
    #back = np.random.randint(255, size=(640, 1024) )

    print(img.shape, img.max())
    print(back.shape, back.max())

    image, image_ilu, mask, h = ren.generate(img, back)
    print(image.shape, image.max())
    print(mask.shape, mask.max())
Beispiel #3
0
    def __getitem__(self, idx):

        # read image
        image, label = self.data[(idx) % len(self.data)]
        #A,A_inv = F.compute_norm_mat( image.shape[1], image.shape[0] )
        #image = F.equalization(image,A,A_inv)
        image = utility.to_channels(image, self.num_channels)

        # read background
        if self.bbackimage:
            idxk = random.randint(1, len(self.databack) - 1)
            back = self.databack[idxk]
            back = F.resize_image(back,
                                  640,
                                  1024,
                                  resize_mode='crop',
                                  interpolate_mode=cv2.INTER_LINEAR)
            back = utility.to_channels(back, self.num_channels)
        else:
            back = np.ones((640, 1024, 3), dtype=np.uint8) * 255

        if self.generate == 'image':
            obj = ObjectImageTransform(image)

        elif self.generate == 'image_and_mask':

            image_org, image_ilu, mask, h = self.ren.generate(image, back)

            image_org = utility.to_gray(image_org.astype(np.uint8))
            image_org = utility.to_channels(image_org, self.num_channels)
            image_org = image_org.astype(np.uint8)

            image_ilu = utility.to_gray(image_ilu.astype(np.uint8))
            image_ilu = utility.to_channels(image_ilu, self.num_channels)
            image_ilu = image_ilu.astype(np.uint8)

            mask = mask[:, :, 0]
            mask_t = np.zeros((mask.shape[0], mask.shape[1], 2))
            mask_t[:, :, 0] = (mask == 0).astype(np.uint8)  # 0-backgraund
            mask_t[:, :, 1] = (mask == 1).astype(np.uint8)

            obj_image = ObjectImageTransform(image_org.copy())
            obj_data = ObjectImageAndMaskMetadataTransform(
                image_ilu.copy(), mask_t, np.concatenate(
                    ([label], h), axis=0))  #np.array([label])

        else:
            assert (False)

        if self.transform_image:
            obj_image = self.transform_image(obj_image)

        if self.transform_data:
            obj_data = self.transform_data(obj_data)

        x_img, y_mask, y_lab = obj_data.to_value()
        x_org = obj_image.to_value()

        return x_org, x_img, y_mask, y_lab
    def __getitem__(self, idx):

        # read image
        image, label = self.data[(idx) % len(self.data)]
        image = utility.to_channels(image, self.num_channels)

        # read background
        if self.bbackimage:
            idxk = random.randint(1, len(self.databack) - 1)
            back = self.databack[idxk]  #(idx)%len(self.databack)
            back = F.resize_image(back,
                                  640,
                                  1024,
                                  resize_mode='crop',
                                  interpolate_mode=cv2.INTER_LINEAR)
            back = utility.to_channels(back, self.num_channels)
        else:
            back = np.ones((640, 1024, 3), dtype=np.uint8) * 255

        if self.generate == 'image':
            obj = ObjectImageTransform(image)

        elif self.generate == 'image_and_label':
            _, image, _ = self.ren.generate(image, back)
            image = utility.to_gray(image.astype(np.uint8))
            image_t = utility.to_channels(image, self.num_channels)
            image_t = image_t.astype(np.uint8)
            label = utility.to_one_hot(int(label), self.data.numclass)
            obj = ObjectImageAndLabelTransform(image_t, label)

        elif self.generate == 'image_and_mask':
            _, image, mask = self.ren.generate(image, back)
            image = utility.to_gray(image.astype(np.uint8))
            image_t = utility.to_channels(image, self.num_channels)
            image_t = image_t.astype(np.uint8)
            #print( image_t.shape, image_t.min(), image_t.max(), flush=True )
            #assert(False)
            mask = mask[:, :, 0]
            mask_t = np.zeros((mask.shape[0], mask.shape[1], 2))
            mask_t[:, :, 0] = (mask == 0).astype(np.uint8)  # backgraund
            mask_t[:, :, 1] = (mask == 1).astype(np.uint8)
            obj = ObjectImageAndMaskMetadataTransform(image_t, mask_t,
                                                      np.array([label]))

        else:
            assert (False)

        if self.transform:
            obj = self.transform(obj)

        return obj.to_dict()
def test_dataset_generator():

    data = FactoryDataset.factory(pathname='~/.datasets/',
                                  name=FactoryDataset.bu3dfe,
                                  subset=FactoryDataset.training,
                                  download=True)

    ren = Generator()
    img, y = data[np.random.randint(len(data))]
    img = np.stack((img, img, img), axis=2)

    idx = 1
    pathname = os.path.expanduser('~/.datasets/photo')
    data_back = imutl.imageProvide(pathname, ext='jpg')
    back = data_back[(idx) % len(data_back)]
    back = F.resize_image(back,
                          640,
                          1024,
                          resize_mode='crop',
                          interpolate_mode=cv2.INTER_LINEAR)
    #back = back[:,:,0]
    #back = np.random.randint(255, size=(640, 1024) )

    print(img.shape, img.max())
    print(back.shape, back.max())

    image, mask = ren.generate(img, back)
    print(image.shape, image.max())
    print(mask.shape, mask.max())

    plt.figure()
    plt.subplot(121)
    plt.imshow(image.mean(axis=2), cmap='gray')
    plt.subplot(122)
    plt.imshow(mask[:, :, 0])
    plt.show()