使用 Amazon Textract 分析身份文檔 - Amazon Textract

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

使用 Amazon Textract 分析身份文檔

要分析身份文檔,請使用 AnalyzeID API,並將文檔文件作為輸入傳遞。AnalyzeID會傳回含有分析文字的 JSON 結構。如需詳細資訊,請參閱 分析身分文件

您提供的輸入文檔可以是影像位元組陣列 (base64 編碼影像位元組),或者 Amazon S3 物件。在此步驟中,您會將影像檔案上傳至您的 S3 儲存貯體,並指定檔案名稱。

分析身份文檔 (API)
  1. 如果您尚未:

    1. 使用創建或更新 IAM 用户AmazonTextractFullAccessAmazonS3ReadOnlyAccess許可。如需詳細資訊,請參閱 步驟 1:設定 AWS 帳户並建立 IAM 使用者

    2. 安裝並設定 AWS CLI 和 AWS SDK。如需詳細資訊,請參閱 步驟 2:設定AWS CLI和AWS開發套件

  2. 將含有文檔的影像上傳至您的 S3 儲存貯體。

    如需說明,請參閱「」將物件上傳至 Amazon S3中的Amazon Simple Storage Service 用户指南

  3. 使用下列範例來呼叫 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,請使用雙引號而不是單引號,並用反斜槓(即\)轉義內部雙引號以解決您可能遇到的任何解析器錯誤。舉例來講,請參閲下列內容:

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

    下列範例從 S3 儲存貯體傳入文件,並運行AnalyzeID操作,返回檢測到的鍵值組。在下面的代碼中,替換bucket_name與您 S3 儲存貯體的名稱一起使用,文件名添加儲存貯體中的檔案名稱,以及區域的名稱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 中,將s3bucketsourceDoc以您在步驟 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(); } } }
  4. 這將為您提供AnalyzeIDoperation.