Animated Drawings
repository·main·Indexed 12 days ago
https://github.com/facebookresearch/animateddrawingsAn implementation of the algorithm for animating children's drawings of the human figure. It allows users to animate drawings using motion sequences via BVH files, featuring a Model-View-Controller (MVC) configuration system, a character rigging process, and a Quadruped Extension for animating four-legged animals.
What's inside Animated Drawings
- Characters in Animated Drawings are deformed using As-Rigid-As-Possible (ARAP) shape manipulation. A Python implementation of this algorithm is available in the repository for developers interested in using it for similar purposes.
Customize scenes with multiple characters or backgrounds
mainYou can extend animations by modifying configuration files:
- Multiple Characters: Add multiple entries to the
ANIMATED_CHARACTERSlist in the scene config. - Background Images: Specify an image path within the config to use as a background.
- Custom BVH Skeletons: Use any BVH format. If the skeleton differs from the standard, you must create a new motion config and retarget config file to map the motion to your character.
# Example: Multiple characters from animated_drawings import render render.start('./examples/config/mvc/multiple_characters_example.yaml') # Example: Background image from animated_drawings import render render.start('./examples/config/mvc/background_example.yaml')- Multiple Characters: Add multiple entries to the
Configure the Retarget Config File
mainThe Retarget Config File maps 3D skeletal motion (from a BVH actor) onto a 2D Animated Drawing character rig. It defines how 3D joint positions are projected onto 2D planes, how character body parts are rendered in depth order, and how skeletal joint orientations translate to character bone rotations.
Key configuration areas include:
- Projection: Mapping BVH joints to 2D planes (
frontal,sagittal, orpca). - Depth/Rendering: Determining the order in which character body parts (e.g., arms vs. torso) are rendered to handle overlaps.
- Root Offset: Scaling and projecting the skeleton's 3D translation to the character's 2D movement.
- Joint Mapping: Defining how character bones rotate based on the orientation of pairs of BVH joints.
- Runtime Checks: Conditional removal of joint mappings based on the character's pose (e.g., preventing a 'neck' from flipping on certain character types).
- Projection: Mapping BVH joints to 2D planes (
Configure the top-level MVC Config File
mainThe MVC (Model-View-Controller) configuration file is the top-level configuration passed into
render.start()to generate animations. It follows a Model-View-Controller design pattern where the 'Model' is referred to asscene.Important: Do not modify the base configuration file
animated_drawings/mvc_base_cfg.yaml. Instead, create a new config file containing only the parameters you wish to override. The rendering script will merge your overrides into the base configuration.The file is organized into three main subgroups:
scene(Model): Parameters for the animation environment.view(View): Parameters for rendering, camera, and visual output.controller(Controller): Parameters for execution mode (interactive vs. video rendering).
# Example structure of an MVC config file scene: ADD_FLOOR: True ANIMATED_CHARACTERS: - character_cfg: "path/to/char.yaml" motion_cfg: "path/to/motion.yaml" retarget_cfg: "path/to/retarget.yaml" view: WINDOW_DIMENSIONS: [1920, 1080] controller: MODE: "video_render" OUTPUT_VIDEO_PATH: "output.mp4"Use the Browser-Based Demo
mainIf you want to animate a drawing without installing the code or using the command line, you can use the official web-based demo hosted by Meta.
https://sketch.metademolab.com/Download the Amateur Drawings Dataset
mainThe Amateur Drawings Dataset can be obtained by downloading the annotations and the image archive via
wget.# download annotations (~275Mb) wget https://dl.fbaipublicfiles.com/amateur_drawings/amateur_drawings_annotations.json # download images (~50Gb) wget https://dl.fbaipublicfiles.com/amateur_drawings/amateur_drawings.tarInstall Animated Drawings via Conda and pip
mainTo install Animated Drawings, it is strongly recommended to use a Python virtual environment. The following steps use Conda to create a Python 3.8.13 environment, clone the repository, and install the package in editable mode using
pip install -e ..Note for Mac M1/M2 users: If you encounter architecture errors, ensure your
~/.condarcfile containsosx-arm64andnoarchin its subdirs listing, and does not containosx-64. You can verify this during theconda createstep by checking if the packages being installed areosx-arm64versions.# create and activate the virtual environment conda create --name animated_drawings python=3.8.13 conda activate animated_drawings # clone AnimatedDrawings and use pip to install git clone https://github.com/facebookresearch/AnimatedDrawings.git cd AnimatedDrawings pip install -e .Configure a Character Config File
mainA Character Configuration file (
char_cfg) defines the properties of an Animated Drawing instance.Requirement: The
texture.pngandmask.pngfiles for the character must be located in the same directory as thechar_cfgfile.Key fields include:
heightandwidth: The pixel dimensions of the associatedtexture.pngandmask.png.skeleton: A list of joint dictionaries defining the character's structure. Each joint must have aname, aloc(image-space [x, y] pixels, where 0,0 is top-left), and aparent(the name of the parent joint). The joint named'root'must have a parent ofnull.
height: 1024 width: 1024 skeleton: - name: "root" loc: [512, 512] parent: null - name: "spine" loc: [512, 400] parent: "root"Reconstruct the ChildlikeSHAPES dataset
mainTo obtain the ChildlikeSHAPES dataset, you must reconstruct the full archive from the chunks provided in the releases page using the
catcommand. Note that the archive requires a password.cat datachunk_* > full_archive.7z #(pw=An1m8dR3610Ns)Animate your own drawing using Docker and TorchServe
mainTo automatically generate annotations (mask, texture, and joints) from a raw image, you can use a TorchServe container to run machine learning models.
- Build and run the Docker container from the
torchservedirectory:cd torchserve docker build -t docker_torchserve . docker run -d --name docker_torchserve -p 8080:8080 -p 8081:8081 docker_torchserve - Verify the server is healthy:
curl http://localhost:8080/ping - Run the animation pipeline from the
examplesdirectory:
Example:cd ../examples python image_to_animation.py <path_to_image> <output_folder>python image_to_animation.py drawings/garlic.png garlic_out
# Build and run cd torchserve docker build -t docker_torchserve . docker run -d --name docker_torchserve -p 8080:8080 -p 8081:8081 docker_torchserve # Run animation cd ../examples python image_to_animation.py drawings/garlic.png garlic_out- Build and run the Docker container from the
Animate a drawing using the Quadruped Extension
mainThe Quadruped Extension allows you to animate drawings as four-legged animals. The workflow involves three main steps: generating annotations from your drawing, converting the human-based configuration to an animal configuration, and finally rendering the animation using a scene configuration file.
1. Generate Annotations
Run
image_to_animation.pyon your drawing to create the initial character configuration.2. Convert to Animal Configuration
Use
human_to_animal.pyto transform the generated configuration into an animal-compatible format. Note:human_to_animal.pymust be located in the same directory as the newly generated files or you must provide its full path.3. Configure and Render the Scene
Edit a scene configuration file (e.g.,
four_example.yaml) to point to your new animal configuration. You must update thecharacter_cfgfield underANIMATED_CHARACTERS.# Step 1: Generate annotations python image_to_animation.py path/to/your/file # Step 2: Convert to animal config python human_to_animal path/to/your/yamlConfigure a Motion Config File
mainThe Motion Configuration file defines how a BVH (BioVision Hierarchy) file drives the character animation.
Key parameters:
filepath(str): Path to the BVH file.start_frame_idx/end_frame_idx(int): Range of frames to use from the BVH.frame_time(float): Override the BVH's internal frame time.groundplane_joint(str): A joint name used to adjust the worldspace y-offset so the joint sits at y=0 at the start frame.forward_perp_joint_vectors(list[List[str, str]]): Pairs of joint names used to calculate the skeleton's 'forward' vector during retargeting.scale(float): Uniform scale for the BVH skeleton.up(str): The 'up' direction in the BVH. Supported values:+yor+z.
filepath: "motions/dance.bvh" start_frame_idx: 0 end_frame_idx: 100 frame_time: 0.033 groundplane_joint: "pelvis" forward_perp_joint_vectors: - ["spine", "head"] scale: 1.0 up: "+y"