All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fivefaces.cloud.face_recognition.AWSRekognitionFaceRecognitionService Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fivefaces.cloud.face_recognition;

import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.model.*;
import com.fivefaces.cloud.Utils;
import com.fivefaces.common.exception.ItemNotFoundException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import java.nio.ByteBuffer;
import java.util.List;

@Profile("FACE_RECOGNITION_AWS_REKOGNITION")
@AllArgsConstructor
@Slf4j
@Service
public class AWSRekognitionFaceRecognitionService implements FaceRecognitionService {

    private final Utils utils;
    private AmazonRekognition rekognitionClient;

    @Override
    public void createFace(byte[] photo, String identifier) {
        ByteBuffer imageBytes = ByteBuffer.wrap(photo);
        Image image = new Image().withBytes(imageBytes);
        IndexFacesRequest indexFacesRequest = new IndexFacesRequest()
                .withImage(image)
                .withQualityFilter(QualityFilter.AUTO)
                .withMaxFaces(1)
                .withCollectionId(utils.getAwsRekognitionCollectionId())
                .withExternalImageId(identifier)
                .withDetectionAttributes("DEFAULT");

        IndexFacesResult indexFacesResult = getRekognitionClient().indexFaces(indexFacesRequest);
        log.info("Results for {}", identifier);
        log.info("Faces indexed:");
        List faceRecords = indexFacesResult.getFaceRecords();
        for (FaceRecord faceRecord : faceRecords) {
            log.info("Face ID: {}", faceRecord.getFace().getFaceId());
            log.info("Location: {}", faceRecord.getFaceDetail().getBoundingBox().toString());
        }
    }

    @Override
    public String searchFaces(byte[] photo) {
        ByteBuffer imageBytes = ByteBuffer.wrap(photo);
        Image image = new Image().withBytes(imageBytes);
        SearchFacesByImageRequest searchFacesByImageRequest = new SearchFacesByImageRequest()
                .withCollectionId(utils.getAwsRekognitionCollectionId())
                .withImage(image)
                .withFaceMatchThreshold(80F)
                .withMaxFaces(1);
        SearchFacesByImageResult searchFacesByImageResult = getRekognitionClient().searchFacesByImage(searchFacesByImageRequest);
        List faceImageMatches = searchFacesByImageResult.getFaceMatches();
        Face face = null;
        for (FaceMatch faceMatch : faceImageMatches) {
            if (faceMatch != null) {
                face = faceMatch.getFace();
                break;
            }
        }
        if (face != null) {
            return face.getExternalImageId();
        }
        throw new ItemNotFoundException("No matching face");
    }


    private void createCollection(String collectionId) {
        log.info("Creating Collection : {} ", collectionId);
        CreateCollectionRequest request = new CreateCollectionRequest().withCollectionId(collectionId);
        CreateCollectionResult createCollectionResult;
        synchronized (getRekognitionClient()) {
            createCollectionResult = getRekognitionClient().createCollection(request);
        }
        log.info("CollectionArn : {} ", createCollectionResult.getCollectionArn());
        log.info("Status code : {} ", createCollectionResult.getStatusCode().toString());
    }

    private boolean hasCollection(String collectionId) {
        String paginationToken = null;
        int limit = 10;
        ListCollectionsResult listCollectionsResult = null;
        log.info("Searching Collection for {}", collectionId);
        do {
            if (listCollectionsResult != null) {
                paginationToken = listCollectionsResult.getNextToken();
            }
            ListCollectionsRequest listCollectionsRequest = new ListCollectionsRequest()
                    .withMaxResults(limit)
                    .withNextToken(paginationToken);
            synchronized (getRekognitionClient()) {
                listCollectionsResult = getRekognitionClient().listCollections(listCollectionsRequest);
            }
            List collectionIds = listCollectionsResult.getCollectionIds();
            if (collectionIds.contains(collectionId)) {
                log.info("{} Collection Found", collectionId);
                return true;
            }
        } while (listCollectionsResult.getNextToken() != null);
        return false;
    }

    public AmazonRekognition getRekognitionClient() {
        if (rekognitionClient == null) {
            rekognitionClient = utils.generateRekognitionClient();
        }
        return rekognitionClient;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy