搭建Tensorflow Lite项目
新建一个安卓工程后,将项目目录的展示方式设置成“Project”模式,会看到两个同名的gradle文件,对这两个文件进行修改。
修改项目的build.gradle文件
buildscript {
repositories {
//修改1
google()
jcenter()
mavenLocal()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
//修改2
google()
jcenter()
mavenLocal()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
修改app的build.gradle文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "com.example.word_ocr"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// 修改1 设置不编译模型文件
aaptOptions {
noCompress "tflite"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.android.material:material:1.2.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//修改2 添加TensorFlow Lite依赖库
implementation('org.tensorflow:tensorflow-lite:0.0.0-nightly') { changing = true }
implementation('org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly') { changing = true }
implementation('org.tensorflow:tensorflow-lite-support:0.0.0-nightly') { changing = true }
}
以上操作时,注意保持网路通畅,不要使用代理。
添加模型文件
在src/main目录下新建一个assets目录,将转换好的model.tflite文件放到这个目录下。
加载tflite模型文件
可以参考这篇文章:Tensorflow Lite初探(Android)
模型的输入输出对应:DataType error: cannot resolve DataType of [[[D,
Android——Tensorflow-Lite简单使用
Android加载tensorflow模型文件(.tflite)
手写数字识别–Android Studio 加载tensorflow模型
参考:
使用tensorflow lite遇到的一些坑
AS build.gradle——tensorflow-lite官方项目两个build.gradle详解以及无法下载依赖的解决方法
Android加载tensorflow模型文件(.tflite)