a=[]
for (x,y,w,h) in faces:
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
a.append(eyes)
i+=1
print(a)
眼睛是使用eye_cascade检测多尺度(检测面部内部,但省略(的检测输出。
假设 (ex, ey, ew, eh( 是 4 个坐标,需要在此循环的每次迭代中存储在数组中。
每次迭代的输出如下所示:[54, 46, 90, 103]但有时,它也看起来像这样:[[20 34 56 41],[34 56 78 89]]
有时 (ex, ey, ew, eh( 的值可以在一个数组中最多 4 个数组值。
我们如何将这些多维输出存储在 numpy 数组中?它没有固定的大小,有时它是一个由 4 个坐标组成的数组;有时它是一个由许多组 4 个坐标组成的数组。
将它们收集到一个列表中,然后连接:
>>> out = []
>>> for i in range(5):
... out.append(np.squeeze(np.full([i, 4], i))) # squeeze to make it more difficult
...
>>> out
[array([], shape=(0, 4), dtype=int64), array([1, 1, 1, 1]), array([[2, 2, 2, 2],
[2, 2, 2, 2]]), array([[3, 3, 3, 3],
[3, 3, 3, 3],
[3, 3, 3, 3]]), array([[4, 4, 4, 4],
[4, 4, 4, 4],
[4, 4, 4, 4],
[4, 4, 4, 4]])]
>>> np.r_[('0,2,1', *out)]
array([[1, 1, 1, 1],
[2, 2, 2, 2],
[2, 2, 2, 2],
[3, 3, 3, 3],
[3, 3, 3, 3],
[3, 3, 3, 3],
[4, 4, 4, 4],
[4, 4, 4, 4],
[4, 4, 4, 4],
[4, 4, 4, 4]])
这里我们使用"magic"连接符r_
;它的第一个参数'0,2,1'
的意思是:沿轴0
连接,使所有东西都2
D,如果必须添加轴,则预先存在的尺寸从轴1
开始,因此[1, 1, 1, 1]
在连接之前被重新塑造为[[1, 1, 1, 1]]
。