使用dilb实现人脸检测、识别

使用dlib实现人脸检测与识别需要先下载以下两个文件:

 以下是检测代码:

import dlib
import numpy as np
import cv2
detector = dlib.get_frontal_face_detector()  # 加载正脸检测器,使用dlib
sp = dlib.shape_predictor("dlibModel/shape_predictor_68_face_landmarks.dat")  # 加载人脸关键点检测模型
facerec = dlib.face_recognition_model_v1("dlibModel/dlib_face_recognition_resnet_model_v1.dat")  # 加载人脸识别模型
images_file =  "11.jpg"

if __name__ == '__main__':

    image = cv2.imread(images_file)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    rects = detector(gray, 1)  # 返回人脸,(灰度图,采样次数)
    tzs = []
    for (i, rect) in enumerate(rects):
        shape68 = sp(image, rect)  # 返回68个特征点
        face_descriptor = facerec.compute_face_descriptor(image, shape68)  # 计算人脸的128维的向量
        tz = np.array(face_descriptor)  # 人脸特征值
        tzs.append(tz)
        for p in shape68.parts():
            cv2.circle(image, (p.x, p.y), 2, (0, 0, 255), 1)
        cv2.rectangle(image, (rect.left(), rect.top()), (rect.right(), rect.bottom()), (0, 255, 0), 2)
    print(tzs)
cv2.imshow("Output", image)
cv2.waitKey(-1)

原图

效果图

 打印的特征值:

[array([-0.14579333,  0.06563152,  0.09314926, -0.17351562, -0.08750948,
       -0.03944254, -0.05778341, -0.16003576,  0.18947695, -0.20229495,
        0.22666875, -0.09615702, -0.15724032, -0.00979816, -0.02177811,
        0.23944741, -0.21378906, -0.12338841, -0.04015261,  0.00419115,
        0.07516268,  0.08075333,  0.01858632,  0.07583004, -0.1436058 ,
       -0.33890599, -0.10919157, -0.05613485, -0.01440384, -0.0421476 ,
       -0.02769968,  0.02456936, -0.19791584, -0.02495837, -0.01396229,
        0.15883508, -0.05608664, -0.16127518,  0.14932884, -0.0153573 ,
       -0.27676189,  0.08176911,  0.04226109,  0.19125782,  0.18729328,
       -0.03498868,  0.0062949 , -0.20621458,  0.1477259 , -0.20221941,
       -0.01363696,  0.06110315, -0.06132922,  0.05342007,  0.01454075,
       -0.15085547,  0.06046266,  0.14739415, -0.21987613, -0.03639037,
        0.08528309, -0.04437468,  0.03675304, -0.10165367,  0.14875233,
        0.156369  , -0.08553442, -0.22346984,  0.11466061, -0.14528711,
       -0.05263588,  0.10216871, -0.15159039, -0.19255549, -0.29263291,
       -0.04936468,  0.33185786,  0.09766599, -0.13290709,  0.03094622,
       -0.01321913, -0.01007816,  0.14334042,  0.12662645,  0.04984599,
        0.00495763, -0.08788066,  0.05117153,  0.25504613, -0.03899687,
       -0.01541797,  0.24360959, -0.00281113,  0.03764223,  0.02046303,
        0.08831443, -0.15270098,  0.07429178, -0.16997421, -0.00883572,
        0.01246334,  0.02862958, -0.01707144,  0.16006561, -0.15909354,
        0.23000078, -0.02357504, -0.0060425 , -0.02578063, -0.03944276,
       -0.05486409,  0.01571657,  0.15119481, -0.14951093,  0.16744466,
        0.17089023,  0.05214873,  0.14098236,  0.10236948,  0.05116107,
       -0.08004355,  0.01013784, -0.29215148,  0.0257933 ,  0.08701295,
       -0.02425266,  0.13502946,  0.02853448])]

进行识别时,将已有特征值与检测出的特征值两者之间计算欧式距离,设定阈值,大于阈值的不是同一人,小于阈值的为同一人

dist_ = numpy.linalg.norm(i - d_test)

numpy提供了linalg.norm方法来计算欧式距离,通常阈值设定在0.35即可

转载于:https://www.cnblogs.com/answerThe/p/11540521.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现人脸识别,需要使用OpenCV和Dlib这两个库。以下是使用C++实现人脸识别的基本步骤: 1. 安装OpenCV和Dlib库 首先需要安装OpenCV和Dlib库,并将其包含到C++项目中。可以使用以下命令在Ubuntu上安装这两个库: ``` sudo apt-get install libopencv-dev sudo apt-get install libdlib-dev ``` 2. 加载人脸识别模型 使用Dlib库提供的人脸检测器和68个关键点检测器,需要加载人脸识别模型。可使用以下代码: ``` #include <dlib/opencv.h> #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_processing.h> using namespace dlib; frontal_face_detector detector = get_frontal_face_detector(); shape_predictor sp; deserialize("shape_predictor_68_face_landmarks.dat") >> sp; ``` 3. 加载人脸数据库 将需要识别人脸图片保存到人脸数据库中。可使用以下代码加载人脸数据库: ``` std::vector<matrix<rgb_pixel>> faces; std::vector<std::string> labels; // Load faces from a directory path load_image_dataset(faces, labels, "faces"); ``` 4. 人脸检测和关键点检测 使用Dlib库提供的人脸检测器和68个关键点检测器,对待识别人脸图像进行处理,提取人脸特征。可使用以下代码: ``` // Load the input image cv::Mat inputImg = cv::imread("face.jpg"); // Convert the input image to Dlib's format cv_image<rgb_pixel> dlibImg(inputImg); // Detect faces in the image std::vector<rectangle> dets = detector(dlibImg); // Find the pose of each face std::vector<full_object_detection> shapes; for (unsigned long j = 0; j < dets.size(); ++j) { full_object_detection shape = sp(dlibImg, dets[j]); shapes.push_back(shape); } ``` 5. 人脸识别 将待识别人脸特征与人脸数据库中的特征进行比对,找到最相似的人脸。可使用以下代码: ``` // Compute the face descriptor for each face std::vector<matrix<float,0,1>> faceDescriptors; for (unsigned long i = 0; i < shapes.size(); ++i) { matrix<rgb_pixel> faceChip; extract_image_chip(dlibImg, get_face_chip_details(shapes[i],150,0.25), faceChip); faceDescriptors.push_back(net(faceChip)); } // Find the closest match in the database std::vector<double> distances; std::string bestLabel; double bestDistance = 1.0; for (unsigned long i = 0; i < faces.size(); ++i) { double distance = length(faceDescriptors[0] - faceDescriptors[i]); if (distance < bestDistance) { bestDistance = distance; bestLabel = labels[i]; } } ``` 以上是使用C++实现人脸识别的基本步骤。可以根据实际需求对代码进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值