Download data_tool/Nersemble_data.py from vegagoto123/gha: direct link, hf CLI and curl.
- Browser
- Download file 4.31 kB
-
https://huggingface.co/vegagoto123/gha/resolve/main/data_tool/Nersemble_data.py
- Command line
-
hf download hf://vegagoto123/gha/data_tool/Nersemble_data.py
-
curl -L -o Nersemble_data.py https://huggingface.co/vegagoto123/gha/resolve/main/data_tool/Nersemble_data.py
4.31 kB
| import cv2 | |
| import os | |
| def crop_center_square(img): | |
| # 获取图像的宽度和高度 | |
| height, width = img.shape[:2] | |
| # 计算裁剪区域的左上角和右下角坐标 | |
| if width < height: | |
| top = (height - width) // 2 | |
| bottom = top + width | |
| left = 0 | |
| right = width | |
| else: | |
| left = (width - height) // 2 | |
| right = left + height | |
| top = 0 | |
| bottom = height | |
| # 裁剪图像为正方形 | |
| img_cropped = img[top:bottom, left:right] | |
| return img_cropped | |
| # 指定目录路径 | |
| directory = '/data_160TB/2021/yangkun/gha-gaussian/datasets/real_data/Nersemble_all/data/sequence_EMO-1-shout+laugh_part-2/126/EMO-1-shout+laugh' | |
| # 列出目录中的所有文件 | |
| files = os.listdir(directory) | |
| # 筛选以"cam"开头且以".mp4"结尾的文件路径 | |
| mp4_files = [os.path.join(directory, file) for file in files if file.startswith('cam') and file.endswith('.mp4')] | |
| # 对文件路径进行排序 | |
| mp4_files = sorted(mp4_files) | |
| # 打印筛选出的MP4文件路径 | |
| view_count = 0 | |
| for file_path in mp4_files: | |
| print(file_path) | |
| # 读取视频文件 | |
| video_path = file_path | |
| cap = cv2.VideoCapture(video_path) | |
| # 逐帧读取视频并保存为图像 | |
| frame_count = 0 | |
| while cap.isOpened(): | |
| os.makedirs(os.path.join(directory, 'people', f'frame_{str(frame_count).zfill(4)}', 'images'), exist_ok=True) | |
| write_path = os.path.join(directory, 'people', f'frame_{str(frame_count).zfill(4)}', 'images') | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| cv2.imwrite(write_path + f'/{str(view_count).zfill(4)}.png', crop_center_square(frame)) | |
| frame_count += 1 | |
| view_count +=1 | |
| # 释放视频对象 | |
| cap.release() | |
| import json | |
| import numpy as np | |
| import shutil | |
| # 读取JSON文件 | |
| # with open('/data_160TB/2021/yangkun/gha-gaussian/datasets/real_data/Nersemble/nersemble/036/camera_params.json', 'r') as file: | |
| # data = json.load(file) | |
| # # 对外参按照键值进行排序 | |
| # sorted_world_2_cam = {k: v for k, v in sorted(data['world_2_cam'].items())} | |
| # inverted_world_2_cam = {k: np.linalg.inv(v) for k, v in sorted_world_2_cam.items()} | |
| # # 从字典中获取 NumPy 数组的列表 | |
| # array_list = [array for array in inverted_world_2_cam.values()] | |
| # # 连接所有 NumPy 数组成一个新的 NumPy 数组 | |
| # para_ext = np.concatenate([arr[np.newaxis] for arr in array_list], axis=0) | |
| # # 保存排序好的外参为npy文件 | |
| # np.save('/data_160TB/2021/yangkun/gha-gaussian/datasets/real_data/Nersemble/nersemble/036/EMO-1-shout+laugh/cameras.npy', para_ext) | |
| # # 提取内参并按照指定顺序保存为npy文件 | |
| # intrinsics = [] | |
| # params = data['intrinsics'] | |
| # params = np.array(params) | |
| # # H, W, fx, fy, cx, cy = params[0][0], params[1][1], params[0][2], params[1][2], params[0][3], params[1][3] | |
| # H, W, fx, fy, cx, cy = 1099.2677002*2, 1099.2677002*2, 8198.8134765625, 8198.8134765625, 1099.2677002, 1099.2677002 | |
| # intrinsics.append([H, W, fx, fy, cx, cy]) | |
| # intrinsics = np.array(intrinsics) | |
| # repeated_array = np.tile(intrinsics, (16, 1)) | |
| # # 保存内参为npy文件 | |
| # np.save('/data_160TB/2021/yangkun/gha-gaussian/datasets/real_data/Nersemble/nersemble/036/EMO-1-shout+laugh/images.npy', repeated_array) | |
| def copy_folders(path): | |
| f2 = '/data_160TB/2021/yangkun/gha-gaussian/datasets/real_data/Nersemble/nersemble/036/EMO-1-shout+laugh/cameras.npy' | |
| f3 = '/data_160TB/2021/yangkun/gha-gaussian/datasets/real_data/Nersemble/nersemble/036/EMO-1-shout+laugh/images.npy' | |
| for root, dirs, files in os.walk(path): | |
| for dir_name in dirs: | |
| # if dir_name == "images" or dir_name == "mesh3D" or dir_name == "point_cloud": | |
| # full_path = os.path.join(root, dir_name) | |
| # shutil.rmtree(full_path) | |
| all_path = os.path.join(root, dir_name) | |
| os.makedirs(os.path.join(all_path, 'mesh3D'), exist_ok=True) | |
| final_path = os.path.join(all_path, 'mesh3D') | |
| # shutil.copytree(f1, all_path+'/') | |
| # shutil.copy(f1, all_path) | |
| shutil.copy(f2, final_path) | |
| shutil.copy(f3, final_path) | |
| path = "/data_160TB/2021/yangkun/gha-gaussian/datasets/real_data/Nersemble_all/data/sequence_EMO-1-shout+laugh_part-2/126/EMO-1-shout+laugh/" | |
| copy_folders(path) |