Safa Raffo
Safa Raffo

Reputation: 23

Are the Firebase ML Kit functions asynchronous so that I could run multiple detections using the same detector?

I would like to reuse the same FirebaseVisionTextDetector so that I do not have to create multiple instances of the same object several times. My concern is that if I call detetor.detectInImage(...) on different image bitmaps in a short sequence of time, will the asynchronous properties of the FirebaseVisionTextDetector be able to handle any errors related to this? Or should I be using a different detector for each bitmap?

I am referring to this bit of code in particular, which is part of the Google ML Kit tutorial here:

https://firebase.google.com/docs/ml-kit/android/recognize-text

FirebaseVisionTextDetector detector = FirebaseVision.getInstance().getVisionTextDetector();
Task<FirebaseVisionText> result =
    detector.detectInImage(image)
            .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                @Override
                public void onSuccess(FirebaseVisionText firebaseVisionText) {
                    // Task completed successfully
                    // ...
                }
            })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            // Task failed with an exception
                            // ...
                        }
                    });

As a side note, would someone be able to recommend a way for me to pass the text in the onSuccess method back to the calling method? I'm thinking of using obervables or call back methods.

Upvotes: 2

Views: 1058

Answers (1)

TomH
TomH

Reputation: 2719

Googles Task APIs are by default asynchronous, and the onSuccess or onFailure are callbacks for when they have finished.

You can wait for a task to finish (for where you need it to be synchronous) by calling

Tasks.await(myTask)

Is there a reason you want to pass the text back to the calling method? Can you not just pass it into a new method to do what it is that you need to do?

Upvotes: 1

Related Questions