Beispiel #1
0
    def __getitem__(self, index: int) -> Tuple[Any, Any]:
        img = np.array(Image.open(self.images[index]).convert("RGB"))
        annotation = self.parse_voc_xml(
            ET_parse(self.annotations[index]).getroot())["annotation"]
        bbox_classes = list(
            map(
                lambda x: (
                    int(x["bndbox"]["xmin"]),
                    int(x["bndbox"]["ymin"]),
                    int(x["bndbox"]["xmax"]),
                    int(x["bndbox"]["ymax"]),
                    x["name"],
                ),
                annotation["object"],
            ))
        if self.transforms is not None:
            result = self.transforms(image=img, bboxes=bbox_classes)
        image, bbox_classes = result["image"] / 255.0, result["bboxes"]
        bboxes = np.stack([a[:4] for a in bbox_classes])
        labels = [self.name2class[a[4]] for a in bbox_classes]

        target = {}
        target["boxes"] = torch.tensor(bboxes)
        target["labels"] = torch.tensor(labels)
        target["image_id"] = annotation["filename"]
        target["area"] = torch.tensor(
            (bboxes[:, 3] - bboxes[:, 1]) * (bboxes[:, 2] - bboxes[:, 0]))
        target["iscrowd"] = torch.tensor([False] * len(bboxes))

        return image, target
Beispiel #2
0
    def __getitem__(self, index: int) -> Tuple[Any, Any]:
        """
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is a dictionary of the XML tree.
        """
        img = Image.open(self.images[index]).convert("RGB")
        target = self.parse_voc_xml(ET_parse(self.annotations[index]).getroot())

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target
Beispiel #3
0
    def __getitem__(self, index):
        """
        Args:
            index (int): Index
        Returns:
            tuple: (image, target). target are the properties as annotations in image
        """
        image_name, target_class = self._breed_images[index]
        image_path = join(self.images_folder, image_name)
        image = Image.open(image_path).convert('RGB')
        target = self.parse_voc_xml(
            ET_parse(self.annotations_folder + '/' +
                     self.split[index][0]).getroot())

        if self.transforms is not None:
            image, target = self.transforms(image, target)

        return image, target
    def __getitem__(self, index):
        img = Image.open(self.images[index]).convert('RGB')
        target = self.parse_voc_xml(
            ET_parse(self.annotations[index]).getroot())

        targets, labels = [], []
        for t in target['annotation']['object']:
            bbox = [
                int(t['bndbox']['xmin']),
                int(t['bndbox']['ymin']),
                int(t['bndbox']['xmax']),
                int(t['bndbox']['ymax'])
            ]
            label = classes.index(t['name'])

            targets.append(bbox)
            labels.append(label)

        labels = torch.tensor(labels)
        if self.transform:
            img = self.transform(img)
            targets = self.transform(np.array(targets))

        return img, targets, labels