OpenCV + Dlib對人臉位置進行校準

視覺智慧發表於2018-02-22

 

# 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:
        c

相關文章