python校正人脸_人脸矫正代码

该博客介绍了如何使用Python的dlib库进行人脸校正,通过预测关键点并计算旋转角度实现。代码包括了从灰度图处理到使用Haar级联分类器检测人脸,以及用face_recognition库获取脸部特征点来计算脸部旋转角度。
摘要由CSDN通过智能技术生成

def face_alignment(faces,show=False):

'''

faces: num * width * height * channels ,value = 0~255, dtype = np.uint8,

note: width must equal to height

'''

print(faces.shape)

if len(faces.shape)==4 and faces.shape[3]==1:

faces = faces.reshape(faces.shape[:-1]) # if gray, turns to num * width * height, no channel axis 如果是灰度图,去掉最后一维,否则predictor会报错

num = faces.shape[0]

import numpy as np

faces_aligned = np.zeros(shape=faces.shape,dtype=np.uint8)

import dlib

predictor_path = "./shape_predictor_68_face_landmarks.dat" # dlib提供的训练好的68个人脸关键点的模型,网上可以下

predictor = dlib.shape_predictor(predictor_path) # 用来预测关键点

for i in range(num):

img = faces[i]

rec = dlib.rectangle(0,0,img.shape[0],img.shape[1])

shape = predictor(np.uint8(img),rec) # 注意输入的必须是uint8类型

order=[36,45,30,48,54] # left eye, right eye, nose, left mouth, right mouth 注意关键点的顺序,这个在网上可以找

if show:

plt.pyplot.figure()

plt.pyplot.imshow(img,cmap='gray')

for j in order:

x = shape.part(j).x

y = shape.part(j).y

plt.pyplot.scatter(x,y) # 可以plot出来看看效果,这里我只plot5个点

eye_

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值