Unable to use MlImageAdapter in Android (Java)

I am refactoring my codebase so that I can use MLImage for all the models that I am using (MLKit PoseDetection and a custom detection model TFLite). This was a success, however, I noticed that Google just introduced MlImageAdapter that basically converts an MLimage to TensorImage as the Tflite model requires the image in TensorImage format.

A code snippet from one of their latest commits is below:

public List<Detection> detect(MlImage image, ImageProcessingOptions options) {
    image.getInternal().acquire();
    TensorImage tensorImage = MlImageAdapter.createTensorImageFrom(image);
    List<Detection> result = detect(tensorImage, options);
    image.close();
    return result;
  }

indent preformatted text by 4 spaces

But as I try to use MlImageAdapter in my code base, it is unable to import it. And the dependencies in their BUILD file is puzzling for me and so I cannot add relevant dependencies in gradle file.

The link is below:

Any help would be awesome. Thanks!!!

Hi @Sohail_Zia

Provide us the dependencies you have used for the android project. Take a look here where you can find latest and there if you want to use nightly builds.

Use:

    mavenCentral  // should be already there
    maven {       // add this repo to use snapshots
      name 'ossrh-snapshot'
      url 'http://oss.sonatype.org/content/repositories/snapshots'
    }
}

at project’s build gradle file and:

implementation("org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly-SNAPSHOT")

at app’s build.gradle file

Get back with some feedback on the solution. It will be helpful for the TFLite team

Best

Currently, I am using the following,

implementation 'org.tensorflow:tensorflow-lite:2.5.0'
implementation 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly'
implementation 'org.tensorflow:tensorflow-lite-support:0.2.0'

I tried adding implementation("org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly-SNAPSHOT") , but It didn’t work. Probably it’s caching the 0.2.0 which shadows 0.0.0-nightly. I also tried 0.0.0-nightly-SNAPSHOT!! to make it strict, but this is also won’t do.

I see that MLImageAdapter is currently in snapshot build. So, if you guys have any plans to add this in stable build, then I can wait till then.

Super weird!
Can you upload your code to github to reproduce?

I can’t share the code as it’s a private repo. But I can share screenshots.

https://drive.google.com/drive/folders/13BpwH-zom-GF-G-r3e5FPgIaj-1pLqqK?usp=sharing

BTW I heard that you guys will be releasing this in a stable minor version this quarter. So, I can probably wait till then. Not a big deal.

Hi again
It seems that it works for me

1 Like

I will upload some screenshots


and

1 Like

Check again your dependencies…then rebuild project

https://drive.google.com/file/d/1gfBjfEw8EMkWzALad9dAoBjiHTxkKghm/view?usp=sharing

IT WORKS NOW! But i had to comment out my other tensorflow related dependencies

Everything is perfect now. I updated my dependencies to the following:

implementation "org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly-SNAPSHOT"
implementation 'org.tensorflow:tensorflow-lite:2.5.0'
implementation 'org.tensorflow:tensorflow-lite-gpu:2.5.0'
2 Likes

Thanks a lot for you help, George! You are awesome!

1 Like

Happy to help! Happy coding.