您当前的位置:首页 > 计算机 > 编程开发 > 人工智能

利用人脸81个关键点取出眉眼区域

时间:04-17来源:作者:点击数:

81关键点识别

在这里插入图片描述

眉眼区域

在这里插入图片描述

代码:

#coding=utf-8
import numpy as np
import cv2
import dlib
from scipy.spatial import distance
import os
from imutils import face_utils

# 方法 :检测出的人脸画框
def plot_rectangle(image,faces):
    for face in faces:
        image = cv2.rectangle(image,(face.left(),face.top()),(face.right(),face.bottom()),(255,0,0),4)
    return image

def catch_forehead(image_path,save_path):
    image = cv2.imread(image_path)

    cv2.imshow("原图",image)
    cv2.waitKey()

    dets = detector(image,1)
    # 检测出人脸绘制矩形
    img_result = plot_rectangle(image, dets)
    cv2.imshow("人脸检测",img_result )
    cv2.waitKey()

    sp=image.shape
    h=sp[0]
    w=sp[1]
    print("w:")
    print(w)
    print("h:")
    print(h)
    for k, d in enumerate(dets):
        print("dets{}".format(d))
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))
        shape = predictor(image, d)
        # 显示关键点
        for p in shape.parts():
            #获取横纵坐标
            pt_position = (p.x,p.y)
            #绘制关键点坐标
            cv2.circle(image,pt_position,1,(0,0,255),-1) #5:点的半径;-1:实心圆
        cv2.imshow("81关键点", image)
        cv2.waitKey()


        # shape 是81个标记点坐标
        # shape.part(0).x 是第0个坐标点的x轴
        print('shape.part(0).x,shape.part(0).y')
        print(shape.part(0).x,shape.part(0).y)
        print('shape.part(67).x,shape.part(67).y')
        print(shape.part(67).x,shape.part(67).y)

        d1=shape.part(left_eye_low_point1)
        d2=shape.part(left_eye_low_point2)
        d3=shape.part(right_eye_low_point1)
        d4=shape.part(right_eye_low_point2)
        d = max(d1.y, d2.y, d3.y, d4.y) + 10


        u1=shape.part(left_Eyebrow_up_point1)
        u2=shape.part(left_Eyebrow_up_point2)
        u3=shape.part(right_Eyebrow_up_point1)
        u4=shape.part(right_Eyebrow_up_point2)
        u = min(u1.y, u2.y, u3.y, u4.y) -10

        l = shape.part(left_Eyebrow_left_point1).x
        r = shape.part(right_Eyebrow_right_point1).x
		# forehead_ROI = frame[y2:y1, x1:x2]
        eye = image[u:d,l:r ]

        cv2.imshow('1',eye)
        cv2.waitKey()
        # cv2.imwrite(save_path,eye)


shape_detector_path="./shape_predictor_81_face_landmarks.dat"

#  眼下
left_eye_low_point1=41-1
left_eye_low_point2=42-1
right_eye_low_point1=47-1
right_eye_low_point2=48-1
# 眉上 Eyebrow
left_Eyebrow_up_point1=20-1
left_Eyebrow_up_point2=21-1
right_Eyebrow_up_point1=24-1
right_Eyebrow_up_point2=25-1

#左限制
left_Eyebrow_left_point1= 18-1
#右限制
right_Eyebrow_right_point1= 27-1


detector = dlib.get_frontal_face_detector()# 人脸检测器
predictor = dlib.shape_predictor(shape_detector_path)# 人脸特征点检测器

test_path = './6.png'
to_path = "./result.png"
catch_forehead(test_path,to_path)

在这里插入图片描述
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门