setInterval(async () => {
const detections = await faceapi.detectSingleFace(video).withFaceLandmarks().withFaceDescriptor()
const resizedDetections = faceapi.resizeResults(detections, displaySize)
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
faceapi.draw.drawFaceLandmarks(canvas, resizedDetections)
this.labeledFaceDescriptors = await this.loadLabeledImages()
const faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors, 0.6)
const results = resizedDetections.map(d => faceMatcher.findBestMatch(d.descriptor))
results.forEach((result, i) => {
const box = resizedDetections[i].detection.box
const drawBox = new faceapi.draw.DrawBox(box, {label: result.toString()})
drawBox.draw(canvas)
})
}, 100)
})
function loadLabeledImages() {
try{
const labels = ['Jane', 'Alex']
return Promise.all(
labels.map(async label => {
const descriptions = []
for (let i = 1; i <= 3; i++) {
const img = await faceapi.fetchImage(`public/img/${label}/${i}.jpg`)
const detections = await faceapi.detectSingleFace(img).withFaceLandmarks().withFaceDescriptor()
descriptions.push(detections.descriptor)
}
return new faceapi.LabeledFaceDescriptors(label, this.descriptions)
})
)
}
catch(err){
console.log(err)
}
}
在使用适用于Web应用程序的节点js的浏览器的网络摄像头相机上实现面部识别时,解决了此问题。仅当我在标签中添加多个名称时才会出现此错误...