Segment and Remove Objects in Images with Cognitive Actions

22 Apr 2025
Segment and Remove Objects in Images with Cognitive Actions

In the realm of image processing, the ability to segment and manipulate images can unlock a myriad of possibilities for applications ranging from photo editing to object recognition. The wolverinn/segment-selected-object API offers developers a powerful toolset to achieve just that. By utilizing the Segment-Anything capability, this API allows you to identify, isolate, and even remove specific objects from images, providing a seamless experience for enhancing visual content.

Prerequisites

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

  • API Key: You will need a valid API key for accessing the Cognitive Actions platform.
  • Image URI: An accessible image URL is necessary for the action to process.

Authentication typically involves passing your API key in the request headers, allowing you to securely interact with the API.

Cognitive Actions Overview

Segment and Remove Objects in Image

This action empowers you to segment an object from an image and remove unwanted elements using the lama tool. By specifying coordinates, you can precisely target the area of interest in the image.

  • Category: Image Segmentation
  • Purpose: Identify and isolate a selected object with its mask and remove specific objects from the image.

Input

The input for this action requires the following fields:

  • image (string, required): The URI of the image to be processed.
  • coordinateX (integer, optional): The X-axis position on the image (default is 1).
  • coordinateY (integer, optional): The Y-axis position on the image (default is 1).

Example Input:

{
  "image": "https://replicate.delivery/pbxt/KZAJCRRaZMhmstuVMHAkTkIBlOUh88UOIrxaMT6StREcXbVA/1.jpeg",
  "coordinateX": 480,
  "coordinateY": 400
}

Output

Upon successful execution, the action returns several outputs:

  • selected_mask (array of strings): URLs to the masks of the segmented object.
  • selected_object (array of strings): URLs to the isolated object images.
  • remove_object_res (array of strings): URLs to the images with unwanted objects removed.

Example Output:

{
  "selected_mask": [
    "https://assets.cognitiveactions.com/invocations/3fcb4395-2fbc-4eb1-b8db-b6e25d2308c7/9265054e-6c01-4962-a523-ab20183d0b1d.png",
    "https://assets.cognitiveactions.com/invocations/3fcb4395-2fbc-4eb1-b8db-b6e25d2308c7/9cb1cbdb-0cf0-4970-8115-4a9c65a638bf.png",
    "https://assets.cognitiveactions.com/invocations/3fcb4395-2fbc-4eb1-b8db-b6e25d2308c7/c1f0c02c-0a79-4401-b478-0d7fce91c8c7.png"
  ],
  "selected_object": [
    "https://assets.cognitiveactions.com/invocations/3fcb4395-2fbc-4eb1-b8db-b6e25d2308c7/76debfe5-4821-46a6-8244-d3a636f06af9.png",
    "https://assets.cognitiveactions.com/invocations/3fcb4395-2fbc-4eb1-b8db-b6e25d2308c7/9e6e9910-ab82-432e-89b0-495bad8d6321.png",
    "https://assets.cognitiveactions.com/invocations/3fcb4395-2fbc-4eb1-b8db-b6e25d2308c7/78f855c4-7d54-4e7e-931a-ac4a5e5dbd30.png"
  ],
  "remove_object_res": [
    "https://assets.cognitiveactions.com/invocations/3fcb4395-2fbc-4eb1-b8db-b6e25d2308c7/9be81aca-33b6-4192-be0a-e52d53a9db72.png",
    "https://assets.cognitiveactions.com/invocations/3fcb4395-2fbc-4eb1-b8db-b6e25d2308c7/5db8e925-9e6d-4af4-94c7-69344c2c076f.png",
    "https://assets.cognitiveactions.com/invocations/3fcb4395-2fbc-4eb1-b8db-b6e25d2308c7/6d1b87ac-8759-4d37-b0aa-dbecfb5b499c.png"
  ]
}

Conceptual Usage Example (Python)

Here's how you could implement a call to the Cognitive Actions execution endpoint in 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 = "acfce19a-0d9d-4d37-9b4f-5c20a900e655" # Action ID for Segment and Remove Objects in Image

# Construct the input payload based on the action's requirements
payload = {
    "image": "https://replicate.delivery/pbxt/KZAJCRRaZMhmstuVMHAkTkIBlOUh88UOIrxaMT6StREcXbVA/1.jpeg",
    "coordinateX": 480,
    "coordinateY": 400
}

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 snippet, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The payload is structured according to the requirements of the action, and the endpoint URL is hypothetical but illustrates how to interact with the API.

Conclusion

The Segment and Remove Objects in Image action provides a robust solution for developers looking to enhance their applications with advanced image processing capabilities. By leveraging this Cognitive Action, you can automate the segmentation and removal of objects in images, opening doors to innovative use cases in various domains. Consider exploring further how you can integrate this functionality to elevate your projects!