Friday, March 10, 2023
HomeReactPicture Classification on React Native with TensorFlow.js and MobileNet by Aman Mittal

Picture Classification on React Native with TensorFlow.js and MobileNet by Aman Mittal

Picture Classification on React Native with TensorFlow.js and MobileNet

Revealed on Oct 17, 2019


10 min learn


Lately, the alpha model Tensorflow.js for React Native and Expo functions was launched. It presently supplies the capabilities of loading pre-trained fashions and coaching. Right here is the announcement tweet:

TensorFlow.js supplies many pre-trained fashions that simplify the time-consuming process of coaching a machine studying mannequin from scratch. On this tutorial, we’re going to discover Tensorflow.js and the MobileNet pre-trained mannequin to categorise picture based mostly on the enter picture supplied in a React Native cell utility.

Right here is the hyperlink to the entire code in a Github repo in your reference.



  • Nodejs >= 10.x.x set up in your native dev atmosphere
  • expo-cli
  • Expo Consumer app for Android or iOS, used for testing the app

Integrating TFJS in an Expo app


To begin and use the Tensorflow library in a React Native utility, the preliminary step is to combine the platform adapter. The module tfjs-react-native is the platform adapter that helps loading all main tfjs fashions from the online. It additionally supplies GPU assist utilizing expo-gl.

Open the terminal window, and create a brand new Expo app by executing the command beneath.

expo init mobilenet-tfjs-expo

Subsequent, be sure that to generate Expo managed app. Then navigate contained in the app listing and set up the next dependencies.

yarn add @react-native-community/async-storage

@tensorflow/tfjs @tensorflow/tfjs-react-native

expo-gl @tensorflow-models/mobilenet jpeg-js

Observe: In case you are trying ahead to utilizing react-native-cli to generate an app, you possibly can comply with the clear directions to switch metro.config.js file and different needed steps, talked about right here.

Although you’re utilizing Expo, it’s needed to put in async-storage as tfjs module will depend on that.

Testing TFJS that it’s working


Earlier than we transfer on, allow us to check out that the tfjs is getting loaded into the app earlier than the app is rendered. There’s an asynchronous operate to take action, known as tf.prepared(). Open App.js file, import the required dependencies, and outline an preliminary state isTfReady with a boolean false.

1import React from 'react';

2import { StyleSheet, Textual content, View } from 'react-native';

3import * as tf from '@tensorflow/tfjs';

4import { fetch } from '@tensorflow/tfjs-react-native';


6class App extends React.Element {

7 state = {

8 isTfReady: false

9 };


11 async componentDidMount() {

12 await tf.prepared();

13 this.setState({

14 isTfReady: true

15 });



18 console.log(this.state.isTfReady);

19 }


21 render() {

22 return (

23 <View type={kinds.container}>

24 <Textual content>TFJS prepared? {this.state.isTfReady ? <Textual content>Sure</Textual content> : ''}</Textual content>

25 </View>

26 );

27 }



30const kinds = StyleSheet.create({

31 container: {

32 flex: 1,

33 backgroundColor: '#fff',

34 alignItems: 'middle',

35 justifyContent: 'middle'

36 }



39export default App;

Because the lifecycle technique is asynchronous, it would solely replace the worth of isTfReady to true when tfjs is definitely loaded.

You’ll be able to see the output within the simulator machine as proven beneath.

Or within the console, if utilizing the console assertion because the above snippet.

Loading Tensorflow mannequin


Just like the earlier part, you possibly can load the mannequin getting used on this app (mobilenet) is integrating or not. Loading a tfjs pre-trained mannequin from the online is an costly community name and can take a great period of time. Modify the App.js file to load the MobileNet mannequin. Begin by importing the mannequin.

1import * as mobilenet from '@tensorflow-models/mobilenet';

Subsequent, add one other property to the preliminary state.

1state = {

2 isTfReady: false,

3 isModelReady: false


Then, modify the lifecycle technique.

1async componentDidMount() {

2 await tf.prepared()

3 this.setState({

4 isTfReady: true

5 })

6 this.mannequin = await mobilenet.load()

7 this.setState({ isModelReady: true })


Lastly, the show on the display when the loading of the mannequin is full.

1<Textual content>

2 Mannequin prepared?{' '}

3 {this.state.isModelReady ? <Textual content>Sure</Textual content> : <Textual content>Loading Mannequin...</Textual content>}

4</Textual content>

When the mannequin is being loaded, it would show the next message.

When the loading of the MobileNet mannequin is full, you’re going to get the next output.

Asking consumer permissions


Now that each the platform adapter and the mannequin are presently built-in with the React Native app, add an asynchronous operate to ask for the consumer’s permission to permit entry to the digicam roll. This can be a necessary step when constructing iOS functions utilizing the picture picker element from Expo.

Earlier than, you proceed, run the next command to put in all of the packages supplied by Expo SDK.

expo set up expo-permissions expo-constants expo-image-picker

Subsequent, add the next import statements within the App.js file.

1import Constants from 'expo-constants';

2import * as Permissions from 'expo-permissions';

Within the App class element, add the next technique.

1getPermissionAsync = async () => {

2 if (Constants.platform.ios) {

3 const { standing } = await Permissions.askAsync(Permissions.CAMERA_ROLL);

4 if (standing !== 'granted') {

5 alert('Sorry, we'd like digicam roll permissions to make this work!');

6 }

7 }


Lastly, name this asynchronous technique inside componentDidMount().

1async componentDidMount() {

2 await tf.prepared()

3 this.setState({

4 isTfReady: true

5 })

6 this.mannequin = await mobilenet.load()

7 this.setState({ isModelReady: true })



10 this.getPermissionAsync()

11 }

Convert a uncooked picture right into a Tensor


The appliance would require the consumer to add a picture from their telephone’s digicam roll or gallery. It’s a must to add a handler technique that’s going to load the picture and permit the Tensorflow to decode the info from the picture. Tensorflow helps JPEG and PNG codecs.

Within the App.js file, begin by importing jpeg-js package deal that can be used to decode the info from the picture.

1import * as jpeg from 'jpeg-js';

It decodes the width, top and the binary information from the picture contained in the handler technique imageToTensor that accepts a parameter of the uncooked picture information.

1imageToTensor(rawImageData) {

2 const TO_UINT8ARRAY = true

3 const { width, top, information } = jpeg.decode(rawImageData, TO_UINT8ARRAY)


5 const buffer = new Uint8Array(width * top * 3)

6 let offset = 0

7 for (let i = 0; i < buffer.size; i += 3) {

8 buffer[i] = information[offset]

9 buffer[i + 1] = information[offset + 1]

10 buffer[i + 2] = information[offset + 2]


12 offset += 4

13 }


15 return tf.tensor3d(buffer, [height, width, 3])

16 }

The TO_UINT8ARRAY array represents an array of 8-bit unsigned integers. the constructor technique Uint8Array() is the new ES2017 syntax. There are several types of typed arrays, every having its personal byte vary within the reminiscence.

Load and Classify the picture


Subsequent, we add one other handler technique classifyImage that can learn the uncooked information from a picture and yield outcomes upon classification within the type of predictions.

The picture goes to be learn from a supply and the trail to that picture supply must be saved within the state of the app element. Equally, the outcomes yield by this asynchronous technique should be saved too. Modify the prevailing state within the App.js file for the ultimate time.

1state = {

2 isTfReady: false,

3 isModelReady: false,

4 predictions: null,

5 picture: null


Subsequent, add the asynchronous technique.

1classifyImage = async () => {

2 strive {

3 const imageAssetPath = Picture.resolveAssetSource(this.state.picture);

4 const response = await fetch(imageAssetPath.uri, {}, { isBinary: true });

5 const rawImageData = await response.arrayBuffer();

6 const imageTensor = this.imageToTensor(rawImageData);

7 const predictions = await this.mannequin.classify(imageTensor);

8 this.setState({ predictions });

9 console.log(predictions);

10 } catch (error) {

11 console.log(error);

12 }


The outcomes from the pre-trained mannequin are yield in an array. An instance is proven beneath.

Enable consumer to select the picture


To pick a picture from the machine’s digicam roll utilizing the system’s UI, you’re going to use the asynchronous technique ImagePicker.launchImageLibraryAsync supplied the package deal expo-image-picker. Import the package deal itself.

1import * as Permissions from 'expo-permissions';

Subsequent, add a handler technique selectImage that can be accountable for

  • let the picture to be chosen by the consumer
  • if the picture choice course of shouldn’t be canceled, populate the supply URI object within the state.picture
  • lastly, invoke classifyImage() technique to make predictions from the given enter

1selectImage = async () => {

2 strive {

3 let response = await ImagePicker.launchImageLibraryAsync({

4 mediaTypes: ImagePicker.MediaTypeOptions.All,

5 allowsEditing: true,

6 side: [4, 3]

7 });


9 if (!response.cancelled) {

10 const supply = { uri: response.uri };

11 this.setState({ picture: supply });

12 this.classifyImage();

13 }

14 } catch (error) {

15 console.log(error);

16 }


The package deal expo-image-picker returns an object. In case the consumer cancels the method of choosing a picture, the picture picker module will return a single property: canceled: true. f profitable, the picture picker module returns properties such because the uri of the picture itself. That’s why the if assertion within the above snippet holds a lot significance.

Run the app


To finish this demonstration app, you might want to add a touchable opacity the place the consumer will click on so as to add the picture.

Right here is the entire snippet of the render technique within the App.js file.

1render() {

2 const { isTfReady, isModelReady, predictions, picture } = this.state


4 return (

5 <View type={kinds.container}>

6 <StatusBar barStyle='light-content' />

7 <View type={kinds.loadingContainer}>

8 <Textual content type={kinds.commonTextStyles}>

9 TFJS prepared? {isTfReady ? <Textual content>βœ…</Textual content> : ''}

10 </Textual content>


12 <View type={kinds.loadingModelContainer}>

13 <Textual content type={kinds.textual content}>Mannequin prepared? </Textual content>

14 {isModelReady ? (

15 <Textual content type={kinds.textual content}>βœ…</Textual content>

16 ) : (

17 <ActivityIndicator measurement='small' />

18 )}

19 </View>

20 </View>

21 <TouchableOpacity

22 type={kinds.imageWrapper}

23 onPress={isModelReady ? this.selectImage : undefined}>

24 {picture && <Picture supply={picture} type={kinds.imageContainer} />}


26 {isModelReady && !picture && (

27 <Textual content type={kinds.transparentText}>Faucet to decide on picture</Textual content>

28 )}

29 </TouchableOpacity>

30 <View type={kinds.predictionWrapper}>

31 {isModelReady && picture && (

32 <Textual content type={kinds.textual content}>

33 Predictions: {predictions ? '' : 'Predicting...'}

34 </Textual content>

35 )}

36 {isModelReady &&

37 predictions &&

38 => this.renderPrediction(p))}

39 </View>

40 <View type={kinds.footer}>

41 <Textual content type={kinds.poweredBy}>Powered by:</Textual content>

42 <Picture supply={require('./belongings/tfjs.jpg')} type={kinds.tfLogo} />

43 </View>

44 </View>

45 )

46 }


Right here is the checklist of the entire kinds object.

1const kinds = StyleSheet.create({

2 container: {

3 flex: 1,

4 backgroundColor: '#171f24',

5 alignItems: 'middle'

6 },

7 loadingContainer: {

8 marginTop: 80,

9 justifyContent: 'middle'

10 },

11 textual content: {

12 shade: '#ffffff',

13 fontSize: 16

14 },

15 loadingModelContainer: {

16 flexDirection: 'row',

17 marginTop: 10

18 },

19 imageWrapper: {

20 width: 280,

21 top: 280,

22 padding: 10,

23 borderColor: '#cf667f',

24 borderWidth: 5,

25 borderStyle: 'dashed',

26 marginTop: 40,

27 marginBottom: 10,

28 place: 'relative',

29 justifyContent: 'middle',

30 alignItems: 'middle'

31 },

32 imageContainer: {

33 width: 250,

34 top: 250,

35 place: 'absolute',

36 high: 10,

37 left: 10,

38 backside: 10,

39 proper: 10

40 },

41 predictionWrapper: {

42 top: 100,

43 width: '100%',

44 flexDirection: 'column',

45 alignItems: 'middle'

46 },

47 transparentText: {

48 shade: '#ffffff',

49 opacity: 0.7

50 },

51 footer: {

52 marginTop: 40

53 },

54 poweredBy: {

55 fontSize: 20,

56 shade: '#e69e34',

57 marginBottom: 6

58 },

59 tfLogo: {

60 width: 125,

61 top: 70

62 }


Run the applying by executing the expo begin command from a terminal window. The very first thing you’ll discover is that upon bootstrapping the app within the Expo consumer, it would ask for permissions.

Then, as soon as the mannequin is prepared, it would show the textual content “Faucet to decide on picture” contained in the field. Choose a picture to see the outcomes.

Predicting outcomes can take a while. Listed below are the outcomes of the beforehand chosen picture.



I hope this submit serves the aim of providing you with a head begin in understanding the right way to implement a TesnorFlow.js mannequin in a React Native app, in addition to a greater understanding of picture classification, a core use case in pc vision-based machine studying.

Because the TF.js for React Native is in alpha on the time of penning this submit, we will hope to see many extra superior examples sooner or later to construct real-time functions.
Listed below are some sources that I discover extraordinarily helpful.

Listed below are some sources that I discover extraordinarily helpful.

You will discover the entire code at this Github repo.

Initially revealed at

I am a software program developer and a technical author. On this weblog, I write about Technical writing, Node.js, React Native and Expo.

At the moment, working at Expo. Beforehand, I’ve labored as a Developer Advocate, and Senior Content material Developer with corporations like Draftbit, Vercel and Crowdbotics.


Most Popular

Recent Comments