dlib人脸配准(人脸对齐)

article/2025/9/23 15:07:15

dlib人脸配准有两种方式。

  1. 一种是使用 get_face_chip()方法,使用5个关键点模型来进行配准,这种方法Dlib已经提供了完整的接口(imutils里也有类似函数, face_utils.FaceAligner,代码放在最后面)
  2. 另一种是自己使用68点关键点模型,根据关键点信息求解变换矩阵,然后把变换矩阵应用到整个图像上。

(1)使用 get_face_chip()方法实验结果:
在这里插入图片描述

import cv2
import dlib
import matplotlib.pyplot as plt# 获取图片
image = cv2.imread('33.jpg')image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)# 使用特征提取器get_frontal_face_detector
detector = dlib.get_frontal_face_detector()dets = detector(image_gray, 1)for det in dets:# 将框画在原图上# cv2.rectangle  参数1:图片, 参数2:左上角坐标, 参数2:左上角坐标, 参数3:右下角坐标, 参数4:颜色(R,G,B), 参数2:粗细my_img = cv2.rectangle(image, (det.left(), det.top()), (det.right(), det.bottom()), (0, 255, 0), 2)cv2.imshow("image", image)
cv2.waitKey(0)# 人脸检测器
predictor = dlib.shape_predictor(r'./shape_predictor_68_face_landmarks.dat')
#
for det in dets:shape = predictor(image, det)# 将关键点绘制到人脸上for i in range(68):cv2.putText(image, str(i), (shape.part(i).x, shape.part(i).y), cv2.FONT_HERSHEY_DUPLEX, 0.1, (0, 255,0 ), 1, cv2.LINE_AA)cv2.circle(image, (shape.part(i).x, shape.part(i).y), 1, (0, 0, 255))cv2.imshow("rotated", image)
cv2.waitKey(0)# 人脸对齐
image = dlib.get_face_chip(image, shape, size = 150)
cv2.imshow("68landmarks", image)
cv2.waitKey(0)

原理参考:博文1 博文2

(2)使用68点关键点模型方法如下:
人脸对齐思路:

  • 分别计算左、右眼中心坐标
  • 计算左右眼中心坐标连线与水平方向的夹角θ
  • 计算左右两眼整体中心坐标
  • 以左右两眼整体中心坐标为基点,将图片array逆时针旋转θ

实验结果如下:
在这里插入图片描述

import cv2
import dlib
import numpy as npclass Face_Align(object):def __init__(self, shape_predictor_path):self.detector = dlib.get_frontal_face_detector()self.predictor = dlib.shape_predictor(shape_predictor_path)self.LEFT_EYE_INDICES = [36, 37, 38, 39, 40, 41]self.RIGHT_EYE_INDICES = [42, 43, 44, 45, 46, 47]def rect_to_tuple(self, rect):left = rect.left()right = rect.right()top = rect.top()bottom = rect.bottom()return left, top, right, bottomdef extract_eye(self, shape, eye_indices):points = map(lambda i: shape.part(i), eye_indices)return list(points)def extract_eye_center(self, shape, eye_indices):points = self.extract_eye(shape, eye_indices)xs = map(lambda p: p.x, points)ys = map(lambda p: p.y, points)return sum(xs) // 6, sum(ys) // 6def extract_left_eye_center(self, shape):return self.extract_eye_center(shape, self.LEFT_EYE_INDICES)def extract_right_eye_center(self, shape):return self.extract_eye_center(shape, self.RIGHT_EYE_INDICES)def angle_between_2_points(self, p1, p2):x1, y1 = p1x2, y2 = p2tan = (y2 - y1) / (x2 - x1)print("旋转角度:",np.degrees(np.arctan(tan)))return np.degrees(np.arctan(tan))def get_rotation_matrix(self, p1, p2):angle = self.angle_between_2_points(p1, p2)x1, y1 = p1x2, y2 = p2xc = (x1 + x2) // 2yc = (y1 + y2) // 2M = cv2.getRotationMatrix2D((xc, yc), angle, 1)print("旋转矩阵:",M)return Mdef crop_image(self, image, det):left, top, right, bottom = self.rect_to_tuple(det)return image[top:bottom, left:right]def __call__(self, image=None, image_path=None, save_path=None, only_one=True):'''Face alignment, can select input image variable or image path, when inputimage format that return alignment face image crop or image path as inputwill return None but save image to the save path.:image: Face image input:image_path: if image is None than can input image:save_path: path to save image:detector: detector = dlib.get_frontal_face_detector():predictor: predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")'''if image is not None:# convert BGR format to Grayimage_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)elif image_path is not None:image_gray = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)image = cv2.imread(image_path)height, width = image.shape[:2]print("原图形状:",image.shape)  # 获取图像的形状,返回值是一个包含行数、列数、通道数的元组。# Dector facedets = self.detector(image_gray, 1)# i donate the i_th face detected in imagecrop_images = []for i, det in enumerate(dets):shape = self.predictor(image_gray, det)left_eye = self.extract_left_eye_center(shape)right_eye = self.extract_right_eye_center(shape)M = self.get_rotation_matrix(left_eye, right_eye)rotated = cv2.warpAffine(image, M, (width, height), flags=cv2.INTER_CUBIC)cv2.imshow("xuanzhuan", rotated)cv2.waitKey(0)cropped = self.crop_image(rotated, det)if only_one == True:if save_path is not None:cv2.imwrite(save_path, cropped)return croppedelse:crop_images.append(cropped)return crop_images
if __name__ == "__main__":image = cv2.imread('33.jpg')cv2.imshow("image", image)cv2.waitKey(0)align = Face_Align("./shape_predictor_68_face_landmarks.dat")align = align(image,image_path=None,save_path="test.jpg",only_one=True)print(align.shape)cv2.imshow("align", align)cv2.waitKey(0)

原理参考:博文

face_utils.FaceAligner 主要代码

from imutils import face_utilsdetector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
fa = face_utils.FaceAligner(predictor, desiredFaceWidth=256)gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
dets = self.detector(gray, 1) 
shape= self.predictor(gray, dets[0])frame_align = fa.align(frame, gray, dets[0])  # align face 人脸对齐后裁剪cv2.imshow("frame_align", frame_align)
cv2.waitKey(0)

http://chatgpt.dhexx.cn/article/Nb1bCNw3.shtml

相关文章

基于OpenCV的人脸对齐步骤详解及源码实现

目录 1. 前言2. 人脸对齐基本原理与步骤3. 人脸对齐代码实现 1. 前言 在做人脸识别的时候,前期的数据处理过程通常会遇到一个问题,需要将各种人脸从不同尺寸的图像中截取出来,再进行人脸对齐操作:即将人脸截取出来并将倾斜的人脸…

人脸识别 (4) 人脸对齐

参考:FaceDetector/face_align.ipynb at master faciallab/FaceDetector GitHub 中文:从零开始搭建人脸识别系统(二):人脸对齐 - 知乎 Face Alignment Step-by-Step 1、Align Faces by Spatial Transform Operati…

OpenCV实现人脸对齐

点击上方“小白学视觉”,选择加"星标"或“置顶” 重磅干货,第一时间送达 一、人脸对齐介绍 在人脸识别中有一个重要的预处理步骤-人脸对齐,该操作可以大幅度提高人脸识别的准确率与稳定性,但是早期的OpenCV版本不支持人…

人脸对齐(一)--定义及作用

参考: http://www.thinkface.cn/thread-4354-1-1.html http://www.thinkface.cn/thread-4488-1-1.html 人脸对齐任务即根据输入的人脸图像,自动定位出面部关键特征点,如眼睛、鼻尖、嘴角点、眉毛以及人脸各部件轮…

人脸关键点对齐

转:https://www.jianshu.com/p/e4b9317a817f 摘要: 从传统方法到深度学习方法,对人脸关键点定位/人脸对齐的发展进行梳理,对该领域中经典的方法,最新成果进行汇总,并给出相应的paper原文,项目主…

人脸对齐介绍

转自:https://cloud.tencent.com/community/article/532672 一、 人脸对齐,也叫做人脸特征点检测,图为人脸特征点例子 二、 人脸对齐有哪些应用? 1 五官定位 2 表情识别 3 人脸漫画、素描生成 4 增强现实 5 换脸 6 3D建模 三、 人…

常见的人脸对齐方法 python

人脸对齐 1. 通过Dlib库 1.1.环境需求: opencv-python dlib下载dlib库的68关键点文件: http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 然后解压后得到shape_predictor_68_face_landmarks.dat。 其次,下面可能需要有一…

QGIS中也有“fishnet”——QGIS怎么生成渔网

QGIS堪称Mac上的ArcGIS,免费小巧,插件丰富,比ArcGIS更容易上手。今天分享的是如何在QGIS中生成规则网格,也就是ArcGIS中的fishnet功能怎么在QGIS中完美实现。 # 环境声明:QGIS3.10 MacOS10.15.6 # 分享背景&#xff…

ArcMap Fishnet生成规则网格

在利用ArcGIS处理数据时,有时需要对整个工作区域进行规则网格划分,这个在ArcGIS中是一件非常简单的事情,现在就将利用ArcGIS生成规则网格的步骤详细的介绍一下。 首先我有一个工作的范围,比如图一所示的范围: 我要在研…

POJ 1408 Fishnet

题目大意: 有一个1*1的正方形,分别给出下,上,左,右边每个边上的n个点,对边对应点连线,问这些线段相交的最大的四边形面积是多少(面积最大的定义是必须当前面积内没有更小的四边形内…

创建渔网工具

创建渔网(create fishnet)工具是指创建由矩形像元组成的渔网。输出可以是折线或面要素。创建渔网需要三条基本信息:渔网的空间范围、行数和列数以及旋转的角度。要指定这些基本信息可通过多种方法。例如,您可能不确定准确的行数和…

ARCGIS怎么在地图上绘制长宽500m*500m的fishnet

用的是ArcGIS 10.6的版本。 参考教程:https://my.oschina.net/u/4416039/blog/3308651 首先先导入深圳市的shp文件 Layers右键,点击Properties 需要将坐标系改成投影坐标系 点击Projected Coordinate Systems 点击UTM 点击WGS 1984 选择WGS 1984 UT…

fishboat

渔船产品功能说明文档1.0 线上地址:http://101.200.121.215:9090/ git前端: https://gitee.com/cinblx/fishboat-ui.git git后端:https://gitee.com/cinblx/fishboat-server.git 产品介绍 为什么使用该产品 该产品致力于两方面。1.实现基础功能&#xff…

文献阅读笔记5——《Composited FishNet: Fish Detection and Species Recognition From Low-Quality ...》

《Composited FishNet: Fish Detection and Species Recognition From Low-Quality Underwater Videos》 写文章模板论文 Abstact 重要意义:水下视频中鱼类的自动检测和识别对于渔业资源评估和生态环境监测具有重要意义。 问题和挑战:由于水下图像质量…

使用Arc Map创建渔网(fishnet)

使用Arc Map创建渔网(fishnet) 工具位置:ArcToolbox----数据管理工具----采样----创建渔网 (Arcgis 10.2以上版本)(找不到工具可以ControlF选择工具项搜索) 添加图层,设置坐标系&am…

arcgis fishnet渔网功能学习

听说要来跟我请教fishnet功能,吓我一跳,arcgis不就只有个创建渔网功能吗??纳尼?! 赶紧捡捡知识点,避免一问三不知.。 版本12.4后工具位于data management/sampling/create fishnet,12.4之前应…

POJ1408-Fishnet

全解题报告索引目录 -> 【北大ACM – POJ试题分类】 转载请注明出处:http://exp-blog.com ------------------------------------------------------------------------- 大致题意: 一个1X1的正方形,每条边上有n个不同的点(…

关于在ArcGIS里创建fishnet时只有几个网格的解决办法

在ArcGIS里创建渔网时可能会出现以下情况,例如只有两个网格 可以看到在创建渔网的窗口中导入的数据中上下左右的单位是经纬度,不是米制单位。 这是由于在创建渔网时没有将坐标系进行转换,利用的shp数据本身的坐标系是度分秒单位的&#xf…

【ArcGIS微课1000例】0002:创建渔网(Create fishnet)

本文讲解ArcGIS软件中渔网(fishnet)工具的原理,方法及使用技巧。 文章目录 微课目标工具介绍实现过程微课目标 如下图所示,影像为无人机航测生产的DOM,现在需要在ArcGIS平台中进行DLG数据采集(数字化),由于测区较大,需要创建500*500的渔网,并对影像进行裁剪下发给多…

ArcGIS 10.2生成渔网(fishnet)

https://blog.csdn.net/lucky51222/article/details/72514885 工具路径:Data Management Tools→Feature Class→Create Fishnet。 (1)确定输出路径及文件名; (2)选择渔网范围,本例选择北方地区…