我该如何在Android Studio中使用Amazon Rekognition AWS检测人脸?

7
我尝试了很多方法,但都没有成功。我没有找到任何有关Android的源代码示例(关于rekognition)。
开发者指南中有JAVA的源代码,但即使我尝试过TT,也不能实现它。
我试图通过从外部存储(从模拟器)发送图像文件来检测人脸,但我不知道哪里出错了(我不擅长编码)。这是我的代码。
AmazonRekognitionClient amazonRekognitionClient;
Image getAmazonRekognitionImage;
DetectFacesRequest detectFaceRequest;
DetectFacesResult detectFaceResult;
File file = new File(Environment.getExternalStorageDirectory(),"sungyeol.jpg.jpg");

public void test_00(View view) {
 ByteBuffer imageBytes;
 try{
        InputStream inputStream = new FileInputStream(file.getAbsolutePath().toString());
        imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
        Log.e("InputStream: ",""+inputStream);
        Log.e("imageBytes: ","");
        getAmazonRekognitionImage.withBytes(imageBytes);

        // Initialize the Amazon Cognito credentials provider
        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                getApplicationContext(),
                "us-east-2:.......", // Identity Pool ID
                Regions.US_EAST_2 // Region
        );

        //I want "ALL" attributes
        amazonRekognitionClient = new AmazonRekognitionClient(credentialsProvider);

        detectFaceRequest = new DetectFacesRequest()
                .withAttributes(Attribute.ALL.toString())
                .withImage(getAmazonRekognitionImage);

        detectFaceResult = amazonRekognitionClient.detectFaces(detectFaceRequest);
        detectFaceResult.getFaceDetails();

    }
 catch(Exception ex){
   Log.e("Error on something:","Message:"+ex.getMessage());
 }

这里是我的错误信息

02-04 09:30:07.268 29405-29405/? E/InputStream:: java.io.FileInputStream@a9b23e7
02-04 09:30:07.271 29405-29405/? E/Error on something:: Message:Attempt to invoke virtual method 'com.amazonaws.services.rekognition.model.Image com.amazonaws.services.rekognition.model.Image.withBytes(java.nio.ByteBuffer)' on a null object reference

什么是空对象引用?

我试图更改文件路径,但他说找不到这个文件...当我更改为这个路径时,会出现错误。

顺便说一下,我已经向Android模拟器中的用户请求了访问文件夹的权限。

请帮助我。

PS. 抱歉我的英语不好。

提前谢谢。

1个回答

3

现在我已经解决了问题。我经历了很多事情<3 <3 <3。谢谢。

我是泰国人,因为特定语言缺乏信息,所以我必须更加努力地寻找解决方案。以下是我的解决方案:

0.有一个端点用于设置Rekognition--> http://docs.aws.amazon.com/general/latest/gr/rande.html#rekognition_region

1.在“null对象引用问题”上,我发现我必须首先创建一个新对象,例如“Image image = new Image();”<--“new”命令在该类中创建一个对象实例

2.在上述错误之后,还会出现更多错误(NetworkOnMainThreadException上的错误),因此我尝试了一切直到我找到这个页面--> https://docs.aws.amazon.com/cognito/latest/developerguide/getting-credentials.html 该页面说...

enter image description here

因此,我查找了有关AsyncTask的更多信息,然后创建了AsyncTask类,然后将所有关于初始化、请求和响应的代码移动到AsyncTask类中。最终我的代码工作了...TT,总之sungyeol.jpg.jpg文件也工作了。

例如

private void testTask(){
  .... all code in the main thread particularly on the requests and responses
  from the services
 //print the response or the result
 //Log.e() makes the message in the android monitor red like an error
 Log.e("Response:", [responseparameter.toString()]);

}

//create the inherited class from the AsyncTask Class
//(you can create within your activity class)
class AsyncTaskRunner extends AsyncTask<String,String,String>{
    @Override 
    public String doInBackground(String ... input){
        testTask(); // call the testTask() method that i have created
        return null; // this override method must return String
    }

} 

//I've created a button for running the task
public void buttonTask(View view){
    AsyncTaskRunner runner = new AsyncTaskRunner();
    runner.execute();

}

了解更多关于AsyncTask的信息:

https://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask

http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html#.WJdkqVOLTIU

希望这些有所帮助 :)


如果您能够发布您的解决方案,那将非常有帮助,我也处于同样的情况。 - Shivaraj Patil
我已经编辑了我的帖子,添加了更多信息,希望这些能对你有所帮助。 - I am someone
1
@ShivarajPatil,请给我们提供解决方案。 - Atul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接