【问题标题】:Object Counting from image using TensorFlow.js使用 TensorFlow.js 从图像中进行对象计数
【发布时间】:2020-01-11 04:29:38
【问题描述】:

我正在研究用于对象检测的预训练模型 coco-SSD。我已经成功地从图像中检测到对象,但现在我必须计算特定对象的数量,谁能帮忙

public async predictWithCocoModel()
{
    const model = await cocoSSD.load();
    this.detectFrame(this.video,model);
}
detectFrame = (video, model) => {
model.detect(video).then(predictions => {
this.renderPredictions(predictions);
requestAnimationFrame(() => {
this.detectFrame(video, model);});
});
}
renderPredictions = predictions => {
    const canvas = <HTMLCanvasElement> document.getElementById ("canvas");

    const ctx = canvas.getContext("2d");
    canvas.width  = 300;
    canvas.height = 300;
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    // Fonts
    const font = "16px sans-serif";
    ctx.font = font;
    ctx.textBaseline = "top";
    ctx.drawImage(this.video,0, 0,300,300);
    predictions.forEach(prediction => {
    const x = prediction.bbox[0];
    const y = prediction.bbox[1];
    const width = prediction.bbox[2];
    const height = prediction.bbox[3];
    // Bounding box
    ctx.strokeStyle = "#00FFFF";
    ctx.lineWidth = 2;
    ctx.strokeRect(x, y, width, height);
    // Label background
    ctx.fillStyle = "#00FFFF";
    const textWidth = ctx.measureText(prediction.class).width;
    const textHeight = parseInt(font, 10); // base 10
    ctx.fillRect(x, y, textWidth + 4, textHeight + 4);
    });
    predictions.forEach(prediction => {

    const x = prediction.bbox[0];
    const y = prediction.bbox[1];
    ctx.fillStyle = "#000000";
    ctx.fillText(prediction.class, x, y);});
};

这是我正在使用的代码。这仅显示检测到的对象上的矩形,但我必须计算特定对象的数量并将它们存储在一个变量中以供将来使用,例如 person:3

【问题讨论】:

    标签: javascript tensorflow machine-learning tensorflow.js


    【解决方案1】:

    预测对象包含预测框的类别。计数器可以与条件语句一起使用,用于对检测某个类别的对象进行计数。

    let i = 0
    predictions.forEach(prediction => {
        const x = prediction.bbox[0];
        const y = prediction.bbox[1];
        const width = prediction.bbox[2];
        const height = prediction.bbox[3];
        // check for class
        if (prediction.class === 'classOfInterest') {
          i++
        }
    
    });
    

    【讨论】:

      猜你喜欢
      • 2019-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      相关资源
      最近更新 更多