

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

# 使用 Amazon Textract 分析身份文档
<a name="analyzing-document-identity"></a>

要分析身份证件，您可以使用 AnalyZEID API，然后将文档文件作为输入进行传递。`AnalyzeID`返回一个 JSON 结构，其中包含分析的文本。有关更多信息，请参阅 [分析身份证件](how-it-works-identity.md)。

您可以提供输入文档作为图像字节数组 (base64 编码的图像字节) 或 Amazon S3 对象。在此过程中，您将图像文件上传到您的 S3 存储桶并指定文件名称。

**分析身份证明文件 (API)**

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

   1. 使用创建或更新 IAM 用户`AmazonTextractFullAccess`和`AmazonS3ReadOnlyAccess`权限。有关更多信息，请参阅 [第 1 步：设置 AWS 账户并创建 IAM 用户](setting-up.md#setting-up-iam)。

   1. 安装和配置 AWS CLI 和 AWS 开发工具包。有关更多信息，请参阅 [第 2 步：设置AWS CLI和AWS软件开发工具包](setup-awscli-sdk.md)。

1. 将包含文档的图像上传到您的 S3 存储桶。

   有关说明，请参阅[将对象上传到 Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/user-guide/UploadingObjectsintoAmazonS3.html)中的*Amazon Simple Service 用户指南*.

1. 使用以下示例调用 `AnalyzeID` 操作。

------
#### [ CLI ]

   

   以下示例从 S3 存储桶获取输入文件并运行`AnalyzeID`对它进行操作。在下面的代码中，替换*桶*将您的 S3 存储桶的名称用于*文件*将包含存储桶中的文件的名称以及的值*领域*有的名字`region`与您的账户关联。

   

   ```
   aws textract analyze-id --document-pages '[{"S3Object":{"Bucket":"bucket","Name":"name"}}]' --region region
   ```

   您还可以通过向输入中添加另一个 S3 对象，使用驾驶执照的正面和背面调用 API。

   ```
   aws textract analyze-id --document-pages '[{"S3Object":{"Bucket":"bucket","Name":"name front"}}, {"S3Object":{"Bucket":"bucket","Name":"name back"}}]' --region us-east-1
   ```

   如果您在 Windows 设备上访问 CLI，请使用双引号而不是单引号，并用反斜杠（即\$1）转义内部双引号，以解决可能遇到的任何解析器错误。有关示例，请参阅下面的内容：

   ```
   aws textract analyze-id --document-pages "[{\"S3Object\":{\"Bucket\":\"bucket\",\"Name\":\"name\"}}]" --region region
   ```

------
#### [ Python ]

   以下示例从 S3 存储桶获取输入文件并运行`AnalyzeID`在其上操作，返回检测到的键值对。在下面的代码中，替换*bucket\$1name*将您的 S3 存储桶的名称用于*file\$1name*将包含存储桶中的文件的名称以及的值*领域*有的名字`region`与您的账户关联。

   ```
   import boto3
   
   bucket_name = "bucket-name"
   file_name = "file-name"
   region = "region-name"
   
   def analyze_id(region, bucket_name, file_name):
   
       textract_client = boto3.client('textract', region_name=region)
       response = textract_client.analyze_id(DocumentPages=[{"S3Object":{"Bucket":bucket_name,"Name":file_name}}])
   
       for doc_fields in response['IdentityDocuments']:
           for id_field in doc_fields['IdentityDocumentFields']:
               for key, val in id_field.items():
                   if "Type" in str(key):
                       print("Type: " + str(val['Text']))
               for key, val in id_field.items():
                   if "ValueDetection" in str(key):
                       print("Value Detection: " + str(val['Text']))
               print()
   
   analyze_id(region, bucket_name, file_name)
   ```

------
#### [ Java ]

   以下示例从 S3 存储桶获取输入文件并运行`AnalyzeID`对其进行操作，返回检测到的数据。在函数 main 中，将以下值`s3bucket`和`sourceDoc`将包含您在步骤 2 中使用的 Amazon S3 存储桶名称和文档图像的名称。

   ```
   /*
      Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
      SPDX-License-Identifier: Apache-2.0
   */
   
   package com.amazonaws.samples;
   
   import com.amazonaws.regions.Regions;
   import com.amazonaws.services.textract.AmazonTextractClient;
   import com.amazonaws.services.textract.AmazonTextractClientBuilder;
   import com.amazonaws.services.textract.model.*;
   import java.util.ArrayList;
   import java.util.List;
   
   public class AnalyzeIdentityDocument {
   
       public static void main(String[] args) {
   
           final String USAGE = "\n" +
                   "Usage:\n" +
                   "    <s3bucket><sourceDoc> \n\n" +
                   "Where:\n" +
                   "    s3bucket - the Amazon S3 bucket where the document is located. \n" +
                   "    sourceDoc - the name of the document. \n";
   
           if (args.length != 1) {
               System.out.println(USAGE);
               System.exit(1);
           }
   
           String s3bucket = "bucket-name"; //args[0];
           String sourceDoc = "sourcedoc-name";  //args[1];
           AmazonTextractClient textractClient = (AmazonTextractClient) AmazonTextractClientBuilder.standard()
                   .withRegion(Regions.US_EAST_1)
                   .build();
   
           getDocDetails(textractClient, s3bucket, sourceDoc);
       }
   
       public static void getDocDetails(AmazonTextractClient textractClient, String s3bucket, String sourceDoc ) {
   
          try {
   
               S3Object s3 = new S3Object();
               s3.setBucket(s3bucket);
               s3.setName(sourceDoc);
   
               com.amazonaws.services.textract.model.Document myDoc = new com.amazonaws.services.textract.model.Document();
               myDoc.setS3Object(s3);
   
               List<Document> list1 = new ArrayList();
               list1.add(myDoc);
   
               AnalyzeIDRequest idRequest = new AnalyzeIDRequest();
               idRequest.setDocumentPages(list1);
   
               AnalyzeIDResult result = textractClient.analyzeID(idRequest);
               List<IdentityDocument> docs =  result.getIdentityDocuments();
               for (IdentityDocument doc: docs) {
   
                   List<IdentityDocumentField>idFields = doc.getIdentityDocumentFields();
                   for (IdentityDocumentField field: idFields) {
                       System.out.println("Field type is "+ field.getType().getText());
                       System.out.println("Field value is "+ field.getValueDetection().getText());
                   }
               }
   
          } catch (Exception e) {
               e.printStackTrace();
          }
       }
   }
   ```

------

1. 这将为您提供的 JSON 输出`AnalyzeID`operation.