zoukankan      html  css  js  c++  java
  • python dlib opencv 人脸68点特征检测

    不得不感慨,现在现成的东西太多了,直接拿来用就行了

    dlib安装(指定版本安装,避免踩坑)

    pip install dlib==19.7.0

    dlib中训练好的文件http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

    下载解压到项目中

    代码

     1 import numpy as np
     2 import cv2 as cv
     3 import dlib
     4 
     5 detector = dlib.get_frontal_face_detector()
     6 predictor = dlib.shape_predictor('dlib/shape_predictor_68_face_landmarks.dat')
     7 
     8 
     9 def Detect_face(camera_idx):
    10     # camera_idx: 电脑自带摄像头或者usb摄像头
    11     cv.namedWindow('detect')
    12     cap = cv.VideoCapture(camera_idx)
    13 
    14     while cap.isOpened():
    15         cv.namedWindow('detect', cv.WINDOW_AUTOSIZE)
    16         ok, frame = cap.read()
    17         # frame = cv.flip(frame, 1, dst=None)
    18         if not ok:
    19             break
    20         gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    21         rects = detector(gray, 0)
    22         for i in range(len(rects)):
    23             landmarks = np.matrix([[p.x, p.y] for p in predictor(frame, rects[i]).parts()])
    24             for idx, point in enumerate(landmarks):
    25                 pos = (point[0, 0], point[0, 1])
    26                 # print(idx, pos)
    27                 cv.circle(frame, pos, 1, color=(0, 255, 0))
    28                 font = cv.FONT_HERSHEY_SIMPLEX
    29                 cv.putText(frame, str(idx + 1), pos, font, 0.4, (0, 255, 255), 1, cv.LINE_AA)
    30         cv.imshow('detect', frame)
    31         c = cv.waitKey(10)
    32         if c & 0xFF == ord('q'):
    33             break
    34     cap.release()
    35     cv.destroyAllWindows()
    36 
    37 
    38 if __name__ == '__main__':
    39     Detect_face(0)

    效果图

    真好使啊~~~

  • 相关阅读:
    21 viewPager--- hzScrollView ----llContainer
    21 ViewPager RadioGroup
    CLEAR REFRESH FEEE的区别
    在ALV中更新数据库表
    cl_gui_cfw=>flush
    cl_gui_cfw=>dispatch
    数据库表-DD01L DD02L DD03L-保存数据表和域的消息
    SAP 锁机制
    ABAP 搜索帮助
    SAP Basis常用事务代码
  • 原文地址:https://www.cnblogs.com/MC-Curry/p/10301015.html
Copyright © 2011-2022 走看看