倾斜头检测失败了,但在现场流中成功

发布于 2025-02-01 15:46:07 字数 2094 浏览 3 评论 0原文

我正在研究一个项目,需要从图像中检测到面部并将其存储到目录以进行进一步使用。 我正在使用openCV(haar_cascade_classifier) 它仅检测到直头。为了检测倾斜的头。我尝试了音调版本haar_cascade.但是问题仅在实时视频中检测到面。如果我运行web camera并使用haar_cascade_classifier_atl2.xml文件检测。它检测到倾斜的头部,但是如果我从图像目录中运行循环并传递图像以检测面部,则不会检测到相同的角度。意味着在视频中检测到,而不是从图像(相同角度)中检测到的。 这是代码,

import cv2
from math import sin, cos, radians
import time
import fnmatch
import os

total_number_of_frame = 0
framedirectory = "D:/finalpaper/extractedframes"
number_of_extracted_frames = len(fnmatch.filter(os.listdir('D:/finalpaper/extractedframes'), '*.png'))
print(number_of_extracted_frames)
face = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_alt2.xml")

settings = {
    'scaleFactor': 1.3,
    'minNeighbors': 3,
    'minSize': (50, 50)
}

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]

for i in range(number_of_extracted_frames):
    framenumber = str(i)+".png"
    #joining path to directory
    filepath = os.path.join(framedirectory,framenumber)
    frame = cv2.imread(filepath)
    for angle in [0, -25, 25]:
        rimg = rotate_image(frame, angle)
        detected = face.detectMultiScale(rimg, **settings)
        if len(detected):
            detected = [rotate_point(detected[-1], frame, -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(frame, (x, y), (x+w, y+h), (255,0,0), 2)


    cv2.imshow("frame",frame)
    cv2.waitKey(700)

如果我使用的代码与Web摄像机一起运行,则它会检测到面部,但是如果我使用的循环未检测到倾斜头。

I am working on a project where I need to detect face from the image and store it to directory for further use.
I am using openCV (Haar_cascade_classifier)
It is detecting only straight head. for detection of the tilted head.I tried tune version haar_cascade.but the problem it is only detecting face in live video.If I run webcamera and detect using haar_cascade_classifier_atl2.xml file . it detect the tilted head but if If I run loop from my image directory and pass images to detect face it is not detecting the same angle.means It is detecting in video but not from image (same angle)
here is the code

import cv2
from math import sin, cos, radians
import time
import fnmatch
import os

total_number_of_frame = 0
framedirectory = "D:/finalpaper/extractedframes"
number_of_extracted_frames = len(fnmatch.filter(os.listdir('D:/finalpaper/extractedframes'), '*.png'))
print(number_of_extracted_frames)
face = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_alt2.xml")

settings = {
    'scaleFactor': 1.3,
    'minNeighbors': 3,
    'minSize': (50, 50)
}

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]

for i in range(number_of_extracted_frames):
    framenumber = str(i)+".png"
    #joining path to directory
    filepath = os.path.join(framedirectory,framenumber)
    frame = cv2.imread(filepath)
    for angle in [0, -25, 25]:
        rimg = rotate_image(frame, angle)
        detected = face.detectMultiScale(rimg, **settings)
        if len(detected):
            detected = [rotate_point(detected[-1], frame, -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(frame, (x, y), (x+w, y+h), (255,0,0), 2)


    cv2.imshow("frame",frame)
    cv2.waitKey(700)

If above code I use to run with web camera it detect face but if I am using my loop it is not detecting tilted head.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文