百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-native-tensorflow

> 前端框架
开源

一个用于 React Native 的 TensorFlow 推理库

358 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个用于 React Native 的 TensorFlow 推理库

react-native-tensorflow

Note: This project is not maintained anymore

A TensorFlow inference library for react native. It follows the android inference api from TensorFlow: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/android

Getting started

$ npm install react-native-tensorflow --save

Linking

$ react-native link react-native-tensorflow

Additional steps for iOS

For the iOS setup you will need CocoaPods.

Create a Podfile in the iOS directory with the following content:

target ''
       pod 'TensorFlow-experimental'

Then run pod install.

Usage

This library provides a api to directly interact with TensorFlow and a simple image recognition api. For most use cases for image recognition the image recognition api should suffice.

Image recognition

First you need to add the TensorFlow model as well as the label file to the project. There are a few ways to do that as described here

Next you need to initialize the TfImageRecognition class using the model and label files and then call the recognize function of the class with the image to recognize:

…

Direct API

Note: It is not recommended to use this API as it has some major problem described in the second point in the known issues and is quite difficult to use in its current state.

First you need to add the TensorFlow model to the project. There are a few ways to do that as described here

After adding the model and creating a TensorFlow instance using the model you will need to feed your data as a array providing the input name, shape and data type. Then run the inference and lastly fetch the result.

javascript
import { TensorFlow } from 'react-native-tensorflow';

const tf = new TensorFlow('tensorflow_inception_graph.pb')
await tf.feed({name: "inputName", data: [1,2,3], shape:[1,2,4], dtype: "int64"})
await tf.run(['outputNames'])
const output = await tf.fetch('outputName')    
console.log(output)

Check the android TensorFlow example for more information on the API: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowImageClassifier.java

Fetching files

  • Add as react native asset

Create the file rn-cli.config.js in the root of the project and add the following code where the array contains all the file endings you want to bundle (in this case we bundle pb and txt files next to the defaults).

module.exports = {
  getAssetExts() {
    return ['pb', 'txt']
  }
}

Then you can require the asset in the code, for example: require('assets/tensorflow_inception_graph.pb')

  • Add as iOS / Android asset

Put the file in the android/src/main/assets folder for Android and for iOS put the file, using XCode, in the root of the project. In the code you can just reference the file path for the asset.

  • Load from file system

Put the file into the file system and reference using the file path.

  • Fetch via url

Pass a url to fetch the file from a url. This won't store it locally, thus the next time the code is executed it will fetch it again.

Supported data types

  • DOUBLE
  • FLOAT
  • INT32
  • INT64
  • UINT8
  • BOOL - On Android will be converted into a byte array
  • STRING - On Android will be converted into a byte array

Known issues

  • When using the image recognition api the results don't match exactly between Android and iOS. Most of the time they seem reasonable close though.
  • When using the direct api the data to feed to TensorFlow needs to be provided on the JS side and is then passed to the native side. Transferring large payloads this way is very inefficient and will likely have a negative performance impact. The same problem exists when loading large data, like images, from the native side into JS side for processing.
  • The TensorFlow library itself as well as the TensorFlow models are quite large in size resulting in large builds.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javaandroidmobilereact-nativetensorflow

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架