face-api 人脸比对,两张图片比对是否为同一个人

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

const faceapi = require('@vladmandic/face-api');
const canvas = require('canvas');

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

function compareFace(args) {
    const {Image, Canvas, ImageData} = canvas;
    faceapi.env.monkeyPatch({Image, Canvas, ImageData});
// 加载模型
    Promise.all([
        faceapi.nets.faceRecognitionNet.loadFromDisk('./models'),
        faceapi.nets.faceLandmark68Net.loadFromDisk('./models'),
        faceapi.nets.ssdMobilenetv1.loadFromDisk('./models'),
        // faceapi.loadSsdMobilenetv1Model
    ]).then(start());

// 开始人脸识别
    async function start() {

        try {
            // Load the models

            // Load the images
            const image1 = await canvas.loadImage(args.pathImage1);
            const image2 = await canvas.loadImage(args.pathImage2);
            // const image1 = await canvas.loadImage(path.join(__dirname,"rr1.png"));
            // const image2 = await canvas.loadImage(path.join(__dirname,"rr2.png"));
            console.log("begin:"+new Date())
            // Detect faces in the images
            const face1 = await faceapi.detectSingleFace(image1).withFaceLandmarks().withFaceDescriptor();
            console.log("end1:"+new Date())
            const face2 = await faceapi.detectSingleFace(image2).withFaceLandmarks().withFaceDescriptor();
            console.log("end2:"+new Date())
            // 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, 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.');
            }
        } catch (error) {
            console.log('No face detected.');
            console.error(`Error occurred: ${error}`);
            throw error;
        }
    }
}

let args = {};
args.pathImage1 = "http://192.168.1.6/documents/renxianqi.png";
args.pathImage2 = "http://192.168.1.6/documents/wubai.png";

compareFace(args)

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

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 C++ 和 dlib 库实现人脸比对,可以使用 ResNet-34 模型。以下是一些大致的步骤: 1. 从 dlib 官网下载和安装 dlib 库,并安装依赖项。 2. 准备好两张比对人脸图片。 3. 加载 ResNet-34 模型并提取两张人脸图片的特征向量。 4. 使用特征向量计算欧氏距离或余弦相似度等距离度量指标,得出两张图片的相似度。 下面是一个简单的实现代码: ```cpp #include <dlib/dnn.h> #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_io.h> #include <iostream> #include <vector> using namespace std; using namespace dlib; // 定义 ResNet-34 模型结构 using ResNet = loss_multiclass_log<fc<1000, avg_pool_everything< res<basic_block, 34, 16, 16, 1>>>>; int main() { // 加载人脸检测器 frontal_face_detector detector = get_frontal_face_detector(); // 加载 ResNet-34 模型 ResNet net; deserialize("dlib_resnet34.dat") >> net; // 加载待比对两张人脸图片 array<array2d<unsigned char>, 2> imgs; load_image(imgs[0], "face1.jpg"); load_image(imgs[1], "face2.jpg"); // 对两张图片进行人脸检测和特征提取 std::vector<matrix<float, 0, 1>> face_descriptors; for (int i = 0; i < 2; ++i) { // 检测人脸位置和大小 std::vector<rectangle> faces = detector(imgs[i]); if (faces.size() == 0) { cout << "No faces detected!" << endl; return 1; } // 提取人脸特征向量 matrix<rgb_pixel> face_chip; extract_image_chip(imgs[i], get_face_chip_details(faces[0], 150, 0.25), face_chip); face_descriptors.push_back(net(face_chip)); } // 计算两张图片的相似度,这里采用欧氏距离作为距离度量指标 double dist = length(face_descriptors[0] - face_descriptors[1]); cout << "Distance between two faces: " << dist << endl; return 0; } ``` 需要注意的是,这里的 ResNet-34 模型是基于 ImageNet 数据集训练的,因此对于人脸比对任务可能需要微调模型。另外,对于大规模人脸比对任务,需要考虑加速特征提取和相似度计算的方法,比如 GPU 加速和局部敏感哈希等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值