def read_flow_sequence(filename, num_frames):
    file1 = os.path.splitext(os.path.basename(filename))[0]
    folder = os.path.split(filename)[0]
    ext = os.path.splitext(os.path.basename(filename))[1]

    filej = file1
    for i in range(num_frames - 1):
        filei = int(file1) + i + 1
        if "SPMC" in filename:
            flow_forward = flowlib.read_flow(
                folder + "/Forward/{:>04}".format(filej).format() + "_" +
                "{:>04}".format(filei).format() + ".flo")
            flow_backward = flowlib.read_flow(
                folder + "/Backward/{:>04}".format(filei).format() + "_" +
                "{:>04}".format(filej).format() + ".flo")
        else:
            flow_forward = flowlib.read_flow(
                folder.replace("480p", "Forward") + "/" +
                "{:>05}".format(filej).format() + "_" +
                "{:>05}".format(filei).format() + ".flo")
            flow_backward = flowlib.read_flow(
                folder.replace("480p", "Backward") + "/" +
                "{:>05}".format(filei).format() + "_" +
                "{:>05}".format(filej).format() + ".flo")
        filej = filei
        if i == 0:
            flow_forward_seq = flow_forward
            flow_backward_seq = flow_backward
        else:
            flow_forward_seq = np.concatenate((flow_forward_seq, flow_forward),
                                              axis=2)
            flow_backward_seq = np.concatenate(
                (flow_backward_seq, flow_backward), axis=2)

    return flow_forward_seq, flow_backward_seq
def read_flow_sintel(filename, num_frames, substr="clean"):
    file1 = os.path.splitext(os.path.basename(filename))[0]
    folder = os.path.split(filename)[0]
    flowfolder = folder.replace(substr, "flow")
    maskfolder = folder.replace(substr, "occlusions")
    invalfolder = folder.replace(substr, "invalid")
    start_frame = int(file1.split("_")[1])
    for i in range(num_frames - 1):
        # name = "/frame_" + "{:>04}".format(str(start_frame + i )).format()
        name = "/frame_" + "{:>04}".format(str(start_frame + i - 1)).format()
        # print("flow file: ",name)
        try:
            flowi = flowlib.read_flow(flowfolder + name + ".flo")
            flow_maski = sic.imread(maskfolder + name + ".png", 0).astype(
                np.float32) / 255.
            flow_invali = sic.imread(invalfolder + name + ".png", 0).astype(
                np.float32) / 255.

            flow_maski = np.expand_dims((flow_maski), axis=2)
            flow_invali = np.expand_dims((flow_invali), axis=2)
        except:
            return None, None, None
        if i == 0:
            # initialize the sequence
            flow_seq = flowi
            flow_seq_mask = flow_maski
            flow_seq_inval = flow_invali
        else:
            flow_seq = np.concatenate((flow_seq, flowi), axis=2)
            flow_seq_mask = np.concatenate((flow_seq_mask, flow_maski), axis=2)
            flow_seq_inval = np.concatenate((flow_seq_inval, flow_invali),
                                            axis=2)
    return flow_seq, flow_seq_mask, flow_seq_inval