Closed kiiadi closed 7 years ago
For problem one do you have any other dependencies in your maven project or on the classpath? That looks like a classic version issue to me - can you post your full pom.xml
? I created a brand-new project with the following pom and I'm able to call Rekognition
and get back labels.
This is the full pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>kylthoms</groupId>
<artifactId>rekognition</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.62</version>
</dependency>
</dependencies>
</project>
Regarding problem two it looks like you're trying to use an S3 endpoint with the Rekognition client - this isn't going to work. Rekognition makes calls to S3 under the hood for you, there's no need to provide an explicit S3 endpoint - or even any endpoint if you're using a standard region. I was able to get it to work with the following code-snippet.
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.RegionUtils;
import com.amazonaws.services.rekognition.AmazonRekognitionClient;
import com.amazonaws.services.rekognition.model.DetectLabelsRequest;
import com.amazonaws.services.rekognition.model.DetectLabelsResult;
import com.amazonaws.services.rekognition.model.Image;
import com.amazonaws.services.rekognition.model.Label;
import com.amazonaws.services.rekognition.model.S3Object;
public class RekognitionTest {
public static void main(String[] args) {
AWSCredentials credentials = new DefaultAWSCredentialsProviderChain().getCredentials();
DetectLabelsRequest request = new DetectLabelsRequest()
.withImage(new Image().withS3Object(new S3Object().withName("djt.jpg").withBucket("kylthoms-images")))
.withMaxLabels(10)
.withMinConfidence(80F);
AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(credentials)
.withRegion(RegionUtils.getRegion("eu-west-1"));
DetectLabelsResult result = rekognitionClient.detectLabels(request);
for (Label label : result.getLabels()) {
System.out.println(label.getName() + " : " + label.getConfidence());
}
}
}
Kiiadi
Thanks. I don't have my dev laptop to hand to grab the full pom right now. I had been investigating the classpath as I had the same suspicions but was drawing a blank. I'll get the full pom as soon as possible.
For problem two I did get it working last night. The AWS docs indicate the need to provide the endpoint on this page https://docs.aws.amazon.com/rekognition/latest/dg/get-started-exercise-detect-labels.html, thats where I got it from. I have now been successful calling it.
Thanks for the follow up, much appreciated. Will post again when I've got on my dev laptop.
Kiiadi
I was about to post my maven pom when I decided to print out the classloader URLs for the app at startup to check what was on the VM classpath. Turns out eclipse at some point had added another project to the build path that caused a jar clash. An older version of AWS SDK appeared earlier in the list. My bad ... thanks for prompting the thought process. Sometimes it is the most obvious problem. Great service btw.
No problem glad you go it sorted out! Let us know if you've got any other questions but I'll close this off for now.
Anyone please help me in starting with aws rekognition using java.
@MohitPuri1996 I had a very similar challenge, it got resolved when I used `
`
android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90) at java.net.InetAddress.getAllByName(InetAddress.java:787) at com.android.okhttp.Dns$1.lookup(Dns.java:39) at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:200) at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:148) at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:90) at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:190) at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:142) at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:104) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:392) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:325) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:489) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:262) at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218) at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(Unknown Source:0) at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:162) at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:75) at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:371) at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:212) at com.amazonaws.services.rekognition.AmazonRekognitionClient.invoke(AmazonRekognitionClient.java:3006) at com.amazonaws.services.rekognition.AmazonRekognitionClient.detectLabels(AmazonRekognitionClient.java:1031) at s.com.textrecognizeraws.MainActivity.detectTextRequest(MainActivity.java:162) at s.com.textrecognizeraws.MainActivity.onClick(MainActivity.java:87) at android.view.View.performClick(View.java:6909) at android.widget.TextView.performClick(TextView.java:12693) at android.view.View$PerformClick.run(View.java:26199) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
@soumincrts that looks like a problem with security restrictions, rather than the SDK -
android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
Copied from https://www.reddit.com/r/aws/comments/5g2dc2/problems_trying_to_invoke_the_rekognition_api/
Hi all, I'm trying to invoke the new Rekognition API through Java, following the example on the AWS docs. Unfortunately I'm running into a couple of problems.
Problem One - runtime errors including the SDK in maven I created a simple maven project with the SDK dependency as follows
When I execute the code I get this error
Some google-fu turned up old SO posts but nothing that seemed relevant to the current or recent SDK versions.
Problem Two - authentication errors Giving up on maven temporarily I downloaded the SDK zip and created a plain old Java project with the SDK library and all third party libraries. This runs (suggesting a problem with the jar downloaded through maven, yet to be investigated). But now I get some form of auth error. The client code is as follows
Executing this, which is very similar to the AWS docs, results in this error. The access/secret key used has permission to access the S3 bucket and object in question, that has been verified.
I'm going to continue to investigate, but if anyone else is trying out the awesome new service (thanks /u/jeffbarr) and had success let me know ...