AWS SDK を使用して Amazon Rekognition コレクション内の顔を一覧表示する - AWS SDK コードサンプル

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK を使用して Amazon Rekognition コレクション内の顔を一覧表示する

次のコード例は、Amazon Rekognition コレクション内の顔を一覧表示する方法を示します。

詳細については、「コレクションに顔を保存する」を参照してください。

.NET
AWS SDK for .NET
注記

については、こちらを参照してください 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 の詳細については、「 API リファレンスListFaces」の「」を参照してください。 AWS SDK for .NET

CLI
AWS CLI

コレクション内の顔を一覧表示するには

次の list-faces コマンドは、指定されたコレクション内の顔を一覧表示します。

aws rekognition list-faces \ --collection-id MyCollection

出力:

{ "FaceModelVersion": "3.0", "Faces": [ { "BoundingBox": { "Width": 0.5216310024261475, "Top": 0.3256250023841858, "Left": 0.13394300639629364, "Height": 0.3918749988079071 }, "FaceId": "0040279c-0178-436e-b70a-e61b074e96b0", "ExternalImageId": "image1.jpg", "Confidence": 100.0, "ImageId": "f976e487-3719-5e2d-be8b-ea2724c26991" }, { "BoundingBox": { "Width": 0.5074880123138428, "Top": 0.3774999976158142, "Left": 0.18302799761295319, "Height": 0.3812499940395355 }, "FaceId": "086261e8-6deb-4bc0-ac73-ab22323cc38d", "ExternalImageId": "image2.jpg", "Confidence": 99.99930572509766, "ImageId": "ae1593b0-a8f6-5e24-a306-abf529e276fa" }, { "BoundingBox": { "Width": 0.5574039816856384, "Top": 0.37187498807907104, "Left": 0.14559100568294525, "Height": 0.4181250035762787 }, "FaceId": "11c4bd3c-19c5-4eb8-aecc-24feb93a26e1", "ExternalImageId": "image3.jpg", "Confidence": 99.99960327148438, "ImageId": "80739b4d-883f-5b78-97cf-5124038e26b9" }, { "BoundingBox": { "Width": 0.18562500178813934, "Top": 0.1618019938468933, "Left": 0.5575000047683716, "Height": 0.24770599603652954 }, "FaceId": "13692fe4-990a-4679-b14a-5ac23d135eab", "ExternalImageId": "image4.jpg", "Confidence": 99.99340057373047, "ImageId": "8df18239-9ad1-5acd-a46a-6581ff98f51b" }, { "BoundingBox": { "Width": 0.5307819843292236, "Top": 0.2862499952316284, "Left": 0.1564060002565384, "Height": 0.3987500071525574 }, "FaceId": "2eb5f3fd-e2a9-4b1c-a89f-afa0a518fe06", "ExternalImageId": "image5.jpg", "Confidence": 99.99970245361328, "ImageId": "3c314792-197d-528d-bbb6-798ed012c150" }, { "BoundingBox": { "Width": 0.5773710012435913, "Top": 0.34437501430511475, "Left": 0.12396000325679779, "Height": 0.4337500035762787 }, "FaceId": "57189455-42b0-4839-a86c-abda48b13174", "ExternalImageId": "image6.jpg", "Confidence": 100.0, "ImageId": "0aff2f37-e7a2-5dbc-a3a3-4ef6ec18eaa0" }, { "BoundingBox": { "Width": 0.5349419713020325, "Top": 0.29124999046325684, "Left": 0.16389399766921997, "Height": 0.40187498927116394 }, "FaceId": "745f7509-b1fa-44e0-8b95-367b1359638a", "ExternalImageId": "image7.jpg", "Confidence": 99.99979400634766, "ImageId": "67a34327-48d1-5179-b042-01e52ccfeada" }, { "BoundingBox": { "Width": 0.41499999165534973, "Top": 0.09187500178813934, "Left": 0.28083300590515137, "Height": 0.3112500011920929 }, "FaceId": "8d3cfc70-4ba8-4b36-9644-90fba29c2dac", "ExternalImageId": "image8.jpg", "Confidence": 99.99769592285156, "ImageId": "a294da46-2cb1-5cc4-9045-61d7ca567662" }, { "BoundingBox": { "Width": 0.48166701197624207, "Top": 0.20999999344348907, "Left": 0.21250000596046448, "Height": 0.36125001311302185 }, "FaceId": "bd4ceb4d-9acc-4ab7-8ef8-1c2d2ba0a66a", "ExternalImageId": "image9.jpg", "Confidence": 99.99949645996094, "ImageId": "5e1a7588-e5a0-5ee3-bd00-c642518dfe3a" }, { "BoundingBox": { "Width": 0.18562500178813934, "Top": 0.1618019938468933, "Left": 0.5575000047683716, "Height": 0.24770599603652954 }, "FaceId": "ce7ed422-2132-4a11-ab14-06c5c410f29f", "ExternalImageId": "image10.jpg", "Confidence": 99.99340057373047, "ImageId": "8d67061e-90d2-598f-9fbd-29c8497039c0" } ] }

詳細については、「Amazon Rekognition 開発者ガイド」の「コレクション内の顔と関連するユーザーを一覧表示します。」を参照してください。

  • API の詳細については、「 コマンドリファレンスListFaces」の「」を参照してください。 AWS CLI

Java
SDK for Java 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 の詳細については、「 API リファレンスListFaces」の「」を参照してください。 AWS SDK for Java 2.x

Kotlin
SDK for Kotlin
注記

については、こちらを参照してください GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

suspend fun listFacesCollection(collectionIdVal: String?) { val request = ListFacesRequest { collectionId = collectionIdVal maxResults = 10 } RekognitionClient { 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 の詳細については、ListFacesAWS「 SDK for Kotlin API リファレンス」の「」を参照してください。

Python
SDK for Python (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 の詳細については、ListFacesAWS「 SDK for Python (Boto3) API リファレンス」の「」を参照してください。