magic-research / magic-animate

[CVPR 2024] MagicAnimate: Temporally Consistent Human Image Animation using Diffusion Model
https://showlab.github.io/magicanimate/
BSD 3-Clause "New" or "Revised" License
10.4k stars 1.07k forks source link

Easy to use Inference Code #71

Open Bilal143260 opened 10 months ago

Bilal143260 commented 10 months ago

If someone has tried inferencing the model using shell scripts provided by the authors you may have found that videos are not saved in output directory.

I have a written a simple script for inferencing the model and it's easy to use.

import argparse
import numpy as np
from PIL import Image
import imageio

from demo.animate import MagicAnimate

def read_video(video_path):
    reader = imageio.get_reader(video_path)
    return reader

def read_image(image_path, size=512):
    image = Image.open(image_path)
    return np.array(image.resize((size, size)))

def main(reference_image_path, motion_sequence_path, seed, steps, guidance_scale):
    animator = MagicAnimate()

    reference_image = read_image(reference_image_path)
    motion_sequence = read_video(motion_sequence_path)

    animation = animator(reference_image, motion_sequence, seed, steps, guidance_scale)
    return animation

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Animate images using MagicAnimate.")
    parser.add_argument("reference_image", help="Path to the reference image")
    parser.add_argument("motion_sequence", help="Path to the motion sequence video")
    parser.add_argument("--seed", type=int, default=1, help="Random seed (default: 1)")
    parser.add_argument("--steps", type=int, default=25, help="Sampling steps (default: 25)")
    parser.add_argument("--guidance_scale", type=float, default=7.5, help="Guidance scale (default: 7.5)")

    args = parser.parse_args()

    main(args.reference_image, args.motion_sequence, args.seed, args.steps, args.guidance_scale)

run this script like this

python script_name.py path_to_reference_image.jpg path_to_motion_sequence.mp4 --seed 123 --steps 50 --guidance_scale 5.0
Bilal143260 commented 10 months ago

pull request already created for this

FurkanGozukara commented 10 months ago

awesome ty

JackChen890311 commented 9 months ago

Hi thanks for this! I'm wondering if there is a typo at line 10, where you should return reader instead of return video_path? Also at line 22 you could just pass motion_sequence_path to animator and it will handle it.

Bilal143260 commented 9 months ago

Hi thanks for this! I'm wondering if there is a typo at line 10, where you should return reader instead of return video_path? Also at line 22 you could just pass motion_sequence_path to animator and it will handle it.

yes you are right. I have edited it and fixed the typo. Thanks

ddpasa commented 9 months ago

Is it possible to make this support non-square images or densepose videos?

FurkanGozukara commented 9 months ago

Is it possible to make this support non-square images or densepose videos?

i didn't try non-square but i tried 768x768 , it was terrible

so probably you can make. you need to edit code