ai-mood-analyzer
Version:
An Ai enabled React Native re-usable component to detect human mood from the given image.
54 lines (43 loc) • 1.92 kB
text/typescript
import '@tensorflow/tfjs-react-native';
import * as tf from '@tensorflow/tfjs';
import * as mobilenet from '@tensorflow-models/mobilenet';
import { Canvas, Image as CanvasImage } from 'canvas';
const MoodAnalyzer = async (base64: string | undefined) => {
if (!base64) return;
try {
//"Initializing TensorFlow.js..."
await tf.ready();
//"TensorFlow.js is ready"
const model = await mobilenet.load();
//"Model loaded successfully"
const canvas = new Canvas(224, 224); // Provide width and height
const context = canvas.getContext("2d");
const image = new CanvasImage();
image.src = `data:image/jpeg;base64,${base64}`;
image.onload = async () => {
try {
console.log("Image loaded successfully");
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0, image.width, image.height);
const imageData = context.getImageData(0, 0, image.width, image.height);
const tensor = tf.browser.fromPixels(imageData);
const predictions = await model.classify(tensor);
if (predictions.length > 0) {
const moodPrediction = predictions[0].className;
return moodPrediction.includes("happy") ? "Happy" : "Sad";
} else {
return "No predictions found";
}
} catch (error) {
console.error("Error analyzing mood:", error);
}
};
image.onerror = (error: Error) => {
console.error("Error loading image:", error);
};
} catch (error) {
console.error("Error initializing TensorFlow.js or loading model:", error);
}
};
export default MoodAnalyzer;