输出的长度是多少const output = await model.executeAsync(data)
?
您应该在output
;
output[X] = detection_boxes // shape: [1, x, 4] x: number of bounding boxes
output[Y] = detection_scores // shape: [1, x] x: number of scores
output[Z] = detection_classes // shape: [1, x] x: number of classes
然后您可以通过以下方式获取预测;
const boxes = output[0].dataSync()
const scores = output[1].arraySync()
const classes = output[2].dataSync()
然后,您可以通过执行以下操作来构造具有所有预测边界框的预测对象;
buildDetectedObjects(scores, threshold, imageWidth, imageHeight, boxes, classes, classesDir) {
const detectionObjects = []
scores.forEach((score, i) => {
if (score > threshold) {
const bbox = [];
const minY = boxes[i * 4] * imageHeight;
const minX = boxes[i * 4 + 1] * imageWidth;
const maxY = boxes[i * 4 + 2] * imageHeight;
const maxX = boxes[i * 4 + 3] * imageWidth;
bbox[0] = minX;
bbox[1] = minY;
bbox[2] = maxX - minX;
bbox[3] = maxY - minY;
detectionObjects.push({
class: classes[i],
label: classesDir[classes[i]].name,
score: score.toFixed(4),
bbox: bbox
})
}
})
return detectionObjects
}
classesDir
成为一本包含培训课程的字典;
let classesDir = {
1: {
name: 'Class name 1',
id: 1,
},
2: {
name: 'Class name 2',
id: 2,
}
}
预测对象将是一个包含对象的数组;
[{
bbox:[x,y,width,height],
class: X,
label: class name,
score: 0.XYZ
},
{
bbox:[x,y,width,height],
class: X,
label: class name,
score: 0.XYZ
}]