dlib安装_从零到壹系列(2)——Opencv+dlib实现人脸检测与关键点定位

4e531ddc790e528989bbb50cc2356d84.png

欢迎大家关注微信公众号:baihuaML白话机器学习

码字不易,如转载请私信我!!

98481e436a0b3b38a8f7e9f4bac3cc93.png

在这里,我们一起分享AI的故事。

您可以在后台留言,关于机器学习、深度学习的问题,我们会选择其中的优质问题进行回答!

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》==========================

安装dlib

pip install dlib

下载训练模型

训练模型用于是人脸识别的关键,用于查找图片的关键点。

下载地址:http://dlib.net/files/

下载文件:shape_predictor_68_face_landmarks.dat.bz2

直接上代码---人脸检测

# -*- coding: utf-8 -*-
import sys
import dlib
import cv2

detector = dlib.get_frontal_face_detector() #获取人脸分类器

# 传入的命令行参数
for f in sys.argv[1:]:
    # opencv 读取图片,并显示
    img = cv2.imread(f, cv2.IMREAD_COLOR)

    # 摘自官方文档:
    # image is a numpy ndarray containing either an 8bit grayscale or RGB image.
    # opencv读入的图片默认是bgr格式,我们需要将其转换为rgb格式;都是numpy的ndarray类。
    b, g, r = cv2.split(img)    # 分离三个颜色通道
    img2 = cv2.merge([r, g, b])   # 融合三个颜色通道生成新图片

    dets = detector(img, 1) #使用detector进行人脸检测 dets为返回的结果
    print("Number of faces detected: {}".format(len(dets)))  # 打印识别到的人脸个数
    # enumerate是一个Python的内置方法,用于遍历索引
    # index是序号;face是dets中取出的dlib.rectangle类的对象,包含了人脸的区域等信息
    # left()、top()、right()、bottom()都是dlib.rectangle类的方法,对应矩形四条边的位置
    for index, face in enumerate(dets):
        print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(), face.bottom()))

        # 在图片中标注人脸,并显示
        left = face.left()
        top = face.top()
        right = face.right()
        bottom = face.bottom()
        cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 3)
        cv2.namedWindow(f, cv2.WINDOW_AUTOSIZE)
        cv2.imshow(f, img)

# 等待按键,随后退出,销毁窗口
k = cv2.waitKey(0)
cv2.destroyAllWindows()
--------------------- 
作者:hongbin_xu 
来源:CSDN 
原文:https://blog.csdn.net/hongbin_xu/article/details/78347484 
版权声明:本文为博主原创文章,转载请附上博文链接!

效果图:

4d151ed3804f2a9121f05070bffda122.png

直接上代码---关键点定位

#coding=utf-8
 
import cv2
import dlib
 
path = "img/meinv.png"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(
    "C:Python36Libsite-packagesdlib-datashape_predictor_68_face_landmarks.dat"
)
 
dets = detector(gray, 1)
for face in dets:
    shape = predictor(img, face)  # 寻找人脸的68个标定点
    # 遍历所有点,打印出其坐标,并圈出来
    for pt in shape.parts():
        pt_pos = (pt.x, pt.y)
        cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
    cv2.imshow("image", img)
 
cv2.waitKey(0)
cv2.destroyAllWindows()

代码转自:https://yq.aliyun.com/articles/629871

效果图

95b5bb32797130fc2e57220821be5d3f.png

最新春招袭来!

一大波实习,招聘机会也陆续出现!为了大家能够更好地交流。我们也拉了几个算法面试群,感兴趣的小伙伴可以加我微信,欢迎入群~注明:面试群。另外,我们也有算法群,欢迎各位加入,注明:算法群!扫码加下面微信好友!特别提醒:不要骚扰小姐姐~~

efe465feced5aeb3c114314737141f64.png

欢迎加入深度学习、机器学习技术研讨群!

745224003

6905cd4e90510d4809e74c8608bc0b50.png

欢迎关注我们的微信公众号:baihuaML,白话机器学习

9508008d3ccc8ecd53821cad7f8b6327.png

关注知乎“会写代码的好厨师”

ab0f5535d2a791e0ff61a461bf5818f6.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
人脸比对是指将两张人脸图像进行比较,判断它们是否是同一个人。利用dlibopencv和facenet可以实现人脸比对的功能。 首先,需要用dlib进行人脸检测和对齐,再利用opencv将人脸图像转换为facenet所需的格式,最后使用facenet进行人脸特征提取和比对。 以下是一个简单的c++程序,实现了两张人脸图像的比对: ```c++ #include <iostream> #include <dlib/opencv.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include "dlib/image_processing/frontal_face_detector.h" #include "dlib/image_processing.h" #include "dlib/dnn.h" using namespace std; using namespace dlib; // 人脸对齐 void align_face(cv::Mat& img, full_object_detection& shape) { // 人脸对齐点 const int left_eye_points[6] = {36, 37, 38, 39, 40, 41}; const int right_eye_points[6] = {42, 43, 44, 45, 46, 47}; // 获取左右眼平均坐标 cv::Point2d left_eye(0, 0); cv::Point2d right_eye(0, 0); for (int i = 0; i < 6; i++) { left_eye.x += shape.part(left_eye_points[i]).x(); left_eye.y += shape.part(left_eye_points[i]).y(); right_eye.x += shape.part(right_eye_points[i]).x(); right_eye.y += shape.part(right_eye_points[i]).y(); } left_eye.x /= 6; left_eye.y /= 6; right_eye.x /= 6; right_eye.y /= 6; // 计算旋转角度 double angle = atan2(right_eye.y - left_eye.y, right_eye.x - left_eye.x) * 180 / M_PI; // 计算缩放比例 double scale = 1.0 / (right_eye.x - left_eye.x); // 构造仿射变换矩阵 cv::Mat rot_mat = cv::getRotationMatrix2D(left_eye, angle, scale); cv::Mat warp_dst; cv::warpAffine(img, warp_dst, rot_mat, img.size()); // 截取对齐后的人脸 int x = left_eye.x - (right_eye.x - left_eye.x); int y = left_eye.y - (right_eye.y - left_eye.y) / 2; int w = right_eye.x - left_eye.x + (right_eye.x - left_eye.x); int h = right_eye.y - left_eye.y + (right_eye.y - left_eye.y) / 2; cv::Mat aligned_face = warp_dst(cv::Rect(x, y, w, h)); img = aligned_face.clone(); } // 人脸比对 double face_compare(cv::Mat& img1, cv::Mat& img2) { // 加载人脸检测器和关键点检测器 frontal_face_detector detector = get_frontal_face_detector(); shape_predictor sp; deserialize("shape_predictor_68_face_landmarks.dat") >> sp; // 检测人脸并对齐 std::vector<rectangle> faces1 = detector(img1); std::vector<rectangle> faces2 = detector(img2); if (faces1.empty() || faces2.empty()) { return -1.0; } full_object_detection shape1 = sp(img1, faces1[0]); full_object_detection shape2 = sp(img2, faces2[0]); align_face(img1, shape1); align_face(img2, shape2); // 转换图像格式 matrix<rgb_pixel> face1; matrix<rgb_pixel> face2; assign_image(face1, cv_image<rgb_pixel>(img1)); assign_image(face2, cv_image<rgb_pixel>(img2)); // 加载facenet模型 anet_type net; deserialize("dlib_face_recognition_resnet_model_v1.dat") >> net; // 提取人脸特征向量 std::vector<matrix<float,0,1>> face1_feats = net(face1); std::vector<matrix<float,0,1>> face2_feats = net(face2); // 计算欧氏距离 double distance = length(face1_feats[0] - face2_feats[0]); return distance; } int main() { // 加载图片 cv::Mat img1 = cv::imread("face1.jpg"); cv::Mat img2 = cv::imread("face2.jpg"); // 人脸比对 double distance = face_compare(img1, img2); // 输出结果 if (distance >= 0) { cout << "distance: " << distance << endl; } else { cout << "no face detected" << endl; } return 0; } ``` 其中,`align_face`函数用于人脸对齐,`face_compare`函数用于人脸比对。两个函数都使用了dlibopencv库。 在这个程序中,我们使用了预训练好的人脸检测器、关键点检测器和facenet模型,它们都可以在dlib官网上下载。如果你想实现更高效的人脸比对,可以考虑使用GPU加速。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值