Ejemplo n.º 1
0
    def execute(self, image_path):
        """
        Show the result of the network on the given image
        :param image_path: The path of the image
        """
        is_cuda = next(self.model.parameters()).is_cuda

        image = Image.open(image_path).convert('RGB')

        width, height = image.size
        new_width = math.sqrt(6 * (10**5) * width / height)
        new_width = int(new_width)
        new_height = height * new_width // width

        resized = image.resize((new_width, new_height), Image.ANTIALIAS)

        image = torch.from_numpy(image_pillow_to_numpy(image))
        resized = torch.from_numpy(image_pillow_to_numpy(resized))

        if is_cuda:
            result = self.model(
                torch.autograd.Variable(
                    resized.unsqueeze(0).float().cuda()))[0]
        else:
            result = self.model(
                torch.autograd.Variable(resized.unsqueeze(0).float().cpu()))[0]

        self.loss.show_ytrue(resized.cpu().numpy(),
                             result.cpu().detach().numpy())
        lines = self.loss.ytrue_to_lines(image.cpu().numpy(),
                                         result.cpu().detach().numpy())
        for line, pos in lines:
            show_numpy_image(line, invert_axes=True)
Ejemplo n.º 2
0
    def __getitem__(self, index):
        image = Image.open(self.list[index]).convert("RGB")

        width, height = image.size
        new_width = math.sqrt(6 * (10**5) * width / height)
        new_width = int(new_width)
        new_height = height * new_width // width

        if new_width < width:
            resized = image.resize((new_width, new_height), Image.ANTIALIAS)
        else:
            resized = image

        return image_pillow_to_numpy(resized), image_pillow_to_numpy(
            image), self.list[index]
Ejemplo n.º 3
0
    def __getitem__(self, index):
        image_path, regions = self.labels[index % len(self.labels)]

        image = Image.open(image_path).convert('RGB')
        width, height = image.size
        new_width = math.sqrt(6 * (10**5) * width / height)
        # new_width = new_width * uniform(0.8, 1.2)
        new_width = int(new_width)
        new_height = height * new_width // width
        resized = image.resize((new_width, new_height), Image.ANTIALIAS)

        new_regions = []
        for region in regions:
            x0, y0, x1, y1, h = region
            x0 = x0 * new_height // height
            y0 = y0 * new_height // height
            x1 = x1 * new_height // height
            y1 = y1 * new_height // height
            h = h * new_height // height
            new_regions.append([x0, y0, x1, y1, h])

        if len(new_regions) == 0:
            label = np.zeros((new_height, new_width, 2), dtype=np.float32)
        else:
            label = self.loss.document_to_ytrue(
                np.array([new_width, new_height], dtype='int32'),
                np.array(new_regions, dtype='int32'))

        # angle = randint(-45, 45)
        # label = rotate(label, angle, order=0)
        # image = rotate(image, angle, order=0)

        # image = np.swapaxes(image, 0, 2)
        # image = np.swapaxes(image, 1, 2)

        label = np.swapaxes(label, 0, 2)
        label = np.swapaxes(label, 1, 2)

        # if self.transform:
        #     image = self.helper.augment(image, distort=False)

        # c = gauss_distort([image[0], image[1], image[2], label[0], label[1]])
        # image = np.stack((c[0], c[1], c[2]), axis=0)
        # label = np.stack((c[3], c[4]), axis=0)

        return image_pillow_to_numpy(resized), image_pillow_to_numpy(
            image), image_path, label
Ejemplo n.º 4
0
    def __getitem__(self, index):
        image_pillow, label = self.generate_image_with_label(index)
        image = image_pillow_to_numpy(image_pillow)

        try:
            return torch.from_numpy(image), (self.loss.preprocess_label(
                label, image.shape[2]), label, image.shape[2])
        except:
            return self.__getitem__(index)
    def __getitem__(self, index):
        id, text = self.labels[index]
        ids = id.split("-")
        image_path = os.path.join(self.images_path, ids[0] + "/" + ids[0] + "-" + ids[1] + "/" + ids[0] + "-" + ids[1] + "-" + ids[2] + ".png")

        # Load the image
        image = Image.open(image_path).convert('RGB')
        width, height = image.size

        image = image.resize((width * self.height // height, self.height), Image.ANTIALIAS)

        if self.transform:
            image = self.document_helper.paster_into_random_background(image)

        image = image_pillow_to_numpy(image)
        return torch.from_numpy(image), (self.loss.preprocess_label(text, width * self.height // height), text, image.shape[2])
Ejemplo n.º 6
0
    def __getitem__(self, index):
        image_path, text = self.labels[index]

        # Load the image
        image = Image.open(image_path).convert('RGB')
        width, height = image.size

        if self.height is not None:
            image = image.resize((width * self.height // height, self.height),
                                 Image.ANTIALIAS)

        if self.transform:
            image = self.document_helper.paster_into_random_background(image)

        image = image_pillow_to_numpy(image)

        return torch.from_numpy(image), (self.loss.preprocess_label(
            text, width * self.height // height), text, image.shape[2])