CV2 的 detectMultiScale 方法 Return 是什么?
What Does CV2's detectMultiScale Method Return?
在 CV2 中,我可以从上传的图像生成人脸。
faces = faceCascade.detectMultiScale(
read_img,
scaleFactor = 1.1,
minNeighbors = 0,
minSize=(100,100)
)
how_many_faces = len(faces)
how_many_faces return正确的面数。
如果我将这些面附加到数组中...
our_faces = []
for i in faces:
our_faces.append(i)
return str(our_faces)
...和returnour_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), array([173, 141, 260, 260],
dtype=int32), array([184, 141, 260, 260], dtype=int32), array([143,
119, 286, 286], dtype=int32), array([167, 119, 286, 286],
dtype=int32), array([143, 131, 286, 286], dtype=int32), array([155,
131, 286, 286], dtype=int32), array([167, 131, 286, 286],
dtype=int32), array([144, 105, 315, 315], dtype=int32), array([157,
105, 315, 315], dtype=int32), array([131, 118, 315, 315],
dtype=int32), array([144, 118, 315, 315], dtype=int32), array([157,
118, 315, 315], dtype=int32), array([170, 118, 315, 315],
dtype=int32), array([130, 87, 346, 346], dtype=int32), array([115,
101, 346, 346], dtype=int32), array([130, 101, 346, 346],
dtype=int32), array([144, 101, 346, 346], dtype=int32), array([159,
101, 346, 346], dtype=int32), array([130, 115, 346, 346],
dtype=int32), array([ 87, 70, 419, 419], dtype=int32)]
我是否可以假设此数组包含每张脸的所有数据,并且它是一个 Numpy 数组?如果是这样,如何将数组中的数据转换回图像格式?
faceCascade.detectMultiScale()
returns 一个矩形列表,因此它不包含检测到的人脸图像,您不能仅从该列表重建人脸。
如果您想获取面部图像,您需要:
- 保留您最初在其中查找面孔的图像的副本,并且
- 使用 Numpy 切片或类似方法提取边界在
faceCascade.detectMultiScale()
返回的 faces
列表中的矩形
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
在 CV2 中,我可以从上传的图像生成人脸。
faces = faceCascade.detectMultiScale(
read_img,
scaleFactor = 1.1,
minNeighbors = 0,
minSize=(100,100)
)
how_many_faces = len(faces)
how_many_faces return正确的面数。
如果我将这些面附加到数组中...
our_faces = []
for i in faces:
our_faces.append(i)
return str(our_faces)
...和returnour_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), array([173, 141, 260, 260], dtype=int32), array([184, 141, 260, 260], dtype=int32), array([143, 119, 286, 286], dtype=int32), array([167, 119, 286, 286], dtype=int32), array([143, 131, 286, 286], dtype=int32), array([155, 131, 286, 286], dtype=int32), array([167, 131, 286, 286], dtype=int32), array([144, 105, 315, 315], dtype=int32), array([157, 105, 315, 315], dtype=int32), array([131, 118, 315, 315], dtype=int32), array([144, 118, 315, 315], dtype=int32), array([157, 118, 315, 315], dtype=int32), array([170, 118, 315, 315], dtype=int32), array([130, 87, 346, 346], dtype=int32), array([115, 101, 346, 346], dtype=int32), array([130, 101, 346, 346], dtype=int32), array([144, 101, 346, 346], dtype=int32), array([159, 101, 346, 346], dtype=int32), array([130, 115, 346, 346], dtype=int32), array([ 87, 70, 419, 419], dtype=int32)]
我是否可以假设此数组包含每张脸的所有数据,并且它是一个 Numpy 数组?如果是这样,如何将数组中的数据转换回图像格式?
faceCascade.detectMultiScale()
returns 一个矩形列表,因此它不包含检测到的人脸图像,您不能仅从该列表重建人脸。
如果您想获取面部图像,您需要:
- 保留您最初在其中查找面孔的图像的副本,并且
- 使用 Numpy 切片或类似方法提取边界在
faceCascade.detectMultiScale()
返回的
faces
列表中的矩形
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