旋转面部检测

Mar*_*lly 26 opencv face-detection

是否有用于检测在图像平面中旋转的面的库?或者是否有某种方法可以使用级联来进行直接面部检测并使用opencv来执行此操作?

Ehs*_*Kia 9

这是我用Python cv2编写的一个简单的

它不是最有效的东西,它使用etarion提出的天真的方式,但它适用于正常的头部倾斜(它检测从-40到40头部倾斜的任何东西,这几乎与你可以倾斜你的头部一样多保持直立.

import cv2
from math import sin, cos, radians

camera =  cv2.VideoCapture(0)
face = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")

settings = {
    'scaleFactor': 1.3, 
    'minNeighbors': 3, 
    'minSize': (50, 50), 
    'flags': cv2.cv.CV_HAAR_FIND_BIGGEST_OBJECT|cv2.cv.CV_HAAR_DO_ROUGH_SEARCH
}

def rotate_image(image, angle):
    if angle == 0: return image
    height, width = image.shape[:2]
    rot_mat = cv2.getRotationMatrix2D((width/2, height/2), angle, 0.9)
    result = cv2.warpAffine(image, rot_mat, (width, height), flags=cv2.INTER_LINEAR)
    return result

def rotate_point(pos, img, angle):
    if angle == 0: return pos
    x = pos[0] - img.shape[1]*0.4
    y = pos[1] - img.shape[0]*0.4
    newx = x*cos(radians(angle)) + y*sin(radians(angle)) + img.shape[1]*0.4
    newy = -x*sin(radians(angle)) + y*cos(radians(angle)) + img.shape[0]*0.4
    return int(newx), int(newy), pos[2], pos[3]

while True:
    ret, img = camera.read()

    for angle in [0, -25, 25]:
        rimg = rotate_image(img, angle)
        detected = face.detectMultiScale(rimg, **settings)
        if len(detected):
            detected = [rotate_point(detected[-1], img, -angle)]
            break

    # Make a copy as we don't want to draw on the original image:
    for x, y, w, h in detected[-1:]:
        cv2.rectangle(img, (x, y), (x+w, y+h), (255,0,0), 2)

    cv2.imshow('facedetect', img)

    if cv2.waitKey(5) != -1:
        break

cv2.destroyWindow("facedetect")
Run Code Online (Sandbox Code Playgroud)


小智 6

就个人而言,我不知道图书馆。但是,我能说的是,使用眼睛检测Haar Cascade,并在眼睛之间画一条线。然后,您可以使用该atan功能并找到头部旋转的角度。(假设头部不旋转时,人的双眼在同一水平面上)

deg = atan( (leftEye.y - rightEye.y) / (leftEye.x - rightEye.x) )
Run Code Online (Sandbox Code Playgroud)

一旦你得到这个角度,将你的图像旋转负deg度数,你应该有一张可以使用 Haar Cascades 检测到的脸。


eta*_*ion 2

天真的方式:

  • 生成角度列表(例如,从 -170 到 180,以 10 度为步长)
  • 对于n列表中的每个角度:
    • 将图像旋转一定n角度
    • 在旋转图像上运行人脸检测器
    • 计算原始图像中检测到的人脸的位置(撤消旋转)
  • 对所有角度的连接结果执行非极大值抑制(您可能会从相邻角度获得多次检测)