Enhance Your Portraits with Cognitive Actions: Relighting and Background Integration

22 Apr 2025
Enhance Your Portraits with Cognitive Actions: Relighting and Background Integration

In the world of image processing, the ability to seamlessly integrate subjects with backgrounds while maintaining realistic lighting is a game-changer. The IC-Light Background Cognitive Actions allow developers to enhance portraits by relighting them with selected backgrounds, leveraging advanced models like Stable Diffusion and BriaRMBG. This guide will walk you through the capabilities of the Relight Portraits with Backgrounds action, helping you effectively integrate it into your applications.

Prerequisites

To get started with the Cognitive Actions, you will need:

  • An API key for the Cognitive Actions platform to authenticate your requests.
  • Ensure your application can make HTTP requests and handle JSON payloads effectively.

Authentication typically involves passing the API key in the request headers, which is crucial for secure access to the action functionalities.

Cognitive Actions Overview

Relight Portraits with Backgrounds

Purpose:
This action enhances portraits by integrating them with selected background images, ensuring cohesive lighting and generating normal maps for added depth and realism.

Category:
Image Enhancement

Input

The input schema for this action requires several fields, each contributing to the final output. Below is an overview of the required and optional fields:

  • Required Fields:
    • backgroundImage: (string, URI) The backdrop for the subject image.
    • prompt: (string) A descriptive text guiding the relighting process.
    • subjectImage: (string, URI) The primary image undergoing enhancements.
  • Optional Fields:
    • seed: (integer) A fixed random seed for reproducibility (optional).
    • steps: (integer) Number of diffusion steps (default: 25).
    • width: (integer) Image width in pixels (default: 512).
    • height: (integer) Image height in pixels (default: 640).
    • lightSource: (string) Specifies the lighting setup (default: "Use Background Image").
    • outputFormat: (string) Defines the output file format (default: "webp").
    • guidanceScale: (number) A scale for classifier-free guidance (default: 2).
    • outputQuality: (integer) Compression quality of the image (default: 80).
    • numberOfImages: (integer) Number of unique images to generate (default: 1).
    • additionalPrompt: (string) Supplementary prompt to enhance image quality (default: "best quality").
    • computeNormalMaps: (boolean) Whether to compute normal maps (default: false).
    • highResolutionScale: (number) Scaling factor for resolution enhancement (default: 1.5).
    • avoidAttributesPrompt: (string) Descriptions of undesirable attributes to exclude (default: "lowres, bad anatomy, bad hands, cropped, worst quality").
    • highResolutionDenoiseAmount: (number) Denoising adjustment for high-resolution outputs (default: 0.5).

Example Input:

{
  "steps": 25,
  "width": 512,
  "height": 832,
  "prompt": "Woman, detailed face, sci-fi RGB glowing, cyberpunk",
  "lightSource": "Use Background Image",
  "outputFormat": "webp",
  "subjectImage": "https://replicate.delivery/pbxt/KtCKrs9sxPF3HciwoWL0TTVM9Nde7ySDWpO9S2flTiyi9Pp3/i3.png",
  "guidanceScale": 2,
  "outputQuality": 80,
  "numberOfImages": 1,
  "backgroundImage": "https://replicate.delivery/pbxt/KxPIbJUjSmVBlvn0M3C8PAz6brN5Z0eyZSGcKIVw3XfJ6vNV/7.webp",
  "additionalPrompt": "best quality",
  "computeNormalMaps": false,
  "highResolutionScale": 1.5,
  "avoidAttributesPrompt": "lowres, bad anatomy, bad hands, cropped, worst quality",
  "highResolutionDenoiseAmount": 0.5
}

Output

The output of this action typically consists of a list of URLs pointing to the generated images.

Example Output:

[
  "https://assets.cognitiveactions.com/invocations/2cdd8a69-705e-4349-9bf1-08a823a2a69e/20557ef9-7e8a-45db-9ae8-93bcff2cce5d.webp"
]

Conceptual Usage Example (Python)

Here’s how you can call the Relight Portraits with Backgrounds action using Python:

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 = "379c5b60-384f-45bb-96b7-4cd16fbb5aa8"  # Action ID for Relight Portraits with Backgrounds

# Construct the input payload based on the action's requirements
payload = {
    "steps": 25,
    "width": 512,
    "height": 832,
    "prompt": "Woman, detailed face, sci-fi RGB glowing, cyberpunk",
    "lightSource": "Use Background Image",
    "outputFormat": "webp",
    "subjectImage": "https://replicate.delivery/pbxt/KtCKrs9sxPF3HciwoWL0TTVM9Nde7ySDWpO9S2flTiyi9Pp3/i3.png",
    "guidanceScale": 2,
    "outputQuality": 80,
    "numberOfImages": 1,
    "backgroundImage": "https://replicate.delivery/pbxt/KxPIbJUjSmVBlvn0M3C8PAz6brN5Z0eyZSGcKIVw3XfJ6vNV/7.webp",
    "additionalPrompt": "best quality",
    "computeNormalMaps": false,
    "highResolutionScale": 1.5,
    "avoidAttributesPrompt": "lowres, bad anatomy, bad hands, cropped, worst quality",
    "highResolutionDenoiseAmount": 0.5
}

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 constructed based on the action's input schema. This example demonstrates how to send a request to the Cognitive Actions service, handle the response, and print the results.

Conclusion

The Relight Portraits with Backgrounds action provides a powerful way to enhance portrait images by integrating them with backgrounds while ensuring realistic lighting. By leveraging this Cognitive Action, developers can easily improve their applications’ image processing capabilities. Whether you're looking to create stunning visuals for marketing materials or enhance personal projects, this tool is a valuable addition to your toolkit. Explore the possibilities and let your creativity shine!