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

How can I DetectFaces in Amazon Rekognition AWS with Android Studio?

我尝试了很多方法,但我无法成功。我还没有找到 Android(about 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。抱歉我的英语不好

提前致谢。

现在我可以接受这些问题了。我经历过很多很多事情<3 <3 <3。 谢谢

我是泰国人,我不得不更加努力地寻找解决方案,因为缺乏特定语言的信息。这是我的解决方案。

我的解决方案是:

0.There是Rekognition的设置端点--> http://docs.aws.amazon.com/general/latest/gr/rande.html#rekognition_region

1.On a "null object reference issue" 我发现我必须先创建一个新对象,例如 "Image image = new Image();" <-- "new" 命令在其中创建一个对象实例class

2.After上面的错误,还有更多的错误(Errors on NetworkOnMainThreadException),所以我尝试了一切直到找到这个页面--> https://docs.aws.amazon.com/cognito/latest/developerguide/getting-credentials.html 页面说...

因此,我查找了有关 AsyncTask 的更多信息,然后我创建了一个 AsyncTask class,然后我将所有关于初始化、请求、响应的代码移至 AsyncTask class. ตอนรันตอนท้ายๆน้ำตาจิไหล我的代码工作...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

希望这些对您有所帮助:)