CV:利用cv2+dlib库自带frontal_face_detector(人脸征检测器)实现人脸检测与人脸标记之《极限男人帮》和《NBA全明星球员》

CV:利用cv2+dlib库自带frontal_face_detector(人脸征检测器)实现人脸检测与人脸标记之《极限男人帮》和《NBA全明星球员》


输出结果

设计思路

实现代码

import cv2
import dlib
import numpy
import sys

PREDICTOR_PATH = "F:\File_Python\Resources\shape_predictor_68_face_landmarks.dat" 

detector = dlib.get_frontal_face_detector()

predictor = dlib.shape_predictor(PREDICTOR_PATH)

class NoFaces(Exception):
    pass

im = cv2.imread("F:\File_Python\Resources\Allstart.jpg")

rects = detector(im,1)

if len(rects) >= 1:
    print("{} faces detected".format(len(rects)))

if len(rects) == 0:
    raise NoFaces

for i in range(len(rects)):

    landmarks = numpy.matrix([[p.x,p.y] for p in predictor(im,rects[i]).parts()])
    im = im.copy()

    for idx,point in enumerate(landmarks):
        pos = (point[0,0],point[0,1])
        #cv2.putText(im,str(idx),pos,
                    #fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
                    #fontScale=0.4,

                    #color=(0,0,255))
        cv2.circle(im,pos,0.5,color=(0,255,0))  

cv2.namedWindow("im",2)
cv2.imshow("im",im)
cv2.waitKey(0)

相关模型下载

Landmark人脸68个关键点检测dat模型库(shape_predictor_68_face_landmarks.dat)

更多参考文献

OpenCV:利用OpenCV+dlib库实现对《极限男人帮》和《NBA全明星球员》人脸检测与人脸标记

(0)

相关推荐