def __init__(self, root_folder, metadata_file="labels.json",
                 description="Generic Dataset JSON-Based of image with Age labels", dataset_normalizers=None):
        """
        Initialization of a dataset of image with ages labeled.
        Metadata is built from a JSON file.
        :param root_folder:
        :param metadata_file: JSON file URI, which is composed by the following format: {'IMAGE_FILE_NAME': AGE_RANGE_STRING}

                              Example:
                                {'test/image1.jpg': '[23,34]'}

                              Note: the working directory when loading the metadata_file is root_folder.
        :param description: description of the dataset for report purposes.
        :param dataset_normalizers: list of normalizers to normalize when storing image inside this dataset.
        :return:
        """
        # This dataset class is also capable of creating datasets.
        mkdir_p(root_folder)

        # When metadata_file is not an absolute URI, it is related to root_folder.
        if not self._is_absolute_uri(metadata_file):
            metadata_file = os.path.join(root_folder, metadata_file)

        Dataset.__init__(self, root_folder, metadata_file, description)

        self.autoencoded_uris = {}

        if not dataset_normalizers:
            dataset_normalizers = []

        self.normalizers = dataset_normalizers
Beispiel #2
0
    def put_image(self, image, autoencode_uri=True, apply_normalizers=True):
        """
        Puts an image in the dataset.
        It must be filled with content, relative uri and metadata in order to be created the dataset.
        :param image: Image to save in the dataset.
        :param autoencode_uri: Boolean flag to set if the URI should be automatically filled by the dataset or not.
        :param dataset_normalizer: normalizer to apply to the image
        :param apply_normalizers: boolean flag to apply normalizers when the image is put into the dataset manually.
        :return:
        """

        if autoencode_uri:
            uri = self._encode_uri_for_image(image)

        else:
            uri = image.get_uri()

        if self._is_absolute_uri(uri):
            raise Exception(
                "Uri for storing into dataset must be relative, not absolute")

        key = uri  # We index by the relative uri
        uri = os.path.join(self.root_folder, uri)
        mkdir_p(os.path.dirname(uri))

        self.metadata_content[key] = image.get_metadata()[0]

        try:
            if not image.is_loaded():
                image.load_from_uri()

            if not image.is_loaded():
                raise Exception("Image may not exist or it is not valid.")

            image_blob = image.get_blob()

            normalizers_applied = 0
            if apply_normalizers:
                for normalizer in self.normalizers:
                    image_blob = normalizer.apply(image_blob)
                    normalizers_applied += 1

            cv2.imwrite(uri, image_blob)
            print("Saved into {} ({} normalizers applied)".format(
                uri, normalizers_applied))

        except Exception as ex:
            print(
                "Could not write image \"{}\" into dataset.Reason: {}".format(
                    image.get_uri(), ex))
            del self.metadata_content[key]