OpenCV + Dlib对人脸位置进行校准

本文通过使用dlib和OpenCV库实现了一个实时的人脸位置校准系统。该系统能够捕捉摄像头画面并检测出人脸的位置,进一步对人脸进行精确地标记。即使在复杂背景下也能准确地定位到人脸特征点。
# coding:utf-8
'''
人脸位置校准
'''
import cv2
import dlib

predictor_path = 'temp/dlib/shape_predictor_5_face_landmarks.dat'
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predictor_path)

camera = cv2.VideoCapture(0)
if not camera.isOpened():
    print("cannot open camear")
    exit(0)

while True:
    ret,frame = camera.read()
    
    if not ret:
        continue
    cv2.imshow('camera',frame)
    frame_new = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
    # 检测脸部
    dets = detector(frame_new, 1)
    print("Number of faces detected: {}".format(len(dets)))    
    num_faces = len(dets)
    if num_faces == 0:
        print("Sorry, there were no faces found in current frame")
        continue
    # 查找脸部位置
    faces = dlib.full_object_detections()
    for detection in dets:
        faces.append(sp(frame_new, detection))    
    images = dlib.get_face_chips(frame_new, faces, size=320)
    for image in images:
        cv_bgr_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        cv2.imshow('image',cv_bgr_img)
    image = dlib.get_face_chip(frame_new, faces[0])
    cv_bgr_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    cv2.imshow('image',cv_bgr_img)
    
    key = cv2.waitKey(1)
    if key == 27:
        break

cv2.destroyAllWindows()

程序运行结果:

转载于:https://my.oschina.net/wujux/blog/1622777

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值