Search code examples
androidc++qttensorflowtensorflow-lite

Use tensorflow lite with Qt


Environment :

  • OS : Win10 64bits
  • Qt: 5.12.3
  • NDK: r18b
  • JDK: jdk1.8.0_201

I would like to use the tensorflow-lite with Qt5, but there are lots of issues when I try to import the java classes. But how could I download the tensorflow-lite-gpu, tensorflow-lite-cpu and tensorflow-lite-support?

The android studio make this work with 3 lines in the build.gradle, I try to add the 3 lines into the build.gradle too.

dependencies {
    // Build off of nightly TensorFlow Lite
    implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
    implementation 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly'
    implementation 'org.tensorflow:tensorflow-lite-support:0.0.0-nightly'    
}

Error messages

At pastebin since it is long

I can build this example by android studio without any issues, how could I use the java api of tensorflow with Qt?


Solution

  • The answer is quite simple, only need to add a few lines into the build.gradle(from QtCreator, projects(alt+x)->other files->build.gradle)

    1. Put following lines in the "android" block of build.gradle

       compileOptions {
         sourceCompatibility = '1.8'
         targetCompatibility = '1.8'
       }
      
       aaptOptions {
         noCompress "tflite"
       }
      
    2. Change dependencies block(not the one in the buildscript) of the build.gradle to

      dependencies {
         implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
      
         // Build off of nightly TensorFlow Lite
         implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
         implementation 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly'
         implementation 'org.tensorflow:tensorflow-lite-support:0.0.0-nightly' 
      }
      

    This should do the job, the complete build.gradle looks like

    buildscript {
        repositories {
            google()
            jcenter()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:3.2.0'
        }
    }
    
    repositories {
        google()
        jcenter()
    }
    
    apply plugin: 'com.android.application'
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    
        // Build off of nightly TensorFlow Lite
        implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
        implementation 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly'
        implementation 'org.tensorflow:tensorflow-lite-support:0.0.0-nightly'
        // Use local TensorFlow library
        // implementation 'org.tensorflow:tensorflow-lite-local:0.0.0'
    }
    
    android {
        /*******************************************************
         * The following variables:
         * - androidBuildToolsVersion,
         * - androidCompileSdkVersion
         * - qt5AndroidDir - holds the path to qt android files
         *                   needed to build any Qt application
         *                   on Android.
         *
         * are defined in gradle.properties file. This file is
         * updated by QtCreator and androiddeployqt tools.
         * Changing them manually might break the compilation!
         *******************************************************/
    
        compileSdkVersion androidCompileSdkVersion.toInteger()
    
        buildToolsVersion androidBuildToolsVersion
    
        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = [qt5AndroidDir + '/src', 'src', 'java']
                aidl.srcDirs = [qt5AndroidDir + '/src', 'src', 'aidl']
                res.srcDirs = [qt5AndroidDir + '/res', 'res']
                resources.srcDirs = ['src']
                renderscript.srcDirs = ['src']
                assets.srcDirs = ['assets']
                jniLibs.srcDirs = ['libs']
           }
        }
    
        compileOptions {
            sourceCompatibility = '1.8'
            targetCompatibility = '1.8'
        }
    
        aaptOptions {
            noCompress "tflite"
        }
    
        lintOptions {
            abortOnError false
        }
    }