예제 #1
0
    def __getitem__(self, index: int):
        """See superclass for documentation

        Notes
        -----
        The return value is a dict with the following fields:
            image_id: int
                The index of the image in the split
            image_path: str
                The path to the image on the file system
            category_id : int
                The single label of the image selected
        """
        img = load_pil_image(self.images_path[index])
        if self.transform is not None:
            img = self.transform(img)

        target = self.parse_json(index)
        annotations = target.pop("annotations")
        tags = [
            self.classes.index(a["name"]) for a in annotations if "tag" in a
        ]
        if len(tags) > 1:
            raise ValueError(
                f"Multiple tags defined for this image ({tags}). This is not supported at the moment."
            )
        if len(tags) == 0:
            raise ValueError(
                f"No tags defined for this image ({self.annotations_path[index]})."
                f"This is not valid in a classification dataset.")
        target["category_id"] = tags[0]

        return img, target
예제 #2
0
 def _return_std(image_path, mean):
     img = np.array(load_pil_image(image_path)) / 255.0
     m2 = np.square(
         np.array([
             img[:, :, 0] - mean[0], img[:, :, 1] - mean[1],
             img[:, :, 2] - mean[2]
         ]))
     return np.sum(np.sum(m2, axis=1), 1), m2.size / 3.0
예제 #3
0
 def _return_mean(image_path):
     img = np.array(load_pil_image(image_path))
     mean = np.array([
         np.mean(img[:, :, 0]),
         np.mean(img[:, :, 1]),
         np.mean(img[:, :, 2])
     ])
     return mean / 255.0
예제 #4
0
    def __getitem__(self, index: int):
        """See superclass for documentation

        Notes
        -----
        The return value is a dict with the following fields:
            image_id : int
                Index of the image inside the dataset
            image_path: str
                The path to the image on the file system
            mask : tensor(H, W)
                Segmentation mask where each pixel encodes a class label
        """
        img = load_pil_image(self.images_path[index])
        target = self.parse_json(index)

        annotations = []
        for obj in target["annotations"]:
            sequences = convert_polygons_to_sequences(
                obj["polygon"]["path"],
                height=target["height"],
                width=target["width"],
            )
            # Discard polygons with less than three points
            sequences[:] = [s for s in sequences if len(s) >= 6]
            if not sequences:
                continue
            annotations.append({
                "category_id": self.classes.index(obj["name"]),
                "segmentation": sequences
            })
        target["annotations"] = annotations

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

        return img, target
예제 #5
0
 def __getitem__(self, index: int):
     img = load_pil_image(self.images_path[index])
     target = self.parse_json(index)
     return img, target
예제 #6
0
 def get_image(self, index: int):
     return load_pil_image(self.images_path[index])
예제 #7
0
 def get_image(self, index: int) -> PILImage.Image:
     return load_pil_image(self.images_path[index])
예제 #8
0
    def __getitem__(self, index: int):
        """
        Notes
        -----
        The return value is a dict with the following fields:
            image_id : int
                Index of the image inside the dataset
            image_path: str
                The path to the image on the file system
            labels : tensor(n)
                The class label of each one of the instances
            masks : tensor(n, H, W)
                Segmentation mask of each one of the instances
            boxes : tensor(n, 4)
                Coordinates of the bounding box enclosing the instances as [x, y, x, y]
            area : float
                Area in pixels of each one of the instances
        """
        img = load_pil_image(self.images_path[index])
        target = self.parse_json(index)

        annotations = []
        for annotation in target["annotations"]:
            if "polygon" not in annotation and "complex_polygon" not in annotation:
                print(
                    f"Warning: missing polygon in annotation {self.annotations_path[index]}"
                )
            # Extract the sequences of coordinates from the polygon annotation
            annotation_type = "polygon" if "polygon" in annotation else "complex_polygon"
            sequences = convert_polygons_to_sequences(
                annotation[annotation_type]["path"],
                height=target["height"],
                width=target["width"],
            )
            # Compute the bbox of the polygon
            x_coords = [s[0::2] for s in sequences]
            y_coords = [s[1::2] for s in sequences]
            min_x = np.min([np.min(x_coord) for x_coord in x_coords])
            min_y = np.min([np.min(y_coord) for y_coord in y_coords])
            max_x = np.max([np.max(x_coord) for x_coord in x_coords])
            max_y = np.max([np.max(y_coord) for y_coord in y_coords])
            w = max_x - min_x + 1
            h = max_y - min_y + 1
            # Compute the area of the polygon
            # TODO fix with addictive/subtractive paths in complex polygons
            poly_area = np.sum([
                polygon_area(x_coord, y_coord)
                for x_coord, y_coord in zip(x_coords, y_coords)
            ])

            # Create and append the new entry for this annotation
            annotations.append({
                "category_id":
                self.classes.index(annotation["name"]),
                "segmentation":
                sequences,
                "bbox": [min_x, min_y, w, h],
                "area":
                poly_area,
            })
        target["annotations"] = annotations

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

        return img, target