【Python脚本】从txt文本中提取x,y,z数据并绘图对比

简易版

# encoding=utf-8

import matplotlib.pyplot as plt


def draw_compare_graph():
    """
    修改root_path为txt所在文件夹路径
    修改两个txt文件名称
    修改plt.title为想要的名称
    保存图片是记得最大化窗口后保存,这样保存下的图片尺寸更大
    :return: void
    """
    # root_path = "D:\\work\\amba\\CV22\\vio_dataset\\5_13\\VID_20210511_201753\\vio-pc-dataset\\"
    root_path = "Y:/dataset/input/xyz_result/"  # txt文件所在目录

    # pc file
    file_pc = open(root_path + 'cpu_ref.txt')
    data_pc = file_pc.readlines()

    # amba file
    file_amba = open(root_path + 'lk3_fast10000_nms.txt')
    data_amba = file_amba.readlines()

    pc_para_0 = []  # timestamp
    pc_para_6 = []  # x
    pc_para_7 = []  # y
    pc_para_8 = []  # z

    for num in data_pc:
        nums = num.split(' ')
        pc_para_0.append(float(nums[0]))
        pc_para_6.append(float(nums[5]))
        pc_para_7.append(float(nums[6]))
        pc_para_8.append(float(nums[7]))

    amba_para_0 = []  # timestamp
    amba_para_6 = []  # x
    amba_para_7 = []  # y
    amba_para_8 = []  # z

    for num in data_amba:
        nums = num.split(' ')
        amba_para_0.append(float(nums[0]))
        amba_para_6.append(float(nums[5]))
        amba_para_7.append(float(nums[6]))
        amba_para_8.append(float(nums[7]))

    plt.figure()
    plt.title("cpu&dsp")  # 可以更改标题便于保存,

    plt.plot(pc_para_0, pc_para_6, "r", label='cpu-x', linestyle='--')  # red
    plt.plot(pc_para_0, pc_para_7, "g", label='cpu-y', linestyle='--')  # green
    plt.plot(pc_para_0, pc_para_8, "b", label='cpu-z', linestyle='--')  # blue

    plt.plot(amba_para_0, amba_para_6, "m", label='dsp-x')  # pinkish red
    plt.plot(amba_para_0, amba_para_7, "c", label='dsp-y')  # cyan
    plt.plot(amba_para_0, amba_para_8, "y", label='dsp-z')  # yellow

    plt.legend()
    plt.show()


if __name__ == '__main__':
    draw_compare_graph()



复杂版

# encoding=utf-8

import matplotlib.pyplot as plt


def draw_one_graph():
    """
    修改root_path为txt所在文件夹路径
    修改两个txt文件名称
    修改plt.title为想要的名称
    保存图片是记得最大化窗口后保存,这样保存下的图片尺寸更大
    :return: void
    """
    # root_path = "D:/work/amba/CV22/vio_dataset/5_13/VID_20210511_201753/vio-pc-dataset/"
    root_path = "F:/vio_dataset/data1015_online5/"  # txt文件所在目录

    # pc file
    file_pc = open(root_path + 'pc-stereo_vio_results-14090.txt')
    data_pc = file_pc.readlines()

    pc_para_0 = []  # timestamp
    pc_para_6 = []  # x
    pc_para_7 = []  # y
    pc_para_8 = []  # z

    for num in data_pc:
        nums = num.split(' ')
        pc_para_0.append(float(nums[0]))
        pc_para_6.append(float(nums[5]))
        pc_para_7.append(float(nums[6]))
        pc_para_8.append(float(nums[7]))

    plt.figure()
    plt.title("pc&amba_harris")  # 可以更改标题便于保存,

    plt.plot(pc_para_0, pc_para_6, "r", label='pc-x', linestyle='--')  # red
    plt.plot(pc_para_0, pc_para_7, "g", label='pc-y', linestyle='--')  # green
    plt.plot(pc_para_0, pc_para_8, "b", label='pc-z', linestyle='--')  # blue

    plt.legend()
    plt.show()


def draw_compare_graph():
    """
    修改root_path为txt所在文件夹路径
    修改两个txt文件名称
    修改plt.title为想要的名称
    保存图片是记得最大化窗口后保存,这样保存下的图片尺寸更大
    :return: void
    """
    # root_path = "D:/work/amba/CV22/vio_dataset/5_13/VID_20210511_201753/vio-pc-dataset/"
    root_path = "F:/vio_dataset/data1015_online7/EKF_Map_mem-debug/"  # txt文件所在目录

    # pc file
    file_pc = open(root_path + 'amba_stereo_vio_results-hpc-2612dede.txt')
    data_pc = file_pc.readlines()

    # amba file
    file_amba = open(root_path + 'amba_stereo_vio_results-origin.txt')

    data_amba = file_amba.readlines()

    pc_para_0 = []  # timestamp
    pc_para_6 = []  # x
    pc_para_7 = []  # y
    pc_para_8 = []  # z

    # skip_lines = 1140

    # skip_line = 0
    for num in data_pc:
        # if skip_line < 1140:
        #     skip_line += 1
        #     continue
        nums = num.split(' ')
        pc_para_0.append(float(nums[0]))
        pc_para_6.append(float(nums[5]))
        pc_para_7.append(float(nums[6]))
        pc_para_8.append(float(nums[7]))

    amba_para_0 = []  # timestamp
    amba_para_6 = []  # x
    amba_para_7 = []  # y
    amba_para_8 = []  # z

    # skip_line_amba = 0
    for num in data_amba:
        # if skip_line_amba < 1140:
        #     skip_line_amba += 1
        #     continue
        nums = num.split(' ')
        amba_para_0.append(float(nums[0]))
        amba_para_6.append(float(nums[5]))
        amba_para_7.append(float(nums[6]))
        amba_para_8.append(float(nums[7]))

    plt.figure()
    plt.title("data1015_online9--amba_compare_vpu")  # 可以更改标题便于保存,

    plt.plot(pc_para_0, pc_para_6, "r", label='pc-x', linestyle='--')  # red
    plt.plot(pc_para_0, pc_para_7, "g", label='pc-y', linestyle='--')  # green
    plt.plot(pc_para_0, pc_para_8, "b", label='pc-z', linestyle='--')  # blue

    plt.plot(amba_para_0, amba_para_6, "m", label='amba-x')  # pinkish red
    plt.plot(amba_para_0, amba_para_7, "c", label='amba-y')  # cyan
    plt.plot(amba_para_0, amba_para_8, "y", label='amba-z')  # yellow

    plt.legend()
    plt.show()


def get_space_num(line, pos):
    space_num = 0
    # print("start pos: " + str(pos))
    for i in range(0, 50):
        if line[pos] == ' ':
            pos += 1
            space_num += 1
        else:
            break

    # print(space_num)
    return space_num


# --------translation: 200.901  0.624514 -0.323002 -0.151102
# --------translation: 200.935 0.610903 -0.32412  -0.1788

def get_vio_result_from_txt_file(fn, key):
    amba_para_0 = []  # timestamp
    amba_para_6 = []  # x
    amba_para_7 = []  # y
    amba_para_8 = []  # z

    with open(fn) as f:
        lines = f.readlines()
        for line in lines:
            # print(line)
            pos = line.find(key)
            if pos > 0:
                pos += len(key)
                end_pos = line.find(' ', pos)
                time_str = line[pos: end_pos]

                space_num1 = get_space_num(line, end_pos)
                end_pos2 = line.find(' ', end_pos + space_num1)
                x_str = line[end_pos + space_num1: end_pos2]

                space_num2 = get_space_num(line, end_pos2)
                end_pos3 = line.find(' ', end_pos2 + space_num2)
                y_str = line[end_pos2 + space_num2: end_pos3]

                space_num3 = get_space_num(line, end_pos3)
                end_pos4 = line.find(' ', end_pos3 + space_num3)
                z_str = line[end_pos3 + space_num3: end_pos4]
                # print(time_str, x_str, y_str, z_str)

                try:
                    time_float = float(time_str)
                    x_float = float(x_str)
                    y_float = float(y_str)
                    z_float = float(z_str)

                    # print(time_float, x_float, y_float, z_float)

                    amba_para_0.append(time_float)
                    amba_para_6.append(x_float)
                    amba_para_7.append(y_float)
                    amba_para_8.append(z_float)

                except Exception:
                    print("error")
                    continue
            # else:
            #     print("no key")

    root_path = "F:/vio_dataset/data1015_online7/"  # txt文件所在目录

    # pc file
    file_pc = open(root_path + 'amba_stereo_vio_results-hpc-2612dede.txt')
    data_pc = file_pc.readlines()

    pc_para_0 = []  # timestamp
    pc_para_6 = []  # x
    pc_para_7 = []  # y
    pc_para_8 = []  # z

    # skip_lines = 1140

    # skip_line = 0
    for num in data_pc:
        # if skip_line < 1140:
        #     skip_line += 1
        #     continue
        nums = num.split(' ')
        pc_para_0.append(float(nums[0]))
        pc_para_6.append(float(nums[5]))
        pc_para_7.append(float(nums[6]))
        pc_para_8.append(float(nums[7]))

    plt.figure()
    plt.title("data1015_online7--pc&amba-multi_thread-lk_vpu")  # 可以更改标题便于保存,

    plt.plot(pc_para_0, pc_para_6, "r", label='pc-x', linestyle='--')  # red
    plt.plot(pc_para_0, pc_para_7, "g", label='pc-y', linestyle='--')  # green
    plt.plot(pc_para_0, pc_para_8, "b", label='pc-z', linestyle='--')  # blue

    plt.plot(amba_para_0, amba_para_6, "m", label='amba-x')  # pinkish red
    plt.plot(amba_para_0, amba_para_7, "c", label='amba-y')  # cyan
    plt.plot(amba_para_0, amba_para_8, "y", label='amba-z')  # yellow

    plt.legend()
    plt.show()


def get_vio_result_from_two_txt_file(fn1, fn2, key):
    amba_para_0 = []  # timestamp
    amba_para_6 = []  # x
    amba_para_7 = []  # y
    amba_para_8 = []  # z

    with open(fn2) as f:
        lines = f.readlines()
        for line in lines:
            # print(line)
            pos = line.find(key)
            if pos > 0:
                pos += len(key)
                end_pos = line.find(' ', pos)
                time_str = line[pos: end_pos]

                space_num1 = get_space_num(line, end_pos)
                end_pos2 = line.find(' ', end_pos + space_num1)
                x_str = line[end_pos + space_num1: end_pos2]

                space_num2 = get_space_num(line, end_pos2)
                end_pos3 = line.find(' ', end_pos2 + space_num2)
                y_str = line[end_pos2 + space_num2: end_pos3]

                space_num3 = get_space_num(line, end_pos3)
                end_pos4 = line.find(' ', end_pos3 + space_num3)
                z_str = line[end_pos3 + space_num3: end_pos4]
                # print(time_str, x_str, y_str, z_str)

                try:
                    time_float = float(time_str)
                    x_float = float(x_str)
                    y_float = float(y_str)
                    z_float = float(z_str)

                    # print(time_float, x_float, y_float, z_float)

                    amba_para_0.append(time_float)
                    amba_para_6.append(x_float)
                    amba_para_7.append(y_float)
                    amba_para_8.append(z_float)

                except Exception:
                    print("error")
                    continue

    pc_para_0 = []  # timestamp
    pc_para_6 = []  # x
    pc_para_7 = []  # y
    pc_para_8 = []  # z

    with open(fn1) as f:
        lines = f.readlines()
        for line in lines:
            # print(line)
            pos = line.find(key)
            if pos > 0:
                pos += len(key)
                end_pos = line.find(' ', pos)
                time_str = line[pos: end_pos]

                space_num1 = get_space_num(line, end_pos)
                end_pos2 = line.find(' ', end_pos + space_num1)
                x_str = line[end_pos + space_num1: end_pos2]

                space_num2 = get_space_num(line, end_pos2)
                end_pos3 = line.find(' ', end_pos2 + space_num2)
                y_str = line[end_pos2 + space_num2: end_pos3]

                space_num3 = get_space_num(line, end_pos3)
                end_pos4 = line.find(' ', end_pos3 + space_num3)
                z_str = line[end_pos3 + space_num3: end_pos4]
                # print(time_str, x_str, y_str, z_str)

                try:
                    time_float = float(time_str)
                    x_float = float(x_str)
                    y_float = float(y_str)
                    z_float = float(z_str)

                    # print(time_float, x_float, y_float, z_float)

                    pc_para_0.append(time_float)
                    pc_para_6.append(x_float)
                    pc_para_7.append(y_float)
                    pc_para_8.append(z_float)

                except Exception:
                    print("error")
                    continue

    plt.figure()
    plt.title("data1015_online7-55ad3d6c")  # 可以更改标题便于保存,

    plt.plot(pc_para_0, pc_para_6, "r", label='pc-x', linestyle='--')  # red
    plt.plot(pc_para_0, pc_para_7, "g", label='pc-y', linestyle='--')  # green
    plt.plot(pc_para_0, pc_para_8, "b", label='pc-z', linestyle='--')  # blue

    plt.plot(amba_para_0, amba_para_6, "m", label='amba-x')  # pinkish red
    plt.plot(amba_para_0, amba_para_7, "c", label='amba-y')  # cyan
    plt.plot(amba_para_0, amba_para_8, "y", label='amba-z')  # yellow

    plt.legend()
    figure = plt.gcf()
    figure.set_size_inches(24, 13.5)
    plt.savefig('output.png', bbox_inches='tight')
    plt.show()


def get_time_and_z_curve_from_txt_file(fn, key_result, key_time):
    amba_para_0 = []  # timestamp
    amba_para_6 = []  # x
    amba_para_7 = []  # y
    amba_para_8 = []  # z

    with open(fn) as f:
        lines = f.readlines()
        for line in lines:
            # print(line)
            pos = line.find(key_result)
            if pos > 0:
                pos += len(key_result)
                end_pos = line.find(' ', pos)
                time_str = line[pos: end_pos]

                space_num1 = get_space_num(line, end_pos)
                end_pos2 = line.find(' ', end_pos + space_num1)
                x_str = line[end_pos + space_num1: end_pos2]

                space_num2 = get_space_num(line, end_pos2)
                end_pos3 = line.find(' ', end_pos2 + space_num2)
                y_str = line[end_pos2 + space_num2: end_pos3]

                space_num3 = get_space_num(line, end_pos3)
                end_pos4 = line.find(' ', end_pos3 + space_num3)
                z_str = line[end_pos3 + space_num3: end_pos4]
                # print(time_str, x_str, y_str, z_str)

                try:
                    time_float = float(time_str)
                    x_float = float(x_str)
                    y_float = float(y_str)
                    z_float = float(z_str)

                    # print(time_float, x_float, y_float, z_float)

                    amba_para_0.append(time_float)
                    amba_para_6.append(x_float)
                    amba_para_7.append(y_float)
                    amba_para_8.append(z_float)

                except Exception:
                    print("error")
                    continue

    res = []
    with open(fn) as f:
        lines = f.readlines()
        for line in lines:
            pos = line.find(key_time)
            if pos > 0:
                pos += len(key_time)
                end_pos = line.find('/n', pos)
                str = line[pos: end_pos]
                try:
                    res_tmp = float(str)
                    res.append(res_tmp)
                except Exception:
                    continue
            # else:
            #     print("start")

    print("time number: ", len(res))
    plt.figure()
    plt.title("data1015_online7 time, key: " + key_time)  # 可以更改标题便于保存,

    for i in range(0, len(amba_para_8)):
        amba_para_8[i] = amba_para_8[i] * 5

    plt.plot(amba_para_0, amba_para_8, "r", label='z', linestyle='--')  # red

    plt.plot(amba_para_0, res, ".", label='time')  # pinkish red

    plt.legend()
    plt.show()


if __name__ == '__main__':
    path = "F:/lk_level4/"
    # get_vio_result_from_txt_file(path + "double-thread.txt", "--------translation: ")
    get_vio_result_from_two_txt_file(path + "level3_double_eye_log.txt",
                                     path + "level6_double_eye_log_new.txt", "--------translation: ")

你可能感兴趣的:(python脚本,python)