text AWS Rekognition示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text AWS Rekognition示例相关的知识,希望对你有一定的参考价值。
// Add dependencies
dependencies {
implementation 'com.amazonaws:aws-android-sdk-rekognition:2.13.+'
}
// Android Sample Code
// Detect Labels in Text
public void detetctLabelsInText() throws IOException {
AmazonRekognitionClient rekognition = new AmazonRekognitionClient(new BasicAWSCredentials("<access-key>", "<secret-access-key>"));
final byte[] imageArray = IOUtils.toByteArray(imageInputStream); // imageInputStream is an InputStream object to the image
final DetectLabelsRequest detectLabelsRequest = new DetectLabelsRequest()
.withImage(new Image().withBytes(ByteBuffer.wrap(imageArray)));
final DetectLabelsResult result = rekognition.detectLabels(detectLabelsRequest);
final List<Label> labels = result.getLabels();
for (final Label label : labels) {
System.out.println(label.getName() + " " + label.getConfidence());
}
}
// Compare two images
public void compareFaces() {
Float similarityThreshold = 70F;
String sourceImage = "source.jpg";
String targetImage = "target.jpg";
ByteBuffer sourceImageBytes = null;
ByteBuffer targetImageBytes = null;
AmazonRekognition rekognitionClient = new AmazonRekognitionClient(new BasicAWSCredentials("<access-key>", "<secret-access-key>"));
//Load source and target images and create input parameters
try (InputStream inputStream = new FileInputStream(new File(sourceImage))) {
sourceImageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
} catch(Exception e) {
System.out.println("Failed to load source image " + sourceImage);
System.exit(1);
}
try (InputStream inputStream = new FileInputStream(new File(targetImage))) {
targetImageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
} catch(Exception e) {
System.out.println("Failed to load target images: " + targetImage);
System.exit(1);
}
Image source = new Image()
.withBytes(sourceImageBytes);
Image target = new Image()
.withBytes(targetImageBytes);
CompareFacesRequest request = new CompareFacesRequest()
.withSourceImage(source)
.withTargetImage(target)
.withSimilarityThreshold(similarityThreshold);
// Call operation
CompareFacesResult compareFacesResult = rekognitionClient.compareFaces(request);
// Display results
List <CompareFacesMatch> faceDetails = compareFacesResult.getFaceMatches();
for (CompareFacesMatch match: faceDetails){
ComparedFace face= match.getFace();
BoundingBox position = face.getBoundingBox();
System.out.println("Face at " + position.getLeft().toString()
+ " " + position.getTop()
+ " matches with " + face.getConfidence().toString()
+ "% confidence.");
}
List<ComparedFace> uncompared = compareFacesResult.getUnmatchedFaces();
System.out.println("There was " + uncompared.size() + " face(s) that did not match");
System.out.println("Source image rotation: " + compareFacesResult.getSourceImageOrientationCorrection());
System.out.println("target image rotation: " + compareFacesResult.getTargetImageOrientationCorrection());
}
以上是关于text AWS Rekognition示例的主要内容,如果未能解决你的问题,请参考以下文章
使用字节的 AWS Rekognition JavaScript 开发工具包
AWS Rekognition和s3调用Python Lambda中的子文件夹
用户无权执行:rekognition:RecognizeCelebrities with a explicit deny
使用 Amazon Rekognition API 进行文本检测和 OCR
使用 Amazon Rekognition API 进行文本检测和 OCR
AWS面部识别试图用Android应用程序中的BasicAWSCredentials描述集合。]