目录
3D数据集下载
读取binvox文件
使用matplotlib创建图
动画效果
完整代码
3D数据集下载
这里以shapenet数据集为例,可以访问外网的可以去直接申请下载;我也准备了一个备份在百度网盘的数据集,可以参考:
ShapeNet简介和下载、binvox文件python示例-CSDN博客
读取binvox文件
需要先安装binvox_rw:
https://github.com/wangqiang9/binvox_rw/tree/main
git clone git@github.com:wangqiang9/binvox_rw.git
cd binvox_rw
pip install .
然后调用接口读取binvox文件:文章来源:https://www.toymoban.com/news/detail-838318.html
# 读取 .binvox 文件
with open(path, "rb") as f:
model = binvox_rw.read_as_3d_array(f)
使用matplotlib创建图
# 转换成 numpy 数组
voxels = model.data
# 旋转
voxels = np.transpose(voxels, (2, 1, 0))
voxels = np.transpose(voxels, (0, 2, 1))
# 创建一个图和坐标轴
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_axis_off()
# 批量隐藏所有边框线
for spine in ax.spines.values():
spine.set_visible(False)
# 隐藏刻度线
ax.tick_params(bottom=False, left=False)
# 创建一个可视化的体素图
ax.voxels(voxels, edgecolor='k')
动画效果
# 保存动画
rot_animation.save(gif_path, dpi=100, writer='imagemagick')
video = VideoFileClip(gif_path)
target_duration = video.duration / 3
compressed_video = video.fx(vfx.speedx, target_duration)
compressed_video.write_videofile(video_path)
完整代码
import numpy as np
import binvox_rw
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import os
from moviepy.editor import *
output_path = "/shapenet/video_ratate"
output_gif_path = "/datasets/shapenet/gif_ratate"
folder_path = "/datasets/shapenet/all/ShapeNetVox32/"
subfolders = [f.path for f in os.scandir(folder_path) if f.is_dir()]
def func(path, gif_path, video_path):
# 读取 .binvox 文件
with open(path, "rb") as f:
model = binvox_rw.read_as_3d_array(f)
# 转换成 numpy 数组
voxels = model.data
# 旋转
voxels = np.transpose(voxels, (2, 1, 0))
voxels = np.transpose(voxels, (0, 2, 1))
# 创建一个图和坐标轴
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_axis_off()
# 批量隐藏所有边框线
for spine in ax.spines.values():
spine.set_visible(False)
# 隐藏刻度线
ax.tick_params(bottom=False, left=False)
# 创建一个可视化的体素图
ax.voxels(voxels, edgecolor='k')
# 保存动画帧函数
def rotate(angle):
ax.view_init(azim=angle)
# 创建动画
rot_animation = animation.FuncAnimation(fig, rotate, frames=np.arange(0, 360, 4), interval=0.5)
# 保存动画
rot_animation.save(gif_path, dpi=100, writer='imagemagick')
video = VideoFileClip(gif_path)
target_duration = video.duration / 3
compressed_video = video.fx(vfx.speedx, target_duration)
compressed_video.write_videofile(video_path)
plt.show()
for subfolder in subfolders:
subfolders_1 = [f.path for f in os.scandir(subfolder) if f.is_dir()]
for path in subfolders_1:
path = os.path.join(path, 'model.binvox')
gif_name = path.split('/')[-2] + ".gif"
video_name = path.split('/')[-2] + ".mp4"
gif_path = os.path.join(output_gif_path, gif_name)
video_path = os.path.join(output_path, video_name)
func(path, gif_path, video_path)
也可以看我在GitHub上写的案例:文章来源地址https://www.toymoban.com/news/detail-838318.html
https://github.com/wangqiang9/binvox_rw/blob/main/convert_to_video.py
到了这里,关于代码讲解:如何把3D数据转换成旋转的视频?的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!