Клиент Rekognition продолжает перестраивать при каждом запросе - PullRequest
0 голосов
/ 10 февраля 2020

Я использую AWS Rekognition для создания приложения, и я понял, что каждый раз, когда я обращаюсь к сервису, соединение с aws продолжает восстанавливаться, что снижает производительность. Есть ли способ сделать одно единственное соединение, которое сохраняется на протяжении всего сеанса? Мой код можно увидеть ниже:

private static final AmazonRekognition rekognitionClient = RekognitionUtil.setupRekognitionClient();

private static AWSCredentialsProvider setupCredentials(String accessKey, String secretKey) {

    AWSCredentialsProvider provider = new AWSCredentialsProvider() {
        @Override
        public AWSCredentials getCredentials() {
            return new AWSCredentials() {
                @Override
                public String getAWSAccessKeyId() {
                    LOG.info("Access key: "  + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey));
                    return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey);
                }

                @Override
                public String getAWSSecretKey() {
                    LOG.info("Secret key: "  + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey));
                    return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey);
                }
            };
        }

        @Override
        public void refresh() {

        }
    };

    return provider;

}


private static AmazonRekognition setupRekognitionClient() {

    AWSCredentialsProvider provider = setupCredentials("xxxx", "xxxx");

    return AmazonRekognitionClientBuilder.standard().withCredentials(provider).withRegion(ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_REGION,"xxx")).build();

}

  private static AWSCredentialsProvider setupCredentials(String accessKey, String secretKey) {

    AWSCredentialsProvider provider = new AWSCredentialsProvider() {
        @Override
        public AWSCredentials getCredentials() {
            return new AWSCredentials() {
                @Override
                public String getAWSAccessKeyId() {
                    LOG.info("Access key: "  + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey));
                    return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey);
                }

                @Override
                public String getAWSSecretKey() {
                    LOG.info("Secret key: "  + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey));
                    return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey);
                }
            };
        }

        @Override
        public void refresh() {

        }
    };

    return provider;

}


private static AmazonRekognition setupRekognitionClient() {

    AWSCredentialsProvider provider = setupCredentials("xxxx", "xxx");

    return AmazonRekognitionClientBuilder.standard().withCredentials(provider).withRegion(ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_REGION,"xxx")).build();

}




 public static String searchCollectionByFace(String collectionId, ByteBuffer sourceByteBuffer) throws Exception {

    LOG.info("Searching face collection by face...");

    String faceId = "";

    try {

        ObjectMapper objectMapper = new ObjectMapper();

        // Get an image object from S3 bucket.
        Image image = new Image().withBytes(sourceByteBuffer);

        // Search collection for faces similar to the largest face in the image.
        SearchFacesByImageRequest searchFacesByImageRequest = new SearchFacesByImageRequest().withCollectionId(collectionId).withImage(image).withFaceMatchThreshold(70F).withMaxFaces(2);

        SearchFacesByImageResult searchFacesByImageResult = rekognitionClient.searchFacesByImage(searchFacesByImageRequest);

        List<FaceMatch> faceImageMatches = searchFacesByImageResult.getFaceMatches();

        for (FaceMatch face : faceImageMatches) {
            LOG.info(face.getFace().getFaceId());
            if(face.getFace().getConfidence() > SIMILARITY_LIMIT){
                faceId = face.getFace().getFaceId();
            }
        }

        return faceId;

    } catch (Exception ex) {

        LOG.error("Error has occurred searching for face", ex);
        throw new Exception();

    }

}

1 Ответ

0 голосов
/ 10 февраля 2020

Можно попытаться выполнить точную настройку:

в конфигурации клиента вы передаете клиенту .

...