【问题标题】:What Does CV2's detectMultiScale Method Return?CV2 的 detectMultiScale 方法返回什么?
【发布时间】:2019-06-21 08:09:41
【问题描述】:

在 CV2 中,我可以从上传的图像中生成人脸。

faces = faceCascade.detectMultiScale(
    read_img,
    scaleFactor = 1.1,
    minNeighbors = 0,
    minSize=(100,100)
)
how_many_faces = len(faces)

how_many_faces 返回正确的面数。

如果我将这些面孔附加到一个数组中......

our_faces = []
for i in faces:
    our_faces.append(i)

return str(our_faces)

...返回our_faces,我得到以下数据:

[array([187, 138, 236, 236], dtype=int32), array([197, 138, 236, 236], dtype=int32), array([163, 130, 260, 260], dtype=int32), array([163, 141, 260, 260], dtype=int32), 数组([173, 141, 260, 260], dtype=int32), 数组([184, 141, 260, 260], dtype=int32), 数组([143, 119, 286, 286], dtype=int32), 数组([167, 119, 286, 286], dtype=int32), 数组([143, 131, 286, 286], dtype=int32), 数组([155, 131, 286, 286], dtype=int32), 数组([167, 131, 286, 286], dtype=int32), 数组([144, 105, 315, 315], dtype=int32), 数组([157, 105, 315, 315], dtype=int32), 数组([131, 118, 315, 315], dtype=int32), array([144, 118, 315, 315], dtype=int32), array([157, 118, 315, 315], dtype=int32), 数组([170, 118, 315, 315], dtype=int32), 数组([130, 87, 346, 346], dtype=int32), 数组([115, 101, 346, 346], dtype=int32), 数组([130, 101, 346, 346], dtype=int32), 数组([144, 101, 346, 346], dtype=int32), 数组([159, 101, 346, 346], dtype=int32), 数组([130, 115, 346, 346], dtype=int32), array([ 87, 70, 419, 419], dtype=int32)]

我是否可以假设这个数组包含每个人脸的所有数据,并且它是一个 Numpy 数组?如果是这样,如何将数组中的这些数据转换回图像格式?

【问题讨论】:

    标签: python arrays opencv cv2


    【解决方案1】:

    faceCascade.detectMultiScale() 返回一个矩形列表,因此它不包含检测到的人脸的图像,并且您不能仅从该列表中重建人脸。

    如果您想获取人脸图像,您需要:

    • 保留您最初在其中寻找面孔的图像的副本,并
    • 使用 Numpy 切片或类似方法提取边界在 faceCascade.detectMultiScale() 返回的 faces 列表中的矩形

    【讨论】:

    • 谢谢马克。这回答了事物的理论部分。通过使用 img = Image.fromarray(...),我能够获得更合理的输出,但我得到一个 ValueError,即“图像数据不足”。我认为最好在另一个问题中提出,因此我将为该错误创建一个单独的问题。
    • 好的,我会留意新的。没问题,问题免费!
    【解决方案2】:
    def crop(image, faces, k=0):
    """
    This function crops the initial image into faces' images seperately. 
    Arguments:
        image  (np array image)
        faces (list of tuples)
    """
    faces_arrays = []
    for (top, right, bottom, left)in faces:
        x0, y0 = left, bottom
        x1, y1 = right, top
        w, h = right-left, top-bottom
        cv2.rectangle(img=image, pt1=(x0, y0), pt2=(x1, y1), color=(255,0,0), thickness=2)
        x2, x3 = x1-w,  x0+w
        # crop the region of interest over a copy 
        face = image[y1:y0, x2:x3].copy()
        faces_arrays.append(face)
        # comment the two following lines if you want to stop saving the crops
        cv2.imwrite('face'+str(k)+'.jpg', face)
        k += 1
    return faces_arrays
    

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 2021-04-15
      • 2021-04-10
      • 2021-07-20
      • 2017-11-29
      相关资源
      最近更新 更多