Créez un fichier manifeste Lookout for Vision à l'aide d'un AWS SDK - Exemples de code de l'AWS SDK

D'autres AWS SDK exemples sont disponibles dans le GitHub dépôt AWS Doc SDK Examples.

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Créez un fichier manifeste Lookout for Vision à l'aide d'un AWS SDK

L'exemple de code suivant montre comment créer un fichier manifeste Lookout for Vision et le charger sur Amazon S3.

Pour plus d'informations, consultez la section Création d'un fichier manifeste.

Python
SDKpour Python (Boto3)
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

class Datasets: @staticmethod def create_manifest_file_s3(s3_resource, image_s3_path, manifest_s3_path): """ Creates a manifest file and uploads to Amazon S3. :param s3_resource: A Boto3 Amazon S3 resource. :param image_s3_path: The Amazon S3 path to the images referenced by the manifest file. The images must be in an Amazon S3 bucket with the following folder structure. s3://amzn-s3-demo-bucket/<train or test>/ normal/ anomaly/ Place normal images in the normal folder and anomalous images in the anomaly folder. :param manifest_s3_path: The Amazon S3 location in which to store the created manifest file. """ output_manifest_file = "temp.manifest" try: # Current date and time in manifest file format. dttm = datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f") # Get bucket and folder from image and manifest file paths. bucket, prefix = image_s3_path.replace("s3://", "").split("/", 1) if prefix[-1] != "/": prefix += "/" manifest_bucket, manifest_prefix = manifest_s3_path.replace( "s3://", "" ).split("/", 1) with open(output_manifest_file, "w") as mfile: logger.info("Creating manifest file") src_bucket = s3_resource.Bucket(bucket) # Create JSON lines for anomalous images. for obj in src_bucket.objects.filter( Prefix=prefix + "anomaly/", Delimiter="/" ): image_path = f"s3://{src_bucket.name}/{obj.key}" manifest = Datasets.create_json_line(image_path, "anomaly", dttm) mfile.write(json.dumps(manifest) + "\n") # Create json lines for normal images. for obj in src_bucket.objects.filter( Prefix=prefix + "normal/", Delimiter="/" ): image_path = f"s3://{src_bucket.name}/{obj.key}" manifest = Datasets.create_json_line(image_path, "normal", dttm) mfile.write(json.dumps(manifest) + "\n") logger.info("Uploading manifest file to %s", manifest_s3_path) s3_resource.Bucket(manifest_bucket).upload_file( output_manifest_file, manifest_prefix ) except ClientError: logger.exception("Error uploading manifest.") raise except Exception: logger.exception("Error uploading manifest.") raise else: logger.info("Completed manifest file creation and upload.") finally: try: os.remove(output_manifest_file) except FileNotFoundError: pass @staticmethod def create_json_line(image, class_name, dttm): """ Creates a single JSON line for an image. :param image: The S3 location for the image. :param class_name: The class of the image (normal or anomaly) :param dttm: The date and time that the JSON is created. """ label = 0 if class_name == "normal": label = 0 elif class_name == "anomaly": label = 1 else: logger.error("Unexpected label value: %s for %s", label, image) raise Exception(f"Unexpected label value: {label} for {image}") manifest = { "source-ref": image, "anomaly-label": label, "anomaly-label-metadata": { "confidence": 1, "job-name": "labeling-job/anomaly-label", "class-name": class_name, "human-annotated": "yes", "creation-date": dttm, "type": "groundtruth/image-classification", }, } return manifest