Skip to content

Torchvision will run into errors because of an argument bug #158

@KeenanWang

Description

@KeenanWang

Firstly, thanks for your excellent work! It's simple and helpful.
When a video has been tracked and is ready to be written to disk, the app.py will call up a function named torchvision.io.write_video. However, the function will rise a bug that AttributeError: 'numpy.float64' object has no attribute 'numerator'[1].
This bug can't be solved instead of changing version of torch[2]. However, there has an another way to save video, which is already in this work.
In app.py and line 329-351.

# generate video after vos inference
def generate_video_from_frames(frames, output_path, fps=30):
    """
    Generates a video from a list of frames.
    
    Args:
        frames (list of numpy arrays): The frames to include in the video.
        output_path (str): The path to save the generated video.
        fps (int, optional): The frame rate of the output video. Defaults to 30.
    """
    # height, width, layers = frames[0].shape
    # fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    # video = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
    # print(output_path)
    # for frame in frames:
    #     video.write(frame)
    
    # video.release()
    frames = torch.from_numpy(np.asarray(frames))
    if not os.path.exists(os.path.dirname(output_path)):
        os.makedirs(os.path.dirname(output_path))
    torchvision.io.write_video(output_path, frames, fps=fps, video_codec="libx264")
    return output_path

The only thing you need to do is comment out the second half of the code and uncomment the first half. Just like codes below.

# generate video after vos inference
def generate_video_from_frames(frames, output_path, fps=30):
    """
    Generates a video from a list of frames.
    
    Args:
        frames (list of numpy arrays): The frames to include in the video.
        output_path (str): The path to save the generated video.
        fps (int, optional): The frame rate of the output video. Defaults to 30.
    """
    height, width, layers = frames[0].shape
    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    video = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
    print(output_path)
    for frame in frames:
        video.write(frame)

    # video.release()
    # frames = torch.from_numpy(np.asarray(frames))
    # if not os.path.exists(os.path.dirname(output_path)):
    #     os.makedirs(os.path.dirname(output_path))
    # torchvision.io.write_video(output_path, frames, fps=int(fps), video_codec="h264")
    return output_path

reference:
[1] https://blog.gitcode.com/6fd94773a180dd043a327d083006d3c7.html
[2] pytorch/vision#8990

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions