import mtcnn # pip install mtcnn
import cv2 as cv
def detect():
camera = cv.VideoCapture(0)
detect = mtcnn.MTCNN() # 调用mtcnn
while True:
success, frame = camera.read() # 读取每一帧
frame = cv.flip(frame,1)
if success != True:
break
face = detect.detect_faces(frame) # 人脸检测
for item in face: # 遍历
if item['confidence'] < 0.9: # 设置阈值
continue
box = item['box']
cv.rectangle(frame, (box[0], box[1]), (box[0] + box[2], box[1] + box[3]), (255, 0, 0), 2) # 绘制矩形
points = item['keypoints'] # 绘制关键点
cv.circle(frame, points['left_eye'], 2, (255, 0, 0),3)
cv.circle(frame, points['right_eye'], 2, (255, 0, 0),3)
cv.circle(frame, points['nose'], 2, (255, 0, 0),3)
cv.circle(frame, points['mouth_left'], 2, (255, 0, 0),3)
cv.circle(frame, points['mouth_right'], 2, (255, 0, 0),3)
confidence = item['confidence']
cv.putText(frame, '{}'.format(confidence), (box[0], box[1] - 10), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv.imshow("frame", frame) # 显示
if cv.waitKey(1) & 0xFF == ord(" "):
break
camera.release()
cv.destroyAllWindows()
if __name__ == "__main__":
detect()
参考文献:https://blog.csdn.net/qq_34108714/article/details/90789288