Creating a manifest file from a CSV file - Rekognition

Creating a manifest file from a CSV file

This example Python script simplifies the creation of a manifest file by using a Comma Separated Values (CSV) file to label images. You create the CSV file. The manifest file is suitable for Multi-label image classification or Multi-label image classification. For more information, see Find objects, scenes, and concepts.

Note

This script doesn't create a manifest file suitable for finding object locations or for finding brand locations.

A manifest file describes the images used to train a model. For example, image locations and labels assigned to images. A manifest file is made up of one or more JSON lines. Each JSON line describes a single image. For more information, see Image-Level labels in manifest files.

A CSV file represents tabular data over multiple rows in a text file. Fields on a row are separated by commas. For more information, see comma separated values. For this script, each row in your CSV file represents a single image and maps to a JSON Line in the manifest file. To create a CSV file for a manifest file that supports Multi-label image classification, you add one or more image-level labels to each row. To create a manifest file suitable for Image classification, you add a single image-level label to each row.

For example, The following CSV file describes the images in the Multi-label image classification (Flowers) Getting started project.

camellia1.jpg,camellia,with_leaves camellia2.jpg,camellia,with_leaves camellia3.jpg,camellia,without_leaves helleborus1.jpg,helleborus,without_leaves,not_fully_grown helleborus2.jpg,helleborus,with_leaves,fully_grown helleborus3.jpg,helleborus,with_leaves,fully_grown jonquil1.jpg,jonquil,with_leaves jonquil2.jpg,jonquil,with_leaves jonquil3.jpg,jonquil,with_leaves jonquil4.jpg,jonquil,without_leaves mauve_honey_myrtle1.jpg,mauve_honey_myrtle,without_leaves mauve_honey_myrtle2.jpg,mauve_honey_myrtle,with_leaves mauve_honey_myrtle3.jpg,mauve_honey_myrtle,with_leaves mediterranean_spurge1.jpg,mediterranean_spurge,with_leaves mediterranean_spurge2.jpg,mediterranean_spurge,without_leaves

The script generates JSON Lines for each row. For example, the following is the JSON Line for the first row (camellia1.jpg,camellia,with_leaves) .

{"source-ref": "s3://bucket/flowers/train/camellia1.jpg","camellia": 1,"camellia-metadata":{"confidence": 1,"job-name": "labeling-job/camellia","class-name": "camellia","human-annotated": "yes","creation-date": "2022-01-21T14:21:05","type": "groundtruth/image-classification"},"with_leaves": 1,"with_leaves-metadata":{"confidence": 1,"job-name": "labeling-job/with_leaves","class-name": "with_leaves","human-annotated": "yes","creation-date": "2022-01-21T14:21:05","type": "groundtruth/image-classification"}}

In the example CSV, the Amazon S3 path to the image is not present. If your CSV file doesn't include the Amazon S3 path for the images, use the --s3_path command line argument to specify the Amazon S3 path to the image.

The script records the first entry for each image in a deduplicated image CSV file. The deduplicated image CSV file contains a single instance of each image found in the input CSV file. Further occurrences of an image in the input CSV file are recorded in a duplicate image CSV file. If the script finds duplicate images, review the duplicate image CSV file and update the deduplicated image CSV file as necessary. Rerun the script with the deduplicated file. If no duplicates are found in the input CSV file, the script deletes the deduplicated image CSV file and duplicate image CSVfile, as they are empty.

In this procedure, you create the CSV file and run the Python script to create the manifest file.

To create a manifest file from a CSV file

  1. Create a CSV file with the following fields in each row (one row per image). Don't add a header row to the CSV file.

    Field 1 Field 2 Field n

    The image name or the Amazon S3 path the image. For example, s3://my-bucket/flowers/train/camellia1.jpg. You can't have a mixture of images with the Amazon S3 path and images without.

    The first image level label for the image.

    One or more additional image-level labels separated by commas. Add only if you want to create a manifest file that supports Multi-label image classification.

    For example camellia1.jpg,camellia,with_leaves or s3://my-bucket/flowers/train/camellia1.jpg,camellia,with_leaves

  2. Save the CSV file.

  3. Run the following Python script. Supply the following arguments:

    • csv_file – The CSV file that you created in step 1.

    • manifest_file – The name of the manifest file that you want to create.

    • (Optional)--s3_path s3://path_to_folder/ – The Amazon S3 path to add to the image file names (field 1). Use --s3_path if the images in field 1 don't already contain an S3 path.

    # Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. # PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-custom-labels-developer-guide/blob/master/LICENSE-SAMPLECODE.) from datetime import datetime, timezone import argparse import logging import csv import os """ Purpose Amazon Rekognition Custom Labels model example used in the service documentation: Shows how to create an image-level (classification) manifest file from a CSV file. You can specify multiple image level labels per image. CSV file format is image,label,label,.. If necessary, use the bucket argument to specify the S3 bucket folder for the images. """ logger = logging.getLogger(__name__) def check_duplicates (csv_file, deduplicated_file, duplicates_file): """ Checks for duplicate images in a CSV file. If duplicate images are found, deduplicated_file is the deduplicated CSV file - only the first occurence of a duplicate is recorded. Other duplicates are recorded in duplicates_file. :param csv_file: The source CSV file :param deduplicated_file: The deduplicated CSV file to create. If no duplicates are found this file is removed. :param duplicates_file: The duplicate images CSV file to create. If no duplicates are found this file is removed. :return: True if duplicates are found, otherwise false. """ logger.info(f"Deduplicating {csv_file}.") duplicates_found = False #Find duplicates with open(csv_file, 'r') as f,\ open(deduplicated_file, 'w') as dedup,\ open(duplicates_file, 'w') as duplicates: reader = csv.reader(f, delimiter=',') dedup_writer = csv.writer(dedup) duplicates_writer = csv.writer(duplicates) entries = set() for row in reader: #Skip empty lines if row==[]: continue key = row[0] if key not in entries: dedup_writer.writerow(row) entries.add(key) else: duplicates_writer.writerow(row) duplicates_found = True if duplicates_found: logger.info(f"Duplicates found check {duplicates_file}.") else: os.remove(duplicates_file) os.remove(deduplicated_file) return duplicates_found def create_manifest_file(csv_file, manifest_file, s3_path): """ Reads a CSV file and creates a Custom Labels classification manifest file :param csv_file: The source CSV file :param manifest_file: The name of the manifest file to create. :param s3_path: The S3 path to the folder that contains the images. """ logger.info(f"Processing CSV file {csv_file}.") image_count = 0 label_count = 0 with open(csv_file, newline='') as csvfile, open(manifest_file, "w") as output_file: image_classifications = csv.reader( csvfile, delimiter=',', quotechar='|') # process each row (image) in CSV file for row in image_classifications: source_ref = str(s3_path)+row[0] image_json = '{"source-ref": "' + source_ref + '",' label_json = '' last_line = '' image_count += 1 # process each image level label for index in range(1, len(row)): image_level_label = row[index] #Skip empty columns if image_level_label=='': continue label_count += 1 label_json += '"' + image_level_label + '": 1,'\ '"' + image_level_label + '-metadata":'\ '{"confidence": 1,'\ '"job-name": "labeling-job/' + image_level_label + '",'\ '"class-name": "' + image_level_label + '",'\ '"human-annotated": "yes",'\ '"creation-date": "' + datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%f') + '",' last_line = '"type": "groundtruth/image-classification"},' if index == len(row)-1: last_line = '"type": "groundtruth/image-classification"}}' label_json += last_line json_line = image_json + label_json + '\n' # write the image JSON Line output_file.write(json_line) output_file.close() logger.info(f"Finished creating manifest file {manifest_file}.\n" f"Images: {image_count}\nLabels: {label_count}") return image_count, label_count def add_arguments(parser): """ Adds command line arguments to the parser. :param parser: The command line parser. """ parser.add_argument( "csv_file", help="The CSV file that you want to process." ) parser.add_argument( "--s3_path", help="The S3 bucket and folder path for the images." " If not supplied, column 1 is assumed to include the S3 path.", required=False ) def main(): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") try: # get command line arguments parser = argparse.ArgumentParser(usage=argparse.SUPPRESS) add_arguments(parser) args = parser.parse_args() s3_path=args.s3_path if s3_path is None: s3_path='' csv_file = args.csv_file manifest_file = os.path.splitext(csv_file)[0]+'.manifest' duplicates_file = f'duplicates-{csv_file}' deduplicated_file = f'deduplicated-{csv_file}' #Create mainfest file if there are no duplicate images. if check_duplicates(csv_file, deduplicated_file, duplicates_file): print( f'Duplicates found. Use {duplicates_file} to view duplicates and then update {deduplicated_file}. ') print(f'{deduplicated_file} contains the first occurence of a duplicate. '\ 'Update as necessary with the correct label information.') print(f'Re-run the script with {deduplicated_file}') else: print('No duplicates found. Creating manifest file') image_count, label_count = create_manifest_file(csv_file, manifest_file, s3_path) print(f"Finished creating manifest file: {manifest_file} \n" f"Images: {image_count}\nLabels: {label_count}") except FileNotFoundError as err: logger.exception(f"File not found.:{err}") print(f"File not found: {err}. Check your input CSV file") except Exception as err: logger.exception(f"An error occured:{err}") print(f"An error occured:{err}") if __name__ == "__main__": main()
  4. If you plan to use a test dataset, repeat steps 1–3 to create a manifest file for your test dataset.

  5. If necessary, copy the images to the Amazon S3 bucket path that you specified in column 1 of the CSV file (or specified in the --s3_path command line). You can use the following AWS S3 command.

    aws s3 cp --recursive your-local-folder s3://your-target-S3-location
  6. Follow the instructions at Creating a dataset with a manifest file to create a dataset.