dlib 人脸检测原理
程序员文章站
2024-03-19 16:51:04
...
找了半天终于找到一篇blog里面说了dlib内部使用的算法
https://talengu.github.io/public/2018/11/12/cv/FaceDetectionComparison/
发现其实是把dlib的examples说了一下
dlib github部分源码
https://github.com/davisking/dlib-models
dlib官方文档
http://dlib.net/python/index.html#dlib.mmod_rectangles
dlib的HOG人脸检测检测不出来下面这张图
换成CNN方法就可以检测出来了
import dlib
import cv2
PREDICTOR_PATH = r'shape_predictor_5_face_landmarks.dat'
DETECTOR_PATH = r'mmod_human_face_detector.dat'
detector = dlib.cnn_face_detection_model_v1(DETECTOR_PATH)
predictor = dlib.shape_predictor(PREDICTOR_PATH)
output_path = "OUTPUT"
def get_landmarks(im, fname):
#gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
print('start detecting')
dets = detector(im, 1)
#print(rects)
if len(dets) == 0:
print('no face')
return
print(dets[0].rect.left(), dets[0].rect.top(), dets[0].rect.right(), dets[0].rect.bottom())
cv2.rectangle(im, (dets[0].rect.left(), dets[0].rect.top()), (dets[0].rect.right(), dets[0].rect.bottom()), (0,255,0), 2)
print('start predicting')
rects = dlib.rectangles()
rects.extend([d.rect for d in dets])
res = predictor(im, rects[0]).parts()
print('end predicting')
for re in res:
cv2.circle(im, (re.x, re.y), 10, (0,0, 255), -1)
print(os.path.join(output_path, fname))
cv2.imwrite(os.path.join(output_path, fname), im)
im = cv2.imread('jinsha.jpg')
get_landmarks(im, 'jinsha.jpg')
上一篇: oracle数据去重多条件