最新动态
face-api 人脸比对,(本地图片)两张图片比对是否为同一个人,实别年龄
2025-01-01 13:27

// 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("https://blog.csdn.net/WWWLSW/article/details/models");
    await faceapi.nets.ageGenderNet.loadFromDisk("https://blog.csdn.net/WWWLSW/article/details/models");
    await faceapi.nets.faceLandmark68Net.loadFromDisk("https://blog.csdn.net/WWWLSW/article/details/models");
    await faceapi.nets.faceRecognitionNet.loadFromDisk("https://blog.csdn.net/WWWLSW/article/details/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();

    以上就是本篇文章【face-api 人脸比对,(本地图片)两张图片比对是否为同一个人,实别年龄】的全部内容了,欢迎阅览 ! 文章地址:http://ww.kub2b.com/news/18026.html
     栏目首页      相关文章      动态      同类文章      热门文章      网站地图      返回首页 企库往资讯移动站 http://ww.kub2b.com/mobile/ , 查看更多   
最新文章
荣耀手机数据恢复教程荣耀手机找回「荣耀手机数据恢复教程」
在使用荣耀手机的过程中,大家难免会遇到不小心删除重要照片的情况。这些照片可能包含了珍贵的回忆,一旦丢失,可能会让人感到非
手机副卡怎么注销手机副卡「手机副卡怎么注销」
随着移动通信技术的不断发展,手机副卡已成为许多家庭或企业用户的常用选择,它为用户提供了更多的便利和灵活性。然而,当不再需
三星s8怎么样值得买吗 64G版苏宁易购4999元s8手机「三星s8怎么样值得买吗 64G版苏宁易购4999元」
  【PConline 导购】当下很多手机都有一流的硬件配置和出色的使用体验,消费者在购机时也会优先考虑这些产品。就拿三星的旗舰
马拉松赛道上的奇特造型,究竟是展示自我还是博眼球,你怎么看?
或许你会发现,每次马拉松比赛都会出现奇特造型的人物。这不,2025象山马拉松也有一些打扮另类的跑者,他们的出现瞬间成为赛道上
售价21万的诺基亚Vertu手机 你肯定没用过!手机超长待机「售价21万的诺基亚Vertu手机 你肯定没用过!」
人人都说知道诺基亚,但你真的通晓诺基亚吗?在诺基亚旗下奢侈手机公司VERTU(威图、纬图)相信很多人都不知道。而Vertu品牌研发
苹果11原相机自拍是反的怎么调回来苹果手机拍照怎么是反的「苹果11原相机自拍是反的怎么调回来」
  苹果iPhone11(系统版本是iOS 14)相机自拍呈现反方向是【平面镜成像】原理,可在自带的相机设置中修改拍摄方向,也可以在自
阿维塔06即将上市,1天后公布价格
06倒计时一天,实际上有必要重新认识一下这个品牌。阿维塔成立于2018年,主要大事件:2021年阿维塔科技全球品牌首发,正式亮相,
最新个人简历电子版 个人简历手机版免费(三篇)手机简历「最新个人简历电子版 个人简历手机版免费(三篇)」
无论是身处学校还是步入社会,大家都尝试过写作吧,借助写作也可以提高我们的语言组织能力。写范文的时候需要注意什么呢?有哪些
360行车记录仪,走过风景,走过你——体验G300 3K版前,我要分享一份记录仪避坑指南!360行车记录仪怎么连接手机「360行车记录仪,走过风景,走过你——体验G300 3K版前,我要分享一份记录仪
***感谢众测君翻牌,感谢品牌方信任***说起,大家应该都不陌生了。这东西除了能让你在说不清的时候说清楚,还有点其他用处。比如