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

1

7

I have tried so many way but i can't succeed. I haven't found any source code examples for Android(about rekognition)

there's a source code in JAVA in the Developer Guide but i cannot implement that even though I tried TT

I try to detect faces by sending an image file from an external storage(from the emulator) I don't know what i did wrong(I'm not good at coding) Here is my code

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());
 }

and here is my errors

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

what is a null object reference? i try to change the file path but he said no such file ... and when I change to this path, there's errors above. by the way I've already asked a user for a permission to access a folder from Emulator in Android

please help me PS. sorry for my bad English

Thank you in advance.

Softpedal answered 4/2, 2017 at 14:48 Comment(0)
S
3

Now I am ok with the issues. I have been through many many things <3 <3 <3. Thank you

I'm Thai and I had to try harder to find the solutions because there's lack of information in the particular language. Here are my solutions.

My solutions are:

0.There is an endpoint for setting for the Rekognition--> http://docs.aws.amazon.com/general/latest/gr/rande.html#rekognition_region

1.On a "null object reference issue" I found that I have to create a new object first such as "Image image = new Image();" <-- The "new" command creates an object instance in that class

2.After the above error, there are more errors (Errors on NetworkOnMainThreadException), so I tried everything until I found this page --> https://docs.aws.amazon.com/cognito/latest/developerguide/getting-credentials.html the page said that ...

enter image description here

Consequently, I looked up for more information about the AsyncTask and after that I created an AsyncTask class and then I move all my code about the initialize, the request, the response to the AsyncTask class. ตอนรันตอนท้ายๆน้ำตาจิไหล my code worked... TT and by the conclusion the sungyeol.jpg.jpg file worked

for example

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();

}

for more information about the 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

I hope these help :)

Softpedal answered 6/2, 2017 at 9:25 Comment(3)
It will be helpful if you can you post your solution, I'm in same situation.Berwickupontweed
I have edited my post for more information, I hope these can help you.Softpedal
@ShivarajPatil पाटील, सोल्युशन द्या कि आम्हाला पणUndershorts

© 2022 - 2024 — McMap. All rights reserved.