

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

# 使用邊界框分類映像物件
<a name="sms-bounding-box"></a>

用來訓練機器學習模型的映像通常包含多個物件。若要分類並區域化影像內的一或多個物件，請使用 Amazon SageMaker Ground Truth 邊界框標籤工作任務類型。在此情況下，區域化表示邊界框的像素位置。您可以使用 Amazon SageMaker AI 主控台的 Ground Truth 區段或 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html) 操作來建立邊界框標籤工作。

**重要**  
對於此任務類型，如果您建立自己的資訊清單檔案，請使用 `"source-ref"` 來識別您要在 Amazon S3 中標籤的每個影像檔案位置。如需詳細資訊，請參閱[輸入資料](sms-data-input.md)。

## 建立邊界框標籤工作 (主控台)
<a name="sms-creating-bounding-box-labeling-job-console"></a>

您可以遵循指示 [建立標籤工作 (主控台)](sms-create-labeling-job-console.md)，了解如何在 SageMaker AI 主控台建立邊界框標籤工作。在步驟 10 中，從**任務類別**下拉式清單選單中選擇**映像**，然後選擇**邊界框**做為任務類型。

Ground Truth 提供與以下類似的工作者使用者介面以進行標籤任務。使用主控台建立標籤工作時，您可以指定指示以協助工作者完成工作，以及最多 50 個可供工作者選擇的標籤。

![\[Gif 顯示如何在類別的物件周圍繪製方塊。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/sms/gifs/bb-sample.gif)


## 建立邊界框標籤工作 (API)
<a name="sms-creating-bounding-box-labeling-job-api"></a>

若要建立邊界框標籤工作，請使用 SageMaker API 作業 `CreateLabelingJob`。此 API 會定義 AWS SDKs此操作。若要查看這項作業支援的特定語言 SDK 清單，請參閱 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html) 的**另請參閱**一節。

設定請求時，請遵循[建立標籤工作 (API)](sms-create-labeling-job-api.md)上的指示並執行下列動作：
+ 此任務類型的註釋前 Lambda 函式會以 `PRE-BoundingBox` 結尾。若要尋找您區域的註釋前 Lambda ARN，請參閱 [PreHumanTaskLambdaArn](https://docs.aws.amazon.com/sagemaker/latest/dg/API_HumanTaskConfig.html#SageMaker-Type-HumanTaskConfig-PreHumanTaskLambdaArn)。
+ 此任務類型的註釋合併 Lambda 函式會以 `ACS-BoundingBox` 結尾。若要尋找您區域的註釋合併 Lambda ARN，請參閱 [AnnotationConsolidationLambdaArn](https://docs.aws.amazon.com/sagemaker/latest/dg/API_AnnotationConsolidationConfig.html#SageMaker-Type-AnnotationConsolidationConfig-AnnotationConsolidationLambdaArn)。

下列是用 [AWS Python SDK (Boto3) 請求](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_labeling_job)，在美國東部 (維吉尼亞北部) 區域建立標籤工作的範例。所有以紅色標示的參數都應該用您的規格和資源加以取代。

```
response = client.create_labeling_job(
    LabelingJobName='example-bounding-box-labeling-job,
    LabelAttributeName='label',
    InputConfig={
        'DataSource': {
            'S3DataSource': {
                'ManifestS3Uri': 's3://bucket/path/manifest-with-input-data.json'
            }
        },
        'DataAttributes': {
            'ContentClassifiers': [
                'FreeOfPersonallyIdentifiableInformation'|'FreeOfAdultContent',
            ]
        }
    },
    OutputConfig={
        'S3OutputPath': 's3://bucket/path/file-to-store-output-data',
        'KmsKeyId': 'string'
    },
    RoleArn='arn:aws:iam::*:role/*,
    LabelCategoryConfigS3Uri='s3://bucket/path/label-categories.json',
    StoppingConditions={
        'MaxHumanLabeledObjectCount': 123,
        'MaxPercentageOfInputDatasetLabeled': 123
    },
    HumanTaskConfig={
        'WorkteamArn': 'arn:aws:sagemaker:region:*:workteam/private-crowd/*',
        'UiConfig': {
            'UiTemplateS3Uri': 's3://bucket/path/worker-task-template.html'
        },
        'PreHumanTaskLambdaArn': 'arn:aws:lambda:us-east-1:432418664414:function:PRE-BoundingBox',
        'TaskKeywords': [
            'Bounding Box',
        ],
        'TaskTitle': 'Bounding Box task',
        'TaskDescription': 'Draw bounding boxes around objects in an image',
        'NumberOfHumanWorkersPerDataObject': 123,
        'TaskTimeLimitInSeconds': 123,
        'TaskAvailabilityLifetimeInSeconds': 123,
        'MaxConcurrentTaskCount': 123,
        'AnnotationConsolidationConfig': {
            'AnnotationConsolidationLambdaArn': 'arn:aws:lambda:us-east-1:432418664414:function:ACS-BoundingBox'
          }
        },
    Tags=[
        {
            'Key': 'string',
            'Value': 'string'
        },
    ]
)
```

### 提供邊界框標籤工作的範本
<a name="sms-create-labeling-job-bounding-box-api-template"></a>

如果您使用 API 來建立標籤工作，則必須在 `UiTemplateS3Uri` 中提供工作者任務範本。複製並修改下列範本。僅修改 [https://docs.aws.amazon.com/sagemaker/latest/dg/sms-creating-instruction-pages.html#sms-creating-quick-instructions](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-creating-instruction-pages.html#sms-creating-quick-instructions)、[https://docs.aws.amazon.com/sagemaker/latest/dg/sms-creating-instruction-pages.html#sms-creating-full-instructions](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-creating-instruction-pages.html#sms-creating-full-instructions) 和 `header`。將此範本上傳至 S3，並在 `UiTemplateS3Uri` 中提供此檔案的 S3 URI。

```
<script src="https://assets.crowd.aws/crowd-html-elements.js"></script>
<crowd-form>
  <crowd-bounding-box
    name="boundingBox"
    src="{{ task.input.taskObject | grant_read_access }}"
    header="please draw box"
    labels="{{ task.input.labels | to_json | escape }}"
  >

    <full-instructions header="Bounding box instructions">
      <ol><li><strong>Inspect</strong> the image</li><li><strong>Determine</strong> 
      if the specified label is/are visible in the picture.</li>
      <li><strong>Outline</strong> each instance of the specified label in the image using the provided “Box” tool.</li></ol>
      <ul><li>Boxes should fit tight around each object</li>
      <li>Do not include parts of the object are overlapping or that cannot be seen, even though you think you can interpolate the whole shape.</li>
      <li>Avoid including shadows.</li>
      <li>If the target is off screen, draw the box up to the edge of the image.</li>    
    </full-instructions>
  
    <short-instructions>
      <h3><span style="color: rgb(0, 138, 0);">Good example</span></h3>
      <p>Enter description of a correct bounding box label and add images</p>
      <h3><span style="color: rgb(230, 0, 0);">Bad example</span></h3>
      <p>Enter description of an incorrect bounding box label and add images</p>
    </short-instructions>
  
  </crowd-bounding-box>
</crowd-form>
```

## 邊框方塊輸出資料
<a name="sms-bounding-box-output-data"></a>

建立邊界框標籤工作後，您的輸出資料將存放在使用 API 時在 `S3OutputPath` 參數中指定的 Amazon S3 儲存貯體中，或在主控台**工作概觀**區段的**輸出資料集位置**欄位中。

例如，成功完成的單一類別邊界框任務的輸出資訊清單檔案將包含下列項目：

```
[
  {
    "boundingBox": {
      "boundingBoxes": [
        {
          "height": 2832,
          "label": "bird",
          "left": 681,
          "top": 599,
          "width": 1364
        }
      ],
      "inputImageProperties": {
        "height": 3726,
        "width": 2662
      }
    }
  }
]
```

`boundingBoxes` 參數會識別根據識別為 “鳥” 的物件所繪製之邊界框的位置，而此位置相對於影像左上角，即 (0,0) 像素座標。在上一個範例中，**`left`** 和 **`top`** 識別邊界框左上角中相對於影像左上角的像素位置。邊界框的維度是以 **`height`** 和 **`width`** 識別。`inputImageProperties` 參數提供原始輸入影像的像素維度。

使用邊界框任務類型時，您可以建立單一和多重類別邊界框標籤工作。已成功完成的多類別邊界框輸出資訊清單檔案將包含以下內容：

```
[
  {
    "boundingBox": {
      "boundingBoxes": [
        {
          "height": 938,
          "label": "squirrel",
          "left": 316,
          "top": 218,
          "width": 785
        },
        {
          "height": 825,
          "label": "rabbit",
          "left": 1930,
          "top": 2265,
          "width": 540
        },
        {
          "height": 1174,
          "label": "bird",
          "left": 748,
          "top": 2113,
          "width": 927
        },
        {
          "height": 893,
          "label": "bird",
          "left": 1333,
          "top": 847,
          "width": 736
        }
      ],
      "inputImageProperties": {
        "height": 3726,
        "width": 2662
      }
    }
  }
]
```

若要進一步了解邊界框標籤工作產生的輸出資訊清單檔案，請參閱[邊界框任務輸出](sms-data-output.md#sms-output-box)。

若要進一步了解 Ground Truth 產生的輸出資訊清單檔案，以及 Ground Truth 用來儲存輸出資料的檔案結構，請參閱[標籤工作輸出資料](sms-data-output.md)。