Introduction
This article demonstrates how to use the Image Analysis 4.0 API to segment an image by removing its background. You will learn how to authenticate with the service and how to use both the REST API and the client SDK to accomplish this task.
Before starting you can test this feature of the service with a NO CODE experience in Azure AI | Vision Studio
๐๏ธBackground Removal in Vision Studio
Prerequisites
Before proceeding with this guide, please review my previous post where I explained this Azure AI Vision service Post Azure AI Vision and then make sure you have followed the steps outlined in the Quickstart page. This means:
- You have gained an understanding of this Azure AI service.
- You have obtained an API key and endpoint URL.
- If you're using the client SDK, you have the appropriate SDK package installed and a running quickstart application.
- If you're making direct 4.0 REST API calls, you have successfully made a
curl.exe
call to the service (or used an alternative tool).
Important: Background removal is only available in certain Azure regions such as East US, France Central, Korea Central, North Europe, Southeast Asia, West Europe, and West US.
Authentication
Python
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials
subscription_key = "YOUR_SUBSCRIPTION_KEY"
endpoint = "YOUR_ENDPOINT_URL"
client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
Csharp
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
string subscriptionKey = "YOUR_SUBSCRIPTION_KEY";
string endpoint = "YOUR_ENDPOINT_URL";
ComputerVisionClient client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(subscriptionKey))
{
Endpoint = endpoint
};
REST API Calls
curl -X POST "YOUR_ENDPOINT_URL/vision/v4.0/analyze?visualFeatures=BackgroundRemoval" </span>
-H "Ocp-Apim-Subscription-Key: YOUR_SUBSCRIPTION_KEY" </span>
-H "Content-Type: application/json" </span>
-d "{'url':'IMAGE_URL'}"
Using the Client SDK
Python
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
image_url = "YOUR_IMAGE_URL";
analysis = client.analyze_image_by_domain("BackgroundRemoval", image_url)
result = analysis.result
Csharp
string imageUrl = "YOUR_IMAGE_URL";
var features = new List<VisualFeatureTypes?>() { VisualFeatureTypes.BackgroundRemoval };
var result = await client.AnalyzeImageAsync(imageUrl, features);
// The 'result' object will contain the image with the background removed
Limitations
- Maximum image size: 4 MB
- Supported image formats: JPEG, PNG, GIF, BMP
- API call limit: Varies depending on the subscription plan
Demos
Console App in .NET
I share the repository on GitHub Here
Gradio Web App in Python
I share the repository on GitHub Here
Conclusion
Background removal is a powerful tool in image processing, especially useful in applications like e-commerce, photography, and more. With Azure's Image Analysis 4.0 API, performing this task is now easier and more accessible than ever.
I hope this explanation has been greatly helpful! Feel free to leave your comments and questions.
๐Until next time, community
Top comments (0)