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)
eyes is the detection output from detectMultiScale using eye_cascade (detecting inside a face, but ommitted).
眼睛是使用eye_cascade检测来自detectMultiScale的检测输出(在面部内部检测,但是省略)。
Let's say (ex, ey, ew, eh) are 4 coordinates that need to be stored in an array for each iteration of this loop.
让我们说(例如,ey,ew,eh)是4个坐标,需要在这个循环的每次迭代中存储在数组中。
The output for each iteration looks like this: [54, 46, 90, 103] But sometimes, it also looks like this: [[20 34 56 41], [34 56 78 89]]
每次迭代的输出如下所示:[54,46,90,103]但有时,它看起来像这样:[[20 34 56 41],[34 56 78 89]]
Sometimes the value of (ex, ey, ew, eh) can be upto 4 array values of in one array.
有时(ex,ey,ew,eh)的值可以是一个数组中最多4个数组值。
How can we store these multidimensional outputs in a numpy array? It does not have a fixed size, sometimes, it is an array of 4 coordinates; sometimes it is an array of an array of many sets of 4 coordinates.
我们如何将这些多维输出存储在一个numpy数组中?它没有固定的大小,有时,它是一个4坐标的数组;有时它是一组由4组坐标组成的数组。
1 个解决方案
#1
0
Collect them into a list and then concatenate:
将它们收集到一个列表中然后连接:
>>> 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]])
Here we use the "magic" concatenator r_
; its first argument '0,2,1'
means: concatenate along axis 0
, make everything 2
D, if axes have to be added the preexisting dimensions start at axis 1
., so [1, 1, 1, 1]
is reshaped to [[1, 1, 1, 1]]
before concatenation.
这里我们使用“魔术”连接器r_;它的第一个参数'0,2,1'表示:沿轴0连接,使所有2D都成为2D,如果必须添加轴,则预先存在的尺寸从轴1开始,因此[1,1,1,1]被重新形成为[ [1,1,1,1]]在连接之前。
#1
0
Collect them into a list and then concatenate:
将它们收集到一个列表中然后连接:
>>> 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]])
Here we use the "magic" concatenator r_
; its first argument '0,2,1'
means: concatenate along axis 0
, make everything 2
D, if axes have to be added the preexisting dimensions start at axis 1
., so [1, 1, 1, 1]
is reshaped to [[1, 1, 1, 1]]
before concatenation.
这里我们使用“魔术”连接器r_;它的第一个参数'0,2,1'表示:沿轴0连接,使所有2D都成为2D,如果必须添加轴,则预先存在的尺寸从轴1开始,因此[1,1,1,1]被重新形成为[ [1,1,1,1]]在连接之前。