gpt4 book ai didi

java - Rekognition 客户端不断根据每个请求进行重建

转载 作者:行者123 更新时间:2023-12-01 18:32:12 24 4
gpt4 key购买 nike

我正在使用 AWS Rekognition 构建应用程序,我意识到每次我向服务发出请求时,都会重新建立与 AWS 的连接,这会降低性能。有什么方法可以建立在整个 session 期间持续存在的单一连接吗?我的代码如下:

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();

}

}

最佳答案

您可以尝试微调:

在客户端配置中,您pass to the client .

关于java - Rekognition 客户端不断根据每个请求进行重建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60141400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com