React Native AI Image Recognition and Suggestions

With the advancement of AI and machine learning, integrating image recognition features into React Native applications has become easier than ever. In this blog, we will explore how to implement AI-powered image recognition in a React Native app and generate suggestions based on the detected objects.

Advantages of AI Image Recognition in React Native

Enhanced User Experience – AI-driven image recognition enables intuitive interactions by recognizing and categorizing objects in real-time.

▪️Automation – Reduces manual effort by automatically tagging and suggesting relevant items.

▪️Improved Accessibility – Helps visually impaired users by describing images.

▪️Business Insights – Useful in e-commerce for product recommendations and in security for surveillance.

▪️Cost Efficiency – Reduces reliance on manual data entry and human intervention.

When to Use AI Image Recognition

▪️E-commerce Apps: Auto-tagging products in images.

▪️Healthcare Apps: Identifying medical conditions from images.

▪️Security Apps: Recognizing faces, objects, or anomalies in surveillance footage.

▪️Social Media Apps: Categorizing and filtering uploaded content.

▪️Travel & Navigation Apps: Identifying landmarks and providing information.

Benefits of Firebase ML Kit for Image Recognition

▪️Cloud-based & On-device APIs: Offers both cloud-based and offline models.

▪️High Accuracy: Uses Google’s advanced machine learning models.

▪️Easy Integration: Works seamlessly with React Native.

▪️Scalability: Efficiently handles large datasets and multiple users.

Prerequisites

Before we start, ensure you have the following:

▪️Node.js installed

▪️React Native 0.76 environment set up

▪️Firebase account (for ML Kit) or TensorFlow.js

▪️Android Studio / Xcode for emulator testing

Project Folder Structure

ImageRecognitionApp/
│── android/ # Android project files
│── ios/ # iOS project files
│── src/ # Main source code
│ │── components/ # UI components
│ │── screens/ # Screens (CameraScreen, RecognitionScreen)
│ │── App.js # Entry point of the app
│── package.json # Dependencies and scripts
│── metro.config.js # Metro bundler configuration
│── babel.config.js # Babel configuration
│── index.js # Entry file

Setting Up the Project

Run the following command to create a new React Native project:

npx react-native@latest init ImageRecognitionApp
cd ImageRecognitionApp

Installing Dependencies

We will use react-native-vision-camera for capturing images and Firebase ML Kit for image recognition.

npm install react-native-vision-camera
npm install @react-native-firebase/app @react-native-firebase/ml

For iOS, run:

cd ios && pod install

Configuring Firebase

▪️Go to Firebase Console

▪️Create a new project

▪️Add your app (Android/iOS)

▪️Download and place google-services.json (Android) or GoogleService-Info.plist (iOS) in your project

▪️Enable ML Kit’s Vision API

Implementing Camera Capture

We will use react-native-vision-camera to take pictures.

Requesting Camera Permission

Ensure the app requests the necessary permissions.

import React, {useEffect} from 'react';
import {createStackNavigator} from '@react-navigation/stack';
import {NavigationContainer} from '@react-navigation/native';
import {useCameraPermission} from 'react-native-vision-camera';
import CameraScreen from './src/screens/CameraScreen';
import RecognitionScreen from './src/screens/RecognitionScreen';

type RootStackParamList = {
Camera: undefined;
Recognition: {imageUri: string}; // Ensure this matches the parameter you're passing
};

const Stack = createStackNavigator<RootStackParamList>();

const App = () => {
const {hasPermission, requestPermission} = useCameraPermission();

useEffect(() => {
  if (!hasPermission) {
    requestPermission();
  }
}, [hasPermission, requestPermission]);

return (
  <NavigationContainer>
    <Stack.Navigator initialRouteName="Camera">
      <Stack.Screen name="Camera" component={CameraScreen} />
      <Stack.Screen name="Recognition" component={RecognitionScreen} />
    </Stack.Navigator>
  </NavigationContainer>
);
};

export default App;

Explanation

▪️React, useEffect: React core functionality and lifecycle method.

▪️CreateStackNavigator, NavigationContainer: Used to handle navigation between screens.

▪️UseCameraPermission: Hook from react-native-vision-camera to manage camera permissions.

▪️CameraScreen, RecognitionScreen: The two screens in our app.

▪️RootStackParamList: Defines the types of screen navigation parameters:

▪️Camera: No parameters.

▪️Recognition: Accepts an imageUri parameter (string).

▪️CreateStackNavigator<RootStackParamList>(): Creates the stack navigator and ensures correct TypeScript type checking.

▪️HasPermission: Checks if the app has camera access.

▪️RequestPermission(): Requests camera access if not granted.

▪️UseEffect(): Runs the permission request logic when the app starts.

▪️NavigationContainer: Wraps the navigation stack.

▪️Stack.Navigator: Starts with the CameraScreen (initialRouteName=”Camera”).

▪️Navigates to RecognitionScreen when a picture is taken.

Capturing an Image

import React, {useRef} from 'react';
import {View, Text, TouchableOpacity, StyleSheet} from 'react-native';
import {Camera, useCameraDevice} from 'react-native-vision-camera';
import {useNavigation} from '@react-navigation/native';

const CameraScreen: React.FC = () => {
const navigation = useNavigation();
const cameraRef = useRef<Camera>(null);
const device = useCameraDevice('back');

const takePicture = async () => {
  if (cameraRef.current) {
    const photo = await cameraRef.current.takePhoto();
    navigation.navigate('Recognition', {imageUri: photo.path});
  }
};

return (
  <View style={styles.container}>
    {device && (
      <Camera
        ref={cameraRef}
        style={styles.camera}
        device={device}
        isActive={true}
        photo={true}
      />
    )}
    <TouchableOpacity onPress={takePicture} style={styles.button}>
      <Text style={styles.buttonText}>Take Picture</Text>
    </TouchableOpacity>
  </View>
);
};

const styles = StyleSheet.create({
container: {
  flex: 1,
  justifyContent: 'center',
  alignItems: 'center',
},
camera: {
  flex: 1,
},
button: {
  padding: 16,
  backgroundColor: 'blue',
  alignItems: 'center',
  borderRadius: 8,
},
buttonText: {
  color: 'white',
  fontSize: 16,
},
});

export default CameraScreen;

Explanation

▪️useNavigation(): Gets the navigation object to switch between screens.

▪️useRef<Camera>(null): Stores a reference to the camera component.

▪️useCameraDevice(‘back’): Selects the back camera for capturing images.

▪️cameraRef.current.takePhoto(): Captures an image from the camera.

▪️navigation.navigate(‘Recognition’, {imageUri: photo.path}): Sends the image path to the RecognitionScreen for processing.

Camera Component:

▪️Uses ref={cameraRef} to control the camera.

▪️device={device} ensures the back camera is used.

▪️isActive={true} keeps the camera running.

▪️photo={true} enables image capture.

Implementing Image Recognition

Now, let’s process the captured image using Firebase ML Kit.

import React, {useEffect, useState} from 'react';
import {View, Text, Image, ActivityIndicator, StyleSheet} from 'react-native';
import ml from '@react-native-firebase/ml';
import {RouteProp} from '@react-navigation/native';
import {StackNavigationProp} from '@react-navigation/stack';

// Define navigation stack types
type RootStackParamList = {
Recognition: {imageUri: string};
};

// Define props type for RecognitionScreen
interface RecognitionScreenProps {
route: RouteProp<RootStackParamList, 'Recognition'>;
navigation: StackNavigationProp<RootStackParamList, 'Recognition'>;
}

const suggestionsMap: Record<string, string[]> = {
Dog: ['Buy dog food', 'Take your dog for a walk', 'Find pet-friendly parks'],
Cat: [
  'Get cat treats',
  'Check out new scratching posts',
  'Look for cat grooming services',
],
Car: [
  'Check fuel levels',
  'Schedule maintenance',
  'Look for nearby car washes',
],
Food: ['Try new recipes', 'Order takeout', 'Visit a nearby restaurant'],
Laptop: [
  'Update software',
  'Clean your keyboard',
  'Check for latest accessories',
],
Book: ['Find similar books', 'Join a book club', 'Write a book review'],
};

const getSuggestions = (labels: string[]): string[] => {
return labels.flatMap(label => suggestionsMap[label] || []);
};

const RecognitionScreen: React.FC<RecognitionScreenProps> = ({route}) => {
const {imageUri} = route.params;
const [labels, setLabels] = useState<string[]>([]);
const [loading, setLoading] = useState<boolean>(true);

useEffect(() => {
  const recognizeImage = async () => {
    try {
      const result = await ml().imageLabelerProcessImage(imageUri);
      const detectedLabels = result.map((label: { text: any; }) => label.text);
      setLabels(detectedLabels);
    } catch (error) {
      console.error(error);
    }
    setLoading(false);
  };
  recognizeImage();
}, [imageUri]);

const suggestions = getSuggestions(labels);

return (
  <View style={styles.container}>
    <Image source={{uri: imageUri}} style={styles.image} />
    {loading ? (
      <ActivityIndicator size="large" style={styles.loader} />
    ) : (
      labels.map((label, index) => (
        <Text key={index} style={styles.label}>
          {label}
        </Text>
      ))
    )}
    <View>
      <Text style={styles.suggestionTitle}>Suggestions:</Text>
      {suggestions.length > 0 ? (
        suggestions.map((item, index) => (
          <Text key={index} style={styles.suggestionItem}>
            - {item}
          </Text>
        ))
      ) : (
        <Text style={styles.noSuggestions}>No suggestions available</Text>
      )}
    </View>
  </View>
);
};

const styles = StyleSheet.create({
container: {
  flex: 1,
  padding: 20,
},
image: {
  height: 300,
  resizeMode: 'contain',
},
loader: {
  marginTop: 20,
},
label: {
  fontSize: 16,
  marginVertical: 4,
},
suggestionTitle: {
  fontSize: 18,
  fontWeight: 'bold',
  marginTop: 20,
},
suggestionItem: {
  fontSize: 16,
  marginVertical: 2,
},
noSuggestions: {
  fontSize: 16,
  fontStyle: 'italic',
  marginTop: 10,
},
});

export default RecognitionScreen;

Explanation

▪️useEffect runs once when the component mounts.

▪️ml().imageLabelerProcessImage(imageUri):

  • Sends the image to Firebase ML Kit.
  • Returns recognized labels (e.g., “Dog”, “Car”).

▪️Errors are logged if recognition fails.

▪️After processing, loading is set to false.

coma

Conclusion

In this blog, we explored how to integrate AI-powered image recognition in a React Native app using Firebase ML Kit. We also learned how to generate suggestions based on detected objects. AI-based image recognition provides automation, enhances accessibility, and improves user experience, making it valuable in multiple industries.

Nandkishor S

Software Engineer

Nandkishor Shinde is a React Native Developer with 5+ years of experience. With a primary focus on emerging technologies like React Native and React.js. His expertise spans across the domains of Blockchain and e-commerce, where he has actively contributed and gained valuable insights. His passion for learning is evident as he always remains open to acquiring new knowledge and skills.

Keep Reading

Keep Reading

A Deep Dive into Modern Clinical Workflows with AI Agents & CDS Hooks

Register Now
  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?