UNPKG

ai-mood-analyzer

Version:

An Ai enabled React Native re-usable component to detect human mood from the given image.

70 lines (53 loc) 2.04 kB
# MoodAnalyzer MoodAnalyzer is a reusable function that uses TensorFlow.js and MobileNet to analyze the mood from an image. ## Installation You can install the package using npm: ```bash npm install mood-analyzer ## Usage Here’s an example of how to use the MoodAnalyzer function in your React Native project: import React, { useState } from 'react'; import { View, Button, Image, Text } from 'react-native'; import * as ImagePicker from 'react-native-image-picker'; import MoodAnalyzer from 'mood-analyzer'; const App = () => { const [imageUri, setImageUri] = useState<string | null>(null); const [mood, setMood] = useState<string | null>(null); const pickImage = async () => { const result = await ImagePicker.launchImageLibrary({ mediaType: 'photo', includeBase64: true, }); if (result.assets && result.assets.length > 0) { const uri = result.assets[0].uri; if (uri) { setImageUri(uri); const moodResult = await MoodAnalyzer(result.assets[0].base64); setMood(moodResult); } } }; return ( <View> <Button title="Pick an Image" onPress={pickImage} /> {imageUri && <Image source={{ uri: imageUri }} style={{ width: 200, height: 200 }} />} {mood && <Text>Mood: {mood}</Text>} </View> ); }; export default App; ## Function MoodAnalyzer(base64: string | undefined): Promise<string | undefined> Analyzes the mood from a base64-encoded image string and returns a mood prediction. ## Parameters base64: A base64-encoded string of the image to analyze. ## Returns A promise that resolves to a string indicating the mood (“Happy” or “Sad”) or an error message if the analysis fails. ## Dependencies @tensorflow/tfjs: ^3.9.0 @tensorflow/tfjs-react-native: ^0.5.0 @tensorflow-models/mobilenet: ^2.1.0 canvas: ^2.8.0 react-native-image-picker: ^4.0.6 ## License This project is licensed under the MIT License - see the LICENSE file for details.