【发布时间】: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