2025年4月15日 星期二 乙巳(蛇)年 正月十六 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

dilb人脸识别+68关键点检测

时间:04-17来源:作者:点击数:39

**简单的人脸识别 记个笔记 **

在这里插入图片描述
在这里插入图片描述
  • # 导入库
  • import cv2.cv2 as cv2
  • import numpy as np
  • import dlib
  • import matplotlib.pyplot as plt
  • # 方法1:显示图像
  • def show_image(image,title):
  • # opencv读取图像是BGR 而matplotlib是 RGB 格式
  • img_RGB = image[:, :, ::-1]#BGR To RGB
  • plt.title(title)
  • plt.imshow(img_RGB)
  • plt.axis("off")
  • # 方法2 :检测出的人脸画框
  • def plot_rectangle(image,faces):
  • for face in faces:
  • image = cv2.rectangle(image,(face.left(),face.top()),(face.right(),face.bottom()),(0,255,0),4)
  • return image
  • #主函数
  • def main():
  • #读取图片
  • img = cv2.imread('5.jpg')
  • #灰度化
  • img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  • #调用dlib中的检测器
  • detector = dlib.get_frontal_face_detector()
  • dets = detector(img_gray,1)#1:将图片放大一倍,便于识别人脸
  • #检测出人脸绘制矩形
  • img_result = plot_rectangle(img,dets)
  • # cv2.imwrite("6.jpg", img_result) # 将图片保存为6.jpg
  • #创建画布
  • plt.figure(figsize=(9,6))
  • plt.suptitle("face detection with dlib",fontsize=14,fontweight="bold")
  • #显示最终结果
  • show_image(img_result,"face detection")
  • plt.show() #缺少这句,函数不报错 但是不显示图像
  • if __name__ == '__main__':
  • main()

人脸识别+关键点标记68

Python+OpenCV+dlib实现人脸68个关键点检测并标注

Python+OpenCV+dlib实现人脸68个关键点检测并标注
在这里插入图片描述
  • '''
  • 人脸关键点检测——dlib
  • 1 dlib.get_frontal_face_detector() 获取人脸检测器
  • 2 dlib.shape_predictor() 预测人脸关键点
  • '''
  • # import cv2
  • #上面的无法自动补全代码
  • import cv2.cv2 as cv2
  • import matplotlib.pyplot as plt
  • import dlib
  • #读取图片
  • img = cv2.imread("8.jpg")
  • #调用人脸检测器
  • detector = dlib.get_frontal_face_detector()
  • #加载 预测关键点模型(68个关键点)
  • predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
  • #灰度化
  • gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  • #人脸检测
  • faces = detector(gray, 1) # 1:将图片放大一倍,便于识别人脸; 0 原始图像
  • #循环 遍历每一张人脸,绘制矩形框和关键点
  • for face in faces:
  • #绘制矩形框
  • cv2.rectangle(img, (face.left(), face.top()), (face.right(), face.bottom()), (0, 255, 0), 2)
  • #预测关键点
  • shape = predictor(img,face)
  • #显示关键点
  • for pt in shape.parts():
  • #获取横纵坐标
  • pt_position = (pt.x,pt.y)
  • #绘制关键点坐标
  • cv2.circle(img,pt_position,1,(0,0,255),-1) #5:点的半径;-1:实心圆
  • #显示整个效果
  • plt.imshow(img[:, :, ::-1])
  • plt.title("68_face_landmarks")
  • plt.axis("off")
  • # cv2.imwrite("dlib_68_face_landmarks8.jpg", img)
  • plt.show()
在这里插入图片描述
  • '''
  • 人脸关键点检测——dlib
  • 1 dlib.get_frontal_face_detector() 获取人脸检测器
  • 2 dlib.shape_predictor() 预测人脸关键点
  • '''
  • # import cv2
  • #上面的无法自动补全代码
  • import cv2.cv2 as cv2
  • import matplotlib.pyplot as plt
  • import dlib
  • import numpy as np
  • #读取图片
  • img = cv2.imread("5.jpg")
  • #调用人脸检测器
  • detector = dlib.get_frontal_face_detector()
  • #加载 预测关键点模型(68个关键点)
  • predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
  • #灰度化
  • gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  • #人脸检测
  • faces = detector(gray, 1) # 1:将图片放大一倍,便于识别人脸; 0 原始图像
  • for i in range(len(faces)):
  • # 绘制矩形框
  • cv2.rectangle(img, (faces[i].left(), faces[i].top()), (faces[i].right(), faces[i].bottom()), (0, 255, 0), 2)
  • # # 预测关键点
  • # shape = predictor(img, faces[i])
  • # # 显示关键点
  • # for p in shape.parts():
  • # #获取横纵坐标
  • # pt_position = (p.x,p.y)
  • # #绘制关键点坐标
  • # cv2.circle(img,pt_position,1,(0,0,255),-1) #5:点的半径;-1:实心圆
  • landmarks = np.matrix([[p.x, p.y] for p in predictor(img, faces[i]).parts()])
  • for idx, point in enumerate(landmarks):
  • # 68点的坐标
  • pos = (point[0, 0], point[0, 1])
  • # 利用cv2.circle给每个特征点画一个圈,共68个
  • cv2.circle(img, pos, 2, color=(0, 0, 255))
  • # 利用cv2.putText输出1-68
  • font = cv2.FONT_HERSHEY_SIMPLEX
  • cv2.putText(img, str(idx + 1), pos, font, 0.5, (255, 0,0 ), 1, cv2.LINE_AA)
  • print("index=" + str(idx + 1) + " x=" + str(pos[0]) + " y=" + str(pos[1]))
  • #显示整个效果
  • plt.imshow(img[:, :, ::-1])
  • plt.title("68_face_landmarks")
  • plt.axis("off")
  • plt.show()
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门