Подсчет объектов из изображения с использованием TensorFlow. js - PullRequest
1 голос
/ 11 января 2020

Я работаю над предварительно обученной моделью Coco-SSD для обнаружения объектов. Я успешно обнаружил объекты на изображении, но теперь мне нужно подсчитать количество указанных c объектов, может кто-нибудь помочь

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);});
};

Это код, который я использую. Это показывает только прямоугольник на обнаруженном объекте, но мне нужно посчитать количество указанных c объектов и сохранить их в переменной для будущего использования, такой как person: 3

1 Ответ

0 голосов
/ 11 января 2020

Объект прогнозирования содержит класс прогнозируемого блока. Счетчик может использоваться вместе с условным оператором для подсчета обнаруживающих объектов определенного класса.

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++
    }

});
...