使用 TensorFlow.js 从图像中计算对象
Object Counting from image using TensorFlow.js
我正在研究用于对象检测的预训练模型 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
预测对象包含预测框的class。计数器可以与条件语句一起使用,用于对某个 class.
的检测对象进行计数
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++
}
});
我正在研究用于对象检测的预训练模型 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
预测对象包含预测框的class。计数器可以与条件语句一起使用,用于对某个 class.
的检测对象进行计数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++
}
});