本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用亚马逊 Textract 分析身份证件
要分析身份证件,您可以使用 AnalyzeID API 操作,并将文档文件作为输入传递。AnalyzeID返回包含分析文本的 JSON 结构。有关更多信息,请参阅 分析身份证件。
您可以将输入文档作为图像字节数组(base64 编码的图像字节)或 Amazon S3 对象提供。在此过程中,您将图像文件上传到 S3 存储桶并指定文件名。
分析身份证件 (API)
如果您尚未执行以下操作,请:
向用户提供
AmazonTextractFullAccess和AmazonS3ReadOnlyAccess权限。有关更多信息,请参阅 第 1 步:设置 AWS 账户并创建用户。安装和配置 AWS CLI 和 AWS SDK。有关更多信息,请参阅 步骤 2:设置 AWS CLI and AWS 软件开发工具包。
-
将包含文档的图像上传到您的 S3 存储桶。
有关说明,请参阅《Amazon Simple Storage Service 用户指南》中的将对象上传到 Amazon S3。
使用以下示例调用
AnalyzeID操作。- AWS CLI
-
以下示例从 S3 存储桶中获取输入文件并在其上运行
AnalyzeID操作。在以下代码中,将的Bucket值替换为您的 S3 存储桶的名称,将的值Name替换为存储桶中文件的名称。profile-name替换为可以代入该角色的配置文件名称和region要运行代码的区域。aws textract analyze-id \ --document-pages '{"S3Object":{"Bucket":"bucket","Name":"name"}}' \ --profileprofile-name\ --regionregion您还可以通过向输入中添加另一个 Amazon S3 对象来使用驾驶执照的正面和背面调用 API。
aws textract analyze-id \ --document-pages '[{"S3Object":{"Bucket":"bucket","Name":"name front"}}, {"S3Object":{"Bucket":"bucket","Name":"name back"}}]' \ --profileprofile-name\ --regionregion如果您在 Windows 设备上访问 CLI,请使用双引号而不是单引号,并使用反斜杠 (\) 转义内部双引号,以解决可能遇到的任何解析器错误。例如,请参阅以下内容:
aws textract analyze-id --document-pages "[{\"S3Object\":{\"Bucket\":\"bucket\",\"Name\":\"name\"}}]" --regionregion - Python
-
以下示例从 S3 存储桶中获取输入文件并在其上运行
AnalyzeID操作,返回检测到的键值对。在以下代码中,将的bucket_name值替换为您的 S3 存储桶的名称,将的值file_name替换为存储桶中文件的名称。profile-name替换为可以代入该角色的配置文件名称和region要运行代码的区域。import boto3 def analyze_id(client, bucket_name, file_name): # Analyze document # process using S3 object response = 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() def main(): session = boto3.Session(profile_name='profile-name') client = session.client('textract', region_name='region') bucket_name = "bucket" file_name = "file" analyze_id(client, bucket_name, file_name) if __name__ == "__main__": main() - Java
-
以下示例从 S3 存储桶中获取输入文件并在其上运行
AnalyzeID操作,返回检测到的数据。在函数 main 中,将s3bucket和sourceDoc的值替换为您在步骤 2 中使用的 Amazon S3 存储桶和文档图像的名称。将credentialsProvider的值替换为您的开发人员资料的名称。/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ package com.amazonaws.samples; import com.amazonaws.auth.profile.ProfileCredentialsProvider; 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 AppTest1 { 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); } // set provider credentials AWSCredentialsProvider credentialsProvider = new ProfileCredentialsProvider("default"); String s3bucket = "bucket-name"; //args[0]; String sourceDoc = "sourcedoc-name"; //args[1]; AmazonTextractClient textractClient = (AmazonTextractClient) AmazonTextractClientBuilder.standard().withCredentials(credentialsProvider) .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(); } } } - Java V2
-
以下示例从 S3 存储桶中获取输入文件并在其上运行
AnalyzeID操作,返回检测到的数据。在函数 main 中,将s3bucket和sourceDoc的值替换为您在步骤 2 中使用的 S3 存储桶和文档图像的名称。profile-name在创建的行中替换为TextractClient您的开发者个人资料的名称。import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.textract.TextractClient; import software.amazon.awssdk.services.textract.model.*; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; // snippet-end:[textract.java2._analyze_doc.import] import java.util.Optional; import org.json.JSONObject; /** * Before running this Java V2 code example, set up your development environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectCelebrityVideo { public static void main(String[] args) { final String usage = "\n" + "Usage:\n" + " <bucketName> <docName> \n\n" + "Where:\n" + " bucketName - The name of the Amazon S3 bucket that contains the document. \n\n" + " docName - The document name (must be an image, i.e., book.png). \n"; if (args.length != 2) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; String docName = args[1]; Region region = Region.US_WEST_2; TextractClient textractClient = TextractClient.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create("default")) .build(); analyzeID(textractClient, bucketName, docName); textractClient.close(); } // snippet-start:[textract.java2._analyze_doc.main] public static void analyzeID(TextractClient textractClient, String bucketName, String docName) { try { S3Object s3Object = S3Object.builder() .bucket(bucketName) .name(docName) .build(); // Create a Document object and reference the s3Object instance Document myDoc = Document.builder() .s3Object(s3Object) .build(); AnalyzeIdRequest analyzeIdRequest = AnalyzeIdRequest.builder() .documentPages(myDoc).build(); AnalyzeIdResponse analyzeId = textractClient.analyzeID(analyzeIdRequest); // System.out.println(analyzeExpense.toString()); List<IdentityDocument> Docs = analyzeId.identityDocuments(); for (IdentityDocument doc: Docs) { System.out.println(doc); } } catch (TextractException e) { System.err.println(e.getMessage()); System.exit(1); } } // snippet-end:[textract.java2._analyze_doc.main] }
-
这将为您提供
AnalyzeID操作的 JSON 输出。