googlesamples / mlkit

A collection of sample apps to demonstrate how to use Google's ML Kit APIs on Android and iOS
Apache License 2.0
3.51k stars 2.92k forks source link

Multiple detections at once? #376

Closed utsavDave97 closed 2 years ago

utsavDave97 commented 2 years ago

Hello,

Could you guys please give an example where BarcodeScanner and TextRecognizer can be used at the same time for below statement in MLKit.

GMV provides MultiDetector, MultiProcessor and FocusingProcessor frameworks for performing multiple detections and results filtering. ML Kit does not provide such mechanisms, but the same functionality can be implemented by the developer if desired.

bcdj commented 2 years ago

Do you work on Android or iOS? We don't provide example on using BarcodeScanner and TextRecognizer at the same time, but we provide examples of calling these APIs separately:

https://github.com/googlesamples/mlkit

You can easily chain two APIs together yourself.

utsavDave97 commented 2 years ago

It is for Android. I did see the code samples for the link you provided. But I was just wondering as to whether MLKit has something like MultiDetector, MultiProcessor which GMV used to provide.

You can easily chain two APIs together yourself.

Would it possible for you to maybe just add a code snippet or something?

Appreciate your response.

zhouyiself commented 2 years ago

Sorry for the late reply.

You can use Tasks.whenAllComplete API to chain #process from multiple detectors. Here's a sample for how to chain barcode scanner and text recognizer:

      BarcodeScanner scanner = BarcodeScanning.getClient();
      TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);
      Tasks.whenAllComplete(scanner.process(inputImage), recognizer.process(inputImage))
          .addOnSuccessListener(
              tasks -> {
                for (Task<?> task : tasks) {
                  Object taskResult = task.getResult();
                  if (taskResult instanceof Text) {
                    Text text = (Text) taskResult;
                    // Handle text result.

                  } else if (taskResult instanceof List<?>) {
                    List<?> resultList = (List<?>) taskResult;
                    if (!resultList.isEmpty() && resultList.get(0) instanceof Barcode) {
                      List<Barcode> barcodeList = (List<Barcode>) resultList;
                      // Handle barcode list result.
                    }
                  }
                }
              });

Although the check is bit tricky here if the detector is returning a list because instanceof can only be used to check a list with generic type.

utsavDave97 commented 2 years ago

Appreciate your response @zhouyiself . I got the idea from your code and will close this now.