

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 转换 COCO 数据集
<a name="md-coco-transform-example"></a>

使用以下 Python 示例将边界框信息从 COCO 格式的数据集转换为 Amazon Rekognition Custom Labels 清单文件。该代码会将创建的清单文件上载到您的 Amazon S3 存储桶。该代码还提供了一个 AWS CLI 命令，您可以使用该命令上传您的图像。

**转换 COCO 数据集 (SDK)**

1. 如果您尚未执行以下操作，请：

   1. 确保您具有 `AmazonS3FullAccess` 权限。有关更多信息，请参阅 [设置 SDK 权限](su-sdk-permissions.md)。

   1. 安装并配置 AWS CLI 和 AWS SDKs。有关更多信息，请参阅 [步骤 4：设置 AWS CLI 和 AWS SDKs](su-awscli-sdk.md)。

1. 使用以下 Python 代码转换 COCO 数据集。设置以下值。
   + `s3_bucket`：要在其中存储图像和 Amazon Rekognition Custom Labels 清单文件的 S3 存储桶的名称。
   + `s3_key_path_images`：S3 存储桶 (`s3_bucket`) 中将要放置图像的位置的路径。
   + `s3_key_path_manifest_file`：S3 存储桶 (`s3_bucket`) 中将要放置自定义标签清单文件的位置的路径。
   + `local_path`：示例打开输入 COCO 数据集并保存新的自定义标签清单文件的位置的本地路径。
   + `local_images_path`：要用于训练的图像的本地路径。
   + `coco_manifest`：输入 COCO 数据集的文件名。
   + `cl_manifest_file`：该示例创建的清单文件的名称。该文件保存在 `local_path` 指定的位置。按照惯例，该文件具有扩展名 `.manifest`，但这不是必需要求的。
   + `job_name`：自定义标签作业的名称。

   ```
   import json
   import os
   import random
   import shutil
   import datetime
   import botocore
   import boto3
   import PIL.Image as Image
   import io
   
   #S3 location for images
   s3_bucket = 'bucket'
   s3_key_path_manifest_file = 'path to custom labels manifest file/'
   s3_key_path_images = 'path to images/'
   s3_path='s3://' + s3_bucket  + '/' + s3_key_path_images
   s3 = boto3.resource('s3')
   
   #Local file information
   local_path='path to input COCO dataset and output Custom Labels manifest/'
   local_images_path='path to COCO images/'
   coco_manifest = 'COCO dataset JSON file name'
   coco_json_file = local_path + coco_manifest
   job_name='Custom Labels job name'
   cl_manifest_file = 'custom_labels.manifest'
   
   label_attribute ='bounding-box'
   
   open(local_path + cl_manifest_file, 'w').close()
   
   # class representing a Custom Label JSON line for an image
   class cl_json_line:  
       def __init__(self,job, img):  
   
           #Get image info. Annotations are dealt with seperately
           sizes=[]
           image_size={}
           image_size["width"] = img["width"]
           image_size["depth"] = 3
           image_size["height"] = img["height"]
           sizes.append(image_size)
   
           bounding_box={}
           bounding_box["annotations"] = []
           bounding_box["image_size"] = sizes
   
           self.__dict__["source-ref"] = s3_path + img['file_name']
           self.__dict__[job] = bounding_box
   
           #get metadata
           metadata = {}
           metadata['job-name'] = job_name
           metadata['class-map'] = {}
           metadata['human-annotated']='yes'
           metadata['objects'] = [] 
           date_time_obj = datetime.datetime.strptime(img['date_captured'], '%Y-%m-%d %H:%M:%S')
           metadata['creation-date']= date_time_obj.strftime('%Y-%m-%dT%H:%M:%S') 
           metadata['type']='groundtruth/object-detection'
           
           self.__dict__[job + '-metadata'] = metadata
   
   
   print("Getting image, annotations, and categories from COCO file...")
   
   with open(coco_json_file) as f:
   
       #Get custom label compatible info    
       js = json.load(f)
       images = js['images']
       categories = js['categories']
       annotations = js['annotations']
   
       print('Images: ' + str(len(images)))
       print('annotations: ' + str(len(annotations)))
       print('categories: ' + str(len (categories)))
   
   
   print("Creating CL JSON lines...")
       
   images_dict = {image['id']: cl_json_line(label_attribute, image) for image in images}
   
   print('Parsing annotations...')
   for annotation in annotations:
   
       image=images_dict[annotation['image_id']]
   
       cl_annotation = {}
       cl_class_map={}
   
       # get bounding box information
       cl_bounding_box={}
       cl_bounding_box['left'] = annotation['bbox'][0]
       cl_bounding_box['top'] = annotation['bbox'][1]
    
       cl_bounding_box['width'] = annotation['bbox'][2]
       cl_bounding_box['height'] = annotation['bbox'][3]
       cl_bounding_box['class_id'] = annotation['category_id']
   
       getattr(image, label_attribute)['annotations'].append(cl_bounding_box)
   
   
       for category in categories:
            if annotation['category_id'] == category['id']:
               getattr(image, label_attribute + '-metadata')['class-map'][category['id']]=category['name']
           
       
       cl_object={}
       cl_object['confidence'] = int(1)  #not currently used by Custom Labels
       getattr(image, label_attribute + '-metadata')['objects'].append(cl_object)
   
   print('Done parsing annotations')
   
   # Create manifest file.
   print('Writing Custom Labels manifest...')
   
   for im in images_dict.values():
   
       with open(local_path+cl_manifest_file, 'a+') as outfile:
               json.dump(im.__dict__,outfile)
               outfile.write('\n')
               outfile.close()
   
   # Upload manifest file to S3 bucket.
   print ('Uploading Custom Labels manifest file to S3 bucket')
   print('Uploading'  + local_path + cl_manifest_file + ' to ' + s3_key_path_manifest_file)
   print(s3_bucket)
   s3 = boto3.resource('s3')
   s3.Bucket(s3_bucket).upload_file(local_path + cl_manifest_file, s3_key_path_manifest_file + cl_manifest_file)
   
   # Print S3 URL to manifest file,
   print ('S3 URL Path to manifest file. ')
   print('\033[1m s3://' + s3_bucket + '/' + s3_key_path_manifest_file + cl_manifest_file + '\033[0m') 
   
   # Display aws s3 sync command.
   print ('\nAWS CLI s3 sync command to upload your images to S3 bucket. ')
   print ('\033[1m aws s3 sync ' + local_images_path + ' ' + s3_path + '\033[0m')
   ```

1. 运行该代码。

1. 在程序输出中，记下 `s3 sync` 命令。您在下一个步骤中需要用到它。

1. 在命令提示符处，运行 `s3 sync` 命令。您的图像将上传到 S3 存储桶。如果该命令在上传过程中失败，请再次运行它，直到您的本地图像与 S3 存储桶同步为止。

1. 在程序输出中，记下清单文件的 S3 URL 路径。您在下一个步骤中需要用到它。

1. 按照[使用 SageMaker AI Ground Truth 清单文件创建数据集（控制台）](md-create-dataset-ground-truth.md#md-create-dataset-ground-truth-console)中的说明，使用上传的清单文件创建数据集。对于步骤 8，在 **.manifest 文件位置**中，输入您在上一步中记下的 Amazon S3 URL。如果使用的是 AWS SDK，请执行[使用 SageMaker AI Ground Truth 清单文件 (SDK) 创建数据集](md-create-dataset-ground-truth.md#md-create-dataset-ground-truth-sdk)。