For the complete documentation index, see llms.txt. This page is also available as Markdown.

Object Detection

Run inference on your object detection models hosted on Roboflow.

There are several ways to run object detection inferences using the Hosted API (Legacy). You can use one of our different SDKs, or send a REST request to our hosted endpoint.

To install dependencies, pip install inference-sdk.

# import the inference-sdk
from inference_sdk import InferenceHTTPClient

CLIENT = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key="API_KEY"
)

result = CLIENT.infer(your_image.jpg, model_id="football-players-detection-3zvbc/12")

Linux or MacOS

Retrieving JSON predictions for a local file called YOUR_IMAGE.jpg:

base64 YOUR_IMAGE.jpg | curl -d @- \
"https://serverless.roboflow.com/your-model/42?api_key=YOUR_KEY"

Inferring on an image hosted elsewhere on the web via its URL (don't forget to URL encode it):

curl -X POST "https://serverless.roboflow.com/your-model/42?\
api_key=YOUR_KEY&\
image=https%3A%2F%2Fi.imgur.com%2FPEEvqPN.png"

Windows

You will need to install curl for Windows and GNU's base64 tool for Windows. The easiest way to do this is to use the git for Windows installer which also includes the curl and base64 command line tools when you select "Use Git and optional Unix tools from the Command Prompt" during installation.

Then you can use the same commands as above.

Node.js

These examples use the built-in fetch API. It works in Node.js 18 and later and in modern browsers, so there is no dependency to install.

Inferring on a Local Image

Inferring on an Image Hosted Elsewhere via URL

Web

We have realtime on-device inference available via roboflow.js; see the documentation here.

Swift

Inferring on a Local Image

Objective C

Click here to request an Objective-C snippet.

Kotlin

Inferring on a Local Image

Inferring on an Image Hosted Elsewhere via URL

Java

Inferring on a Local Image

Inferring on an Image Hosted Elsewhere via URL

Gemfile

Gemfile.lock

Inferring on a Local Image

Inferring on an Image Hosted Elsewhere via URL

Inferring on a Local Image

Inferring on an Image Hosted Elsewhere via URL

Inferring on a Local Image

Inferring on an Image Hosted Elsewhere via URL

Inferring on a Local Image

Inferring on an Image Hosted Elsewhere via URL

Try asking Lenny, our AI-powered chatbot, to create a code sample for you!

API Reference

URL

POST https://serverless.roboflow.com/:projectId/:versionNumber

Name
Type
Description

projectId

string

The url-safe version of the dataset name. You can find it in the web UI by looking at the URL on the main project view or by clicking the "Get curl command" button in the train results section of your dataset version after training your model.

version

number

The version number identifying the version of of your dataset

See how to get your project ID and version number here.

There are two ways you can send an image to the Hosted API (Legacy) via a REST request:

  • Attach a base64 encoded image to the POST request body

  • Send a URL of an image file using the image URL query

    • ex: https://serverless.roboflow.com/:datasetSlug/:versionNumber?image=https://imageurl.com

Query Parameters

Name
Type
Description

image

string

URL of the image to add. Use if your image is hosted elsewhere. (Required when you don't POST a base64 encoded image in the request body.) Note: don't forget to URL-encode it.

classes

string

Restrict the predictions to only those of certain classes. Provide as a comma-separated string. Example: dog,cat Default: not present (show all classes)

overlap

number

The maximum percentage (on a scale of 0-100) that bounding box predictions of the same class are allowed to overlap before being combined into a single box.

Default: 30

This parameter has no effect on RF-DETR models.

confidence

number

A threshold for the returned predictions on a scale of 0-100. A lower number will return more predictions. A higher number will return fewer high-certainty predictions.

Default: 40

stroke

number

The width (in pixels) of the bounding box displayed around predictions (only has an effect when format is image).

Default: 1

labels

boolean

Whether or not to display text labels on the predictions (only has an effect when format is image).

Default: false

format

string

Options:

  • json: returns an array of JSON predictions. (See response format tab).

  • image: returns an image with annotated predictions as a binary blob with a Content-Type of image/jpeg.

Default: json

api_key

string

Your API key (obtained via your workspace API settings page)

Request Body

Type
Description

string

A base64 encoded image. (Required when you don't pass an image URL in the query parameters).

The content type should be application/x-www-form-urlencoded with a string body.

Response Format

The hosted API inference endpoint, as well as most of our SDKs, return a JSON object containing an array of predictions. Each prediction has the following properties:

  • x = the horizontal center point of the detected object

  • y = the vertical center point of the detected object

  • width = the width of the bounding box

  • height = the height of the bounding box

  • class = the class label of the detected object

  • confidence = the model's confidence that the detected object has the correct label and position coordinates

Here is an example response object from the REST API:

The image attribute contains the height and width of the image sent for inference. You may need to use these values for bounding box calculations.

Drawing a Box from the Inference API JSON Output

Frameworks and packages for rendering bounding boxes can differ in positional formats. Given the response JSON object's properties, a bounding box can always be drawn using some combination of the following rules:

  • the center point will always be (x,y)

  • the corner points (x1, y1) and (x2, y2) can be found using:

    • x1 = x - (width/2)

    • y1 = y - (height/2)

    • x2 = x + (width/2)

    • y2 = y + (height/2)

The corner points approach is a common pattern and seen in libraries such as Pillow when building the box object to render bounding boxes within an Image.

Don't forget to iterate through all detections found when working with predictions!

Last updated

Was this helpful?