计算机视觉之dlib人脸关键点绘制及微笑测试

打印 上一主题 下一主题

主题 979|帖子 979|积分 2947

dlib人脸关键点绘制及微笑测试


  
1 dlib人脸关键点


1.1 dlib

dlib 是一个强盛的呆板学习库,广泛用于人脸检测和人脸关键点检测。它提供了一个预训练的 68 点人脸关键点检测模型,可以正确地定位人脸的各个部位(如眼睛、鼻子、嘴巴等)
1.2 人脸关键点检测

dlib 的 68 点人脸关键点检测模型基于 HOG(Histogram of Oriented Gradients)特性和线性分类器,联合了形状预测算法。它可以检测人脸的以下区域:
下巴(0-16)
右眉毛(17-21)
左眉毛(22-26)
鼻子(27-35)
右眼(36-41)
左眼(42-47)
嘴巴(48-67)


1.3 检测模型

dlib 提供了一个预训练的 68 点人脸关键点检测模型,可以从以下链接下载:
https://github.com/davisking/dlib-models/blob/master/shape_predictor_68_face_landmarks.dat.bz2/
1.4 凸包

凸包(Convex Hull) 是计算多少中的一个重要概念,指的是在二维或更高维空间中,包含一组点的最小凸多边形或凸多面体。凸包在图像处置惩罚、计算机视觉、模式辨认等领域有广泛应用,例如在人脸关键点检测中,可以用凸包来界说人脸区域的边界
1.5 笑容检测

界说了两个函数,MAR:衡量嘴巴的伸开水平,
和MJR:衡量嘴巴宽度与下巴宽度的比例,
人脸关键点如上,当微笑时嘴巴长款和面颊长度都会发生改变,通过两个函数举行比较检测,举行判定是否微笑
  1. def MAR(shape):
  2.     x = shape[50]
  3.     y = shape[50].reshape(1,2)
  4.     A = euclidean_distances(shape[50].reshape(1,2),shape[58].reshape(1,2))
  5.     B = euclidean_distances(shape[51].reshape(1,2),shape[57].reshape(1,2))
  6.     C = euclidean_distances(shape[52].reshape(1,2),shape[56].reshape(1,2))
  7.     D = euclidean_distances(shape[48].reshape(1,2),shape[54].reshape(1,2))
  8.     return ((A+B+C)/3)/D
  9. def MJR(shape):
  10.     M = euclidean_distances(shape[48].reshape(1,2),shape[54].reshape(1,2))
  11.     J = euclidean_distances(shape[3].reshape(1,2),shape[13].reshape(1,2))
  12.     return M/J
复制代码
1.6 函数



  • detector = dlib.get_frontal_face_detector()加载人脸检测器
  • predictor = dlib.shape_predictor(‘shape_predictor_68_face_landmarks.dat’) 关键点预测器
  • detector(gray, 1)检测人脸

    • gray检测的灰度图
    • 1 表示对图像举行上采样次数

2 人脸检测代码


2.1 关键点绘制

代码展示:
  1. import cv2
  2. import numpy as np
  3. import dlib
  4. img = cv2.imread('lyf.png')
  5. detector = dlib.get_frontal_face_detector()
  6. faces = detector(img,0)
  7. predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
  8. for face in faces:
  9.     shape = predictor(img,face)
  10.     landmarks = np.array([[p.x,p.y] for p in shape.parts()])
  11.     for idx,point in enumerate(landmarks):
  12.         pos = [point[0],point[1]]
  13.         cv2.circle(img,pos,2,color=(0,255,0),thickness=-1)
  14.         cv2.putText(img,str(idx),pos,cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,0.4,(255,255,255),1,cv2.LINE_AA)
  15. cv2.imshow('img',img)
  16. cv2.waitKey(0)
  17. cv2.destroyAllWindows()
复制代码
运行结果:

2.2 关键点连线

代码展示:
  1. import cv2
  2. import numpy as np
  3. import dlib
  4. def drawLine(start,end):
  5.     pts = shape[start:end]
  6.     for l in  range(1,len(pts)):
  7.         pta = tuple(pts[l-1])
  8.         ptb = tuple(pts[l])
  9.         cv2.line(img,pta,ptb,(0,255,0),1)
  10. def drawConvexHull(start,end):
  11.     facial = shape[start:end+1]
  12.     mouthHull = cv2.convexHull(facial)
  13.     cv2.drawContours(img,[mouthHull],-1,(0,255,0),1)
  14. img = cv2.imread('lyf.png')
  15. detector = dlib.get_frontal_face_detector()
  16. faces = detector(img,0)
  17. predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
  18. for face in faces:
  19.     shape = predictor(img,face)
  20.     shape = np.array([[p.x,p.y] for p in shape.parts()])
  21.     drawConvexHull(36,41)
  22.     drawConvexHull(42,47)
  23.     drawConvexHull(48, 59)
  24.     drawConvexHull(60, 67)
  25.     drawLine(0,17)
  26.     drawLine(17, 22)
  27.     drawLine(22, 27)
  28.     drawLine(27, 36)
  29. cv2.imshow('img',img)
  30. cv2.waitKey(0)
  31. cv2.destroyAllWindows()
复制代码
运行结果:

2.3 微笑检测

代码展示:
  1. import cv2import numpy as npimport dlibdetector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')v = cv2.VideoCapture('jjy_dyx.mp4')from sklearn.metrics.pairwise import euclidean_distancesfrom PIL import Image, ImageDraw, ImageFontdef cv2AddChineseText(img, text, position, textColor=(255, 255, 255), textSize=30):    """ 向图片中添加中文 """    if (isinstance(img, np.ndarray)):  # 判定是否OpenCV图片类型        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))#实现array到image的转换    draw = ImageDraw.Draw(img)# 在img图片上创建一个画图的对象    # 字体的格式    fontStyle = ImageFont.truetype("simsun.ttc", textSize, encoding="utf-8")    draw.text(position, text, textColor, font=fontStyle) # 绘制文本    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)# 转换回OpenCV格式def MAR(shape):
  2.     x = shape[50]
  3.     y = shape[50].reshape(1,2)
  4.     A = euclidean_distances(shape[50].reshape(1,2),shape[58].reshape(1,2))
  5.     B = euclidean_distances(shape[51].reshape(1,2),shape[57].reshape(1,2))
  6.     C = euclidean_distances(shape[52].reshape(1,2),shape[56].reshape(1,2))
  7.     D = euclidean_distances(shape[48].reshape(1,2),shape[54].reshape(1,2))
  8.     return ((A+B+C)/3)/D
  9. def MJR(shape):
  10.     M = euclidean_distances(shape[48].reshape(1,2),shape[54].reshape(1,2))
  11.     J = euclidean_distances(shape[3].reshape(1,2),shape[13].reshape(1,2))
  12.     return M/J
  13. while True:    r,img = v.read()    if not r:        break    faces = detector(img,0)    for face in faces:        shape = predictor(img,face)        shape= np.array([[p.x,p.y] for p in shape.parts()])        mar = MAR(shape)        mjr =MJR(shape)        result = '正常'        print('mar:',mar,'mjr:',mjr)        if mar>0.5:            result = '大笑'        elif mjr>0.4:            result = '微笑'        mouthHull = cv2.convexHull(shape[48:61])        img = cv2AddChineseText(img,result,mouthHull[0,0],1)        cv2.drawContours(img,[mouthHull],-1,(0,255,0),1)    cv2.imshow('img', img)    key = cv2.waitKey(1)    if key == 32:        breakv.release()cv2.waitKey(0)cv2.destroyAllWindows()
复制代码
运行结果:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

来自云龙湖轮廓分明的月亮

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表