This time, I would like to make a simple application using Amazon's Rekognition face recognition service. When you hear face recognition, you may have a difficult image, but if you use this Rekoginition, you can easily make it, so please try using it through a sample.
Only the excerpted source is posted in the article, so if you are interested, I will post the source I made this time in here. please refer.
--You have an AWS account. --The access key and secret key of the IAM user who has the policy of "Amazon Rekognition Full Access" must be acquired.
This is the screen of the app I made this time.
Collection creation
ListCollectionsResult listCollectionsResult = rekognitionClient.listCollections(new ListCollectionsRequest());
if (!listCollectionsResult.getCollectionIds().contains(COLLECTION_ID)) {
rekognitionClient.createCollection(new CreateCollectionRequest().withCollectionId(COLLECTION_ID));
}
Instead of simply creating a collection, I try to create one if it doesn't exist.
Rekognition API | Contents |
---|---|
listCollections | The list of collections will be returned. |
createCollection | Create a collection. |
Store comparison source image
IndexFacesRequest indexFacesRequest = new IndexFacesRequest()
.withCollectionId(COLLECTION_ID)
.withExternalImageId(id)
.withImage(new Image().withBytes(ByteBuffer.wrap(multipartFile.getBytes())));
IndexFacesResult indexFacesResult = rekognitionClient.indexFaces(indexFacesRequest);
Rekognition API | Contents |
---|---|
indexFaces | Store your face photo in the collection. |
Property | Description | Available characters |
---|---|---|
collectionId | Name of the created collection | a-zA-Z0-9_.- |
externalImageId | Arbitrary data | a-zA-Z0-9_.-: |
image | Comparison source image (binary) |
This time, we will manage the ID that uniquely identifies the user in externalImageId
.
SearchFacesByImageRequest searchFacesByImageRequest = new SearchFacesByImageRequest()
.withCollectionId(COLLECTION_ID)
.withImage(new Image().withBytes(ByteBuffer.wrap(multipartFile.getBytes())))
.withFaceMatchThreshold(90F)
.withMaxFaces(1);
try {
SearchFacesByImageResult searchFacesByImageResult = rekognitionClient.searchFacesByImage(searchFacesByImageRequest);
} catch (InvalidParameterException e) {
//Error handling
}
Please note that InvalidParameterException
will occur if the face cannot be recognized from the uploaded image.
Rekognition API | Contents |
---|---|
searchFacesByImage | Search from the collection based on the face photo to compare. |
Property | Description | Available characters |
---|---|---|
collectionId | Name of the created collection | a-zA-Z0-9_.- |
image | Comparison source image (binary) | |
faceMatchThreshold | Similarity threshold. 70 by default% | |
maxFaces | Number of cases to be acquired from matching faces in descending order of similarity |
In this sample, we tried to get the one with the highest similarity from the ones with 90% or more similarity.
SearchFacesByImageResult can be obtained in the following format, so get externalFaceId
from it.
{
"FaceMatches": [
{
"Face": {
"BoundingBox": {
"Height": number,
"Left": number,
"Top": number,
"Width": number
},
"Confidence": number,
"ExternalImageId": "string", <--this
"FaceId": "string",
"ImageId": "string"
},
"Similarity": number
}
],
"FaceModelVersion": "string",
"SearchedFaceBoundingBox": {
"Height": number,
"Left": number,
"Top": number,
"Width": number
},
"SearchedFaceConfidence": number
}
If it matches the ID entered on the screen, authentication is OK, otherwise it is NG.
-I tried face recognition with Amazon Rekognition
Recommended Posts