detector = dlib.get_frontal_face_detector()
# 人脸检测
dets = detector(img, 1)
# len(dets) 即为检测到的人脸个数
print("Number of faces detected: {}".format(len(dets)))
Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。
基本语法是通过 {} 和 : 来代替以前的 % 。
format 函数可以接受不限个参数,位置可以不按顺序。
实例
>>>"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序 'hello world' >>> "{0} {1}".format("hello", "world") # 设置指定位置 'hello world' >>> "{1} {0} {1}".format("hello", "world") # 设置指定位置 'world hello world'
参考:https://www.runoob.com/python/att-string-format.html
face_landmark_detection.py, 利用训练好的 shape_predictor("shape_predictor_68_face_landmarks.dat") / 人脸 68 点特征检测器,进行人脸面部轮廓特征提取:
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
shape = predictor(img, dets[0])
dets[0]是图像中识别的第一个人脸在图像中所在的位置,得到shape和,在和img可以得到人脸的128D特征值