本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搜尋使用者 (映像)
針對與所提供映像中偵測到的最大人臉相符的集合使用者,SearchUsersByImage
會搜尋指定的 CollectionID。預設情況下,SearchUsersByImage 傳回相似度分數大於 80% 的 UserID。相似性表示 UserID 與提供的映像中偵測到的最大人臉相符的程度。如果傳回多個 UserID,則會依相似度分數最高到最低的順序列出這些 UserID。或者,您可以使用 UserMatchThreshold 來指定不同的值。如需詳細資訊,請參閱管理集合中的使用者。
若要依映像搜尋使用者 (SDK)
-
如果您尚未執行:
-
建立或更新具有 AmazonRekognitionFullAccess
許可的使用者。如需詳細資訊,請參閱步驟 1:設定 AWS 帳戶並建立使用者。
-
安裝和設定 AWS CLI 和 AWS SDKs。如需詳細資訊,請參閱步驟 2:設定 AWS CLI 和 AWS SDKs。
-
使用下列範例來呼叫 SearchUsersByImage
操作。
- Java
-
此 Java 範例搜尋基於輸入映像集合中的使用者,使用 SearchUsersByImage
操作。
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder;
import com.amazonaws.services.rekognition.model.Image;
import com.amazonaws.services.rekognition.model.S3Object;
import com.amazonaws.services.rekognition.model.SearchUsersByImageRequest;
import com.amazonaws.services.rekognition.model.SearchUsersByImageResult;
import com.amazonaws.services.rekognition.model.UserMatch;
public class SearchUsersByImage {
//Replace bucket, collectionId and photo with your values.
public static final String collectionId = "MyCollection";
public static final String s3Bucket = "bucket";
public static final String s3PhotoFileKey = "input.jpg";
public static void main(String[] args) throws Exception {
AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient();
// Get an image object from S3 bucket.
Image image = new Image()
.withS3Object(new S3Object()
.withBucket(s3Bucket)
.withName(s3PhotoFileKey));
// Search collection for users similar to the largest face in the image.
SearchUsersByImageRequest request = new SearchUsersByImageRequest()
.withCollectionId(collectionId)
.withImage(image)
.withUserMatchThreshold(70F)
.withMaxUsers(2);
SearchUsersByImageResult result =
rekognitionClient.searchUsersByImage(request);
System.out.println("Printing search result with matched user and similarity score");
for (UserMatch match: result.getUserMatches()) {
System.out.println(match.getUser().getUserId() + " with similarity score " + match.getSimilarity());
}
}
}
- AWS CLI
-
此 AWS CLI 命令會根據 SearchUsersByImage
操作的輸入映像來搜尋集合中的使用者。
aws rekognition search-users-by-image --image '{"S3Object":{"Bucket":"s3BucketName
","Name":"file-name
"}}' --collection-id MyCollectionId
--region region-name
- Python
-
下列範例會使用 SearchUsersByImage
操作,根據輸入映像搜尋集合中的使用者。
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.)
import boto3
from botocore.exceptions import ClientError
import logging
import os
logger = logging.getLogger(__name__)
session = boto3.Session(profile_name='profile-name')
client = session.client('rekognition')
def load_image(file_name):
"""
helper function to load the image for indexFaces call from local disk
:param image_file_name: The image file location that will be used by indexFaces call.
:return: The Image in bytes
"""
print(f'- loading image: {file_name}')
with open(file_name, 'rb') as file:
return {'Bytes': file.read()}
def search_users_by_image(collection_id, image_file):
"""
SearchUsersByImage operation with user ID provided as the search source
:param collection_id: The ID of the collection where user and faces are stored.
:param image_file: The image that contains the reference face to search for.
:return: response of SearchUsersByImage API
"""
logger.info(f'Searching for users using an image: {image_file}')
try:
response = client.search_users_by_image(
CollectionId=collection_id,
Image=load_image(image_file)
)
print(f'- found {len(response["UserMatches"])} matches')
print([f'- {x["User"]["UserId"]} - {x["Similarity"]}%' for x in response["UserMatches"]])
except ClientError:
logger.exception(f'Failed to perform SearchUsersByImage with given image: {image_file}')
raise
else:
print(response)
return response
def main():
collection_id = "collection-id"
IMAGE_SEARCH_SOURCE = os.getcwd() + '/image_path'
search_users_by_image(collection_id, IMAGE_SEARCH_SOURCE)
if __name__ == "__main__":
main()
SearchUsersByImage 操作要求
SearchUsersByImage
的請求包括要搜尋的集合和來源映像位置。在此範例中,來源映像儲存於 Amazon S3 儲存貯體 (S3Object
)。另外指定要傳回的使用者最大臉數 (MaxUsers
) 以及必須與要傳回的使用者相符的最小可信度 (UserMatchThreshold
)。
{
"CollectionId": "MyCollection",
"Image": {
"S3Object": {
"Bucket": "bucket",
"Name": "input.jpg"
}
},
"MaxUsers": 2,
"UserMatchThreshold": 99
}
SearchUsersByImage 操作回應
適用於 SearchUsersByImage
的回應包括適用於 SearchedFace
的物件 FaceDetail
,以及適用於每個物件的 UserId
、Similarity
和 UserStatus
的 UserMatches 清單。如果輸入映像包含多個人臉,則還將傳回 UnsearchedFaces 清單。
{
"SearchedFace": {
"FaceDetail": {
"BoundingBox": {
"Width": 0.23692893981933594,
"Top": 0.19235000014305115,
"Left": 0.39177176356315613,
"Height": 0.5437348484992981
}
}
},
"UserMatches": [
{
"User": {
"UserId": "demoUser1",
"UserStatus": "ACTIVE"
},
"Similarity": 100.0
},
{
"User": {
"UserId": "demoUser2",
"UserStatus": "ACTIVE"
},
"Similarity": 99.97946166992188
}
],
"FaceModelVersion": "6",
"UnsearchedFaces": [
{
"FaceDetails": {
"BoundingBox": {
"Width": 0.031677018851041794,
"Top": 0.5593535900115967,
"Left": 0.6102562546730042,
"Height": 0.0682177022099495
}
},
"Reasons": [
"FACE_NOT_LARGEST"
]
},
{
"FaceDetails": {
"BoundingBox": {
"Width": 0.03254449740052223,
"Top": 0.6080358028411865,
"Left": 0.516062319278717,
"Height": 0.06347997486591339
}
},
"Reasons": [
"FACE_NOT_LARGEST"
]
}
]
}