libero_npy / check.py
miniFranka's picture
Upload folder using huggingface_hub
56e15ac verified
import numpy as np
import cv2
import os
from tqdm import tqdm
def create_video_from_npy(npy_path, output_video_path, fps=30):
data = np.load(npy_path, allow_pickle=True)
if len(data) == 0:
print("empty data!")
return
first_frame = data[0]['image_primary']
height, width, channels = first_frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
print(f"创建视频: {output_video_path}")
print(f"视频尺寸: {width}x{height}, 帧率: {fps}fps")
print(f"total frame number: {len(data)}")
for i, frame_data in enumerate(tqdm(data, desc="处理帧")):
image = frame_data['image_primary']
if image.dtype != np.uint8:
image = (image * 255).astype(np.uint8)
if image.shape[2] == 3:
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
out.write(image)
out.release()
print(f"视频已保存到: {output_video_path}")
def create_video_with_timestamps(npy_path, output_video_path, fps=30):
data = np.load(npy_path, allow_pickle=True)
if len(data) == 0:
print("数据为空,无法创建视频")
return
first_frame = data[0]['image_primary']
height, width, channels = first_frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
print(f"创建带时间戳的视频: {output_video_path}")
print(f"视频尺寸: {width}x{height}, 帧率: {fps}fps")
print(f"总帧数: {len(data)}")
for i, frame_data in enumerate(tqdm(data, desc="处理帧")):
print(frame_data['state'])
# input()
image = frame_data['image_primary']
if image.dtype != np.uint8:
image = (image * 255).astype(np.uint8)
if image.shape[2] == 3:
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.putText(image, f"Frame: {i}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
if 'state' in frame_data:
state = frame_data['state']
cv2.putText(image, f"Pos: {state[:3]}", (10, 70),
cv2.FONT_HERSHEY_SIMPLEX, 0.2, (0, 255, 0), 2)
out.write(image)
out.release()
print(f"带时间戳的视频已保存到: {output_video_path}")
def batch_create_videos(npy_directory, output_directory, fps=30, with_timestamps=False):
os.makedirs(output_directory, exist_ok=True)
npy_files = [f for f in os.listdir(npy_directory) if f.endswith('.npy')]
print(f"找到 {len(npy_files)} 个.npy文件")
for npy_file in npy_files:
npy_path = os.path.join(npy_directory, npy_file)
video_name = os.path.splitext(npy_file)[0] + '.mp4'
output_path = os.path.join(output_directory, video_name)
print(f"\n处理文件: {npy_file}")
if with_timestamps:
create_video_with_timestamps(npy_path, output_path, fps)
else:
create_video_from_npy(npy_path, output_path, fps)
if __name__ == "__main__":
npy_path = "/media/liuzhuoyang/data/libero/npy/libero_spatial_no_noops/episode_1.npy"
output_path = "/media/liuzhuoyang/data/libero/videos/0829_132753.mp4"
data = np.load(npy_path,allow_pickle=True)
print(data[0].keys())
print(data[0]['image_primary'].shape)
print(data[0]['image_wrist'].shape)
exit(0)
os.makedirs(os.path.dirname(output_path), exist_ok=True)
create_video_from_npy(npy_path, output_path)
timestamp_output_path = "/media/liuzhuoyang/data/libero/videos/0829_132753_timestamp.mp4"
create_video_with_timestamps(npy_path, timestamp_output_path)