Open yhiroya2000 opened 2 months ago
This is the code that I ran. I tried to save videos of amass files.
import os
import numpy as np
from aitviewer.configuration import CONFIG as C
from aitviewer.headless import HeadlessRenderer
from aitviewer.renderables.smpl import SMPLSequence
def process_file(npz_path, output_path):
"""Process a single .npz file."""
print(f"Processing file: {npz_path}")
c = (149 / 255, 85 / 255, 149 / 255, 1)
smpl_seq = SMPLSequence.from_amass(
npz_data_path=npz_path,
fps_out=60.0,
color=c,
name="AMASS Running",
show_joint_angles=True,
)
# Create the headless renderer and add the sequence.
# Ensure proper cleanup of resources by using a context manager if available.
v = HeadlessRenderer()
v.scene.add(smpl_seq)
v.scene.camera.position = np.array([10.0, 2.5, 0.0])
# Save video with correct path
v.save_video(output_path) # Use video_path instead of video_dir
smpl_seq.release()
print(f"Video saved to: {output_path}")
del v
""" except Exception as e:
print(f"An error occurred while processing file {npz_path}: {e}")
# Ensure that the renderer is cleaned up properly if an error occurs
del v # Explicitly delete the renderer to free resources
"""
if __name__ == "__main__":
accad_dir = os.path.join(C.datasets.amass, "ACCAD")
output_dir = os.path.join(C.export_dir, "headless")
# Ensure output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Iterate over all .npz files in ACCAD directory and its subdirectories
for root, _, files in os.walk(accad_dir):
for file in files:
if file.endswith(".npz"):
npz_path = os.path.join(root, file)
# Generate output path for the video
video_name = os.path.splitext(file)[0] + ".mp4"
output_path = os.path.join(output_dir, video_name)
# Process each file
print(f"Processing: npz_path={npz_path}, output_path={output_path}")
process_file(npz_path, output_path)
Yes, that is unfortunately a known problem, see #12 . The cause is that the viewer is not completely cleaned up after the first use, which appears to be related to the moderngl-window
implementation of pyqt5
. You could try to use pyglet
as the backend instead, but this might be slower:
from aitviewer.configuration import CONFIG as C
C.update_conf({"window_type": "pyglet"})
Another possibility is to only instantiate the HeadlessRenderer
once and re-use it. I.e. after your first rendering pass, you remove all the nodes you don't need anymore (v.scene.remove(*nodes)
) and add the new ones, but you always keep a reference to the same headless renderer object.
Finally, you can also solve the problem by starting the script as a new process each time you want to render something. For example, you could re-write your script to take npz_path
and output_path
as inputs via command line arguments. And then you call that script from another script that iterates over all the rendering passes you want to do. Something like:
import subprocess
npz_files = ["some/path/test1.npz", "some/path/test2.npz"]
for npz_file in npz_files:
command = "python render_something.py --npz_file npz_file --output_path npz_file.replace('.npz', '.mp4')"
subprocess.run(command, shell=True, check=True)
you should claim "v = HeadlessRenderer()" only once in if name == "main":, it works for me
Hi, I want to save all videos in the directory.
When I tried to save videos by using HeadlessRenderer(), " _moderngl.Error: the program belongs to a different context " happened.
The first video is saved perfectly, but this error happened on the second video.
What should I do?
Here is the detail of the error code.