【问题标题】:image aling with cv2 instead of HOG使用 cv2 而不是 HOG 进行图像处理
【发布时间】:2020-07-09 13:04:55
【问题描述】:

您好,我正在研究面部识别。

为了提高性能,我想使用面部对齐。

当我使用例如 Adrian 描述的 HOG 面部标识符时,我会得到一个对齐的图像。

from imutils.face_utils import rect_to_bb
from dlib import get_frontal_face_detector

detector = dlib.get_frontal_face_detector()
shape_predictor = dlib.shape_predictor('/home/base/Documents/facial_landmarks/shape_predictor_5_face_landmarks.dat')
fa = face_utils.facealigner.FaceAligner(shape_predictor, desiredFaceWidth=112, desiredLeftEye=(0.3, 0.3))

img=cv2.imread(pathtoimage)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
        rects = detector(gray, 2)  

for rect in rects:                
            (x, y, w, h) = rect_to_bb(rect)
            faceAligned = fa.align(img, gray, rect)

但是,我必须在嵌入式硬件上工作,而且 HOG 面部识别速度不够快。最好的工作是 cv2 lbpcascader。

使用 cv2 我也得到了找到的人脸框,但使用它不起作用。

faces_detected = face_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=4) 

在使用 HOG 的其他示例中,坐标是从 HOG-rect 中提取的:

(x, y, w, h) = rect_to_bb(rect)

然后与

一起使用
aligned_face = fa.align(img, gray, dlib.rectangle(left = x, top=y, right=w, bottom=h))

这个想法是将 x,y,w,h 与 cv2 值交换。不幸的是,这不起作用,因为上面的两行会导致完全错误的对齐。在第一个代码示例中,包含但未使用 rect_to_bb 函数。

我检查了这些值,但它们以某种方式关闭:

  • 224x224 图片
  • 156 70 219 219 cv2 值(当然略有不同)
  • 165 101 193 193 带有rect_to_bb的矩形值
  • [(165, 101) (358, 294)] 矩形值

我检查了 rect_to_bb 函数,但这似乎很简单:

def rect_to_bb(rect):
    # take a bounding predicted by dlib and convert it
    # to the format (x, y, w, h) as we would normally do
    # with OpenCV
    x = rect.left()
    y = rect.top()
    w = rect.right() - x
    h = rect.bottom() - y

    # return a tuple of (x, y, w, h)
    return (x, y, w, h)

【问题讨论】:

    标签: python opencv-python facial-identification facial-landmark-alignment


    【解决方案1】:

    打字时我得到了答案……经典

    对齐功能需要的边界框标记略有不同。 在rect_to_bb()函数中可以看到。

    def rect_to_bb(rect):
        # take a bounding predicted by dlib and convert it
        # to the format (x, y, w, h) as we would normally do
        # with OpenCV
        x = rect.left()
        y = rect.top()
        w = rect.right() - x
        h = rect.bottom() - y
    
        # return a tuple of (x, y, w, h)
        return (x, y, w, h)
    

    rect.right(cv2 中的 w)和 rect.bottom(cv2 中的 h)用 x 和 y 减去。因此,在对齐功能中,您必须添加值,否则馈送到对齐功能的图像非常小并且变形。这也可以是来自 cv2 检测的值。

    aligned_face = fa.align(img, gray, dlib.rectangle(left = x, top=y, right=w+x, bottom=h+y))
    

    保持健康

    【讨论】:

      猜你喜欢
      • 2020-10-25
      • 2020-04-26
      • 2011-06-13
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多