face-api 人脸比对,(本地图片)两张图片比对是否为同一个人,实别年龄

本文介绍如何使用Node.js和TensorFlow.js库在JavaScript中实现人脸识别功能,通过比较两张图片的面部特征向量判断是否为同一个人。
摘要由CSDN通过智能技术生成

// demo下载 https://download.csdn.net/download/WWWLSW/88950321

// @canvas/image can decode jpeg, png, webp
const image = require('@canvas/image');
const fs = require('fs');
const tf = require('@tensorflow/tfjs-node');
const faceapi = require('@vladmandic/face-api');

const threshold = 0.4; // 可根据实际情况调整

async function compareFace() {
    const imageFile1 = 'wubai.png';
    const imageFile2 = 'renxianqi.png';
    const buffer1 = fs.readFileSync(imageFile1);
    const buffer2 = fs.readFileSync(imageFile2);
    const canvas1 = await image.imageFromBuffer(buffer1);
    const canvas2 = await image.imageFromBuffer(buffer2);
    const imageData1 = image.getImageData(canvas1);
    const imageData2 = image.getImageData(canvas2);

    const image1 = tf.tidy(() => {
        const data = tf.tensor(Array.from(imageData1?.data || []), [canvas1.height, canvas1.width, 4], 'int32');
        const channels = tf.split(data, 4, 2);
        const rgb = tf.stack([channels[0], channels[1], channels[2]], 2);
        const reshape = tf.reshape(rgb, [1, canvas1.height, canvas1.width, 3]);
        return reshape;
    });
    const image2 = tf.tidy(() => {
        const data = tf.tensor(Array.from(imageData2?.data || []), [canvas2.height, canvas2.width, 4], 'int32');
        const channels = tf.split(data, 4, 2);
        const rgb = tf.stack([channels[0], channels[1], channels[2]], 2);
        const reshape = tf.reshape(rgb, [1, canvas2.height, canvas2.width, 3]);
        return reshape;
    });

    // load models
    await faceapi.nets.ssdMobilenetv1.loadFromDisk("./models");
    await faceapi.nets.ageGenderNet.loadFromDisk("./models");
    await faceapi.nets.faceLandmark68Net.loadFromDisk("./models");
    await faceapi.nets.faceRecognitionNet.loadFromDisk("./models");
    console.log("begin:"+new Date())
    const face1 = await faceapi // run detection
        .detectSingleFace(image1)
        .withFaceLandmarks()
        .withAgeAndGender()
        .withFaceDescriptor();
    console.log("end1:"+new Date())
    const face2 = await faceapi // run detection
        .detectSingleFace(image2)
        .withFaceLandmarks()
        .withAgeAndGender()
        .withFaceDescriptor();
    console.log("end2:"+new Date())
    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, threshold);

        // Compare the face descriptors of the second image
        const result = faceMatcher.findBestMatch(face2.descriptor);
        const person = result.label;
        if (person == "person 1") {
            console.log('这两张图片可能是同一个人。');
        } else {
            console.log('这两张图片可能是不同的人。');
        }
        // Output the result
        console.log(`Similarity: ${result.toString()}`);
        return result;
    } else {
        throw new Error('Unable to detect faces in one or both images.');
    }
}

compareFace();

 // demo下载 https://download.csdn.net/download/WWWLSW/88950321

  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值