Enhance Image Segmentation in Your Apps with SAM ViT-H Cognitive Actions

22 Apr 2025
Enhance Image Segmentation in Your Apps with SAM ViT-H Cognitive Actions

In the world of computer vision, image segmentation is a crucial task that allows applications to understand and interpret visual content. The peter65374/sam-vit API offers developers a powerful tool: the SAM (Segment Anything) ViT-H image encoder. This Cognitive Action provides an efficient way to process and segment images, enabling you to build applications that can analyze and manipulate visual data effectively.

Prerequisites

Before you start using the SAM ViT-H Cognitive Action, ensure you have the following:

  • An API key for the Cognitive Actions platform. This key will be used for authentication when making requests to the API.
  • Familiarity with JSON format, as the input and output will be structured in JSON.

Authentication is typically handled by passing your API key in the request headers, allowing you to securely access the Cognitive Actions capabilities.

Cognitive Actions Overview

Use SAM ViT-H Image Encoder

The Use SAM ViT-H Image Encoder action allows you to process and segment images using the advanced capabilities of the SAM ViT-H image encoder. This action falls under the category of image-segmentation.

Input

The input for this action requires the following field:

  • sourceImage (string, required): A URI pointing to the source image you want to process. It must be in a valid URI format.

Example Input:

{
  "sourceImage": "https://replicate.delivery/pbxt/JiJLYup30p9BZ87oEA4frk2zdceRq7ExURSV9OtVqFf8MBlp/ComfyUI_00007_test.png"
}

Output

Upon successful execution, the action returns a URI pointing to the processed segmentation output, which is typically in a NumPy format.

Example Output:

https://assets.cognitiveactions.com/invocations/a4e82c69-8e7c-4c31-83e9-1f5baebabd7d/7fbb4352-b833-4786-b646-f547d0325368.npy

Conceptual Usage Example (Python)

Here’s a conceptual example of how you might call the SAM ViT-H image encoder using Python. This code demonstrates how to construct and send the input payload to the Cognitive Actions API:

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 = "e94df0c0-8392-4ba7-bee5-ee9bd6b5fe58" # Action ID for Use SAM ViT-H Image Encoder

# Construct the input payload based on the action's requirements
payload = {
    "sourceImage": "https://replicate.delivery/pbxt/JiJLYup30p9BZ87oEA4frk2zdceRq7ExURSV9OtVqFf8MBlp/ComfyUI_00007_test.png"
}

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 action_id corresponds to the specific action you want to invoke, in this case, the SAM ViT-H image encoder.
  • The payload is structured to include the required sourceImage field.
  • The response will contain the output URI that you can use to access the segmentation result.

Conclusion

The SAM ViT-H Cognitive Action provides a straightforward way to integrate advanced image segmentation capabilities into your applications. By leveraging this action, developers can enhance their applications' visual processing abilities, enabling new use cases in areas like image analysis, automation, and content creation.

Explore the possibilities that the SAM ViT-H image encoder unlocks for your projects today!