小白玩机器学习(2)--- P5.js + PL5.js 使用摄像头进行物体识别

接上一节内容

修改sketch.js代码部分

let capture;
let classifier;
let flag = 0;

function ModelReady() {
    // 效果,加载图片之后缓慢输出model is ready
    console.log('model is ready');
    flag2 = 1;
    classifier.predict(capture, GetResult);
}
    
function GetResult(error, results){
    if(error){
        console.error(error);
    }else{
        console.log(results);
        // 第一个预测值
        let name = results[0].className;
        let prob = results[0].probability;
        // 设置颜色
        fill([0,255,0]);
        textSize(25);
        image(capture, 0, 0, width, height);
        // 将预测结果输出
        text(name, 10, height-20);
        flag = 1;
    }   
}

function setup(){
    createCanvas(480, 480);
    capture = createCapture(VIDEO);  //camera capture function
    capture.hide();
    classifier = ml5.imageClassifier('MobileNet', capture, ModelReady); 
}

function draw(){
    if(flag == 1){
        classifier.predict(capture, GetResult);
    } 
}

运行之后启动电脑摄像头即可进行物体识别

发布了26 篇原创文章 · 获赞 6 · 访问量 1397

猜你喜欢

转载自blog.csdn.net/Sabrina_cc/article/details/104858195