opencv - 旋转人脸检测

标签 opencv face-detection

是否有用于检测在图像平面中旋转的人脸的库?或者有什么方法可以使用级联通过 opencv 进行直立人脸检测吗?

最佳答案

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

关于opencv - 旋转人脸检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5015124/

相关文章:

java - 此代码始终返回图像未加载,路径是否正确。这是 android NDK 中的 opencv

c# - 如何使用 opencv 均衡图像的对比度和亮度?

opencv - CvSVM 问题

opencv 错误 : Null pointer(Null filename) in unknown function with facedetect. cpp

python - 人脸检测后缩小图像尺寸

java - OpenCV4Android 错误 : Assertion failed in Core. minMaxLoc(mROI) 方法

c++ - 在 Windows 10/OpenCV 下访问网络摄像头的问题

javascript - Microsoft Face Api 响应时间?

python - 在 Caffe 中生成用于对象检测的 lmdb 文件

java - 人脸特征检测——眼角、眉毛