Animate Portraits with the charlesmccarthy/liveportrait Cognitive Actions

22 Apr 2025
Animate Portraits with the charlesmccarthy/liveportrait Cognitive Actions

In today’s digital landscape, the ability to bring still images to life can significantly enhance user engagement. The charlesmccarthy/liveportrait API provides a powerful Cognitive Action designed for animating portrait images using driving videos. This action transforms static portraits into dynamic animations, offering a unique way to engage audiences across various applications.

Prerequisites

Before you start integrating the Cognitive Actions, ensure you have the following:

  • An API key for the Cognitive Actions platform. This key will allow you to authenticate your requests.
  • Basic knowledge of making HTTP requests and handling JSON data in your preferred programming language.

Typically, authentication is achieved by passing your API key in the headers of your requests, allowing you to securely access the available actions.

Cognitive Actions Overview

Animate Portrait Image

Description: This action animates a still portrait image by utilizing a driving video, creating a lifelike animated version of the image. It employs the Mbuke cog to process the input and generate the animation.

  • Category: image-animation

Input

The input schema for the Animate Portrait Image action requires the following fields:

  • image (string, required): A URI pointing to the portrait image. This should be in a format compatible with the expected input for processing.
    • Example: https://replicate.delivery/pbxt/LECJXqGPZTRxGRMIVch48jG9anWqAKhtSB4uLQaPkpoQdGdK/genimage555824704_1.png
  • video (string, required): A URI pointing to the driving video. This video is used to animate the portrait image.
    • Example: https://replicate.delivery/pbxt/LECJYHhKXvgg4bnRi9AMUWlm5fW54Cvuqoe1uh5GPOm52RLm/d6.mp4
  • flagDoCrop (boolean, optional): Indicates whether the portrait image should be cropped to focus on the face. The default value is true.
  • flagPasteBack (boolean, optional): Indicates whether the animated face should be pasted back into the original image. The default value is true.

Example Input:

{
  "image": "https://replicate.delivery/pbxt/LECJXqGPZTRxGRMIVch48jG9anWqAKhtSB4uLQaPkpoQdGdK/genimage555824704_1.png",
  "video": "https://replicate.delivery/pbxt/LECJYHhKXvgg4bnRi9AMUWlm5fW54Cvuqoe1uh5GPOm52RLm/d6.mp4"
}

Output

Upon successful execution, the action returns a URI pointing to the generated animated video.

  • Example Output:
https://assets.cognitiveactions.com/invocations/ab6b7fde-bb60-4e88-873d-7f2a2259d211/c007a163-830c-4696-b317-395ed9ef1001.mp4

Conceptual Usage Example (Python)

Here’s a conceptual Python snippet demonstrating how you might call the Animate Portrait Image action:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"  # Hypothetical endpoint

action_id = "bf6692e7-67d7-43e8-ae79-d04fdb53717b"  # Action ID for Animate Portrait Image

# Construct the input payload based on the action's requirements
payload = {
    "image": "https://replicate.delivery/pbxt/LECJXqGPZTRxGRMIVch48jG9anWqAKhtSB4uLQaPkpoQdGdK/genimage555824704_1.png",
    "video": "https://replicate.delivery/pbxt/LECJYHhKXvgg4bnRi9AMUWlm5fW54Cvuqoe1uh5GPOm52RLm/d6.mp4"
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload}  # Hypothetical structure
    )
    response.raise_for_status()  # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body: {e.response.text}")

In this example, replace "YOUR_COGNITIVE_ACTIONS_API_KEY" with your actual API key. The action_id is set to the ID of the Animate Portrait Image action, and the payload is structured according to the required input schema.

Conclusion

The charlesmccarthy/liveportrait API's Cognitive Action for animating portrait images offers developers an innovative way to create engaging visual content. By leveraging a driving video, you can transform static images into captivating animations, enhancing user interaction in your applications.

Consider exploring additional use cases, such as integrating animation features into social media apps, marketing platforms, or personal projects. The possibilities are endless, and the ease of integration allows developers to focus on creativity while the API handles the technical details.