gha / data_tool /kinectdata_process.py
vegagoto123's picture
Add files using upload-large-folder tool
3217786 verified
Raw History Blame Contribute Delete
11.6 kB
import os
import glob
import shutil
import numpy as np
import xml.etree.ElementTree as ET
import cv2
def load_camera_xml(xml_path):
tree = ET.parse(str(xml_path))
root = tree.getroot()
Ks = []
poses = []
dist_coeffs = []
for i in range(4):
name = f'camera_{i}_intrinsic_matrix'
K = np.reshape(np.array(root.find(name).find('data').text.split(), dtype=np.float32), [3, 3])
# K[0, 0] = K[0, 0]
# K[1, 1] = K[1, 1]
# K[0, 2] = 0.5 *
# K[1, 2] = 0.5
# Ks.append([1920., 1080., K[0, 0], K[1, 1], 960., 540.])
Ks.append(K)
name = f'camera_{i}_rotation_matrix'
R = np.reshape(np.array(root.find(name).find('data').text.split(), dtype=np.float32), [3, 3])
name = f'camera_{i}_translation_vector'
T = np.reshape(np.array(root.find(name).find('data').text.split(), dtype=np.float32), [3, 1])/1000
pose = np.concatenate((R, T), axis=1)
eye = np.eye(4, dtype=np.float32)
eye[0:3] = pose
pose = eye
poses.append(pose)
name = f'camera_{i}_distortion_coeffs'
dist_coeff = np.reshape(np.array(root.find(name).find('data').text.split(), dtype=np.float32), [1, 5])
dist_coeffs.append(dist_coeff)
Ks = np.array(Ks)
poses = np.linalg.inv(np.array(poses))
dist_coeffs = np.array(dist_coeffs)
# poses = np.array(poses)
return Ks, poses, dist_coeffs
def get_render_pose(para_ext, render_views=120):
Rs = np.repeat(para_ext[0:1, 0:4, 0:3], render_views, 0)
Ts_yz = np.repeat(para_ext[0:1, 1:4, 3:4], render_views, 0)
interp_x = np.linspace(para_ext[0, 0, 3], para_ext[1, 0, 3], render_views, dtype=np.float32)
Ts_x = interp_x[:, np.newaxis, np.newaxis]
Ts = np.concatenate([Ts_x, Ts_yz], 1)
poses = np.concatenate([Rs, Ts], -1)
return poses[:, :, :]
# #将kinect图像复制到images下
# for k in range(10):
# source_path = f'/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/kinect_data/batch_data/yangkun_new/{str(k).zfill(3)}'
# tgt_path = f'/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/poeples/person_{str(k+1)}'
# for i in range(4):
# folder_path = os.path.join(source_path, str(i))
# png_files = glob.glob(os.path.join(folder_path, '*.png'))
# png_files_names = [os.path.basename(file_path) for file_path in png_files]
# for png_file in png_files_names:
# frame_name = png_file.replace('test', 'frame')[0:-4]
# frame_path = os.path.join(tgt_path, frame_name, 'images')
# os.makedirs(frame_path, exist_ok=True)
# shutil.copy(os.path.join(folder_path,png_file), frame_path + f'/{str(i).zfill(4)}.png')
##写入参数
# para_path = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/kinect_data/parameter.xml'
# Ks, poses, dist_coeffs = load_camera_xml(para_path)
# print(Ks)
# render_views = get_render_pose(poses)
# np.save('/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/kinect_data/render_path.npy', render_views)
# tgt_path = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/poeples/person_0'
# for i in range(150):
# mesh_path = os.path.join(tgt_path, f'frame{str(i)}','mesh3D')
# os.makedirs(mesh_path, exist_ok=True)
# np.save(mesh_path + '/cameras.npy', poses)
# np.save(mesh_path + '/images.npy', Ks)
#把mask图像复制到masked文件夹下
# source_path = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/kinect_data/mask/out'
# tgt_path = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/poeples/person_0'
# for i in range(0, 150):
# mask_path = os.path.join(source_path,f'frame{str(i)}', 'images')
# tgtmask_path = os.path.join(tgt_path,f'frame{str(i)}', 'masked')
# # os.makedirs(tgtmask_path, exist_ok=True)
# shutil.copytree(mask_path, tgtmask_path, dirs_exist_ok=True)
def modify_obj_file_with_color(input_file, output_file, x_offset, z_offset):
with open(input_file, 'r') as file:
lines = file.readlines()
modified_lines = []
for line in lines:
if line.startswith('v '):
parts = line.split()
# 解析顶点坐标
x = float(parts[1]) - x_offset
y = -float(parts[2])
z = -float(parts[3]) - z_offset
# 重新构建顶点行,保留颜色信息(如果存在)
if len(parts) > 4:
color_info = " ".join(parts[4:])
modified_line = f"v {x} {y} {z} {color_info}\n"
else:
modified_line = f"v {x} {y} {z}\n"
modified_lines.append(modified_line)
else:
modified_lines.append(line)
with open(output_file, 'w') as file:
file.writelines(modified_lines)
##修改obj flame的位置
# # 使用示例
# input_file = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/poeples/person_0/frame0/mesh3D/0003_points3Da_1.obj'
# output_file = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/poeples/person_0/frame0/mesh3D/0003_points3D.obj'
# x_offset = -0.15
# z_offset = -0.3
# modify_obj_file_with_color(input_file, output_file, x_offset, z_offset)
##去畸变
# para_path = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/kinect_data/parameter.xml'
# Ks, poses, dist_coeffs = load_camera_xml(para_path)
# source_path = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/poeples/person_0'
# for i in range(1, 150):
# for j in range(4):
# pic_path = source_path + f'/frame{str(i)}/masked/{str(j).zfill(4)}.png'
# dist_coeff = dist_coeffs[j, 0, :] # 示例径向畸变系数
# camera_matrix = Ks[j, :, :]
# image = cv2.imread(pic_path)
# undistorted_image = cv2.undistort(image, camera_matrix, dist_coeff)
# cv2.imwrite(source_path + f'/frame{str(i)}/masked/{str(j).zfill(4)}.png', undistorted_image)
# 把初始flame复制到每一个mesh3D文件夹里
def copy_obj_files(source_dir, destination_dir):
try:
# 遍历源文件夹中的所有文件和文件夹
for file_name in os.listdir(source_dir):
# 构建源文件的完整路径
source_file = os.path.join(source_dir, file_name)
# 检查文件是否是 .obj 文件
if file_name.endswith(('.obj', '.npy')):
# 构建目标文件的完整路径
destination_file = os.path.join(destination_dir, file_name)
# 复制 .obj 文件到目标文件夹
shutil.copy(source_file, destination_file)
print(f"已复制文件 {source_file} 到 {destination_file}")
except OSError as e:
print(f"复制文件时发生错误:{e}")
source_path = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/poeples/person_10'
for i in range(1, 150):
for j in range(4):
obj_path = source_path + f'/frame{str(i)}/mesh3D'
os.makedirs(source_path + f'/frame{str(i)}/mesh3D', exist_ok=True)
copy_obj_files('/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/poeples/person_0/frame0/mesh3D', source_path + f'/frame{str(i)}/mesh3D')
# ##写入华为外参
# def load_camera_xml(xml_path):
# tree = ET.parse(str(xml_path))
# root = tree.getroot()
# Ks = []
# poses = []
# dist_coeffs = []
# for i in range(2):
# name = f'camera_{i}_intrinsic_matrix'
# K = np.reshape(np.array(root.find(name).find('data').text.split(), dtype=np.float32), [3, 3])
# # K[0, 0] = K[0, 0]
# # K[1, 1] = K[1, 1]
# # K[0, 2] = 0.5 *
# # K[1, 2] = 0.5
# Ks.append([1080., 1920., 1495.07, 1495.07, 540., 960.])
# # Ks.append(K)
# name = f'camera_{i}_rotation_matrix'
# R = np.reshape(np.array(root.find(name).find('data').text.split(), dtype=np.float32), [3, 3])
# name = f'camera_{i}_translation_vector'
# T = np.reshape(np.array(root.find(name).find('data').text.split(), dtype=np.float32), [3, 1])/1000
# pose = np.concatenate((R, T), axis=1)
# eye = np.eye(4, dtype=np.float32)
# if i == 0:
# eye[0:3] = [[0.061394, 0.869224, -0.490592, 113.487], [0.998099, -0.050835, 0.034837, 13.0695], [0.005341, -0.491798, -0.870693, 269.432]]
# elif i == 1:
# eye[0:3] = [[-0.103505, -0.991412, 0.079936, 15.4573], [-0.992358, 0.108363, 0.059015, 3.2613], [-0.06717, -0.073217, -0.995051, 299.719]]
# pose = eye
# poses.append(pose)
# name = f'camera_{i}_distortion_coeffs'
# dist_coeff = np.reshape(np.array(root.find(name).find('data').text.split(), dtype=np.float32), [1, 5])
# dist_coeffs.append(dist_coeff)
# Ks = np.array(Ks)
# poses = np.linalg.inv(np.array(poses))
# dist_coeffs = np.array(dist_coeffs)
# # poses = np.array(poses)
# return Ks, poses, dist_coeffs
# para_path = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/kinect_data/parameter.xml'
# Ks, poses, dist_coeffs = load_camera_xml(para_path)
# # print(Ks)
# # render_views = get_render_pose(poses)
# # np.save('/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/kinect_data/render_path.npy', render_views)
# tgt_path = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/poeples/huaweiperson_0'
# for i in range(5):
# mesh_path = os.path.join(tgt_path, f'frame{str(i)}','mesh3D')
# os.makedirs(mesh_path, exist_ok=True)
# np.save(mesh_path + '/cameras.npy', poses)
# np.save(mesh_path + '/images.npy', Ks)
# from pywavefront import Wavefront
# def ply_to_obj(input_file, output_file):
# try:
# # 读取 PLY 文件
# with open(input_file, 'r') as ply_file:
# lines = ply_file.readlines()
# vertices = []
# # 提取点的坐标和 RGB 值
# for line in lines:
# if line.startswith("element vertex"):
# num_vertices = int(line.split()[2])
# elif line.startswith("end_header"):
# break
# for line in lines[-num_vertices:]:
# x, y, z, r, g, b = line.split()[:6]
# vertices.append((float(x), float(y), float(z), int(r), int(g), int(b)))
# # 保存为 OBJ 文件
# with open(output_file, 'w') as obj_file:
# for vertex in vertices:
# obj_file.write(f"v {vertex[0]} {vertex[1]} {vertex[2]} {vertex[3]} {vertex[4]} {vertex[5]}\n")
# print(f"已将 PLY 文件 {input_file} 转换为 OBJ,并保存为 {output_file}")
# except Exception as e:
# print(f"转换文件时发生错误:{e}")
# # # 例子:将名为 "input.ply" 的 PLY 文件转换为 OBJ,并保存为 "output.obj"
# # ply_to_obj("/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/poeples/huaweiperson_0/frame0/mesh3D/0001_points3D.ply", "/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/poeples/huaweiperson_0/frame0/mesh3D/0001_points3D.obj")