gpt4 book ai didi

opencv - 旋转人脸检测

转载 作者:太空宇宙 更新时间:2023-11-03 20:37:21 25 4
gpt4 key购买 nike

是否有用于检测在图像平面中旋转的人脸的库?或者有什么方法可以使用级联通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com