title: Python - Performant Video stream buffer
body: <p>How do I build a performant video stream buffer that I can do numpy array operations on?  </p>
  
  <p>This is my implementation currently - I just shift the previous array forward 1 frame and assign the last element to the current frame.  </p>
  
  <pre><code>import numpy as np
  import cv2
  import time
  cap = cv2.VideoCapture(0)
  status, frame = cap.read()
  buffer = np.empty([100, frame.shape[0], frame.shape[1], frame.shape[2]])
  i=0
  total = 100
  while i &lt; total:
      if not i: 
          start = time.time()
      status, frame = cap.read()
      t = time.time()
      if i &lt; total/2:
          buffer[i] = frame
      else:
          buffer[:-1] = buffer[1:]
          buffer[-1] = frame
      if i == total/2: 
          middle = t
      i += 1
      # Calculations on the buffer ommitted for brevity but include mean, std, etc.
  
  stop = time.time()
  print((middle-start)/(total/2))
  print((stop-middle)/(total/2))
  </code></pre>
  
  <p>It takes about 350X longer to shift the array as opposed to simply assigning the values of a frame to an element of the array.  I know this is because I am shifting all the pointers in the array which is unnecessary and expensive. Keeping the frames in order is nice but not necessary.</p>