如何使用Android Studio在Amazon Rekognition AWS中检测到Faces?

I a*_*one 5 java amazon-web-services android-studio amazon-rekognition

我已经尝试了很多方法,但是我无法成功。我尚未找到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());
 }
Run Code Online (Sandbox Code Playgroud)

这是我的错误

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
Run Code Online (Sandbox Code Playgroud)

什么是空对象引用?我尝试更改文件路径,但他说没有此类文件...,当我更改为该路径时,上面有错误。顺便说一句,我已经向用户询问了从Android中的模拟器访问文件夹的权限

请帮助我PS。对不起,我的英语不好

先感谢您。

I a*_*one 3

现在我对这些问题已经很满意了。我经历了很多很多事情<3 <3 <3。\n谢谢你

\n\n

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

\n\n

我的解决方案是:

\n\n

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

\n\n

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

\n\n

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

\n\n

在此输入图像描述

\n\n

因此,我查找了有关 AsyncTask 的更多信息,之后创建了一个 AsyncTask 类,然后将有关初始化、请求、响应的所有代码移至 AsyncTask 类。\xe0\xb8\x95\xe0\xb8\xad\xe0\xb8\x99\xe0\xb8\xa3\xe0\xb8\xb1\xe0\xb8\x99\xe0\xb8\x95\xe0\xb8\xad\xe0 \xb8\x99\xe0\xb8\x97\xe0\xb9\x89\xe0\xb8\xb2\xe0\xb8\xa2\xe0\xb9\x86\xe0\xb8\x99\xe0\xb9\x89\xe0\xb8 \xb3\xe0\xb8\x95\xe0\xb8\xb2\xe0\xb8\x88\xe0\xb8\xb4\xe0\xb9\x84\xe0\xb8\xab\xe0\xb8\xa5 我的代码有效... TT 并得出结论 sungyeol.jpg.jpg 文件有效

\n\n

例如

\n\n
private void testTask(){\n  .... all code in the main thread particularly on the requests and responses\n  from the services\n //print the response or the result\n //Log.e() makes the message in the android monitor red like an error\n Log.e("Response:", [responseparameter.toString()]);\n\n}\n\n//create the inherited class from the AsyncTask Class\n//(you can create within your activity class)\nclass AsyncTaskRunner extends AsyncTask<String,String,String>{\n    @Override \n    public String doInBackground(String ... input){\n        testTask(); // call the testTask() method that i have created\n        return null; // this override method must return String\n    }\n\n} \n\n//I\'ve created a button for running the task\npublic void buttonTask(View view){\n    AsyncTaskRunner runner = new AsyncTaskRunner();\n    runner.execute();\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

有关 AsyncTask 的更多信息:

\n\n

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

\n\n

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

\n\n

我希望这些有帮助:)

\n