如何使用opencv和dlib检测前额区域? [英] How can i detect the forehead region using opencv and dlib?

查看:404
本文介绍了如何使用opencv和dlib检测前额区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于研究目的,我想检测前额区域。

I want to detect the forehead region for research purposes.

我尝试将roi的宽度作为左眼最左点与左手之间的距离。右眼的最右点。就宽度而言,这似乎可行,但是当涉及到高度时,我需要一个适用于所有人的解决方案。请注意,我不希望头发出现在投资回报率中。我已经实现了高度的实现,但不适用于所有人。

I have tried taking the width of my roi as the distance between the leftmost point of the left eye and the rightmost point of the right eye. This seems to work as far as width is concerned however when it comes to height i need a solution that works for all people. Note i do not want hair to appear in my roi. I have an implementation of the height but it does not work for all people.

x = int((shape[21][0]+shape[22][0])/2)
y = int((shape[21][1]+shape[22][1])/2)
w = abs(shape[39][0]-shape[42][0])
h = abs(y-shape[29][1])

y_mid = int((y + (y - h)) / 2)
roi = image[y-h:y_mid,int(x-w/2):int(x+w/2)]

if(roi.shape[0] !=0 and roi.shape[1] != 0):
    roi = imutils.resize(roi, width=250, inter=cv2.INTER_CUBIC)
    cv2.imshow('ROI', roi)

cv2.rectangle(image,(int(x-w/2), int(y-h)), (int(x + w/2), y_mid), (0, 0, 255), 1)
cv2.imshow('Image', image)


推荐答案

检测额头的代码:

import face_recognition
import cv2
import PIL.Image
import PIL.ImageDraw
import os
#image=cv2.imread(img_path)
unknown_image = face_recognition.load_image_file("16.png")
face_locations = face_recognition.face_locations(unknown_image) # detects all the faces in image
t = len(face_locations)
print(len(face_locations))
print(face_locations)
face_landmarks_list = face_recognition.face_landmarks(unknown_image)
# Drawing rectangles over the faces
pil_image = PIL.Image.fromarray(unknown_image)
for face_location in face_locations:
    #print(face_location)
    top,right,bottom,left =face_location
    draw_shape = PIL.ImageDraw.Draw(pil_image)
    im = PIL.Image.open("16.png")
    #bottom=34
    k = face_landmarks_list[0]['right_eyebrow']
    bottom= face_landmarks_list[0]['right_eyebrow'][0][1]
    for k1 in k :   
        if(bottom>k1[1]):
            bottom=k1[1]
    k = face_landmarks_list[0]['left_eyebrow']
    lbottom= face_landmarks_list[0]['left_eyebrow'][0][1]
    for k1 in k :   
        if(lbottom>k1[1]):
            lbottom=k1[1]
    bottom=min(bottom,lbottom)
    print(bottom)
    im = im.crop((left, top, right, bottom))
    im.save("m2.jpg")    
    draw_shape.rectangle([left, top, right, bottom],outline="blue")

这篇关于如何使用opencv和dlib检测前额区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆