Azure AI Vision - Remove backgrounds from images
Tags: azure, ai, csharp, python
Published: 2023-08-26 23:34:19



![Cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3cytluu4x7ldlftc26l.png)

## 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] (https://portal.vision.cognitive.azure.com/)

🎞️[Background Removal in Vision Studio](https://assetspost.blob.core.windows.net/gif/AI-Studio-BackgroundRemoval.gif)

## Prerequisites

Before proceeding with this guide, please review my previous post where I explained this Azure AI Vision service [Post Azure AI Vision](https://dev.to/ppiova/azure-cognitive-for-vision-large-foundation-model-45ad) and then make sure you have followed the steps outlined in the [Quickstart page](https://bit.ly/QuickstartImageAnalysis40). 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

```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

```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

```bash
curl -X POST "YOUR_ENDPOINT_URL/vision/v4.0/analyze?visualFeatures=BackgroundRemoval" \
     -H "Ocp-Apim-Subscription-Key: YOUR_SUBSCRIPTION_KEY" \
     -H "Content-Type: application/json" \
     -d "{'url':'IMAGE_URL'}"
```

## Using the Client SDK

### Python

```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

```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](https://github.com/ppiova/Azure-ComputerVision-BackgroundRemoval)

### Gradio Web App in Python
I share the repository on GitHub [Here](https://github.com/ppiova/AIVision-ImageRetrieval)

## 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