| import cv2 | |
| import os | |
| def extract_frame(video_path, frame_number, output_path): | |
| # 打开视频文件 | |
| cap = cv2.VideoCapture(video_path) | |
| if not cap.isOpened(): | |
| print(f"无法打开视频文件: {video_path}") | |
| return | |
| # 设置视频捕捉的位置到指定帧 | |
| cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number) | |
| # 读取指定的帧 | |
| success, frame = cap.read() | |
| if success: | |
| # 保存帧为指定路径的文件 | |
| cv2.imwrite(output_path, frame) | |
| print(f"已成功提取帧 {frame_number} 并保存为 {output_path}") | |
| else: | |
| print(f"未能读取帧 {frame_number}。请检查帧编号是否超出范围。") | |
| # 释放资源 | |
| cap.release() | |
| if __name__ == "__main__": | |
| video_file = "/mnt/lpai-dione/ssai/cvg/team/wjj/ViViD/dataset/ViViD/dresses/densepose/803137_detail.mp4" # 替换为你的 MP4 文件路径 | |
| frame_to_extract = 24 # 需要提取的帧编号 | |
| output_file = "/mnt/lpai-dione/ssai/cvg/team/wjj/ViViD/configs/valid/densepose_images/803137_in_xl.jpg" # 替换为你想保存的路径 | |
| # 创建包含输出文件的目录(如果不存在) | |
| output_dir = os.path.dirname(output_file) | |
| if not os.path.exists(output_dir) and output_dir: | |
| os.makedirs(output_dir) | |
| extract_frame(video_file, frame_to_extract, output_file) |