React Native 的 TensorFlow Lite 組件

這是 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 安裝,按以下步驟初始化后安裝:
  1. 初始化 Pod 文件:
    cd ios
    pod init
    
  2. 打開 Podfile 添加:
    target '[your project's name]' do
        pod 'TensorFlowLite', '1.12.0'
    end
    
  3. 安裝:
    pod install
    

手動 Link

react-native link 不好使的情況試一下手動添加:

iOS

  1. 打開 XCode 的 project navigator,右擊 Libraries ? Add Files to [項目名稱]
  2. 找到 node_modules ? tflite-react-native 然后選擇 TfliteReactNative.xcodeproj
  3. 打開 XCode 的 project navigator,選中你的項目,添加 libTfliteReactNative.a 到項目的 Build Phases ? Link Binary With Libraries

Android

  1. 打開 android/app/src/main/java/[...]/MainApplication.java
    - 添加 import com.reactlibrary.TfliteReactNativePackage; 到文件開頭的 imports 中
    - 添加 new TfliteReactNativePackage()getPackages() 方法的列表中
  2. android/settings.gradle 中添加以下內(nèi)容 :
    include ':tflite-react-native'
    project(':tflite-react-native').projectDir = new File(rootProject.projectDir,   '../node_modules/tflite-react-native/android')
    
  3. android/app/build.gradle 文件的 dependencies 中添加:
    compile project(':tflite-react-native')
    

添加模型到項目

iOS

打開 XCode, 右擊項目文件夾,點擊 Add Files to "項目名稱"...,選擇模型文件和標(biāo)簽文件。

Android

  1. 打開 Android Studio,右擊 app 文件夾,然后 New > Folder > Assets Folder。點擊 Finish 后生成 assets 文件夾。

  2. 將模型和標(biāo)簽文件復(fù)制到 app/src/main/assets。

  3. 打開 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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 原文地址:https://medium.com/airbnb-engineering/react-native-a...
    莫寂嵐閱讀 3,589評論 0 9
  • 持續(xù)更新中...... 一套企業(yè)級的 UI 設(shè)計語言和 React 實現(xiàn)。 https://mobile.ant....
    日不落000閱讀 6,051評論 0 35
  • 白瑩槐香入口甜,三五成群采摘忙。 錦繡風(fēng)雷夢里來,他年身影其間找。
    小蜜蜂_2941閱讀 415評論 0 0
  • 姓名:巢環(huán)環(huán) 公司:寧波大發(fā)化纖有限公司 期數(shù):第264期六項精進 組名:努力一組 【日精進打卡第12天】共12天...
    巢環(huán)環(huán)閱讀 183評論 0 0
  • 有時候面對一個人渣,只能忍耐,因為顧忌太多,怕影響不好,但是卻對自己造成內(nèi)傷,心理上備受傷害。 一直以來,總是提醒...
    xianma123456閱讀 408評論 1 3

友情鏈接更多精彩內(nèi)容