Passio Nutrition-AI
  • Nutrition-AI SDK Overview
  • Guides
    • Nutrition AI SDK
      • SDK Key and minimum requirements
      • Installation
      • Configure the SDK
      • Use Cases
        • Food recognition
        • Nutrition data
        • Barcode scanning
        • Nutrition Facts scanning
        • Search, Food Icons, RefCode
        • Speech recognition
        • Nutrition Advisor
        • Suggestions and Meal Plans
        • User created foods and reports
    • iOS SDK Docs
      • Before getting started
      • Getting the ml models to the device
      • Run the demos first
      • Adding Passio SDK into your project
      • Initialize and configure the SDK
      • Start/Stop food detection
      • Food Recognition Delegate
      • Migration from SDK 1.4.X to 2.x
      • SDK API
      • Quick Start Guide
        • Installation
        • Configure the SDK (UIKit)
        • Recognise food using image
        • Food Details
    • Android SDK Docs
      • Getting started
        • Include the library
        • Camera
        • SDK Initialization and Configuration
        • Food detection session
        • Visual, Barcode and Packaged Food detection
        • Nutritional Database
        • Sandbox app
      • Troubleshooting on Android
      • Migration from SDK version 1.4.x to 2.x
      • Quick Start Guide
        • Importing the Android SDK to a project
        • Configure SDK and handle the result
        • RecognizeImageRemote
        • Food Details
    • React Native SDK Docs
      • Getting Started
        • Installation
        • SDK Initialization and Configuration
      • API Reference
        • configure & requestCameraAuthorization
        • recognizeImageRemote
        • searchForFood & searchForFoodSemantic
        • startFoodDetection
          • FoodDetectionEvent
        • fetchFoodItemForProductCode
        • fetchFoodItemForRefCode
        • fetchFoodItemForDataInfo
        • startNutritionFactsDetection
        • fetchFoodItemForPassioID
        • recognizeSpeechRemote
        • Fetch Nutrients
        • onDowloadingPassioModelCallBacks
        • detectFoodFromImageURI
        • addToPersonalization
        • updateLanguage
        • fetchHiddenIngredients
        • fetchVisualAlternatives
        • fetchPossibleIngredients
      • Properties
        • PassioFoodItem
        • PassioFoodDataInfo
        • PassioFoodAmount
        • PassioIngredient
        • PassioNutrients
        • UnitMass
        • ServingUnit
        • ServingSize
        • PassioSearchResult
        • NutritionFacts
        • PassioNutritionPreview
        • PassioSpeechRecognitionModel
        • PassioLogAction
        • PassioAdvisorFoodInfo
        • FoodCandidates
        • FoodDetectionEvent
        • NutritionDetectionEvent
        • DetectedCandidate
        • BarcodeCandidate
        • PackagedFoodCode
        • AmountEstimate
        • ImagesInfo
        • PassioStatus
        • FoodDetectionEvent
        • NutritionFacts
        • PassioMealPlan
        • PassioMealPlanItem
      • Nutriton Advisor
        • initConversation
        • sendMessage
        • sendImage
        • Guide
          • useNutritionAdvisor
        • Properties
          • PassioAdvisorResponse
          • PassioAdvisorMessageResultStatus
          • PassioAdvisorResultStatus
      • Quick Start Guide
        • Installation
        • Configure the SDK
        • RecognizeImageRemote
        • Food Detail
      • Guide
        • Integrate SDK Setup
        • Integrate Quick Scan
        • Integrate Food Search
        • Integrate Food Editor with Recipe
        • Integrate MealPlan
        • Integrate Suggestions
        • Integrate recognizeImageRemote
      • Components
        • DetectionCameraView
        • PassioIconView
      • Changelog
      • More
        • Display Macro Chart
          • MockFood
          • Utils PassioNutrient
        • Display Micro Progress
        • Getting nutrition value for a FoodItem
        • Getting nutrition value for a Recipe
        • Recalculate the nutrition value based on serving size options.
      • Migrations
        • Migrating from SDK 2.X to version 3.X
        • Structure Migrations From SDK 3.X to SDK 2.X
        • [Deprecated] Migrating from SDK 1.X to version 2.X
        • [Deprecated] Getting Started (v1)
          • Installation
          • SDK Initialization and Configuration
          • Start food detection
          • FoodDetectionEvent
          • Nutritional Database
      • Troubleshooting on RN
      • V2
        • Getting Started
          • Installation
          • SDK Initialization and Configuration
          • Start food detection
          • FoodDetectionEvent
        • RN SDK API
          • Properties
            • PersonalizedAlternative
            • FoodSearchResult
            • PassioNutrient
            • FoodDetectionEvent
            • DownloadModelCallBack
            • UPCProduct
            • ServingUnit
            • PassioStatus
            • PassioIDAttributes
            • PassioIDEntityType
            • PassioFoodItem
            • PassioRecipe
            • ServingSize
            • Measurement
            • UnitMass
            • NutritionFacts
        • Food Image
        • Search Food Item
        • Quick Scan
        • Multi Scan
        • MealPlan
        • Recipe
      • How do I Integrate a Passio SDK in EXPO?
    • Flutter SDK Docs
      • Getting Started
    • Before You Continue
    • Setup For Android
    • Initialize and configure the SDK
  • Fundamentals
    • Nutrition-AI Developer FAQ
      • Nutrition Data
      • Supported Phones
      • Security
      • Testing Volume Estimation
      • Testing Nutrition-AI SDK
      • Nutrition-AI Test Methodology
    • Nutrition API - Mobile SDK interoperability
      • JSON Response parsing
  • Versions
    • 3.2.4
      • SDK Key and minimum requirements
      • Installation
      • Configure the SDK
      • Use Cases
        • Food recognition
        • Nutrition data
        • Barcode scanning
        • Nutrition Facts scanning
        • Search, Food Icons, RefCode
        • Speech recognition
        • Nutrition Advisor
        • Suggestions and Meal Plans
        • User created foods and reports
    • 3.2.2
      • SDK Key and minimum requirements
      • Installation
      • Configure the SDK
      • Use Cases
        • Food recognition
        • Nutrition data
        • Barcode scanning
        • Nutrition Facts scanning
        • Search, Food Icons, RefCode
        • Speech recognition
        • Nutrition Advisor
        • Suggestions and Meal Plans
        • User created foods and reports
    • 3.2.0
      • SDK Key and minimum requirements
      • Installation
      • Configure the SDK
      • Use Cases
        • Food recognition
        • Nutrition data
        • Barcode scanning
        • Nutrition Facts scanning
        • Search, Food Icons, RefCode
        • Speech recognition
        • Nutrition Advisor
        • Suggestions and Meal Plans
    • 3.1.4
      • SDK Key and minimum requirements
      • Installation
      • Configure the SDK
      • Use Cases
        • Food recognition
        • Nutrition data
        • Barcode scanning
        • Search, Food Icons, RefCode
        • Speech recognition
        • Nutrition Advisor
        • Suggestions and Meal Plans
Powered by GitBook
On this page
  • How can I implement the startFoodDetection API and retrieve food items?
  • Example
  • useMultiScanning
  • MultiScan
  • MultiScanResult
  • AlternativeFood
  • Utils Methods
  • Result
Export as PDF
  1. Guides
  2. React Native SDK Docs
  3. V2

Multi Scan

You can utilize the `startFoodDetection` API to scan food items through the device Camera.

How can I implement the startFoodDetection API and retrieve food items?

/**
     * Begin food detection using the device's camera.
     * @param options - An object to determine which types of scanning should be performed.
     * @param callback - A callback to repeatedly receive food detection events as they occur.
     * @returns A `Subscription` that should be retained by the caller while food detection is running. Call `remove` on the subscription to terminate food detection.
     */
    
useEffect(() => {
    // Configuration for food detection
    const config: FoodDetectionConfig = {
      detectBarcodes: true,
      detectPackagedFood: true,
      detectNutritionFacts: false,
    };

    // Start food detection and subscribe to events
    const subscription = PassioSDK.startFoodDetection(
      config,
      async (detection: FoodDetectionEvent) => {
        const { candidates } = detection;

        // Check if there are candidates with relevant information
        if (
          candidates &&
          ((candidates.detectedCandidates &&
            candidates.detectedCandidates.length > 0) ||
            (candidates.packagedFoodCode &&
              candidates.packagedFoodCode.length > 0) ||
            (candidates.barcodeCandidates &&
              candidates.barcodeCandidates.length > 0))
        ) {
          // Get attributes for the detected food candidates
          // This method is mentioned below in utility method
          const attributes = await getAttributesForFoodCandidates(candidates);

          // Update passioIDAttributes state
          setPassioIDAttributes((food) => {
            const copyOfPassioIDAttributes: PassioIDAttributes[] = [];

            // Filter out duplicate entries
            attributes.forEach((value) => {
              if (food.every((item) => item.passioID !== value.passioID)) {
                copyOfPassioIDAttributes.push(value);
              }
            });

            // If there are new attributes, update the state
            if (copyOfPassioIDAttributes.length > 0) {
              return [...food, ...copyOfPassioIDAttributes];
            } else {
              return food;
            }
          });

          setIsLoading(false);
        }
      }
    );

  

    // Cleanup function to unsubscribe when the component unmounts
    return () => subscription.remove();
  }, []);

Example

Require @gorhom/bottom-sheet for bottom sheet

In this , we establish the useMultiscan hook, which furnishes methods and data such as food, and use of DetectionCameraView

useMultiScanning

import {
  type FoodDetectionConfig,
  type FoodDetectionEvent,
  type PassioIDAttributes,
  PassioSDK,
} from '@passiolife/nutritionai-react-native-sdk-v2';
import { getAttributesForFoodCandidates } from '../../utils';
import { useEffect, useState, useCallback } from 'react';

/**
 * Custom hook for handling multi-scanning using PassioSDK.
 * It provides functions and state variables related to the detection of multiple food items.
 */
export function useMultiScanning() {
  // State variables
  const [isLoading, setIsLoading] = useState(true);
  const [passioIDAttributes, setPassioIDAttributes] = useState<
    PassioIDAttributes[]
  >([]);

  useEffect(() => {
    // Configuration for food detection
    const config: FoodDetectionConfig = {
      detectBarcodes: true,
      detectPackagedFood: true,
      detectNutritionFacts: false,
    };

    // Start food detection and subscribe to events
    const subscription = PassioSDK.startFoodDetection(
      config,
      async (detection: FoodDetectionEvent) => {
        const { candidates } = detection;

        // Check if there are candidates with relevant information
        if (
          candidates &&
          ((candidates.detectedCandidates &&
            candidates.detectedCandidates.length > 0) ||
            (candidates.packagedFoodCode &&
              candidates.packagedFoodCode.length > 0) ||
            (candidates.barcodeCandidates &&
              candidates.barcodeCandidates.length > 0))
        ) {
          // Get attributes for the detected food candidates
           // This method is mentioned below in utility method
          const attributes = await getAttributesForFoodCandidates(candidates);

          // Update passioIDAttributes state
          setPassioIDAttributes((food) => {
            const copyOfPassioIDAttributes: PassioIDAttributes[] = [];

            // Filter out duplicate entries
            attributes.forEach((value) => {
              if (food.every((item) => item.passioID !== value.passioID)) {
                copyOfPassioIDAttributes.push(value);
              }
            });

            // If there are new attributes, update the state
            if (copyOfPassioIDAttributes.length > 0) {
              return [...food, ...copyOfPassioIDAttributes];
            } else {
              return food;
            }
          });

          setIsLoading(false);
        }
      }
    );


    // Cleanup function to unsubscribe when the component unmounts
    return () => subscription.remove();
  }, []);

  // Function to delete a food item by its passioID
  const onDeleteFoodItem = useCallback((passioID: string) => {
    setPassioIDAttributes((prev) =>
      prev.filter((value) => value.passioID !== passioID)
    );
  }, []);

  // Return the hook's public API
  return {
    isLoading,
    passioIDAttributes,
    onDeleteFoodItem,
  };
}

MultiScan

import { ActivityIndicator, View } from 'react-native';
import {
  blackBackgroundStyle,
  bottomSheet,
  cameraStyle,
} from './MultiScanningStyle';

import BottomSheet from '@gorhom/bottom-sheet';
import { DetectionCameraView } from '@passiolife/nutritionai-react-native-sdk-v2';
import React, { useMemo, useRef } from 'react';
import { gestureHandlerRootHOC } from 'react-native-gesture-handler';
import { useMultiScanning } from './hooks/useMultiScanning';
import { MultiScanResult } from './MultiScanResult';
export const MultiScan = gestureHandlerRootHOC(() => {
  const { isLoading, passioIDAttributes, onDeleteFoodItem } =
    useMultiScanning();

  const bottomSheetModalRef = useRef<BottomSheet>(null);

  const snapPoints = useMemo(() => ['20%', '75%'], []);

  return (
    <View style={blackBackgroundStyle}>
      <DetectionCameraView style={cameraStyle} />
      <BottomSheet
        ref={bottomSheetModalRef}
        index={0}
        snapPoints={snapPoints}
        backgroundStyle={bottomSheet.backgroundStyle}
        enableContentPanningGesture={
          isLoading || passioIDAttributes.length <= 3
        }
      >
        {isLoading || passioIDAttributes.length === 0 ? (
          <ActivityIndicator />
        ) : (
          <View style={[bottomSheet.container]}>
            <MultiScanResult
              attributes={passioIDAttributes}
              onClearResultPress={onDeleteFoodItem}
            />
          </View>
        )}
      </BottomSheet>
    </View>
  );
});

MultiScanResult

import { StyleSheet } from 'react-native';

import { BottomSheetFlatList } from '@gorhom/bottom-sheet';
import { type PassioIDAttributes } from '@passiolife/nutritionai-react-native-sdk-v2';
import React from 'react';
import { MultiScanResultItem } from './MultiScanResultItem';

export interface MultiScanResultProps {
  attributes: PassioIDAttributes[];
  onClearResultPress: (item: PassioIDAttributes) => void;
}

export const MultiScanResult = ({
  attributes,
  onClearResultPress,
}: MultiScanResultProps) => {
  const styles = multiScanResultStyle();

  const renderItem = ({ item }: { item: PassioIDAttributes }) => {
    return (
      <MultiScanResultItem
        attribute={item}
        onClearResultPress={onClearResultPress}
      />
    );
  };

  return (
    <BottomSheetFlatList
      contentContainerStyle={styles.flatList}
      data={attributes}
      renderItem={renderItem}
      keyExtractor={(item) => item.passioID}
      extraData={attributes}
    />
  );
};

const multiScanResultStyle = () =>
  StyleSheet.create({
    flatList: {
      marginVertical: 8,
      paddingBottom: 250,
    },
  });

MultiScanResultItem

import React from 'react';
import { Image, Pressable, StyleSheet, Text, View } from 'react-native';

import {
  IconSize,
  PassioIconView,
  type PassioIDAttributes,
} from '@passiolife/nutritionai-react-native-sdk-v2';
import AlternativeFood from '../quick/AlternativeFood';

export interface MultiScanResultItemProps {
  attribute: PassioIDAttributes;
  onClearResultPress?: (item: PassioIDAttributes) => void;
}

const MultiScanResultItem = ({
  attribute,
  onClearResultPress,
}: MultiScanResultItemProps) => {
  const styles = quickScanStyle();

  const onAlternativeFoodItemChange = () => {};

  return (
    <View style={styles.itemContainer}>
      <View style={styles.foodResult}>
        <PassioIconView
          style={styles.itemIcon}
          config={{
            passioID: attribute.imageName ?? attribute.passioID,
            iconSize: IconSize.PX180,
            passioIDEntityType: attribute.entityType,
          }}
        />
        <Text style={styles.itemFoodName}>{attribute.name}</Text>
      </View>
      <AlternativeFood
        onAlternativeFoodItemChange={onAlternativeFoodItemChange}
        passioId={attribute.passioID}
      />
      <Pressable
        style={styles.clearResult}
        onPress={() => onClearResultPress?.(attribute)}
      >
        <Image
          resizeMode="contain"
          style={styles.clearResultItem}
          source={require('../../../assets/ic_close_white.png')}
        />
      </Pressable>
    </View>
  );
};

const quickScanStyle = () =>
  StyleSheet.create({
    itemContainer: {
      padding: 12,
      marginHorizontal: 16,
      backgroundColor: 'white',
      marginTop: 16,
      left: 0,
    },
    foodResult: {
      flexDirection: 'row',
      alignItems: 'center',
    },
    itemFoodName: {
      flex: 1,
      textTransform: 'capitalize',
      marginHorizontal: 8,
      fontSize: 16,
    },
    itemIcon: {
      height: 60,
      width: 60,
    },
    clearResult: {
      position: 'absolute',
      top: -9,
      backgroundColor: 'blue',
      overflow: 'hidden',
      padding: 4,
      height: 26,
      width: 26,
      justifyContent: 'center',
      borderRadius: 30,
      alignItems: 'center',
      right: 0,
    },
    clearResultItem: {
      height: 8,
      width: 8,
    },
  });
export default React.memo(MultiScanResultItem);

AlternativeFood

import React, { useState, useEffect } from 'react';
import { FlatList, Pressable, StyleSheet, Text } from 'react-native';
import {
  IconSize,
  PassioIconView,
  type PassioID,
  type PassioIDAttributes,
} from '@passiolife/nutritionai-react-native-sdk-v2';
import { getAlternateFoodItems, getAttributesForPassioID } from '../utils';

interface AlternativeFoodProps {
  passioId: PassioID;
  onAlternativeFoodItemChange?: (item: PassioIDAttributes) => void;
}

const AlternativeFood = React.memo(
  ({ passioId, onAlternativeFoodItemChange }: AlternativeFoodProps) => {
    const [alternativeAttributes, setAlternativeAttributes] =
      useState<PassioIDAttributes[]>();
    const styles = alternativeFoodStyle();

    useEffect(() => {
      console.log('alternative called ----');
      async function init() {
        const passioAttributeID = await getAttributesForPassioID(passioId);
        if (passioAttributeID != null) {
          const list = await getAlternateFoodItems(passioAttributeID);
          setAlternativeAttributes(list);
        }
      }
      init();
    }, [passioId]);

    if (alternativeAttributes?.length === 0) {
      return null;
    }

    return (
      <FlatList
        horizontal
        data={alternativeAttributes}
        renderItem={({ item }) => {
          return (
            <Pressable
              onPress={() => onAlternativeFoodItemChange?.(item)}
              style={styles.itemContainer}
            >
              <PassioIconView
                style={styles.itemIcon}
                config={{
                  passioID: item.imageName ?? item.passioID,
                  iconSize: IconSize.PX180,
                  passioIDEntityType: item.entityType,
                }}
              />
              <Text style={styles.itemFoodName}>{item.name}</Text>
            </Pressable>
          );
        }}
        keyExtractor={(item) => item.passioID.toString()}
      />
    );
  }
);

const alternativeFoodStyle = () =>
  StyleSheet.create({
    itemContainer: {
      marginHorizontal: 8,
      alignItems: 'center',
      marginVertical: 16,
      flexDirection: 'row',
    },
    itemFoodName: {
      marginHorizontal: 8,
      flex: 1,
      textTransform: 'capitalize',
      fontSize: 12,
    },
    itemIcon: {
      height: 24,
      width: 24,
    },
  });

export default AlternativeFood;

Utils Methods

getAttributesForFoodCandidates

export async function getAttributesForFoodCandidates({
  detectedCandidates,
  barcodeCandidates,
  packagedFoodCode,
}: FoodCandidates): Promise<PassioIDAttributes[]> {
  const detectionCandidateAttributes = detectedCandidates?.map(
    ({ passioID }) => {
      return PassioSDK.getAttributesForPassioID(passioID);
    }
  );
  const barcodeCandidatesAttributes = barcodeCandidates?.map(({ barcode }) => {
    return PassioSDK.fetchAttributesForBarcode(barcode);
  });

  const ocrCandidatesAttributes = packagedFoodCode?.map((ocrCode) => {
    return PassioSDK.fetchPassioIDAttributesForPackagedFood(ocrCode);
  });
  const attrs = await Promise.all([
    ...(barcodeCandidatesAttributes ?? []),
    ...(ocrCandidatesAttributes ?? []),
    ...detectionCandidateAttributes,
  ]);
  return attrs.filter(notEmpty);
}

getAlternateFoodItems


export async function getAlternateFoodItems(
  passioIDAttributes: PassioIDAttributes
): Promise<PassioIDAttributes[]> {
  const childrenAttributes = passioIDAttributes.children.map(
    async ({ passioID }) => {
      return PassioSDK.getAttributesForPassioID(passioID);
    }
  );
  const siblingAttributes = passioIDAttributes.siblings.map(
    async ({ passioID }) => {
      return PassioSDK.getAttributesForPassioID(passioID);
    }
  );
  const parentAttributes = passioIDAttributes.parents.map(
    async ({ passioID }) => {
      return PassioSDK.getAttributesForPassioID(passioID);
    }
  );

  const combinedAttributes = [
    ...childrenAttributes,
    ...siblingAttributes,
    ...parentAttributes,
  ];

  const attrs = await Promise.all(combinedAttributes);
  return attrs.filter(notEmpty);
}

getAttributesForPassioID

export async function getAttributesForPassioID(
  passioID: PassioID
): Promise<PassioIDAttributes | null> {
  return await PassioSDK.getAttributesForPassioID(passioID);
}

Yeah. Added Multi Scan.

Result

PreviousQuick ScanNextMealPlan

Last updated 1 year ago