Пример #1
0
def compute_TVL1(video_path):
    """Compute the TV-L1 optical flow."""
    TVL1 = DualTVL1()
    cap = cv2.VideoCapture(video_path)

    ret, frame1 = cap.read()
    prev = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
    prev = cv2.resize(prev, (_IMAGE_SIZE, _IMAGE_SIZE))
    flow = []
    vid_len = _video_length(video_path)
    for _ in range(vid_len - 2):
        ret, frame2 = cap.read()
        curr = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
        curr = cv2.resize(curr, (_IMAGE_SIZE, _IMAGE_SIZE))
        curr_flow = TVL1.calc(prev, curr, None)
        assert (curr_flow.dtype == np.float32)
        # truncate [-20, 20]
        curr_flow[curr_flow >= 20] = 20
        curr_flow[curr_flow <= -20] = -20
        # scale to [-1, 1]
        max_val = lambda x: max(max(x.flatten()), abs(min(x.flatten())))
        curr_flow = curr_flow / max_val(curr_flow)
        flow.append(curr_flow)
        prev = curr
    cap.release()
    flow = np.array(flow)
    return flow
Пример #2
0
def compute_TVL1(video_path):
    print(video_path)
    TVL1 = DualTVL1()
    cap = cv2.VideoCapture(video_path)

    ret, frame1 = cap.read()
    if (ret == False):
        return
    prev = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
    prev = cv2.resize(prev, (_IMAGE_SIZE, _IMAGE_SIZE))
    flow = []
    vid_len = _video_length(video_path)
    for _ in range(vid_len - 1):
        ret, frame2 = cap.read()
        if (ret == False):
            continue
        curr = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
        curr = cv2.resize(curr, (_IMAGE_SIZE, _IMAGE_SIZE))
        curr_flow = TVL1.calc(prev, curr, None)
        assert (curr_flow.dtype == np.float32)
        # truncate [-20, 20]
        curr_flow[curr_flow >= 20] = 20
        curr_flow[curr_flow <= -20] = -20
        # scale to [-1, 1]
        #max_val = lambda x: max(max(x.flatten()), abs(min(x.flatten())))
        #curr_flow = curr_flow / max_val(curr_flow)
        curr_flow = 2 * (curr_flow - np.min(curr_flow)) / np.ptp(curr_flow) - 1
        flow.append(curr_flow)
        prev = curr
    cap.release()
    flow = np.array(flow)
    return flow
Пример #3
0
def compute_TVL1(video_path):

  TVL1 = DualTVL1()
  TVL1.setScalesNumber(1)
  TVL1.setWarpingsNumber(2)
  cap = cv2.VideoCapture(video_path)

  ret, frame1 = cap.read()

  # Error check - for corrupt file
  if ret:
    prev = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
    prev = cv2.resize(prev, (_IMAGE_SIZE, _IMAGE_SIZE))
  else:
    return -1

  flow = []
  vid_len = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
  count = 0
  frameCount = 0
  
  for _ in range(vid_len - 2):

    # reduce frames by 8
    count += 1
    if count % 8 == 0:
      frameCount += 1
      ret, frame2 = cap.read()

      # Error check - for corrupt file
      if ret:
        curr = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
        curr = cv2.resize(curr, (_IMAGE_SIZE, _IMAGE_SIZE))
      else:
        return -1

      curr_flow = TVL1.calc(prev, curr, None)
      assert(curr_flow.dtype == np.float32)

      # truncate pixel values to [-20, 20]
      curr_flow[curr_flow >= 20] = 20
      curr_flow[curr_flow <= -20] = -20

      # rescale between [-1, 1]
      max_val = lambda x: max(max(x.flatten()), abs(min(x.flatten())))
      curr_flow = curr_flow / max_val(curr_flow)
      flow.append(curr_flow)  
      prev = curr

  set_frameCount(frameCount)
  cap.release()
  flow = np.array(flow)
  flow = flow.reshape(_BATCH_SIZE, _FRAME_COUNT, _IMAGE_SIZE, _IMAGE_SIZE, 2)
  return flow
Пример #4
0
def compute_TVL1_ndarray(image_array):
    prev_image = image_array[0, 0, :]
    prev_image = np.squeeze(np.mean(prev_image, axis=-1))
    TVL1 = DualTVL1()
    flow = []
    for i in range(1, image_array.shape[1]):
        cur_image = image_array[0, i, :]
        cur_image = np.squeeze(np.mean(cur_image, axis=-1))
        cur_flow = TVL1.calc(prev_image, cur_image, None)
        prev_image = cur_image

        flow.append(cur_flow)
    flow = np.array(flow)
    return flow
Пример #5
0
def perform_ofb(v):
    v = v.astype('uint8')
    f, r, c, d = v.shape
    previous_frame = cv2.cvtColor(v[0], cv2.COLOR_BGR2GRAY)
    flows = np.zeros((f - 1, r, c, 2))
    tvl1 = DualTVL1()
    for i in range(1, f):
        current_frame = cv2.cvtColor(v[i], cv2.COLOR_BGR2GRAY)
        flow = tvl1.calc(previous_frame, current_frame, None)

        xflow = flow[..., 0]
        yflow = flow[..., 1]
        flows[i - 1, :, :, 0] = xflow
        flows[i - 1, :, :, 1] = yflow

        previous_frame = current_frame

    return np.array(flows)
Пример #6
0
def compute_TVL1(image_dir):
    """Compute TV-L1 optical flow."""
    image_list = sorted(os.listdir(image_dir))
    prev_image = cv2.imread(os.path.join(image_dir, image_list[0]))
    prev_image = cv2.cvtColor(prev_image, cv2.COLOR_RGB2GRAY)
    TVL1 = DualTVL1()
    flow = []
    for i in range(1, len(image_list)):
        cur_image = cv2.imread(os.path.join(image_dir, image_list[i]))
        cur_image = cv2.cvtColor(cur_image, cv2.COLOR_RGB2GRAY)
        cur_flow = TVL1.calc(prev_image, cur_image, None)
        prev_image = cur_image

        max_val = lambda x: max(max(x.flatten()), abs(min(x.flatten())))
        # cur_flow = cur_flow / max_val(cur_flow)
        flow.append(cur_flow)
    flow = np.array(flow)
    return flow
Пример #7
0
def compute_optical_flow_tvl1(video_frames_folder):
    """Compute the TV-L1 optical flow."""
    TVL1 = DualTVL1()

    # Collect RGB frame paths.
    rgb_frame_files = os.listdir(video_frames_folder)
    rgb_frame_files = [frame_file for frame_file in rgb_frame_files
                       if frame_file.endswith(FLAGS.expected_rgb_frame_suffix)]
    rgb_frame_files.sort()
    num_frames = len(rgb_frame_files)
    assert num_frames >= 2, "Only find %d (<=2) RGB frames under %s." % (num_frames, video_frames_folder)

    # Iteratively compute optical flows.
    optical_flows = []
    prev_frame = image_funcs.rgb_to_gray(image_funcs.read_image(rgb_frame_files[0], to_float=False))
    for i in range(1, num_frames):
        cur_frame = image_funcs.rgb_to_gray(image_funcs.read_image(rgb_frame_files[1], to_float=False))
        cur_flow = TVL1.calc(prev_frame, cur_frame, None)
        assert (cur_flow.dtype == np.float32)
        optical_flows.append(cur_flow)
        prev_frame = cur_frame
    return optical_flows