我在node js中使用face-api。当我匹配两个图像时,结果 始终返回相同的值,例如“相似度:人 1 (0) FaceMatch { _label: '人 1', _distance: 0 }"
Node js 人脸识别类
const path = require('path');
class FaceRecognition {
constructor() {
this.faceMatcher = null;
}
async compareFaces(imgPath1, imgPath2) {
try {
// Load the models
await this.loadModel();
// Load the images
const image1 = await canvas.loadImage(imgPath1);
const image2 = await canvas.loadImage(imgPath2);
// Detect faces in the images
const face1 = await faceapi.detectSingleFace(image1).withFaceLandmarks().withFaceDescriptor();
const face2 = await faceapi.detectSingleFace(image2).withFaceLandmarks().withFaceDescriptor();
// Check if faces were detected in both images
if (face1 && face2) {
// Create an array of face descriptors
const faceDescriptors = [face1.descriptor];
// Create a FaceMatcher with the face descriptors
const faceMatcher = new faceapi.FaceMatcher(faceDescriptors);
// Compare the face descriptors of the second image
const result = faceMatcher.findBestMatch(face2.descriptor);
// Output the result
console.log(`Similarity: ${result.toString()}`);
return result;
} else {
throw new Error('Unable to detect faces in one or both images.');
}
} catch (error) {
console.error(`Error occurred: ${error}`);
throw error;
}
}
}
您提供的结果表明,两张人脸之间的相似度比较导致匹配标记为“人1”,距离为0。这表明根据比较算法。
标签“人1”和距离0表示两张脸完美匹配。算法确定第二张图像中的脸部与第一张图像中的脸部相同。
需要注意的是,确切的标签和距离值可能会有所不同,具体取决于具体实现以及用于训练人脸匹配模型的数据集。在您的例子中,结果显示这些面孔被认为是距离为 0 的匹配,表明非常相似。
如果您还有任何其他问题,或者还有什么我可以帮助您的,请告诉我。
Chatpgt 这样回答我,但我不这么认为。
问题是什么?你能帮我吗?
我解决了生成两个图像的描述并将描述与faceapi.euclidean Distance进行匹配的问题。
我有一个小问题,我猜它正确的看起来像 0 更相似,而 1 则不然。此外,该函数接受描述符 和 作为参数。