這是 flutter_tflite 移植的 React Native 版本,使用最新的 TensorFlow Lite 1.12.0。支持:
- 圖像檢測
- 目標(biāo)識別(可選 SSD MobileNet 或 Tiny YOLOv2)
項目地址:https://github.com/shaqian/tflite-react-native

yolo.jpg
安裝
npm install tflite-react-native --save
react-native link react-native-ssh-sftp
- iOS 端 TensorFlow Lite 需要用 Pod 安裝,按以下步驟初始化后安裝:
- 初始化 Pod 文件:
cd ios pod init - 打開 Podfile 添加:
target '[your project's name]' do pod 'TensorFlowLite', '1.12.0' end - 安裝:
pod install
手動 Link
react-native link 不好使的情況試一下手動添加:
iOS
- 打開 XCode 的 project navigator,右擊
Libraries?Add Files to [項目名稱] - 找到
node_modules?tflite-react-native然后選擇TfliteReactNative.xcodeproj - 打開 XCode 的 project navigator,選中你的項目,添加
libTfliteReactNative.a到項目的Build Phases?Link Binary With Libraries
Android
- 打開
android/app/src/main/java/[...]/MainApplication.java
- 添加import com.reactlibrary.TfliteReactNativePackage;到文件開頭的 imports 中
- 添加new TfliteReactNativePackage()到getPackages()方法的列表中 - 在
android/settings.gradle中添加以下內(nèi)容 :include ':tflite-react-native' project(':tflite-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/tflite-react-native/android') - 在
android/app/build.gradle文件的 dependencies 中添加:compile project(':tflite-react-native')
添加模型到項目
iOS
打開 XCode, 右擊項目文件夾,點擊 Add Files to "項目名稱"...,選擇模型文件和標(biāo)簽文件。
Android
打開 Android Studio,右擊
app文件夾,然后 New > Folder > Assets Folder。點擊 Finish 后生成 assets 文件夾。將模型和標(biāo)簽文件復(fù)制到
app/src/main/assets。打開
android/app/build.gradle,在android塊中加入以下設(shè)定。
aaptOptions {
noCompress 'tflite'
}
使用方法介紹
初始化
import Tflite from 'tflite-react-native';
let tflite = new Tflite();
導(dǎo)入模型和標(biāo)簽
tflite.loadModel({
model: 'models/mobilenet_v1_1.0_224.tflite',// 模型文件,必填
labels: 'models/mobilenet_v1_1.0_224.txt', // 標(biāo)簽文件,必填
numThreads: 1, // 線程數(shù),默認(rèn)為 1
},
(err, res) => {
if(err)
console.log(err);
else
console.log(res);
});
圖像檢測
tflite.runModelOnImage({
path: imagePath, // 圖像文件地址,必填
imageMean: 128.0, // mean,默認(rèn)為 127.5
imageStd: 128.0, // std,默認(rèn)為 127.5
numResults: 3, // 返回結(jié)果數(shù),默認(rèn)為 5
threshold: 0.05 // 可信度閾值,默認(rèn)為 0.1
},
(err, res) => {
if(err)
console.log(err);
else
console.log(res);
});
- 輸出格式:
{
index: 0,
label: "person",
confidence: 0.629
}
目標(biāo)檢測:
- SSD MobileNet
tflite.detectObjectOnImage({
path: imagePath,
model: 'SSDMobileNet',
imageMean: 127.5,
imageStd: 127.5,
threshold: 0.3, // 可信度閾值,默認(rèn)為 0.1
numResultsPerClass: 2,// 單個類別的返回結(jié)果數(shù), 默認(rèn)為 5
},
(err, res) => {
if(err)
console.log(err);
else
console.log(res);
});
- Tiny YOLOv2
tflite.detectObjectOnImage({
path: imagePath,
model: 'YOLO',
imageMean: 0.0,
imageStd: 255.0,
threshold: 0.3, // 可信度閾值,默認(rèn)為 0.1
numResultsPerClass: 2, // 單個類別的返回結(jié)果數(shù), 默認(rèn)為 5
anchors: [...], // 默認(rèn)為 [0.57273,0.677385,1.87446,2.06253,3.33843,5.47434,7.88282,3.52778,9.77052,9.16828]
blockSize: 32, // 默認(rèn)為 32
},
(err, res) => {
if(err)
console.log(err);
else
console.log(res);
});
- 輸出格式:
{
detectedClass: "hot dog",
confidenceInClass: 0.123,
rect: {
x: 0.15,
y: 0.33,
w: 0.80,
h: 0.27
}
}
完整的例子
https://github.com/shaqian/tflite-react-native/tree/master/example