將更多影像新增至資料集 - Rekognition

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

將更多影像新增至資料集

您可以使用 Amazon Rekognition 自訂標籤主控台或呼叫UpdateDatasetEntries API,將更多影像新增至資料集。

添加更多圖像(控制台)

當您使用 Amazon Rekognition 自訂標籤主控台時,您可以從本機電腦上傳影像。映像檔會新增至 Amazon S3 儲存貯體位置 (主控台或外部),用於建立資料集的映像存放在該位置。

若要將更多影像新增至資料集 (主控台)
  1. 開啟亞馬遜重新認知主控台,網址為 https://console.aws.amazon.com/rekognition/

  2. 在左窗格中選擇使用自訂標籤。顯示 Amazon Rekognition 自訂標籤登陸頁面。

  3. 在左側導覽窗格中選擇 Projects (專案)。將顯示「專案」檢視。

  4. 選擇您要使用的專案。

  5. 在左側導覽窗格的專案名稱下,選擇 Databet (資料集)。

  6. 選擇 Actions (動作),然後選取要新增影像的資料集。

  7. 選擇您要上傳至資料集的影像。您可以拖動圖像或選擇要從本地計算機上傳的圖像。您一次最多可以上傳 30 張圖片。

  8. 選擇 [上傳圖片]。

  9. 選擇 Save changes (儲存變更)。

  10. 標記影像。如需詳細資訊,請參閱標記檔案

新增更多影像 (SDK)

UpdateDatasetEntries更新或將 JSON 行添加到清單文件中。您將 JSON 行作為GroundTruth字段中的字節 64 編碼數據對象傳遞。如果您使用AWS SDK 來呼叫UpdateDatasetEntries,SDK 會為您編碼資料。每個 JSON 行都包含單一影像的資訊,例如指派的標籤或邊界方框資訊。例如:

{"source-ref":"s3://bucket/image","BB":{"annotations":[{"left":1849,"top":1039,"width":422,"height":283,"class_id":0},{"left":1849,"top":1340,"width":443,"height":415,"class_id":1},{"left":2637,"top":1380,"width":676,"height":338,"class_id":2},{"left":2634,"top":1051,"width":673,"height":338,"class_id":3}],"image_size":[{"width":4000,"height":2667,"depth":3}]},"BB-metadata":{"job-name":"labeling-job/BB","class-map":{"0":"comparator","1":"pot_resistor","2":"ir_phototransistor","3":"ir_led"},"human-annotated":"yes","objects":[{"confidence":1},{"confidence":1},{"confidence":1},{"confidence":1}],"creation-date":"2021-06-22T10:11:18.006Z","type":"groundtruth/object-detection"}}

如需詳細資訊,請參閱建立清單檔案

使用source-ref欄位作為關鍵字,以識別您要更新的影像。如果資料集不包含相符的source-ref欄位值,則會將 JSON 行新增為新影像。

若要將更多影像新增至資料集 (SDK)
  1. 若您尚未這樣做,請安裝AWS CLI並設定和AWS SDK。如需詳細資訊,請參閱步驟 4:設定 AWS CLI 以及 AWS SDKs

  2. 使用下列範例將 JSON 行新增至資料集。

    CLI

    以您要使GroundTruth用的 JSON 行取代的值。您需要轉義 JSON 行中的任何特殊字符。

    aws rekognition update-dataset-entries\ --dataset-arn dataset_arn \ --changes '{"GroundTruth" : "{\"source-ref\":\"s3://your_bucket/your_image\",\"BB\":{\"annotations\":[{\"left\":1776,\"top\":1017,\"width\":458,\"height\":317,\"class_id\":0},{\"left\":1797,\"top\":1334,\"width\":418,\"height\":415,\"class_id\":1},{\"left\":2597,\"top\":1361,\"width\":655,\"height\":329,\"class_id\":2},{\"left\":2581,\"top\":1020,\"width\":689,\"height\":338,\"class_id\":3}],\"image_size\":[{\"width\":4000,\"height\":2667,\"depth\":3}]},\"BB-metadata\":{\"job-name\":\"labeling-job/BB\",\"class-map\":{\"0\":\"comparator\",\"1\":\"pot_resistor\",\"2\":\"ir_phototransistor\",\"3\":\"ir_led\"},\"human-annotated\":\"yes\",\"objects\":[{\"confidence\":1},{\"confidence\":1},{\"confidence\":1},{\"confidence\":1}],\"creation-date\":\"2021-06-22T10:10:48.492Z\",\"type\":\"groundtruth/object-detection\"}}" }' \ --cli-binary-format raw-in-base64-out \ --profile custom-labels-access
    Python

    使用下列程式碼。提供以下命令行參數:

    • 資料集-您要更新之資料集的 ARN。

    • 更新文件-包含 JSON 行更新的文件。

    # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Shows how to add entries to an Amazon Rekognition Custom Labels dataset. """ import argparse import logging import time import json import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def update_dataset_entries(rek_client, dataset_arn, updates_file): """ Adds dataset entries to an Amazon Rekognition Custom Labels dataset. :param rek_client: The Amazon Rekognition Custom Labels Boto3 client. :param dataset_arn: The ARN of the dataset that yuo want to update. :param updates_file: The manifest file of JSON Lines that contains the updates. """ try: status="" status_message="" # Update dataset entries. logger.info("Updating dataset %s", dataset_arn) with open(updates_file) as f: manifest_file = f.read() changes=json.loads('{ "GroundTruth" : ' + json.dumps(manifest_file) + '}') rek_client.update_dataset_entries( Changes=changes, DatasetArn=dataset_arn ) finished=False while finished is False: dataset=rek_client.describe_dataset(DatasetArn=dataset_arn) status=dataset['DatasetDescription']['Status'] status_message=dataset['DatasetDescription']['StatusMessage'] if status == "UPDATE_IN_PROGRESS": logger.info("Updating dataset: %s ", dataset_arn) time.sleep(5) continue if status == "UPDATE_COMPLETE": logger.info("Dataset updated: %s : %s : %s", status, status_message, dataset_arn) finished=True continue if status == "UPDATE_FAILED": error_message = f"Dataset update failed: {status} : {status_message} : {dataset_arn}" logger.exception(error_message) raise Exception (error_message) error_message = f"Failed. Unexpected state for dataset update: {status} : {status_message} : {dataset_arn}" logger.exception(error_message) raise Exception(error_message) logger.info("Added entries to dataset") return status, status_message except ClientError as err: logger.exception("Couldn't update dataset: %s", err.response['Error']['Message']) raise def add_arguments(parser): """ Adds command line arguments to the parser. :param parser: The command line parser. """ parser.add_argument( "dataset_arn", help="The ARN of the dataset that you want to update." ) parser.add_argument( "updates_file", help="The manifest file of JSON Lines that contains the updates." ) def main(): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") try: #get command line arguments parser = argparse.ArgumentParser(usage=argparse.SUPPRESS) add_arguments(parser) args = parser.parse_args() print(f"Updating dataset {args.dataset_arn} with entries from {args.updates_file}.") # Update the dataset. session = boto3.Session(profile_name='custom-labels-access') rekognition_client = session.client("rekognition") status, status_message=update_dataset_entries(rekognition_client, args.dataset_arn, args.updates_file) print(f"Finished updates dataset: {status} : {status_message}") except ClientError as err: logger.exception("Problem updating dataset: %s", err) print(f"Problem updating dataset: {err}") except Exception as err: logger.exception("Problem updating dataset: %s", err) print(f"Problem updating dataset: {err}") if __name__ == "__main__": main()
    Java V2
    • 資料集-您要更新之資料集的 ARN。

    • 更新文件-包含 JSON 行更新的文件。

    /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ package com.example.rekognition; import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rekognition.RekognitionClient; import software.amazon.awssdk.services.rekognition.model.DatasetChanges; import software.amazon.awssdk.services.rekognition.model.DatasetDescription; import software.amazon.awssdk.services.rekognition.model.DatasetStatus; import software.amazon.awssdk.services.rekognition.model.DescribeDatasetRequest; import software.amazon.awssdk.services.rekognition.model.DescribeDatasetResponse; import software.amazon.awssdk.services.rekognition.model.RekognitionException; import software.amazon.awssdk.services.rekognition.model.UpdateDatasetEntriesRequest; import software.amazon.awssdk.services.rekognition.model.UpdateDatasetEntriesResponse; import java.io.FileInputStream; import java.io.InputStream; import java.util.logging.Level; import java.util.logging.Logger; public class UpdateDatasetEntries { public static final Logger logger = Logger.getLogger(UpdateDatasetEntries.class.getName()); public static String updateMyDataset(RekognitionClient rekClient, String datasetArn, String updateFile ) throws Exception, RekognitionException { try { logger.log(Level.INFO, "Updating dataset {0}", new Object[] { datasetArn}); InputStream sourceStream = new FileInputStream(updateFile); SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream); DatasetChanges datasetChanges = DatasetChanges.builder() .groundTruth(sourceBytes).build(); UpdateDatasetEntriesRequest updateDatasetEntriesRequest = UpdateDatasetEntriesRequest.builder() .changes(datasetChanges) .datasetArn(datasetArn) .build(); UpdateDatasetEntriesResponse response = rekClient.updateDatasetEntries(updateDatasetEntriesRequest); boolean updated = false; //Wait until update completes do { DescribeDatasetRequest describeDatasetRequest = DescribeDatasetRequest.builder() .datasetArn(datasetArn).build(); DescribeDatasetResponse describeDatasetResponse = rekClient.describeDataset(describeDatasetRequest); DatasetDescription datasetDescription = describeDatasetResponse.datasetDescription(); DatasetStatus status = datasetDescription.status(); logger.log(Level.INFO, " dataset ARN: {0} ", datasetArn); switch (status) { case UPDATE_COMPLETE: logger.log(Level.INFO, "Dataset updated"); updated = true; break; case UPDATE_IN_PROGRESS: Thread.sleep(5000); break; case UPDATE_FAILED: String error = "Dataset update failed: " + datasetDescription.statusAsString() + " " + datasetDescription.statusMessage() + " " + datasetArn; logger.log(Level.SEVERE, error); throw new Exception(error); default: String unexpectedError = "Unexpected update state: " + datasetDescription.statusAsString() + " " + datasetDescription.statusMessage() + " " + datasetArn; logger.log(Level.SEVERE, unexpectedError); throw new Exception(unexpectedError); } } while (updated == false); return datasetArn; } catch (RekognitionException e) { logger.log(Level.SEVERE, "Could not update dataset: {0}", e.getMessage()); throw e; } } public static void main(String args[]) { String updatesFile = null; String datasetArn = null; final String USAGE = "\n" + "Usage: " + "<project_arn> <dataset_arn> <updates_file>\n\n" + "Where:\n" + " dataset_arn - the ARN of the dataset that you want to update.\n\n" + " update_file - The file that includes in JSON Line updates.\n\n"; if (args.length != 2) { System.out.println(USAGE); System.exit(1); } datasetArn = args[0]; updatesFile = args[1]; try { // Get the Rekognition client. RekognitionClient rekClient = RekognitionClient.builder() .credentialsProvider(ProfileCredentialsProvider.create("custom-labels-access")) .region(Region.US_WEST_2) .build(); // Update the dataset datasetArn = updateMyDataset(rekClient, datasetArn, updatesFile); System.out.println(String.format("Dataset updated: %s", datasetArn)); rekClient.close(); } catch (RekognitionException rekError) { logger.log(Level.SEVERE, "Rekognition client error: {0}", rekError.getMessage()); System.exit(1); } catch (Exception rekError) { logger.log(Level.SEVERE, "Error: {0}", rekError.getMessage()); System.exit(1); } } }