Effortlessly Composite Images with Zero-Shot Material Transfer using camenduru/zest

24 Apr 2025
Effortlessly Composite Images with Zero-Shot Material Transfer using camenduru/zest

Cognitive Actions provide developers with powerful, pre-built functionality to enhance their applications. The camenduru/zest API offers several capabilities, including the ability to perform Zero-Shot Material Transfer from one image to another. This feature allows for high-quality material composition, enabling developers to create stunning visuals with minimal effort. In this article, we will explore how to integrate this action into your applications effectively.

Prerequisites

Before you start using the Cognitive Actions, ensure that you have the following:

  • An API key for the Cognitive Actions platform, which you will need for authentication.
  • Basic familiarity with making API calls in Python.

For authentication, you'll typically pass your API key in the request headers. This allows you to securely access the Cognitive Actions API.

Cognitive Actions Overview

Perform Zero-Shot Material Transfer from Image

The Perform Zero-Shot Material Transfer from Image action allows you to overlay materials from one image onto another base image using the ZeST model. This action is categorized under image-processing and is particularly useful for tasks that require seamless integration of materials.

Input

The input for this action requires two fields:

  • inputImage: The URI of the input image to be processed. This image serves as the base for compositing. It is required and must be a valid URL.
  • materialImage: The URI of the material image that will be overlaid on the input image. It is also required and must be a valid URL.

Here’s an example of how the input JSON payload looks:

{
  "inputImage": "https://replicate.delivery/pbxt/Kl1u9jl4YgM5t22DVNR3xzSQxECr7pAoVvjm2OaV6urHVXf4/1.jpg",
  "materialImage": "https://replicate.delivery/pbxt/Kl23gJODaW7EuxrDzBG9dcgqRdMaYSWmBQ9UexnwPiL7AnIr/3.jpg"
}

Output

Upon successful execution, this action returns a URL to the composited image, which is a combination of the input image and the material image. For instance, the output might look like this:

https://assets.cognitiveactions.com/invocations/57c847ac-294f-450a-a1f6-b27deec8b846/d64d4b76-374d-46bf-b91c-78a7437130ef.png

Conceptual Usage Example (Python)

Below is a conceptual Python code snippet illustrating how to invoke the Zero-Shot Material Transfer 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 = "e5ea8641-f460-472d-bcc1-b2900499cbe5"  # Action ID for Perform Zero-Shot Material Transfer from Image

# Construct the input payload based on the action's requirements
payload = {
    "inputImage": "https://replicate.delivery/pbxt/Kl1u9jl4YgM5t22DVNR3xzSQxECr7pAoVvjm2OaV6urHVXf4/1.jpg",
    "materialImage": "https://replicate.delivery/pbxt/Kl23gJODaW7EuxrDzBG9dcgqRdMaYSWmBQ9UexnwPiL7AnIr/3.jpg"
}

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 and input payload are structured to match the requirements for the Zero-Shot Material Transfer action.

Conclusion

The camenduru/zest API's Zero-Shot Material Transfer action provides a straightforward way to enhance images by overlaying materials seamlessly. By integrating this action into your applications, you can create visually appealing results with minimal complexity. Consider exploring additional use cases for this powerful cognitive capability, and happy coding!