在python调用摄像头进行人脸检测中使用dlib包进行人脸特征的检测。在该检测中可以检测出人脸的68个特征点。从而判断出人脸的位置。
dlib包安装:conda install dlib
代码如下:
# coding=utf-8
import cv2
import dlib
import numpy
def pingce():
# 初始化dlib人脸检测器
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# 初始化显示窗口
#win = dlib.image_window()
cap=cv2.VideoCapture(0)
bili=[]
while cap.isOpened():
ret, cv_img = cap.read()
img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY)
dets1 = detector(img, 1)
# print("Number of faces detected: {}".format(len(dets1)))
for k, d in enumerate(dets1):
shape = predictor(img, d)
landmark = numpy.matrix([[p.x, p.y] for p in shape.parts()])
# print("face_landmark:")
# print (landmark[36][0][0,0])
# print (landmark[36][0][0,1])# 打印关键点矩阵
# print (landmark[39][0][0,0])
# print (landmark[39][0][0,1])
# print (landmark[37][0][0,0])
# print (landmark[37][0][0,1])
# print (landmark[41][0][0,0])
# print (landmark[41][0][0,1])
# print (landmark[38][0][0,0])
# print (landmark[38][0][0,1])
# print (landmark[40][0][0,0])
# print (landmark[40][0][0,1])
# pos1= (landmark[37][0][0,0], landmark[37][0][0,1])
# cv2.circle(cv_img, pos1, 3, color=(0, 255, 0))
# pos2= (landmark[40][0][0,0], landmark[40][0][0,1])
# cv2.circle(cv_img, pos2, 3, color=(0, 255, 0))
# pos3= (landmark[38][0][0,0], landmark[38][0][0,1])
# cv2.circle(cv_img, pos3, 3, color=(0, 255, 0))
# pos4= (landmark[36][0][0,0], landmark[36][0][0,1])
# cv2.circle(cv_img, pos4, 3, color=(0, 255, 0))
# pos5= (landmark[39][0][0,0], landmark[39][0][0,1])
# cv2.circle(cv_img, pos5, 3, color=(0, 255, 0))
# pos6= (landmark[41][0][0,0], landmark[41][0][0,1])
# cv2.circle(cv_img, pos6, 3, color=(0, 255, 0))
l1=numpy.sqrt(numpy.square(landmark[37][0][0,0]-landmark[41][0][0,0])+numpy.square(landmark[37][0][0,1]-landmark[41][0][0,1]))
l2=numpy.sqrt(numpy.square(landmark[38][0][0,0]-landmark[40][0][0,0])+numpy.square(landmark[38][0][0,1]-landmark[40][0][0,1]))
l3=numpy.sqrt(numpy.square(landmark[36][0][0,0]-landmark[39][0][0,0])+numpy.square(landmark[36][0][0,1]-landmark[39][0][0,1]))
lbili=(l1+l2)/(2*l3)
print("左眼高宽比:"+str(lbili))
# print (landmark[42][0][0,0])
# print (landmark[42][0][0,1])# 打印关键点矩阵
# print (landmark[46][0][0,0])
# print (landmark[46][0][0,1])
# print (landmark[44][0][0,0])
# print (landmark[44][0][0,1])
# print (landmark[43][0][0,0])
# print (landmark[43][0][0,1])
# print (landmark[45][0][0,0])
# print (landmark[45][0][0,1])
# print (landmark[47][0][0,0])
# print (landmark[47][0][0,1])
# pos7= (landmark[43][0][0,0], landmark[43][0][0,1])
# cv2.circle(cv_img, pos7, 3, color=(0, 255, 0))
# pos8= (landmark[46][0][0,0], landmark[46][0][0,1])
# cv2.circle(cv_img, pos8, 3, color=(0, 255, 0))
# pos9= (landmark[44][0][0,0], landmark[44][0][0,1])
# cv2.circle(cv_img, pos9, 3, color=(0, 255, 0))
# pos10= (landmark[42][0][0,0], landmark[42][0][0,1])
# cv2.circle(cv_img, pos10, 3, color=(0, 255, 0))
# pos11= (landmark[45][0][0,0], landmark[45][0][0,1])
# cv2.circle(cv_img, pos11, 3, color=(0, 255, 0))
# pos12= (landmark[47][0][0,0], landmark[47][0][0,1])
# cv2.circle(cv_img, pos12, 3, color=(0, 255, 0))
r1=numpy.sqrt(numpy.square(landmark[43][0][0,0]-landmark[47][0][0,0])+numpy.square(landmark[43][0][0,1]-landmark[47][0][0,1]))
r2=numpy.sqrt(numpy.square(landmark[44][0][0,0]-landmark[46][0][0,0])+numpy.square(landmark[44][0][0,1]-landmark[46][0][0,1]))
r3=numpy.sqrt(numpy.square(landmark[42][0][0,0]-landmark[45][0][0,0])+numpy.square(landmark[42][0][0,1]-landmark[45][0][0,1]))
rbili=(r1+r2)/(2*r3)
print("右眼高宽比:"+str(rbili))
# pos= (landmark[48][0][0,0], landmark[48][0][0,1])
# cv2.circle(cv_img, pos, 3, color=(0, 255, 0))
# pos= (landmark[54][0][0,0], landmark[54][0][0,1])
# cv2.circle(cv_img, pos, 3, color=(0, 255, 0))
# pos= (landmark[51][0][0,0], landmark[51][0][0,1])
# cv2.circle(cv_img, pos, 3, color=(0, 255, 0))
# pos= (landmark[57][0][0,0], landmark[57][0][0,1])
# cv2.circle(cv_img, pos, 3, color=(0, 255, 0))
r1=numpy.sqrt(numpy.square(landmark[51][0][0,0]-landmark[57][0][0,0])+numpy.square(landmark[51][0][0,1]-landmark[57][0][0,1]))
r2=numpy.sqrt(numpy.square(landmark[48][0][0,0]-landmark[54][0][0,0])+numpy.square(landmark[48][0][0,1]-landmark[54][0][0,1]))
zuibu=r1/r3
print("嘴部高宽比:"+str(zuibu))
print("----------------------------------------------------------")
# zuidata=landmark[48:,:]
# pos = (zuidata[30][0, 0], zuidata[30][0, 1])
# cv2.circle(cv_img, pos, 3, color=(0, 255, 0))
# for idx, point in enumerate(zuidata):
# for idx, point in enumerate(yan):
# # print(point[0,0])
# if(minx>point[0,0]):
# minx=point[0,0]
# if(maxx
# if(miny>point[0,1]):
# miny=point[0,1]
# if(maxy
# print(point[0,1])
# pos = (point[0, 0], point[0, 1])
# cv2.circle(cv_img, pos, 3, color=(0, 255, 0))
# for idx, point in enumerate(zuidata):
# # print(point[0,0])
# if(minx>point[0,0]):
# minx=point[0,0]
# if(maxx
# if(miny>point[0,1]):
# miny=point[0,1]
# if(maxy
# print(point[0,1])
# pos = (point[0, 0], point[0, 1])
# cv2.circle(cv_img, pos, 3, color=(0, 255, 0))
# b=(maxy-miny)/(maxx-minx)
# bili.append(b)
# 检测人脸
# dets = detector(img, 0)
# # print("Number of faces detected: {}".format(len(dets)))
# for i, d in enumerate(dets):
# # print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
# # i, d.left(), d.top(), d.right(), d.bottom()))
# cv2.rectangle(cv_img, (d.left(), d.top()), (d.right(), d.bottom()), (0, 255, 0), 2)
# 显示视频
cv2.imshow('Video', cv_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
pingce()
上述代码中间注释部分为打印输出特征点,特征点在数组中保存着,因此检测不同的部位只需要选取不同特征点的位置即可。
利用这些特征点可以进行人脸表情的判断。人脸数据集的标定。