import jittor as jt import jrender as jr jt.flags.use_cuda = 1 # 开启GPU加速 import os import tqdm import numpy as np import imageio import argparse # 获取当前文件所在目录路径和数据目录路径 current_dir = os.path.dirname(os.path.realpath(__file__)) data_dir = os.path.join(current_dir, 'data') def main(): # 创建命令行参数解析器 parser = argparse.ArgumentParser() parser.add_argument('-i', '--filename-input', type=str, default=os.path.join(data_dir, 'obj/spot/spot_triangulated.obj')) parser.add_argument('-o', '--output-dir', type=str, default=os.path.join(data_dir, 'results/output_render')) args = parser.parse_args() # other settings camera_distance = 2.732 elevation = 30 azimuth = 0 # load from Wavefront .obj file mesh = jr.Mesh.from_obj(args.filename_input, load_texture=True, texture_res=5, texture_type='surface', dr_type='softras') # create renderer with SoftRas renderer = jr.Renderer(dr_type='softras') os.makedirs(args.output_dir, exist_ok=True) # draw object from different view loop = tqdm.tqdm(list(range(0, 360, 4))) writer = imageio.get_writer(os.path.join(args.output_dir, 'rotation.gif'), mode='I') imgs = [] from PIL import Image for num, azimuth in enumerate(loop): # rest mesh to initial state mesh.reset_() loop.set_description('Drawing rotation') renderer.transform.set_eyes_from_angles(camera_distance, elevation, azimuth) rgb = renderer.render_mesh(mesh, mode='rgb') image = rgb.numpy()[0].transpose((1, 2, 0)) writer.append_data((255*image).astype(np.uint8)) writer.close() # draw object from different sigma and gamma loop = tqdm.tqdm(list(np.arange(-4, -2, 0.2))) renderer.transform.set_eyes_from_angles(camera_distance, elevation, 45) writer = imageio.get_writer(os.path.join(args.output_dir, 'bluring.gif'), mode='I') for num, gamma_pow in enumerate(loop): # rest mesh to initial state mesh.reset_() renderer.set_gamma(10**gamma_pow) renderer.set_sigma(10**(gamma_pow - 1)) loop.set_description('Drawing blurring') images = renderer.render_mesh(mesh, mode='rgb') image = images.numpy()[0].transpose((1, 2, 0)) # [image_size, image_size, RGB] writer.append_data((255*image).astype(np.uint8)) writer.close() # save to textured obj mesh.reset_() mesh.save_obj(os.path.join(args.output_dir, 'saved_spot.obj')) if __name__ == '__main__': main()在每行代码后添加注释
时间: 2024-04-26 18:20:38 浏览: 194
cp.tar.gz_Enough!_as
# 引入所需的库
import jittor as jt
import jrender as jr
jt.flags.use_cuda = 1 # 开启GPU加速
import os
import tqdm
import numpy as np
import imageio
import argparse
# 获取当前文件所在目录路径和数据目录路径
current_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(current_dir, 'data')
def main():
# 创建命令行参数解析器
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--filename-input', type=str,
default=os.path.join(data_dir, 'obj/spot/spot_triangulated.obj')) # 输入文件路径
parser.add_argument('-o', '--output-dir', type=str,
default=os.path.join(data_dir, 'results/output_render')) # 输出文件路径
args = parser.parse_args()
# other settings
camera_distance = 2.732 # 相机距离
elevation = 30 # 抬高角度
azimuth = 0 # 方位角度
# load from Wavefront .obj file
mesh = jr.Mesh.from_obj(args.filename_input, load_texture=True, texture_res=5, texture_type='surface', dr_type='softras') # 从.obj文件载入模型
# create renderer with SoftRas
renderer = jr.Renderer(dr_type='softras') # 创建渲染器
os.makedirs(args.output_dir, exist_ok=True)
# draw object from different view
loop = tqdm.tqdm(list(range(0, 360, 4))) # 视角变换循环
writer = imageio.get_writer(os.path.join(args.output_dir, 'rotation.gif'), mode='I') # 创建gif文件
imgs = []
from PIL import Image
for num, azimuth in enumerate(loop):
# rest mesh to initial state
mesh.reset_() # 重置模型状态
loop.set_description('Drawing rotation')
renderer.transform.set_eyes_from_angles(camera_distance, elevation, azimuth) # 设置相机位置和角度
rgb = renderer.render_mesh(mesh, mode='rgb') # 渲染模型
image = rgb.numpy()[0].transpose((1, 2, 0)) # 转置图片通道
writer.append_data((255*image).astype(np.uint8)) # 写入gif文件
writer.close()
# draw object from different sigma and gamma
loop = tqdm.tqdm(list(np.arange(-4, -2, 0.2))) # 模糊循环
renderer.transform.set_eyes_from_angles(camera_distance, elevation, 45) # 设置相机位置和角度
writer = imageio.get_writer(os.path.join(args.output_dir, 'bluring.gif'), mode='I') # 创建gif文件
for num, gamma_pow in enumerate(loop):
# rest mesh to initial state
mesh.reset_() # 重置模型状态
renderer.set_gamma(10**gamma_pow) # 设置gamma值
renderer.set_sigma(10**(gamma_pow - 1)) # 设置sigma值
loop.set_description('Drawing blurring')
images = renderer.render_mesh(mesh, mode='rgb') # 渲染模型
image = images.numpy()[0].transpose((1, 2, 0)) # [image_size, image_size, RGB]
writer.append_data((255*image).astype(np.uint8)) # 写入gif文件
writer.close()
# save to textured obj
mesh.reset_() # 重置模型状态
mesh.save_obj(os.path.join(args.output_dir, 'saved_spot.obj')) # 保存模型
if __name__ == '__main__':
main()
阅读全文