Image Segmentation
Learn to perform image segmentation using the Hugging Face Inference API.
We'll cover the following
Image segmentation is used to partition an image into different segments (sets of pixels) corresponding to a distinct object. It's usually used for detailed information about the image segments rather than object detection. It finds its use in several applications, especially in biomedical imaging. It can be divided into the following three types:
Semantic: This refers to pixel-level segmentation.
Instance: This is similar to semantic segmentation and is also done on the pixel-level. Here, however, each instance is treated as a distinct segment. For example, instance segmentation of an image of three cats results in all three cats being treated as separate instances.
Panoptic: This is a new type of segmentation and is a hybrid of both semantic and instance segmentation.
Hugging Face provides us with models for all of three aforementioned types.
Image segmentation using the API
The facebook/detr-resnet-50-panoptic
model is recommended for image segmentation task. However, there are many models available. Some panoptic segmentation models are below:
Models for Panoptic Segmentation
Model | Description |
| Based on |
| Based on |
We can call the following endpoint via the POST request method for the image segmentation by replacing the path parameter {model}
with any model mentioned above:
https://api-inference.huggingface.co/models/{model}
Request parameters
This endpoint only takes binary representation of an image file.
The code below does the segmentation of a cat image. We can take input directly from the URL.
// Endpoint URLconst endpointUrl = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50-panoptic";const headerParameters = {"Authorization": "Bearer {{ACCESS_TOKEN}}"};async function classifyImage(imgUrl) {try {// Reading image from URLconst imgResponse = await fetch(imgUrl);const buffer = await imgResponse.buffer();const options = {method: "POST",headers: headerParameters,body: buffer};// Calling endpoint URLconst response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}// URL of the imagelet imgUrl = "https://images.unsplash.com/photo-1604675223954-b1aabd668078";classifyImage(imgUrl);
We specify the endpoint URL with the facebook/detr-resnet-50-panoptic
model at line 2.
Response fields
This API call returns a dictionary object containing possible labels ordered by the likelihood scores and masks of the objects in the image.
Parameter | Type | Description |
| Float | Specifies the likelihood score of the label |
| String | Specifies the predicted label of the image |
| String | Specifies the base64 string code of the object |
We can convert the base64 string to an image, as shown below:
// base64 str of educative logolet mask = "iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAABGdBTUEAALGPC/x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cTXLcJSMgewgeQDcKcYIfaWnLsOxKBvx2wM4TvbuEZgSf2/QJ7pw3CX41/ED4VZgR+XyMSIf3/BeNi1QRIVJB5AAAAAElFTkSuQmCC";let img = "data:image/png;base64," + mask;console.log(`<img src=${img} width="50px" height="50px" />`);
//Example#1let imgUrl = "https://images.unsplash.com/photo-1599889959407-598566c6e1f1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=500&q=40"//Example#2let imgUrl = "https://images.unsplash.com/photo-1643251935745-4209d215f221?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1200&q=40"
Try out the images above by replacing the imgUrl
at line 29 in the widget below, and observe the results.
// Endpoint URLconst endpointUrl = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50-panoptic";const headerParameters = {"Authorization": "Bearer {{ACCESS_TOKEN}}"};async function classifyImage(imgUrl) {try {// Reading image from URLconst imgResponse = await fetch(imgUrl);const buffer = await imgResponse.buffer();const options = {method: "POST",headers: headerParameters,body: buffer};// Calling endpoint URLconst response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}// URL of the imagelet imgUrl = "https://images.unsplash.com/photo-1599889959407-598566c6e1f1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=500&q=40";// Rendering html to show imageconsole.log(`Original image:<br><img src=${imgUrl} width="400px" height="500px">`);classifyImage(imgUrl);