按时间戳匹配点云和图像数据

process_data.py

#encoding:utf-8
# !/usr/bin/python

'''
USE Method:python process_data.py
1.点云pcd文件名是按时间戳保存的.2.相机pic文件名是按时间戳保存的。
此文档是根据点云和图像时间戳,匹配点云和图像数据。
'''


import os
import sys
import argparse
import os.path
import shutil

path =  os.path.abspath('.')

def parse_args():
    """
    Parse input arguments
    """
    parser = argparse.ArgumentParser(description='cut pic and xml 1 to 2')
    parser.add_argument('--pcd', dest='pcd',default = "pcd", type=str)
    parser.add_argument('--pic', dest='pic',default = "pic",type=str)

    # if len(sys.argv) == 1:
    #     parser.print_help()
    #     sys.exit(1)

    args = parser.parse_args()
    return args

if __name__ == '__main__':

    args = parse_args()
    print('Called with args:')
    print(args)
    path_pcd = "/media/data/zs/project/天准车机/caiji/230601/pcd"
    path_pic = "/media/data/zs/project/天准车机/caiji/230601/png"
    pcd = "2023-05-29-17-09-19"
    pic = "2023-05-29-17-09-19/camera_6"
    pcd_path = os.path.join(path_pcd , pcd)
    pic_path = os.path.join(path_pic , pic)
    timestamp = 200 #ms

    # dst_pcd_path = path_pcd + '/' + "not_pic"
    dst_pic_path = path_pcd + '/' + "camera_6"

    if not os.path.exists(dst_pic_path):
        os.mkdir(dst_pic_path)
    else:
        print("此目录已存在!")
        sys.exit(1)

    # if not os.path.exists(dst_pcd_path):
    #     os.mkdir(dst_pcd_path)
    # else:
    #     print("此目录已存在!")
    #     sys.exit(1)

    # for file in os.listdir(pic_path):
    #     pcd_name = os.path.join(pcd_path, os.path.splitext(file)[0] + ".pcd")
    #     if os.path.exists(pcd_name):
    #         print("This file", file, "has pcd !")
    #     elif not os.path.exists(pcd_name):
    #         print("This file", file, "has not pcd !")
    #         shutil.move(os.path.join(pic_path,file), dst_pic_path)

    for file in os.listdir(pcd_path):
        pcd_name_int = (int)(os.path.splitext(file)[0])
        # print(pcd_name_int)
        # print("------------------------------")
        is_matching = False
        for pic_file in os.listdir(pic_path):
            pic_name_int = (int)(os.path.splitext(pic_file)[0])
            if abs(pcd_name_int - pic_name_int) < timestamp:
                #os.rename(filepath,newpath)
                old_pic_path = os.path.join(pic_path , pic_file)
                shutil.copy(old_pic_path, dst_pic_path)
                new_pic_path = os.path.join(dst_pic_path , str(pcd_name_int)+".jpg")
                print(old_pic_path)
                print(new_pic_path)
                os.rename(os.path.join(dst_pic_path , pic_file),new_pic_path)
                is_matching = True
                break
        if is_matching == False:
            for pic_file in os.listdir(pic_path):
                pic_name_int = (int)(os.path.splitext(pic_file)[0])
                if abs(pcd_name_int - pic_name_int) < timestamp*2:
                    #os.rename(filepath,newpath)
                    old_pic_path = os.path.join(pic_path , pic_file)
                    shutil.copy(old_pic_path, dst_pic_path)
                    new_pic_path = os.path.join(dst_pic_path , str(pcd_name_int)+".jpg")
                    print(old_pic_path)
                    print(new_pic_path)
                    os.rename(os.path.join(dst_pic_path , pic_file),new_pic_path)
                    break
    print("Done!")

        #pic_path = os.path.join(pic_path, pic_name)
        #shutil.move(xml_name, dst_path)
        
    #for dirpath, dirnames, filenames in os.walk("f:/"):
     #   for filename in filenames:
      #      if os.path.splitext(filename)[1] == ".txt":
       #        print filepath
        #        copy(filepath, "F:/test/" + filename)
         #       Shutil.move(changeFilePath, dst_path)

猜你喜欢

转载自blog.csdn.net/qq_39523365/article/details/132088740