本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 ListFaces 用 AWS SDK 或 CLI
以下代码示例显示了如何使用 ListFaces。
有关更多信息,请参阅列出集合中的人脸。
- .NET
-
- 适用于 .NET 的 SDK
-
注意
还有更多关于 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to retrieve the list of faces /// stored in a collection. /// </summary> public class ListFaces { public static async Task Main() { string collectionId = "MyCollection2"; var rekognitionClient = new AmazonRekognitionClient(); var listFacesResponse = new ListFacesResponse(); Console.WriteLine($"Faces in collection {collectionId}"); var listFacesRequest = new ListFacesRequest { CollectionId = collectionId, MaxResults = 1, }; do { listFacesResponse = await rekognitionClient.ListFacesAsync(listFacesRequest); listFacesResponse.Faces.ForEach(face => { Console.WriteLine(face.FaceId); }); listFacesRequest.NextToken = listFacesResponse.NextToken; } while (!string.IsNullOrEmpty(listFacesResponse.NextToken)); } }-
有关 API 的详细信息,请参阅 ListFaces 在 适用于 .NET 的 AWS SDK API 参考。
-
- CLI
-
- AWS CLI
-
列出集合中的人脸
以下
list-faces示例列出了指定集合中的面孔。aws rekognition list-faces \ --collection-idMyCollection输出:
{ "FaceModelVersion": "3.0", "Faces": [ { "BoundingBox": { "Width": 0.5216310024261475, "Top": 0.3256250023841858, "Left": 0.13394300639629364, "Height": 0.3918749988079071 }, "FaceId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "ExternalImageId": "image1.jpg", "Confidence": 100.0, "ImageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE22222" }, { "BoundingBox": { "Width": 0.5074880123138428, "Top": 0.3774999976158142, "Left": 0.18302799761295319, "Height": 0.3812499940395355 }, "FaceId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE33333", "ExternalImageId": "image2.jpg", "Confidence": 99.99930572509766, "ImageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE44444" }, { "BoundingBox": { "Width": 0.5574039816856384, "Top": 0.37187498807907104, "Left": 0.14559100568294525, "Height": 0.4181250035762787 }, "FaceId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE55555", "ExternalImageId": "image3.jpg", "Confidence": 99.99960327148438, "ImageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE66666" }, { "BoundingBox": { "Width": 0.18562500178813934, "Top": 0.1618019938468933, "Left": 0.5575000047683716, "Height": 0.24770599603652954 }, "FaceId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE77777", "ExternalImageId": "image4.jpg", "Confidence": 99.99340057373047, "ImageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE88888" } ] }有关更多信息,请参阅《Amazon Rekognition 开发人员指南》中的列出集合中的人脸。
-
有关 API 的详细信息,请参阅 ListFaces
在 AWS CLI 命令参考。
-
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
还有更多关于 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rekognition.RekognitionClient; import software.amazon.awssdk.services.rekognition.model.Face; import software.amazon.awssdk.services.rekognition.model.ListFacesRequest; import software.amazon.awssdk.services.rekognition.model.ListFacesResponse; import software.amazon.awssdk.services.rekognition.model.RekognitionException; import java.util.List; /** * 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 ListFacesInCollection { public static void main(String[] args) { final String usage = """ Usage: <collectionId> Where: collectionId - The name of the collection.\s """; if (args.length < 1) { System.out.println(usage); System.exit(1); } String collectionId = args[0]; Region region = Region.US_EAST_1; RekognitionClient rekClient = RekognitionClient.builder() .region(region) .build(); System.out.println("Faces in collection " + collectionId); listFacesCollection(rekClient, collectionId); rekClient.close(); } public static void listFacesCollection(RekognitionClient rekClient, String collectionId) { try { ListFacesRequest facesRequest = ListFacesRequest.builder() .collectionId(collectionId) .maxResults(10) .build(); ListFacesResponse facesResponse = rekClient.listFaces(facesRequest); List<Face> faces = facesResponse.faces(); for (Face face : faces) { System.out.println("Confidence level there is a face: " + face.confidence()); System.out.println("The face Id value is " + face.faceId()); } } catch (RekognitionException e) { System.out.println(e.getMessage()); System.exit(1); } } }-
有关 API 的详细信息,请参阅 ListFaces 在 AWS SDK for Java 2.x API 参考。
-
- Kotlin
-
- 适用于 Kotlin 的 SDK
-
注意
还有更多关于 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 suspend fun listFacesCollection(collectionIdVal: String?) { val request = ListFacesRequest { collectionId = collectionIdVal maxResults = 10 } RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient -> val response = rekClient.listFaces(request) response.faces?.forEach { face -> println("Confidence level there is a face: ${face.confidence}") println("The face Id value is ${face.faceId}") } } }-
有关 API 的详细信息,请参阅 ListFaces
在 AWS 适用于 Kotlin 的 SDK API 参考。
-
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多关于 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 class RekognitionCollection: """ Encapsulates an Amazon Rekognition collection. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, collection, rekognition_client): """ Initializes a collection object. :param collection: Collection data in the format returned by a call to create_collection. :param rekognition_client: A Boto3 Rekognition client. """ self.collection_id = collection["CollectionId"] self.collection_arn, self.face_count, self.created = self._unpack_collection( collection ) self.rekognition_client = rekognition_client @staticmethod def _unpack_collection(collection): """ Unpacks optional parts of a collection that can be returned by describe_collection. :param collection: The collection data. :return: A tuple of the data in the collection. """ return ( collection.get("CollectionArn"), collection.get("FaceCount", 0), collection.get("CreationTimestamp"), ) def list_faces(self, max_results): """ Lists the faces currently indexed in the collection. :param max_results: The maximum number of faces to return. :return: The list of faces in the collection. """ try: response = self.rekognition_client.list_faces( CollectionId=self.collection_id, MaxResults=max_results ) faces = [RekognitionFace(face) for face in response["Faces"]] logger.info( "Found %s faces in collection %s.", len(faces), self.collection_id ) except ClientError: logger.exception( "Couldn't list faces in collection %s.", self.collection_id ) raise else: return faces-
有关 API 的详细信息,请参阅 ListFaces 在 AWS 适用于 Python 的 SDK (Boto3) API 参考。
-
- SAP ABAP
-
- 适用于 SAP ABAP 的 SDK
-
注意
还有更多关于 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 TRY. oo_result = lo_rek->listfaces( iv_collectionid = iv_collection_id iv_maxresults = iv_max_results ). DATA(lt_faces) = oo_result->get_faces( ). DATA(lv_face_count2) = lines( lt_faces ). DATA(lv_msg3) = |{ lv_face_count2 } face(s) found in collection.|. MESSAGE lv_msg3 TYPE 'I'. CATCH /aws1/cx_rekresourcenotfoundex. MESSAGE 'Collection not found.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.-
有关 API 的详细信息,请参阅 ListFaces 在 AWS 适用于 SAP 的 SDK ABAP API 参考。
-
如需查看完整清单 AWS SDK 开发者指南和代码示例,请参阅 将 Rekognition 与 AWS SDK。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。