python利用dlib获取人脸的68个landmark

yipeiwu_com5年前Python基础

(1) 单人脸情况

import cv2
import dlib

path = "1.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#人脸检测画框
detector = dlib.get_frontal_face_detector()
# 获取人脸关键点检测器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
#获取人脸框位置信息
dets = detector(gray, 1)#1表示采样(upsample)次数 0识别的人脸少点,1识别的多点,2识别的更多,小脸也可以识别
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, 0, 255), 1)#img, center, radius, color, thickness

  cv2.imshow("image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

(2) 多人脸情况

import cv2
import dlib

path1 = "zxc.jpg"
img = cv2.imread(path1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#人脸检测画框
detector = dlib.get_frontal_face_detector()
# 获取人脸关键点检测器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
#获取人脸框位置信息
dets = detector(gray, 1)#1表示采样(upsample)次数 0识别的人脸少点,1识别的多点,2识别的更多,小脸也可以识别

for i in range(len(dets)):
  shape = predictor(img, dets[i]) # 寻找人脸的68个标定点
  # 遍历所有点,打印出其坐标,并圈出来
  for pt in shape.parts():
    pt_pos = (pt.x, pt.y)
    cv2.circle(img, pt_pos, 2, (0, 0, 255), 1)#img, center, radius, color, thickness

cv2.imshow("image", img)

cv2.waitKey(0)#等待键盘输入
cv2.destroyAllWindows()

(3) 获取电脑摄像头实时识别标定

import cv2
import dlib
import numpy as np

cap = cv2.VideoCapture(0)#打开笔记本的内置摄像头,若参数是视频文件路径则打开视频
cap.isOpened()

def key_points(img):
  points_keys = []
  PREDICTOR_PATH = "shape_predictor_68_face_landmarks.dat"
  detector = dlib.get_frontal_face_detector()
  predictor = dlib.shape_predictor(PREDICTOR_PATH)
  rects = detector(img,1)

  for i in range(len(rects)):
    landmarks = np.matrix([[p.x,p.y] for p in predictor(img,rects[i]).parts()])
    for point in landmarks:
      pos = (point[0,0],point[0,1])
      points_keys.append(pos)
      cv2.circle(img,pos,2,(255,0,0),-1)
  return img

while(True):
  ret, frame = cap.read()#按帧读取视频,ret,frame是cap.read()方法的两个返回值。其中ret是布尔值,如果读取帧是正确的则返回True,如果文件读取到结尾,它的返回值就为False。frame就是每一帧的图像,是个三维矩阵。
  # gray = cv2.cvtColor(frame)
  face_key = key_points(frame)
  cv2.imshow('frame',face_key)
  if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()#释放摄像头
cv2.destroyAllWindows()#关闭所有图像窗口

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

闭包在python中的应用之translate和maketrans用法详解

相对来说python对字符串的处理是比较高效的,方法也有很多。其中maketrans和translate两个方法被应用的很多,本文就针对这两个方法的用法做一总结整理。 首先让我们先回顾下...

python 列表输出重复值以及对应的角标方法

如下所示: a = [99,1,2,1,3,4] # 集合存储重复数据 b=set() for i in a: if a.count(i)>1: b.update(...

Python语言进阶知识点总结

Python语言进阶知识点总结

数据结构和算法 算法:解决问题的方法和步骤 评价算法的好坏:渐近时间复杂度和渐近空间复杂度。 渐近时间复杂度的大O标记: - 常量时间复杂度 - 布隆过滤器 / 哈希存储 - 对数时间复...

python使用Plotly绘图工具绘制柱状图

python使用Plotly绘图工具绘制柱状图

本文实例为大家分享了python使用Plotly绘图工具绘制柱状图的具体代码,供大家参考,具体内容如下 使用Plotly绘制基本的柱状图,需要用到的函数是graph_objs 中 Bar...

Python3字符串学习教程

字符串类型是python里面最常见的类型,是不可变类型,支持单引号、双引号、三引号,三引号是一对连续的单引号或者双引号,允许一个字符串跨多行。 字符串连接:前面提到的+操作符可用于字符串...