JSPM

react-native-tensorflow-lite

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 33
  • Score
    100M100P100Q58017F

A react native library for running Tensorflow Lite on Android

Package Exports

  • react-native-tensorflow-lite

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-native-tensorflow-lite) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

react-native-tensorflow-lite

Getting started

$ npm install react-native-tensorflow-lite --save

Mostly automatic installation

$ react-native link react-native-tensorflow-lite

Manual installation

Android

  1. Open up android/app/src/main/java/[...]/MainActivity.java
  • Add import com.reactlibrary.RNTensorflowLitePackage; to the imports at the top of the file
  • Add new RNTensorflowLitePackage() to the list returned by the getPackages() method
  1. Add the following lines to your app's build.gradle(android/app/build.gradle):
    android {
      aaptOptions {
         noCompress 'tflite'
         noCompress 'lite'
      }
     }

Usage

Place your tflite model file and labels.txt in your app's asset folder.

import TFLiteImageRecognition from 'react-native-tensorflow-lite';

// Initialize Tensorflow Lite Image Recognizer
class MyImageClassifier extends Component {

  constructor() {
    super()
    this.state = {}

    try {
      this.classifier = new TFLiteImageRecognition({
        model: "mymodel.tflite",  // Your tflite model in assets folder.
        labels: "label.txt" // Your label file
      })

    } catch(err) {
      alert(err)
    }
  }

  componentWillMount() {
    try {
      const results = await this.classifier.recognize({
        image: "apple.jpg", // Your image path.
        inputShape: 224, // the input shape of your model. If none given, it will be default to 224.
      })

      const resultObj = {name: "Name: " + results[0].name,  confidence: "Confidence: " + results[0].confidence, inference: "Inference: " + results[0].inference + "ms"};
      if(justReturn == false) {
        this.setState(resultObj)
      } else {
        return resultObj;
      }
      
    } catch(err) {
      alert(err)
    }   
  }
  
  componentWillUnmount() {
    this.classifier.close() // Must close the classifier when destroying or unmounting component to release object.
  }

  render() {
    return (
      <View style={styles.container}>
        <View>
          <Text style={styles.results}>
            {this.state.name}
          </Text>
          <Text style={styles.results}>
            {this.state.confidence}
          </Text>
          <Text style={styles.results}>
            {this.state.inference}
          </Text>
        </View>
      </View>
    );
  }
}