Amazon Rekognition 标签检测问题
Amazon Rekognition label detection problem
问题:
我开始使用 Amazon Rekognition 标签检测,问题是我不知道如何将 url 传递给 DetectLabelsRequest () 对象. url 包含一张我需要分析的图像。
代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
instantiateRekognition()
}
private fun instantiateRekognition(){
val rekognitionClient: AmazonRekognition =
AmazonRekognitionClient(BasicAWSCredentials("", ""))
val sourceStream: InputStream = FileInputStream(
"http://placehold.it/120x120&text=image1")
var souImage: Image = Image()
val byteBuffer = ByteBuffer.allocate(sourceStream.toString().length)
souImage.withBytes(byteBuffer)
val request = DetectLabelsRequest().withImage(souImage)
.withMaxLabels(10)
.withMinConfidence(75f)
try {
val result = rekognitionClient.detectLabels(request)
val labels = result.labels
for (label in labels) {
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
URL 个要分析的图像:
这是您使用 Amazon Rekognition Java V2 API.
的解决方案
如果您不熟悉 V2,请参阅此 AWS Java 开发指南主题:
Get started with the AWS SDK for Java 2.x
代码:
package com.example.rekognition;
// snippet-start:[rekognition.java2.detect_labels.import]
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.rekognition.RekognitionClient;
import software.amazon.awssdk.services.rekognition.model.Image;
import software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest;
import software.amazon.awssdk.services.rekognition.model.DetectLabelsResponse;
import software.amazon.awssdk.services.rekognition.model.Label;
import software.amazon.awssdk.services.rekognition.model.RekognitionException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
// snippet-end:[rekognition.java2.detect_labels.import]
/**
* To run this Java V2 code example, ensure that you have setup your development environment, including your credentials.
*
* For information, see this documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class DetectLabels {
public static void main(String[] args) {
final String USAGE = "\n" +
"Usage: " +
"DetectLabels <sourceImage>\n\n" +
"Where:\n" +
"sourceImage - the path to the image (for example, C:\AWS\pic1.png). \n\n";
// if (args.length != 1) {
// System.out.println(USAGE);
// System.exit(1);
// }
String sourceImage = "C:\Users\scmacdon\lake.png" ; // args[0] ;
Region region = Region.US_EAST_1;
RekognitionClient rekClient = RekognitionClient.builder()
.region(region)
.build();
detectImageLabels(rekClient, sourceImage );
rekClient.close();
}
// snippet-start:[rekognition.java2.detect_labels.main]
public static void detectImageLabels(RekognitionClient rekClient, String sourceImage) {
try {
InputStream sourceStream = new URL("http://placehold.it/120x120&text=image1").openStream();
// InputStream sourceStream = new FileInputStream(sourceImage);
SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
// Create an Image object for the source image.
Image souImage = Image.builder()
.bytes(sourceBytes)
.build();
DetectLabelsRequest detectLabelsRequest = DetectLabelsRequest.builder()
.image(souImage)
.maxLabels(10)
.build();
DetectLabelsResponse labelsResponse = rekClient.detectLabels(detectLabelsRequest);
List<Label> labels = labelsResponse.labels();
System.out.println("Detected labels for the given photo");
for (Label label: labels) {
System.out.println(label.name() + ": " + label.confidence().toString());
}
} catch (RekognitionException | FileNotFoundException | MalformedURLException e) {
System.out.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
}
}
// snippet-end:[rekognition.java2.detect_labels.main]
}
给定图像的结果 URL:
我用 URL TOO
进行了测试
这产生了:
您可以在此处找到许多其他 Java V2 Amazon Rekognition 示例:
https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/rekognition
问题:
我开始使用 Amazon Rekognition 标签检测,问题是我不知道如何将 url 传递给 DetectLabelsRequest () 对象. url 包含一张我需要分析的图像。
代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
instantiateRekognition()
}
private fun instantiateRekognition(){
val rekognitionClient: AmazonRekognition =
AmazonRekognitionClient(BasicAWSCredentials("", ""))
val sourceStream: InputStream = FileInputStream(
"http://placehold.it/120x120&text=image1")
var souImage: Image = Image()
val byteBuffer = ByteBuffer.allocate(sourceStream.toString().length)
souImage.withBytes(byteBuffer)
val request = DetectLabelsRequest().withImage(souImage)
.withMaxLabels(10)
.withMinConfidence(75f)
try {
val result = rekognitionClient.detectLabels(request)
val labels = result.labels
for (label in labels) {
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
URL 个要分析的图像:
这是您使用 Amazon Rekognition Java V2 API.
的解决方案如果您不熟悉 V2,请参阅此 AWS Java 开发指南主题:
Get started with the AWS SDK for Java 2.x
代码:
package com.example.rekognition;
// snippet-start:[rekognition.java2.detect_labels.import]
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.rekognition.RekognitionClient;
import software.amazon.awssdk.services.rekognition.model.Image;
import software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest;
import software.amazon.awssdk.services.rekognition.model.DetectLabelsResponse;
import software.amazon.awssdk.services.rekognition.model.Label;
import software.amazon.awssdk.services.rekognition.model.RekognitionException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
// snippet-end:[rekognition.java2.detect_labels.import]
/**
* To run this Java V2 code example, ensure that you have setup your development environment, including your credentials.
*
* For information, see this documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class DetectLabels {
public static void main(String[] args) {
final String USAGE = "\n" +
"Usage: " +
"DetectLabels <sourceImage>\n\n" +
"Where:\n" +
"sourceImage - the path to the image (for example, C:\AWS\pic1.png). \n\n";
// if (args.length != 1) {
// System.out.println(USAGE);
// System.exit(1);
// }
String sourceImage = "C:\Users\scmacdon\lake.png" ; // args[0] ;
Region region = Region.US_EAST_1;
RekognitionClient rekClient = RekognitionClient.builder()
.region(region)
.build();
detectImageLabels(rekClient, sourceImage );
rekClient.close();
}
// snippet-start:[rekognition.java2.detect_labels.main]
public static void detectImageLabels(RekognitionClient rekClient, String sourceImage) {
try {
InputStream sourceStream = new URL("http://placehold.it/120x120&text=image1").openStream();
// InputStream sourceStream = new FileInputStream(sourceImage);
SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
// Create an Image object for the source image.
Image souImage = Image.builder()
.bytes(sourceBytes)
.build();
DetectLabelsRequest detectLabelsRequest = DetectLabelsRequest.builder()
.image(souImage)
.maxLabels(10)
.build();
DetectLabelsResponse labelsResponse = rekClient.detectLabels(detectLabelsRequest);
List<Label> labels = labelsResponse.labels();
System.out.println("Detected labels for the given photo");
for (Label label: labels) {
System.out.println(label.name() + ": " + label.confidence().toString());
}
} catch (RekognitionException | FileNotFoundException | MalformedURLException e) {
System.out.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
}
}
// snippet-end:[rekognition.java2.detect_labels.main]
}
给定图像的结果 URL:
我用 URL TOO
进行了测试这产生了:
您可以在此处找到许多其他 Java V2 Amazon Rekognition 示例:
https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/rekognition