in amplify/backend/function/idvworkflowfn/src/idvfunctions.js [185:300]
registerNewUser: async function (userInfoAsJson) {
// 1. First check the face image quality via DetectFaces
// 2. Use SearchFacesByImage against the collection(s) to check for any duplicate registration
// 3. Index the face image using IndexFaces and use the ExternalImageId (userid, in this case)
// to associate the face embeddings with the ExternalImageId
// 4. Store the face image in the S3 bucket along with the user metadata in DDB (this is already in place)
// NOTE: In 'registerNewUser', we'll showcase the use of S3-stored images when calling the Rekognition APIs
// For details on how to supply image blogs directly to the Rekognition APIs, please see 'registerNewUserWithIdCard'
const bucket = process.env.STORAGE_IDVIMAGEBUCKET_BUCKETNAME;
const userInfoTable = process.env.API_AMAZONREKOGNITIONIDV_USERINFOTABLE_NAME;
const userInfo = JSON.parse(userInfoAsJson);
var response = {
Companyid: userInfo.companyid,
UserId: userInfo.userid,
RegistrationStatus: 'error-failed',
Success: true,
Message: '',
};
const collectionId = await graphqlhelpers.getActiveCollection();
if(!collectionId) {
response.Success = false;
response.Message = "Unable to fetch active collection";
return response;
}
const rek = new Rekognition();
try {
var params = {
Image: {
S3Object: {
Bucket: bucket,
Name: "public/" + userInfo.faceimage
}
}
};
// 1. First check the face image quality via DetectFaces
const detectFacesResponse = await rek.detectFaces(params).promise();
const detectFacesValidation = validateidv.validateDetectFacesResponse(detectFacesResponse);
if (!detectFacesValidation.success) {
response.Message = detectFacesValidation.message;
response.RegistrationStatus = 'error-detectfaces-failed';
response.Success = false;
}
if (response.Success) {
// 2. Use SearchFacesByImage against the collection(s) to check for any duplicate registration
params = {
CollectionId: collectionId,
FaceMatchThreshold: 95,
Image: params.Image // reuse previous
};
const searchFacesResponse = await rek.searchFacesByImage(params).promise();
const duplicateCheckResponse = validateidv.validateDuplicateCheck(searchFacesResponse);
if (!duplicateCheckResponse.success) {
response.Message = duplicateCheckResponse.message;
response.RegistrationStatus = 'error-duplicate-found';
response.Success = false;
}
}
// 0 for faces that haven't indexed in a collection yet
var faceId = "";
// 3. Index the face image using IndexFaces and use the ExternalImageId (userid, in this case)
// to associate the face embeddings with the ExternalImageId
if (response.Success) {
const externalImageId = userInfo.userid;
params = {
CollectionId: collectionId,
DetectionAttributes: [],
ExternalImageId: externalImageId,
Image: params.Image // reuse previous
}
const indexFaceResponse = await rek.indexFaces(params).promise();
if (!indexFaceResponse ||
!indexFaceResponse.FaceRecords ||
indexFaceResponse.FaceRecords.length != 1) {
response.Message = 'IndexFaces failed';
response.RegistrationStatus = 'error-indexfaces-failed';
response.Success = false;
} else {
faceId = indexFaceResponse.FaceRecords[0].Face.FaceId;
response.RegistrationStatus = 'done';
response.Success = true;
console.log("Successfully registered user");
}
}
// Now update ddb entry w/ face embeddings id, if successful so far
// If prev failure, then update reg status in ddb
const ddbResponse = await updateUserInfo(userInfoTable, userInfo, faceId, response.RegistrationStatus);
if (!ddbResponse.success) {
response.Message = 'UserInfo ddb update failed!';
response.RegistrationStatus = 'error-userinfo-ddb-failed';
response.Success = false;
}
else {
// Leave message and status as is
}
}
catch (e) {
response.Message = e.toString();
response.RegistrationStatus = 'unknown-error';
response.Success = false;
console.log(e);
}
return response;
},