

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

# 将 COCO 数据集转换为清单文件格式
<a name="md-transform-coco"></a>

[COCO](http://cocodataset.org/#home) 是一种用于指定大规模物体检测、分段和字幕数据集的格式。此 Python [示例](md-coco-transform-example.md)说明了如何将 COCO 物体检测格式的数据集转换为 Amazon Rekognition Custom Labels [边界框格式的清单文件](md-create-manifest-file-object-detection.md)。本部分还提供了可用于编写自己的代码的信息。

COCO 格式的 JSON 文件由五个部分组成，提供了*整个数据集*的信息。有关更多信息，请参阅 [COCO 数据集格式](md-coco-overview.md)。
+ `info`：有关数据集的一般信息。
+ `licenses `：数据集中图像的许可信息。
+ [`images`](md-coco-overview.md#md-coco-images)：数据集中图像的列表。
+ [`annotations`](md-coco-overview.md#md-coco-annotations)：数据集中所有图像中存在的注释（包括边界框）的列表。
+ [`categories`](md-coco-overview.md#md-coco-categories)：标签类别列表。

您需要来自 `images`、`annotations` 和 `categories` 列表的信息，才能创建 Amazon Rekognition Custom Labels 清单文件。

Amazon Rekognition Custom Labels 清单文件采用 JSON 行格式，其中每行都包含了*一张图像上*的一个或多个物体的边界框和标签信息。有关更多信息，请参阅 [清单文件中的物体定位](md-create-manifest-file-object-detection.md)。

## 将 COCO 对象映射到自定义标签 JSON 行
<a name="md-mapping-coco"></a>

要转换 COCO 格式的数据集，需要将 COCO 数据集映射到 Amazon Rekognition Custom Labels 清单文件以进行物体定位。有关更多信息，请参阅 [清单文件中的物体定位](md-create-manifest-file-object-detection.md)。要为每张图片构建 JSON 行，清单文件需要映射 COCO 数据集`image`和`category`对象字段 IDs。`annotation`

下面是一个 COCO 清单文件示例。有关更多信息，请参阅 [COCO 数据集格式](md-coco-overview.md)。

```
{
    "info": {
        "description": "COCO 2017 Dataset","url": "http://cocodataset.org","version": "1.0","year": 2017,"contributor": "COCO Consortium","date_created": "2017/09/01"
    },
    "licenses": [
        {"url": "http://creativecommons.org/licenses/by/2.0/","id": 4,"name": "Attribution License"}
    ],
    "images": [
        {"id": 242287, "license": 4, "coco_url": "http://images.cocodataset.org/val2017/xxxxxxxxxxxx.jpg", "flickr_url": "http://farm3.staticflickr.com/2626/xxxxxxxxxxxx.jpg", "width": 426, "height": 640, "file_name": "xxxxxxxxx.jpg", "date_captured": "2013-11-15 02:41:42"},
        {"id": 245915, "license": 4, "coco_url": "http://images.cocodataset.org/val2017/nnnnnnnnnnnn.jpg", "flickr_url": "http://farm1.staticflickr.com/88/xxxxxxxxxxxx.jpg", "width": 640, "height": 480, "file_name": "nnnnnnnnnn.jpg", "date_captured": "2013-11-18 02:53:27"}
    ],
    "annotations": [
        {"id": 125686, "category_id": 0, "iscrowd": 0, "segmentation": [[164.81, 417.51,......167.55, 410.64]], "image_id": 242287, "area": 42061.80340000001, "bbox": [19.23, 383.18, 314.5, 244.46]},
        {"id": 1409619, "category_id": 0, "iscrowd": 0, "segmentation": [[376.81, 238.8,........382.74, 241.17]], "image_id": 245915, "area": 3556.2197000000015, "bbox": [399, 251, 155, 101]},
        {"id": 1410165, "category_id": 1, "iscrowd": 0, "segmentation": [[486.34, 239.01,..........495.95, 244.39]], "image_id": 245915, "area": 1775.8932499999994, "bbox": [86, 65, 220, 334]}
    ],
    "categories": [
        {"supercategory": "speaker","id": 0,"name": "echo"},
        {"supercategory": "speaker","id": 1,"name": "echo dot"}
    ]
}
```

下图显示了*数据集*的 COCO 数据集列表如何映射到图像的 Amazon Rekognition Custom Labels JSON 行。图像的每个 JSON 行都有一个 source-ref、作业和作业元数据字段。匹配的颜色表示单张图像的信息。请注意，在清单中，一张图像可能有多个注释和元数据/类别。

![\[图中显示 COCO 清单的结构，其中包含图像、注释和类别。\]](http://docs.aws.amazon.com/zh_cn/rekognition/latest/customlabels-dg/images/coco-transform.png)


**获取单个 JSON 行的 COCO 对象**

1. 对于图像列表中的每张图像，从注释列表中获取注释，其中注释字段 `image_id` 的值与图像 `id` 字段匹配。

1. 对于步骤 1 中匹配的每个注释，请通读 `categories` 列表并获取 `category` 字段 `id` 的值与 `annotation` 对象 `category_id` 字段匹配的每个 `category`。

1. 使用匹配的 `image`、`annotation` 和 `category` 对象为图像创建 JSON 行。要映射字段，请参阅[将 COCO 对象字段映射到自定义标签 JSON 行对象字段](#md-mapping-fields-coco)。

1. 重复步骤 1-3，直到为 `image` 列表中的每个 `images` 对象创建 JSON 行。

有关示例代码，请参阅[转换 COCO 数据集](md-coco-transform-example.md)。

## 将 COCO 对象字段映射到自定义标签 JSON 行对象字段
<a name="md-mapping-fields-coco"></a>

确定 Amazon Rekognition Custom Labels JSON 行的 COCO 对象后，需要将 COCO 对象字段映射到相应的 Amazon Rekognition Custom Labels JSON 行对象字段。以下示例 Amazon Rekognition Custom Labels JSON 行将一张图像 (`id`=`000000245915`) 映射到上面的 COCO JSON 示例。请注意以下信息。
+ `source-ref` 是图像在 Amazon S3 存储桶中的位置。如果 COCO 图像不是存储在 Amazon S3 存储桶中，则需要将它们移到 Amazon S3 存储桶中。
+ `annotations` 列表中包含了图像上每个物体的 `annotation` 对象。`annotation` 对象包含边界框信息（`top`、`left`、`width`、`height`）和标签标识符 (`class_id`)。
+ 标签标识符 (`class_id`) 映射到元数据中的 `class-map` 列表。该列表会列出图像上使用的标签。

```
{
	"source-ref": "s3://custom-labels-bucket/images/000000245915.jpg",
	"bounding-box": {
		"image_size": {
			"width": 640,
			"height": 480,
			"depth": 3
		},
		"annotations": [{
			"class_id": 0,
			"top": 251,
			"left": 399,
			"width": 155,
			"height": 101
		}, {
			"class_id": 1,
			"top": 65,
			"left": 86,
			"width": 220,
			"height": 334
		}]
	},
	"bounding-box-metadata": {
		"objects": [{
			"confidence": 1
		}, {
			"confidence": 1
		}],
		"class-map": {
			"0": "Echo",
			"1": "Echo Dot"
		},
		"type": "groundtruth/object-detection",
		"human-annotated": "yes",
		"creation-date": "2018-10-18T22:18:13.527256",
		"job-name": "my job"
	}
}
```

按照以下信息将 Amazon Rekognition Custom Labels 清单文件字段映射到 COCO 数据集 JSON 字段。

### source-ref
<a name="md-source-ref-coco"></a>

图像位置的 S3 格式 URL。该图像必须存储在 S3 存储桶中。有关更多信息，请参阅 [source-ref](md-create-manifest-file-object-detection.md#cd-manifest-source-ref)。如果 `coco_url` COCO 字段指向 S3 存储桶位置，则可以使用 `coco_url` 的值作为 `source-ref` 的值。或者，也可以将 `source-ref` 映射到 `file_name` (COCO) 字段，然后在转换代码中将所需的 S3 路径添加到图像的存储位置。

### *bounding-box*
<a name="md-label-attribute-id-coco"></a>

您选择的标签属性名称。有关更多信息，请参阅 [*bounding-box*](md-create-manifest-file-object-detection.md#md-manifest-source-bounding-box)。

#### image\$1size
<a name="md-image-size-coco"></a>

图像大小（以像素为单位）。映射到 [images](md-coco-overview.md#md-coco-images) 列表中的 `image` 对象。
+ `height`-> `image.height`
+ `width`-> `image.width`
+ `depth`：Amazon Rekognition Custom Labels 未使用该属性，但必须为其提供一个值。

#### annotations
<a name="md-annotations-coco"></a>

`annotation` 对象的列表。图像上的每个物体都有一个 `annotation`。

#### annotation
<a name="md-annotation-coco"></a>

包含图像上物体的一个实例的边界框信息。
+ `class_id`：映射到自定义标签的 `class-map` 列表的数字 ID。
+ `top` -> `bbox[1]`
+ `left` -> `bbox[0]`
+ `width` -> `bbox[2]`
+ `height` -> `bbox[3]`

### *bounding-box*-元数据
<a name="md-metadata-coco"></a>

标签属性的元数据。包含标签和标签标识符。有关更多信息，请参阅 [*bounding-box*-元数据](md-create-manifest-file-object-detection.md#md-manifest-source-bounding-box-metadata)。

#### Objects
<a name="cd-metadata-objects-coco"></a>

图像中的物体数组。按索引映射到 `annotations` 列表。

##### Object
<a name="cd-metadata-object-coco"></a>
+ Amazon Rekognition Custom Labels 未使用该属性，但必须为其指定值 (1)。

#### class-map
<a name="md-metadata-class-map-coco"></a>

适用于图像中检测到的物体的标签（类别）映射。映射到 [categories](md-coco-overview.md#md-coco-categories) 列表中的 category 对象。
+ `id` -> `category.id`
+ `id value` -> `category.name`

#### 类型
<a name="md-type-coco"></a>

必须是 `groundtruth/object-detection`

#### human-annotated
<a name="md-human-annotated-coco"></a>

指定 `yes` 或 `no`。有关更多信息，请参阅 [*bounding-box*-元数据](md-create-manifest-file-object-detection.md#md-manifest-source-bounding-box-metadata)。

#### creation-date -> [image](md-coco-overview.md#md-coco-images).date\$1captured
<a name="md-creation-date-coco"></a>

图像的创建日期和时间。映射到 COCO 图像列表中的图像的 [image](md-coco-overview.md#md-coco-images).date\$1captured 字段。Amazon Rekognition Custom Labels 期望的 `creation-date` 格式为 *Y-M-DTH:M:S*。

#### job-name
<a name="md-job-name-coco"></a>

您选择的作业名称。

# COCO 数据集格式
<a name="md-coco-overview"></a>

COCO 数据集由五个信息部分组成，提供了整个数据集的信息。COCO 物体检测数据集的格式记录在 [COCO 数据格式](http://cocodataset.org/#format-data)中。
+ info：有关数据集的一般信息。
+ licenses：数据集中图像的许可信息。
+ [images](#md-coco-images)：数据集中图像的列表。
+ [annotations](#md-coco-annotations)：数据集中所有图像中存在的注释（包括边界框）的列表。
+ [categories](#md-coco-categories)：标签类别列表。

要创建自定义标签清单，需要使用 COCO 清单文件中的 `images`、`annotations` 和 `categories` 列表。其他两个部分（`info`、`licences`）不是必需的。下面是一个 COCO 清单文件示例。

```
{
    "info": {
        "description": "COCO 2017 Dataset","url": "http://cocodataset.org","version": "1.0","year": 2017,"contributor": "COCO Consortium","date_created": "2017/09/01"
    },
    "licenses": [
        {"url": "http://creativecommons.org/licenses/by/2.0/","id": 4,"name": "Attribution License"}
    ],
    "images": [
        {"id": 242287, "license": 4, "coco_url": "http://images.cocodataset.org/val2017/xxxxxxxxxxxx.jpg", "flickr_url": "http://farm3.staticflickr.com/2626/xxxxxxxxxxxx.jpg", "width": 426, "height": 640, "file_name": "xxxxxxxxx.jpg", "date_captured": "2013-11-15 02:41:42"},
        {"id": 245915, "license": 4, "coco_url": "http://images.cocodataset.org/val2017/nnnnnnnnnnnn.jpg", "flickr_url": "http://farm1.staticflickr.com/88/xxxxxxxxxxxx.jpg", "width": 640, "height": 480, "file_name": "nnnnnnnnnn.jpg", "date_captured": "2013-11-18 02:53:27"}
    ],
    "annotations": [
        {"id": 125686, "category_id": 0, "iscrowd": 0, "segmentation": [[164.81, 417.51,......167.55, 410.64]], "image_id": 242287, "area": 42061.80340000001, "bbox": [19.23, 383.18, 314.5, 244.46]},
        {"id": 1409619, "category_id": 0, "iscrowd": 0, "segmentation": [[376.81, 238.8,........382.74, 241.17]], "image_id": 245915, "area": 3556.2197000000015, "bbox": [399, 251, 155, 101]},
        {"id": 1410165, "category_id": 1, "iscrowd": 0, "segmentation": [[486.34, 239.01,..........495.95, 244.39]], "image_id": 245915, "area": 1775.8932499999994, "bbox": [86, 65, 220, 334]}
    ],
    "categories": [
        {"supercategory": "speaker","id": 0,"name": "echo"},
        {"supercategory": "speaker","id": 1,"name": "echo dot"}
    ]
}
```

## 图像列表
<a name="md-coco-images"></a>

COCO 数据集引用的图像列在图像数组中。每个图像对象都包含了有关该图像的信息，例如图像文件名。在以下示例图像对象中，请注意以下信息以及创建 Amazon Rekognition Custom Labels 清单文件需要哪些字段。
+ `id`：（必需）图像的唯一标识符。`id` 字段映射到注释数组（存储边界框信息的地方）中的 `id` 字段。
+ `license`：（非必需）映射到许可证数组。
+ `coco_url`：（可选）图像的位置。
+ `flickr_url`：（非必需）图像在 Flickr 上的位置。
+ `width`：（必需）图像的宽度。
+ `height`：（必需）图像的高度。
+ `file_name`：（必需）图像文件名。在本示例中，`file_name` 与 `id` 匹配，但 COCO 数据集对此不作要求。
+ `date_captured`：（必需）图像拍摄的日期和时间。

```
{
    "id": 245915,
    "license": 4,
    "coco_url": "http://images.cocodataset.org/val2017/nnnnnnnnnnnn.jpg",
    "flickr_url": "http://farm1.staticflickr.com/88/nnnnnnnnnnnnnnnnnnn.jpg",
    "width": 640,
    "height": 480,
    "file_name": "000000245915.jpg",
    "date_captured": "2013-11-18 02:53:27"
}
```

## 注释（边界框）列表
<a name="md-coco-annotations"></a>

所有图像上的所有物体的边界框信息都存储在注释列表中。单个注释对象包含单个物体的边界框信息以及该物体在图像上的标签。图像上物体的每个实例都有一个注释对象。

在以下示例中，请注意以下信息以及创建 Amazon Rekognition Custom Labels 清单文件需要哪些字段。
+ `id`：（非必需）注释的标识符。
+ `image_id`：（必需）对应于图像数组中的图像 `id`。
+ `category_id`：（必需）用于标识边界框内物体的标签的标识符。它映射到类别数组的 `id` 字段。
+ `iscrowd`：（非必需）指定图像中是否包含物体群。
+ `segmentation`：（非必需）图像中物体的分段信息。Amazon Rekognition Custom Labels 不支持分段。
+ `area`：（非必需）注释的区域。
+ `bbox`：（必需）包含图像中物体的边界框的坐标（以像素为单位）。

```
{
    "id": 1409619,
    "category_id": 1,
    "iscrowd": 0,
    "segmentation": [
        [86.0, 238.8,..........382.74, 241.17]
    ],
    "image_id": 245915,
    "area": 3556.2197000000015,
    "bbox": [86, 65, 220, 334]
}
```

## 类别列表
<a name="md-coco-categories"></a>

标签信息存储在类别数组中。在以下示例类别对象中，请注意以下信息以及创建 Amazon Rekognition Custom Labels 清单文件需要哪些字段。
+ `supercategory`：（非必需）标签的父类别。
+ `id`：（必需）标签标识符。`id` 字段映射到 `annotation` 对象中的 `category_id` 字段。在以下示例中，echo dot 的标识符为 2。
+ `name`：（必需）标签名称。

```
        {"supercategory": "speaker","id": 2,"name": "echo dot"}
```

# 转换 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)。