ml5js利用MobileNet特征分类训练实现是否戴口罩检测

代码语言:html

所属分类:其他

代码描述:ml5js利用MobileNet特征分类训练实现是否戴口罩检测,确保你的网络能访问下载google的mobilenet模型,然后通过摄像头获取戴口罩和不带口罩的数据集,每个数据集至少30张照片以上,然后开始训练,训练完后就可以对摄像头进行实时的识别判断是否带了口罩,还可以对训练的模型进行下载,非常好玩。

代码标签: 特征 分类 训练 实现 是否 戴口罩 ( 口罩 ) 检测

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<html>

<head>
    <meta charset="UTF-8">

    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/ml5.min.js"></script>
    <style>
    </style>
</head>

<body>
    <h2>MobileNet特征检测实现图形分类,注意一定要都访问google,否则无法下载mobienet模型</h2>
    <h3>(用两个类来训练神经网络 - 举个例子,戴口罩和不带口罩)</h3>
    <p>
        类别 1: <input type="text" id="label1Input" value="without_mask"></input>
    类别 2: <input type="text" id="label2Input" value="with_mask"></input>
    </p>
    <video id="video" width="640" height="480" autoplay></video>

    <h6>
        状态: <span id="status"></span>
    </h6>
    <h5>
        结果: <span id="result">N/A</span> (<span id="confidence">N/A</span>)
    </h5>
    <p>
        <button id="label1Button">增加一个没戴口罩的类别数据1</button> - Class 1: <span id="amountOfLabel1Images">0</span> images
    </p>
    <p>
        <button id="label2Button">增加一个戴口罩的类别数据2</button> - Class 2: <span id="amountOfLabel2Images">0</span> images
    </p>
    <p>
        <button id="train">训练</button> <span id="loss"></span>
    </p>
    <p>
        <button id="predict">开始检测!</button>
    </p>
    <p>
        <button id="loadModel">加载模型</button>
        <button id="saveModel">保存模型</button>
    </p>
    <script>

        // Grab all the DOM elements
        const video = document.getElementById('video');
        const status = document.getElementById('status');
        const loading = document.getElementById('loading');
        const label1Button = document.getElementById('label1Button');
        const label2Button = document.getElementById('label2Button');
        const label1Input = document.getElementById('label1Input');
        const label2Input = document.getElementById('label2Input');
        const amountOfLabel1Images = document.getElementById('amountOfLabel1Images');
        const amountOfLabel2Images = document.getElementById('amountOfLabel2Images');
        const train = document.getElementById('train');
        const loss = document.getElementById('loss');
        const result = document.getElementById('result');
        const confidence = document.getElementById('confidence');
        const predict = document.getElementById('predict');
        const loadModel = document.getElementById('loadModel');
        const saveModel = document.getElementById('saveModel');

        // Constant
        const DEFAULT_LABEL_1 = 'without_mask';
        const DEFAULT_LABEL_2 = 'with_mask';
        const THRESHOLD_TRAINING = 20;

        // Global variables
        let totalLoss = 0;
        let isModelReady = false;
        let isCustomModelReady = false;
        let isVideoReady = false;
        let featureExtractor = null;
        let classifier = null;
        let isTrainingCompleted = false;
        let startPredicting = false;

        // Starting function
        const main = () => {
            // Create a webcam capture
            navigator.mediaDevices.getUserMedia({
                video: true
            }).then(stream => {
                video.srcObject = stream;
                video.play();
            });

            // Extract the already learned features from MobileNet
            featureExtractor = ml5.featureExtractor('MobileNet', modelLoaded);
            // Create a new classifier using those features
            classifier = featureExtractor.classification(video, videoReady);

            status.textContent = 'Loading';
            label1Input.textContent = DEFAULT_LABEL_1;
            label2Input.textContent = DEFAULT_LABEL_2;
        };

        label1Input.onchange = () => {
            label1Button.innerText = label1Input.value || 'class 1';
        };

        label2Input.onchange = () => {
            label2Button.innerText = label2Input.value || 'class 2';
        };

        // Update current status
        const updateStatus = () => {
            let text = [];
            if (isVideoReady) text.push(.........完整代码请登录后点击上方下载按钮下载查看

网友评论0