

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將 COCO 資料集轉換為資訊清單檔案格式
<a name="md-transform-coco"></a>

[COCO](http://cocodataset.org/#home) 是一種用於指定大規模物件偵測、分割和字幕資料集的格式。此 Python [範例](md-coco-transform-example.md)會說明如何將 COCO 物件偵測格式資料集轉換為 Amazon Rekognition 自訂標籤[週框方塊格式清單檔案](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 自訂標籤清單檔案。

Amazon Rekognition 自訂標籤清單檔案採用 JSON Lines 格式，其中每行都具有*影像上*一或多個物件的週框方塊和標籤資訊。如需詳細資訊，請參閱[資訊清單檔案中的物件當地語系化](md-create-manifest-file-object-detection.md)。

## 將 COCO 物件對應至自訂標籤 JSON Line
<a name="md-mapping-coco"></a>

若要轉換 COCO 格式資料集，請將 COCO 資料集對應至 Amazon Rekognition 自訂標籤清單檔案，以進行物件本地化。如需詳細資訊，請參閱[資訊清單檔案中的物件當地語系化](md-create-manifest-file-object-detection.md)。若要為每個影像建置 JSON Line，清單檔案需要對應 COCO 資料集 `image`、`annotation` 和 `category` 物件欄位 ID。

以下是 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 自訂標籤 JSON Lines。影像的每個 JSON 行都顯示來源參考、任務和任務中繼資料欄位。相符顏色表示單一影像的資訊。請注意，在資訊清單中，個別影像可能有多個註釋和中繼資料/類別。

![\[圖表顯示 Coco 資訊清單的結構，其中包含影像、註釋和類別。\]](http://docs.aws.amazon.com/zh_tw/rekognition/latest/customlabels-dg/images/coco-transform.png)


**取得單一 JSON Line 的 COCO 物件**

1. 對於影像清單中的每個影像，請從註釋清單中取得註釋，其中註釋欄位 `image_id` 的值會和影像 `id` 欄位相符。

1. 對於步驟 1 中相符的每個註釋，請詳閱 `categories` 清單並取得 `category` 欄位 `id` 的值和 `annotation` 物件 `category_id` 欄位相符的每個 `category`。

1. 使用相符的 `image`、`annotation` 和 `category` 物件為影像建立 JSON Line。若要對應欄位，請參閱 [將 COCO 物件對應至自訂標籤 JSON Line 物件欄位](#md-mapping-fields-coco)。

1. 重複步驟 1 至 3，直到您為 `images` 清單中的每個 `image` 物件建立 JSON Lines 為止。

如需範例程式碼，請參閱 [轉換 COCO 資料集](md-coco-transform-example.md)。

## 將 COCO 物件對應至自訂標籤 JSON Line 物件欄位
<a name="md-mapping-fields-coco"></a>

在您識別 Amazon Rekognition 自訂標籤 JSON Line 的 COCO 物件之後，您需要將 COCO 物件欄位對應至各自的 Amazon Rekognition 自訂標籤 JSON Line 物件欄位。下列 Amazon Rekognition 自訂標籤 JSON Line 範例會將一個影像 (`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 自訂標籤清單檔案欄位對應至 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>

影像大小 (以像素為單位)。對應至`image`影像[清單中的 ](md-coco-overview.md#md-coco-images) 物件。
+ `height`-> `image.height`
+ `width`-> `image.width`
+ `depth`-> Amazon Rekognition 自訂標籤目前未使用，但必須一個值。

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

`annotation` 物件的清單。影像上的每個物件都有一個 `annotation`。

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

包含影像上某個物件執行個體的週框方塊資訊。
+ `class_id` -> 數字 ID 對應至自訂標籤的 `class-map` 清單。
+ `top` -> `bbox[1]`
+ `left` -> `bbox[0]`
+ `width` -> `bbox[2]`
+ `height` -> `bbox[3]`

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

標籤屬性的中繼資料。包括標籤和標籤識別碼。如需詳細資訊，請參閱[*bounding-box*-metadata](md-create-manifest-file-object-detection.md#md-manifest-source-bounding-box-metadata)。

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

影像中的物件陣列。對應至 `annotations` 清單 (依索引)。

##### 物件
<a name="cd-metadata-object-coco"></a>
+ `confidence`->Amazon Rekognition 自訂標籤未使用，但需要值 (1)。

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

套用至影像中偵測到之物件的標籤 (類別) 的對應。對應至[類別](md-coco-overview.md#md-coco-categories)清單中的類別物件。
+ `id` -> `category.id`
+ `id value` -> `category.name`

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

必須為 `groundtruth/object-detection`

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

可指定為 `yes` 或 `no`。如需詳細資訊，請參閱[*bounding-box*-metadata](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 自訂標籤預期 `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 自訂標籤清單檔案所需的欄位。
+ `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 自訂標籤清單檔案所需的欄位。
+ `id` — (非必要) 註釋的識別碼。
+ `image_id` — (必要) 對應於影像陣列中的影像 `id`。
+ `category_id` — (必要) 標籤的識別碼，可識別週框方塊內的物件。它會對應至類別陣列的 `id` 欄位。
+ `iscrowd` — (非必要) 指定影像是否包含一群物件。
+ `segmentation` — (非必要) 影像上物件的分割資訊。Amazon Rekognition 自訂標籤不支援分割。
+ `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 自訂標籤清單檔案所需的欄位。
+ `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 自訂標籤清單檔案。程式碼會將建立的清單檔案上傳至 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 自訂標籤清單檔案之 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)。