Animated Drawings

repository·main·Indexed 12 days ago

https://github.com/facebookresearch/animateddrawings

An 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.

Tokens
5.9K
Snippets
24
Records
28
Agent score
96%

What's inside Animated Drawings

  1. Customize scenes with multiple characters or backgrounds

    main

    You can extend animations by modifying configuration files:

    • Multiple Characters: Add multiple entries to the ANIMATED_CHARACTERS list 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')
  2. Configure the Retarget Config File

    main

    The 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, or pca).
    • 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).
  3. Configure the top-level MVC Config File

    main

    The 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 as scene.

    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:

    1. scene (Model): Parameters for the animation environment.
    2. view (View): Parameters for rendering, camera, and visual output.
    3. 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"
  4. Download the Amateur Drawings Dataset

    main

    The 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.tar
  5. Install Animated Drawings via Conda and pip

    main

    To 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 ~/.condarc file contains osx-arm64 and noarch in its subdirs listing, and does not contain osx-64. You can verify this during the conda create step by checking if the packages being installed are osx-arm64 versions.

    # 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 .
  6. Configure a Character Config File

    main

    A Character Configuration file (char_cfg) defines the properties of an Animated Drawing instance.

    Requirement: The texture.png and mask.png files for the character must be located in the same directory as the char_cfg file.

    Key fields include:

    • height and width: The pixel dimensions of the associated texture.png and mask.png.
    • skeleton: A list of joint dictionaries defining the character's structure. Each joint must have a name, a loc (image-space [x, y] pixels, where 0,0 is top-left), and a parent (the name of the parent joint). The joint named 'root' must have a parent of null.
    height: 1024
    width: 1024
    skeleton:
      - name: "root"
        loc: [512, 512]
        parent: null
      - name: "spine"
        loc: [512, 400]
        parent: "root"
  7. Animate your own drawing using Docker and TorchServe

    main

    To automatically generate annotations (mask, texture, and joints) from a raw image, you can use a TorchServe container to run machine learning models.

    1. Build and run the Docker container from the torchserve directory:
      cd torchserve
      docker build -t docker_torchserve .
      docker run -d --name docker_torchserve -p 8080:8080 -p 8081:8081 docker_torchserve
    2. Verify the server is healthy:
      curl http://localhost:8080/ping
    3. Run the animation pipeline from the examples directory:
      cd ../examples
      python image_to_animation.py <path_to_image> <output_folder>
      Example: 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
  8. Animate a drawing using the Quadruped Extension

    main

    The 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.py on your drawing to create the initial character configuration.

    2. Convert to Animal Configuration

    Use human_to_animal.py to transform the generated configuration into an animal-compatible format. Note: human_to_animal.py must 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 the character_cfg field under ANIMATED_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/yaml
  9. Configure a Motion Config File

    main

    The 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: +y or +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"