Unlocking the Power of Object Classification with TensorFlow.js and React Native

Object classification, a fundamental application of machine learning, has become an essential tool in various industries due to the rapid growth of data generation and collection. It involves identifying and labeling objects in images or videos, with applications in autonomous driving, security systems, medical imaging, scientific research, and more.

Getting Started with TensorFlow.js and React Native

To build an object classification app, we’ll utilize TensorFlow.js, a lightweight library that brings the power of machine learning to JavaScript, and React Native, a popular framework for building cross-platform mobile applications.

Prerequisites

Before we begin, ensure you have Node.js and npm installed locally on your system. You’ll also need an Android or iOS phone or emulator to test the application during development. Additionally, consider using Expo to simplify the development process.

Creating a New React Native Project

Use the Expo CLI to create a new React Native project by running the following command:


npx expo init my-app

Once the project is initialized, add the required dependencies to your package.json file:


"dependencies": {
"expo": "^43.0.0",
"expo-image-picker": "^10.2.2",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-native": "0.68.2",
"@tensorflow/tfjs": "^3.11.0",
"@tensorflow/tfjs-react-native": "^0.6.1"
}

Update your app.json file to configure the dependencies correctly:


{
"expo": {
"name": "my-app",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": [
"**/*"
],
"ios": {
"supportsTablet": true
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#ffffff"
}
},
"web": {
"favicon": "./assets/favicon.png"
}
}
}

Setting Up the Home Screen

Create a basic UI that allows users to pick images from their gallery for classification and display the result when done. Add the following code to your App.tsx file:

import React, { useState, useEffect } from 'eact';
import { View, Image, Button, Text } from 'eact-native';
import * as ImagePicker from 'expo-image-picker';

const App = () => {
  const [image, setImage] = useState(null);

const pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing: true,
      aspect: [4, 3],
      quality: 1,
    });

<pre><code>if (!result.cancelled) {
  setImage(result.uri);
}
</code></pre>

};

return (
    <View>
      <Button title="Pick an image" onPress={pickImage} />
      {image && <Image source={{ uri: image }} style={{ width: 200, height: 200 }} />}
    </View>
  );
};

export default App;

<p><strong>Creating an Object Classification App with MobileNet</strong></p>

<p>MobileNet is a pre-trained machine learning model that helps classify objects. It offers low-performance overhead but sacrifices accuracy. Import MobileNet into your <code>App.tsx</code> file:</p>

<p>“`
import * as tf from ‘@tensorflow/tfjs’;
import { MobileNet } from ‘@tensorflow/tfjs-react-native’;

const mobileNet = new MobileNet();




Create a function that prepares the selected image for processing and calls the model to classify the image data: const classifyImage = async () => { const imageTensor = tf.tensor3d(imageData, [1, 224, 224, 3]); const output = await mobileNet.classify(imageTensor); console.log(output); }; Set up the function to be called every time the user picks a new image using the useEffect Hook: useEffect(() => { classifyImage(); }, [image]); Creating an Object Classification App with Coco SSD Coco SSD is a pre-trained object detection model that can identify multiple objects from a single image. Import Coco SSD into your App.tsx file: ``` import * as tf from '@tensorflow/tfjs'; import { CocoSSD } from '@tensorflow/tfjs-react-native'; const cocoSSD = new CocoSSD();

Create a function that processes the selected image and classifies it using the Coco SSD model:


const classifyImage = async () => {
const imageTensor = tf.tensor3d(imageData, [1, 224, 224, 3]);
const output = await cocoSSD.detect(imageTensor);
console.log(output);
};

Update the useEffect Hook to call this function instead of the MobileNet function:


useEffect(() => {
classifyImage();
}, [image]);

With these steps, you’ve successfully built an object classification app using TensorFlow.js and React Native. You can now run and test the app on your mobile device or emulator.

Leave a Reply