를 사용하여 Lookout for Vision 매니페스트 파일 생성 AWS SDK - AWS SDK 코드 예제

AWS 문서 예제 리포지토리에서 더 많은 SDK GitHub AWS SDK 예제를 사용할 수 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

를 사용하여 Lookout for Vision 매니페스트 파일 생성 AWS SDK

다음 코드 예제에서는 Lookout for Vision 매니페스트 파일을 생성하고 Amazon S3에 업로드하는 방법을 보여줍니다.

자세한 내용은 매니페스트 파일 생성을 참조하십시오.

Python
SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. 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