0

I'm using a video processing tool that needs to input the processing data from each frame into an array.

for p in det.read(frame, fac):  

    point_values = np.array([])

    for j, (x, y) in enumerate(p):  #iteration through points
        point_values = np.append(point_values,y)
        point_values = np.append(point_values,x)

this code runs again each frame. I'm expecting "point_values = np.array([])" to reset the array and then start filling it again.

I'm not sure if my logic is wrong or is it a syntax issue.

Tom Bar-Gal
  • 209
  • 2
  • 4
  • 13
  • What is `det`, `frame`, `fac`, etc? Please add some more details and desired input-output samples. – J...S Jun 17 '18 at 15:28
  • It's really inefficient to make arrays like this. You should use python lists if you're intending to append, or you should instantiate an empty array of fixed size and change those values by index. But numpy arrays are not designed to change size efficiently; this probably runs much slower than a `for` loop using regular python lists. – roganjosh Jun 17 '18 at 15:29
  • See [this](https://stackoverflow.com/a/25649863/4799172) – roganjosh Jun 17 '18 at 15:37
  • @roganjosh The numpy array is supposed to stay in the same length. only change its values. – Tom Bar-Gal Jun 17 '18 at 15:43
  • @J...S p a detection of a face in the image. Frame is the image that is analyzed and fac is data that is not used here but for the detection itself. – Tom Bar-Gal Jun 17 '18 at 15:44
  • I'm confused. You're actively setting the array to length zero in `point_values = np.array([])` and `append` automatically increases its length by 1. So really, your question is how to create an array of fixed, non zero length and keep replacing its values? In that case, what do you consider to be an empty array in place of `point_values = np.array([])`? `np.empty()`? Because it has a fixed length, it needs some default value at each index, maybe even `np.NaN` – roganjosh Jun 17 '18 at 15:51
  • I think the link I gave already solves your issue. Have you read through it? If it doesn't, can you tell me what's missing so I can try address that? I'm not clear on your issue currently. – roganjosh Jun 17 '18 at 15:59

1 Answers1

0

Your code does:

In [77]: p = [(0,0),(0,2),(1,0),(1,2)]
In [78]: arr = np.array([])
In [79]: for j,(x,y) in enumerate(p):
    ...:     arr = np.append(arr,y)
    ...:     arr = np.append(arr,x)
    ...:     
In [80]: arr
Out[80]: array([0., 0., 2., 0., 0., 1., 2., 1.])

No syntax error. The list equivalent is faster and cleaner:

In [85]: alist =[]
In [86]: for x,y in p: alist.extend((y,x))
In [87]: alist
Out[87]: [0, 0, 2, 0, 0, 1, 2, 1]

But you don't give any indication of how this action is supposed to fit within a larger context. You create a new point_values for each p, but then don't do anything with it.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Sorry, I'm not a professional programmer so I'm still getting the rights and wrongs of communicating here :) I'm then using np.linalg to get the 136 dimensional point distance between two points: `np.linalg.norm(point_num - point_values)` – Tom Bar-Gal Jun 17 '18 at 16:28