旋转人脸检测

发布于 2024-10-17 18:48:49 字数 65 浏览 9 评论 0原文

是否有用于检测在图像平面中旋转的面部的库?或者有什么方法可以使用 opencv 的级联进行直立人脸检测来做到这一点?

Is there a library for detecting faces that have been rotated in the image plane? Or is there some way in which I could use a cascade for upright face detection with opencv to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浪推晚风 2024-10-24 18:48:49

这是我用 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")

Here's a simple one I wrote with Python cv2

It's not the most efficient thing, and it uses the naive way suggested by etarion, but it works fairly well for just normal head tilting (It detect anything from -40 to 40 head tilt, which is roughly as much as you can tilt your head staying upright.

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")
神经大条 2024-10-24 18:48:49

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

deg = atan( (leftEye.y - rightEye.y) / (leftEye.x - rightEye.x) )

一旦获得此角度,将图像旋转负 deg 度,您应该有一张可以使用 Haar Cascades 检测到。

Personally, I don't know of a library. But, what I can say is, use an eye detection Haar Cascade, and draw a line between the eyes. Then, you can use the atan function and find the angle by which the head is rotated. (Assuming that the person has both eyes on the same horizontal level when head is not rotated)

deg = atan( (leftEye.y - rightEye.y) / (leftEye.x - rightEye.x) )

Once you get this angle, rotate the image you have by negative deg degrees and you should have a face which can be detected using the Haar Cascades.

很糊涂小朋友 2024-10-24 18:48:49

mtcnn 效果很好。似乎只有当脸部非常接近 90 度或 180 度时才会出现问题。所以如果正常检测失败,只需将图像旋转45度,然后重试。如果图像中存在人脸,则应该检测到它。

不过我很好奇,为什么当脸部恰好旋转 90 度或反转(旋转 180 度)时 mtcnn 会失败

mtcnn works great. It seems it has only issue when face is very near to 90 degree or 180 degree. SO if normal detection fails, just rotate the image by 45 degrees, and try again. If there is a face in the image, then this should detect it.

I am curious though, why does mtcnn fails when face is exactly 90 degree rotated or inverted (180 degrees rotated)

怀念你的温柔 2024-10-24 18:48:49

天真的方式:

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

Naive way:

  • Generate list of angles (for example, from -170 to 180 in 10 degree steps)
  • For each angle n in the list:
    • Rotate image by n degrees
    • Run face detector on rotated image
    • Compute the position of the detected faces in the original image (undo the rotation)
  • Perform non-maximum suppression on the joined result from all angles (you will likely get multiple detections from neighbouring angles)
静赏你的温柔 2024-10-24 18:48:49

您可以使用带有约束 AAM、ASM 方法的词袋/特征袋方法。
但他们也可以给出不收敛于全局最大值的最优解。

另外,haar-like-features 只是特征的集合,您可以使用旋转不变特征并将其放入 adaboost 分类器中。

you can use bag of words/bag of features method with constrains AAM,ASM methods.
but they also can give not optimal solution converges not to global maximum.

also haar-like-features are just collection of features and you can use rotation invariant features and put it then in adaboost classifer.

月朦胧 2024-10-24 18:48:49

我一直在处理非正面图像的人脸检测的相同问题。尝试使用多任务 CNN。它是人脸检测和对齐的最佳解决方案。它能够处理各种姿势、光照、遮挡等问题。

该论文可通过链接获取。该代码可在 GitHub 上找到:链接。我使用了 python 实现,结果非常出色。虽然如果图像有很多人脸,代码会有点慢。

虽然如果你想坚持使用 OpenCV,那么 OpenCV 中已经添加了一个新的用于人脸检测的深度学习模型。结果不如Multi Task CNN。 pyimagesearch 有一个用于人脸检测的 OpenCV 深度学习模型的实现 链接

I had been dealing with the same problem of face detection for non-frontal images. Try using Multi Task CNN. It's the best solution for face detection and alignment. It is able to deal with problems like various poses, lighting, occlusion.

The paper is available at Link. The code is available on GitHub at Link. I used the python implementation and the results are outstanding. Although the code is a little slow if the image has a lot of faces.

Although if you want to stick to OpenCV, then a new deep learning model for face detection has been added to OpenCV. The results are not as good as Multi Task CNN. There's an implementation of OpenCV Deep Learning Model for Face Detection at pyimagesearch Link

凯凯我们等你回来 2024-10-24 18:48:49

此存储库可以将对象检测为旋转边界框: https://github.com/NVIDIA/retinanet-examples< /a>

您可以通过将包含“人脸”类别的图像随机旋转 -30 到 30 度,从 Open Images 创建数据集,然后训练该网络来检测这些人脸。

This repo can detect objects as rotated bounding boxes: https://github.com/NVIDIA/retinanet-examples

You could create a dataset from Open Images by randomly rotating images containing the 'human faces' class by -30 to 30 degrees, then train this network to detect those faces.

灯角 2024-10-24 18:48:49

基于颜色直方图的人脸检测方法与人脸方向无关。

Methods for face detection based on color histogram are independent of face orientation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文