api-inference documentation

Object detection

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Object detection

Object Detection models allow users to identify objects of certain defined classes. These models receive an image as input and output the images with bounding boxes and labels on detected objects.

For more details about the object-detection task, check out its dedicated page! You will find examples and related materials.

Recommended models

Explore all available models and find the one that suits you best here.

Using the API

Python
JavaScript
cURL
import requests

API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50"
headers = {"Authorization": "Bearer hf_***"}

def query(filename):
    with open(filename, "rb") as f:
        data = f.read()
    response = requests.post(API_URL, headers=headers, data=data)
    return response.json()

output = query("cats.jpg")

To use the Python client, see huggingface_hub’s package reference.

API specification

Request

Payload
inputs* string The input image data as a base64-encoded string. If no parameters are provided, you can also provide the image data as a raw bytes payload.
parameters object Additional inference parameters for Object Detection
        threshold number The probability necessary to make a prediction.

Some options can be configured by passing headers to the Inference API. Here are the available headers:

Headers
authorization string Authentication header in the form 'Bearer: hf_****' when hf_**** is a personal user access token with Inference API permission. You can generate one from your settings page.
x-use-cache boolean, default to true There is a cache layer on the inference API to speed up requests we have already seen. Most models can use those results as they are deterministic (meaning the outputs will be the same anyway). However, if you use a nondeterministic model, you can set this parameter to prevent the caching mechanism from being used, resulting in a real new query. Read more about caching here.
x-wait-for-model boolean, default to false If the model is not ready, wait for it instead of receiving 503. It limits the number of requests required to get your inference done. It is advised to only set this flag to true after receiving a 503 error, as it will limit hanging in your application to known places. Read more about model availability here.

For more information about Inference API headers, check out the parameters guide.

Response

Body
(array) object[] Output is an array of objects.
        label string The predicted label for the bounding box.
        score number The associated score / probability.
        box object
                xmin integer The x-coordinate of the top-left corner of the bounding box.
                xmax integer The x-coordinate of the bottom-right corner of the bounding box.
                ymin integer The y-coordinate of the top-left corner of the bounding box.
                ymax integer The y-coordinate of the bottom-right corner of the bounding box.
< > Update on GitHub