Transform Your Images with ControlNet HED Cognitive Actions

22 Apr 2025
Transform Your Images with ControlNet HED Cognitive Actions

In the evolving landscape of image manipulation, the jagilley/controlnet-hed API offers a powerful solution for developers looking to enhance their applications with advanced image processing capabilities. This API leverages ControlNet adapted Stable Diffusion, enabling users to modify images using HED (Holistically-Nested Edge Detection) maps. The pre-built Cognitive Actions provided streamline this process, allowing for sophisticated image recoloring and stylization tasks.

Prerequisites

Before diving into the Cognitive Actions, ensure you have the following prerequisites:

  • API Key: Sign up for access to the Cognitive Actions platform and obtain your API key.
  • Basic Setup: Familiarity with making HTTP requests and handling JSON data in your programming language of choice is helpful.

For authentication, you will typically pass your API key in the request headers as a Bearer token.

Cognitive Actions Overview

Modify Image with HED Maps

Description: This action allows developers to modify images by utilizing HED edge detection maps. It is particularly useful for image recoloring and stylization, retaining detailed structures from the input images.

Category: Image Processing

Input

The input for this action is structured as a JSON object containing several fields. Below is a detailed schema of the required and optional fields:

  • Required Fields:
    • inputImage: URI reference to the input image (e.g., https://example.com/image.png).
    • prompt: Text prompt guiding the model to generate the desired image.
  • Optional Fields:
    • seed: Random seed for reproducibility (integer).
    • scale: Guidance scale controlling fidelity to the prompt (number, default: 9, range: 0.1 to 30).
    • ddimSteps: Number of denoising diffusion implicit model steps (integer, default: 20).
    • negativePrompt: List of negative concepts to avoid in the prompt (string).
    • imageResolution: Resolution of the generated image (string, options: "256", "512", "768", default: "512").
    • numberOfSamples: Number of image samples to generate (string, options: "1", "4", default: "1").
    • additionalPrompt: Supplementary prompt to enhance image quality (string).
    • detectResolution: Resolution for model detection steps (integer, default: 512, range: 128 to 1024).

Example Input:

{
  "scale": 9,
  "prompt": "oil painting of handsome older gentleman, masterpiece",
  "ddimSteps": 20,
  "inputImage": "https://replicate.delivery/pbxt/IJBChpRrky1GqjRBPmtMjtYciqUNgefsCk9fHAJ8QrN9vYBY/old.png",
  "negativePrompt": "longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
  "imageResolution": "512",
  "numberOfSamples": "1",
  "additionalPrompt": "best quality, extremely detailed",
  "detectResolution": 512
}

Output

The output will typically return an array of URLs pointing to the generated modified images. For example:

[
  "https://assets.cognitiveactions.com/invocations/72b9cd52-e971-4ce2-9a08-2df9b628ea5a/74f01bc8-f57f-40bc-9783-b6c99126c52a.png",
  "https://assets.cognitiveactions.com/invocations/72b9cd52-e971-4ce2-9a08-2df9b628ea5a/5a100d2f-4206-420f-9736-4ade070ee0fb.png"
]

Conceptual Usage Example (Python)

Here's how you can invoke the Modify Image with HED Maps action using a conceptual Python snippet:

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 = "ff94ea7a-74bd-4290-a07c-09a4ec2d5c5b"  # Action ID for Modify Image with HED Maps

# Construct the input payload based on the action's requirements
payload = {
    "scale": 9,
    "prompt": "oil painting of handsome older gentleman, masterpiece",
    "ddimSteps": 20,
    "inputImage": "https://replicate.delivery/pbxt/IJBChpRrky1GqjRBPmtMjtYciqUNgefsCk9fHAJ8QrN9vYBY/old.png",
    "negativePrompt": "longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
    "imageResolution": "512",
    "numberOfSamples": "1",
    "additionalPrompt": "best quality, extremely detailed",
    "detectResolution": 512
}

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 code snippet:

  • Replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key.
  • The payload variable is structured according to the input schema, which allows you to specify the details for the image modification.
  • The endpoint URL and request structure are illustrative; ensure you adapt them as necessary for your use case.

Conclusion

The jagilley/controlnet-hed Cognitive Actions open up exciting possibilities for developers looking to integrate advanced image processing capabilities into their applications. With the ability to modify images using HED edge detection maps, you can create stunning visual content that enhances user engagement. Explore the capabilities of these actions and consider how they can fit into your next project!