使用TensorFlow.js从网络摄像头进行实时AI情感检测

目录

配置

获取实时视频源

使用KNN分类器

将代码放在一起

测试结果

下一步是什么?


使用支持HTML5的现代网络浏览器,我们可以轻松访问许多API,例如网络摄像头。当我们需要访问实时数据时,它特别有用。在本文中,我们将使用网络摄像头访问实时数据,并使用转移学习来检测脾气暴躁。

如果您一直在看本系列,那么本文中使用的代码应该看起来很熟悉。我们将使用相同的迁移学习技术,将预先训练的MobileNet模型与我们的自定义实时训练数据结合起来。

配置

创建一个HTML文档并通过导入所需的TensorFlow.jsMobileNetKNN分类器模型开始。

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/knn-classifier"></script>

我们将使用视频标签来使用来自实时摄像机的图像,而不是使用图像或画布标签。

<video autoplay muted id="webcam" width="224" height="224"></video>

我们还需要脾气暴躁中性按钮,才能将实况视频的图像添加到我们的训练数据中:

<button id="grumpy">Grumpy</button>
<button id="neutral">Neutral</button>

让我们添加另一个标签,以在页面而不是控制台上输出我们的预测。

<div id="prediction"></div>

我们最终的HTML文件如下所示:

<html lang="en">
    <head>
        <meta charset="UTF-8">
        <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>
        <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet"></script>
        <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/knn-classifier"></script>
    </head>
    <body>
        <div>
            <h1>Real-Time Grumpiness detection using Tensorflow.js</h1>
        
            <div style="width:100%">
            <video autoplay muted id="webcam" width="224" height="224" style=" margin: auto;"></video>
            </div>
 
            <h3>How are you feeling?</h3>
            <button id="grumpy">Grumpy</button>
            <button id="neutral">Neutral</button>
 
            <div id="prediction"></div>
 
            <script src="grumpinessClassifier.js"></script>
        </div>
    </body>
</html>

获取实时视频源

现在是时候转到JavaScript文件了,我们将通过设置一些重要的变量来开始:

let knn;
let model;
 
const classes = ['Grumpy', 'Neutral'];
const video = document.getElementById('webcam');

我们还需要加载模型并创建KNN分类器的实例:

let knn;
let model;
 
const classes = ['Grumpy', 'Neutral'];
const video = document.getElementById('webcam');

我们现在可以设置网络摄像头以获取视频图片:

const setupWebcam = async () => {
    return new Promise((resolve, reject) => {
        const _navigator = navigator;
        navigator.getUserMedia = navigator.getUserMedia ||
        _navigator.webkitGetUserMedia || _navigator.mozGetUserMedia ||
        _navigator.msGetUserMedia;
        if (navigator.getUserMedia) {
            navigator.getUserMedia({video: true},
                stream => {
                    video.srcObject = stream;
                    video.addEventListener('loadeddata', () => resolve(), false);
                },
            error => reject());
        }
    });
}

使用KNN分类器

现在我们从网络摄像头获取数据,我们将继续使用knnaddExample方法添加示例训练数据。

const addExample = label => {
    // getting the intermediate activation of MobileNet 'conv_preds' and passing that to the KNN classifier.
    const feature = model.infer(video, 'conv_preds');

    // Pass the intermediate activation to the classifier
    knn.addExample(feature, label);
};

// add an example to the specified class on button click
document.getElementById('grumpy').addEventListener('click', () => addExample(0));
document.getElementById('neutral').addEventListener('click', () => addExample(1));

将代码放在一起

这是我们的grumpinessClassifier.js文件的最终外观:

let knn;
let model;
 
knn = knnClassifier.create();
const classes = ['Grumpy', 'Neutral'];
const video = document.getElementById('webcam');
 
async function loadKnnClassifier() {
 
    console.log('Model is Loading..');
    model = await mobilenet.load();
    console.log('Model loaded successfully!');
 
    await setupWebcam();
 
    // Reading the image from the webcam and associate the image with a specific class 
    const addExample = label => {
        // getting the intermediate activation of MobileNet 'conv_preds' and passing that to the KNN classifier.
        const feature = model.infer(video, 'conv_preds');
 
        // Pass the intermediate activation to the classifier
        knn.addExample(feature, label);
    };
 
    // add an example to the specified class on button click
    document.getElementById('grumpy').addEventListener('click', () => addExample(0));
    document.getElementById('neutral').addEventListener('click', () => addExample(1));
 
    while(true) {
        if (knn.getNumClasses() > 0) {
            // Getting activation from mobilenet for the webcam video
            const feature = model.infer(video, 'conv_preds');
            // getting the top prediction from the classifier module
            const prediction = await knn.predictClass(feature);
 
            // printing the prediction with confidence score on the screen
            document.getElementById('prediction').innerText = `
                Predicted emotion: ${classes[prediction.classIndex]}\n
                Probability of prediction: ${prediction.confidences[prediction.classIndex].toFixed(2)}
                `;
        }
 
        // wait for the next animation frame 
        await tf.nextFrame();
    }
}
 
const setupWebcam = async () => {
    return new Promise((resolve, reject) => {
        const _navigator = navigator;
        navigator.getUserMedia = navigator.getUserMedia ||
        _navigator.webkitGetUserMedia || _navigator.mozGetUserMedia ||
        _navigator.msGetUserMedia;
        if (navigator.getUserMedia) {
            navigator.getUserMedia({video: true},
                stream => {
                    video.srcObject = stream;
                    video.addEventListener('loadeddata', () => resolve(), false);
                },
            error => reject());
        }
    });
}
 
loadKnnClassifier();

测试结果

在浏览器中打开文件,然后通过单击表情合适的按钮(脾气暴躁或中性)开始训练自定义分类器。仅捕获少量图像后,您将开始看到结果。

为了获得更好的预测,您必须将更多数据提供给自定义分类器。由于我们是在较小的数据集上训练模型的,因此当其他人尝试您的应用程序时,分类器的AI准确性会很低。为了获得更好的预测结果,您可以将很多不同的人的照片输入到分类器中。

下一步是什么?

在本文中,我们学习了如何使用转移学习来扩展经过预先训练的MobileNet模型,以检测实时网络摄像头数据上的脾气暴躁检测。我们的应用程序现在可以从实时视频帧中识别用户的表情。但是我们仍然必须向应用程序提供表达式才能开始使用它。如果不先进行明确培训就可以启动并运行我们的应用程序,这会很好又方便吗?

下一篇文章中,我们将使用另一个经过预先训练的模型face-api.js来检测表情而无需自己进行任何训练。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值