View a markdown version of this page

AWS SDK または CLI SearchFacesByImageで を使用する - Amazon Rekognition

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

AWS SDK または CLI SearchFacesByImageで を使用する

次のサンプルコードは、SearchFacesByImage を使用する方法を説明しています。

詳細については、「顔を検索する (イメージ)」を参照してください。

.NET
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 search for images matching those /// in a collection. /// </summary> public class SearchFacesMatchingImage { public static async Task Main() { string collectionId = "MyCollection"; string bucket = "amzn-s3-demo-bucket"; string photo = "input.jpg"; var rekognitionClient = new AmazonRekognitionClient(); // Get an image object from S3 bucket. var image = new Image() { S3Object = new S3Object() { Bucket = bucket, Name = photo, }, }; var searchFacesByImageRequest = new SearchFacesByImageRequest() { CollectionId = collectionId, Image = image, FaceMatchThreshold = 70F, MaxFaces = 2, }; SearchFacesByImageResponse searchFacesByImageResponse = await rekognitionClient.SearchFacesByImageAsync(searchFacesByImageRequest); Console.WriteLine("Faces matching largest face in image from " + photo); searchFacesByImageResponse.FaceMatches.ForEach(face => { Console.WriteLine($"FaceId: {face.Face.FaceId}, Similarity: {face.Similarity}"); }); } }
  • API の詳細については、「AWS SDK for .NET API リファレンス」の「SearchFacesByImage」を参照してください。

CLI
AWS CLI

画像内の最大の顔と一致するコレクション内の顔を検索するには

次の のsearch-faces-by-image例では、指定されたイメージ内の最大の顔と一致するコレクション内の顔を検索します。

aws rekognition search-faces-by-image \ --image '{"S3Object":{"Bucket":"amzn-s3-demo-bucket","Name":"ExamplePerson.jpg"}}' \ --collection-id MyFaceImageCollection

出力:

{ "SearchedFaceBoundingBox": { "Width": 0.18562500178813934, "Top": 0.1618015021085739, "Left": 0.5575000047683716, "Height": 0.24770642817020416 }, "SearchedFaceConfidence": 99.993408203125, "FaceMatches": [ { "Face": { "BoundingBox": { "Width": 0.18562500178813934, "Top": 0.1618019938468933, "Left": 0.5575000047683716, "Height": 0.24770599603652954 }, "FaceId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "ExternalImageId": "example-image.jpg", "Confidence": 99.99340057373047, "ImageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE22222" }, "Similarity": 99.97913360595703 }, { "Face": { "BoundingBox": { "Width": 0.18562500178813934, "Top": 0.1618019938468933, "Left": 0.5575000047683716, "Height": 0.24770599603652954 }, "FaceId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE33333", "ExternalImageId": "image3.jpg", "Confidence": 99.99340057373047, "ImageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE44444" }, "Similarity": 99.97913360595703 }, { "Face": { "BoundingBox": { "Width": 0.41499999165534973, "Top": 0.09187500178813934, "Left": 0.28083300590515137, "Height": 0.3112500011920929 }, "FaceId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE55555", "ExternalImageId": "image2.jpg", "Confidence": 99.99769592285156, "ImageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE66666" }, "Similarity": 99.18069458007812 }, { "Face": { "BoundingBox": { "Width": 0.48166701197624207, "Top": 0.20999999344348907, "Left": 0.21250000596046448, "Height": 0.36125001311302185 }, "FaceId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE77777", "ExternalImageId": "image1.jpg", "Confidence": 99.99949645996094, "ImageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE88888" }, "Similarity": 98.66607666015625 } ], "FaceModelVersion": "3.0" }

詳細については、「Amazon Rekognition 開発者ガイド」の「画像付きの顔を検索する」を参照してください。

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

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.SearchFacesRequest; import software.amazon.awssdk.services.rekognition.model.SearchFacesResponse; import software.amazon.awssdk.services.rekognition.model.FaceMatch; 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 SearchFaceMatchingIdCollection { public static void main(String[] args) { final String usage = """ Usage: <collectionId> <sourceImage> Where: collectionId - The id of the collection. \s sourceImage - The path to the image (for example, C:\\AWS\\pic1.png).\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String collectionId = args[0]; String faceId = args[1]; Region region = Region.US_WEST_2; RekognitionClient rekClient = RekognitionClient.builder() .region(region) .build(); System.out.println("Searching for a face in a collections"); searchFacebyId(rekClient, collectionId, faceId); rekClient.close(); } public static void searchFacebyId(RekognitionClient rekClient, String collectionId, String faceId) { try { SearchFacesRequest searchFacesRequest = SearchFacesRequest.builder() .collectionId(collectionId) .faceId(faceId) .faceMatchThreshold(70F) .maxFaces(2) .build(); SearchFacesResponse imageResponse = rekClient.searchFaces(searchFacesRequest); System.out.println("Faces matching in the collection"); List<FaceMatch> faceImageMatches = imageResponse.faceMatches(); for (FaceMatch face : faceImageMatches) { System.out.println("The similarity level is " + face.similarity()); System.out.println(); } } catch (RekognitionException e) { System.out.println(e.getMessage()); System.exit(1); } } }
  • API の詳細については、「AWS SDK for Java 2.x API リファレンス」の「SearchFacesByImage」を参照してください。

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 search_faces_by_image(self, image, threshold, max_faces): """ Searches for faces in the collection that match the largest face in the reference image. :param image: The image that contains the reference face to search for. :param threshold: The match confidence must be greater than this value for a face to be included in the results. :param max_faces: The maximum number of faces to return. :return: A tuple. The first element is the face found in the reference image. The second element is the list of matching faces found in the collection. """ try: response = self.rekognition_client.search_faces_by_image( CollectionId=self.collection_id, Image=image.image, FaceMatchThreshold=threshold, MaxFaces=max_faces, ) image_face = RekognitionFace( { "BoundingBox": response["SearchedFaceBoundingBox"], "Confidence": response["SearchedFaceConfidence"], } ) collection_faces = [ RekognitionFace(face["Face"]) for face in response["FaceMatches"] ] logger.info( "Found %s faces in the collection that match the largest " "face in %s.", len(collection_faces), image.image_name, ) except ClientError: logger.exception( "Couldn't search for faces in %s that match %s.", self.collection_id, image.image_name, ) raise else: return image_face, collection_faces
SAP ABAP
SDK for SAP ABAP
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

TRY. " Create S3 object reference for the image DATA(lo_s3object) = NEW /aws1/cl_reks3object( iv_bucket = iv_s3_bucket iv_name = iv_s3_key ). " Create image object DATA(lo_image) = NEW /aws1/cl_rekimage( io_s3object = lo_s3object ). " Search for matching faces oo_result = lo_rek->searchfacesbyimage( iv_collectionid = iv_collection_id io_image = lo_image iv_facematchthreshold = iv_threshold iv_maxfaces = iv_max_faces ). DATA(lt_face_matches) = oo_result->get_facematches( ). DATA(lv_match_count) = lines( lt_face_matches ). DATA(lv_msg4) = |Face search completed: { lv_match_count } match(es) found.|. MESSAGE lv_msg4 TYPE 'I'. CATCH /aws1/cx_rekresourcenotfoundex. MESSAGE 'Collection not found.' TYPE 'E'. CATCH /aws1/cx_rekinvalids3objectex. MESSAGE 'Invalid S3 object.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.
  • API の詳細については、 AWS SDK for SAP ABAP API リファレンスのSearchFacesByImage」を参照してください。

AWS SDK 開発者ガイドとコード例の完全なリストについては、「」を参照してくださいAWS SDK での Rekognition の使用。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。