code: def optical_flow_magnitude(image_sequence, max_diff=60, gftt_options={}):
      
      flow = []
      prev_img = None
      for img in image_sequence:
          if img.ndim == 3 and img.shape[2] == 3:
              img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
          if prev_img is None:
              prev_img = img
              continue
          (next_points, prev_points) = track_points(prev_img, img, gftt_params=gftt_options)
          distance = np.sqrt(np.sum((next_points - prev_points)**2, 1))
          distance2 = distance[np.nonzero(distance < max_diff)] # Crude outlier rejection
          dm = np.mean(distance2)
          if np.isnan(dm):
              dm = 0
          flow.append(dm)
          prev_img = img
  
      return np.array(flow)
docs: Return optical flow magnitude for the given image sequence
      
      The flow magnitude is the mean value of the total (sparse) optical flow
      between two images.
      Crude outlier detection using the max_diff parameter is used.
      
      Parameters
      ----------------
      image_sequence : sequence
              Sequence of image data (ndarrays) to calculate flow magnitude from
      max_diff : float
              Distance threshold for outlier rejection
      gftt_options : dict
              Keyword arguments to the OpenCV goodFeaturesToTrack function
              
      Returns
      ----------------
      flow : ndarray
              The optical flow magnitude