Hosted API (Remote Server)

Leverage your custom trained model for cloud-hosted inference.

Overview

Each model trained with Roboflow Train is deployed as a custom API you can use to make predictions from any device that has an internet connection. Inference is done on the server so you don't need to worry about the edge device's hardware capabilities.

We automatically scale this API up and down and do load balancing for you so that you can rest assured that your application will be able to handle sudden spikes in traffic without having to pay for GPU time you're not using. Our hosted prediction API has been battle-hardened to handle even the most demanding production applications (including concurrently surviving through the famous Hacker News and Reddit "hugs of death" without so much as batting an eye).

The Example Web App

The easiest way to familiarize yourself with the inference endpoint is to visit the Example Web App. To use the Web App, simply input your model , version and api_key. These will be pre-filled for you after training completes if you click through via the web UI under your versions "Training Results" section.

Then select an image via Choose File. After you have chosen the settings you want, click Run Inference.

On the left side of the screen, you will see example JavaScript code for posting a base64-encoded image to the inference endpoint. Within the form portion of the Web App, you can experiment with changing different API parameters when posting to the API.

Obtaining Your Model Endpoint

To use the inference API, you will need your model url slug, version number and the API key for the workspace the project belongs to. Your API key can be retrieved from in your workspace's settings page under the "Roboflow API" section. Your model url slug is the unique and url-safe version of your dataset name. The easiest way to retrieve it is via the web UI by clicking the "curl command" link:

post
Using the Inference API

https://detect.roboflow.com/:datasetSlug/:versionNumber
You can POST a base64 encoded image directly to your model endpoint. Or you can pass a URL as the image parameter in the query string if your image is already hosted elsewhere.
Request
Response
Request
Path Parameters
datasetSlug
optional
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
optional
number
The version number identifying the version of of your dataset
Query Parameters
image
optional
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
optional
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
optional
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
confidence
optional
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
optional
number
The width (in pixels) of the bounding box displayed around predictions (only has an effect when format is image). Default: 1
labels
optional
boolean
Whether or not to display text labels on the predictions (only has an effect when format is image). Default: false
format
optional
string
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
required
string
Your API key (obtained via your workspace API settings page)
Body Parameters
optional
string
A base64 encoded image. (Required when you don't pass an image URL in the query parameters).
Response
200: OK
JSON format predictions. (x,y) are the box's center pixel coordinates.
{
"predictions": [{
"x": 234.0,
"y": 363.5,
"width": 160,
"height": 197,
"class": "hand",
"confidence": 0.943
}, {
"x": 504.5,
"y": 363.0,
"width": 215,
"height": 172,
"class": "hand",
"confidence": 0.917
}, {
"x": 1112.5,
"y": 691.0,
"width": 139,
"height": 52,
"class": "hand",
"confidence": 0.87
}, {
"x": 78.5,
"y": 700.0,
"width": 139,
"height": 34,
"class": "hand",
"confidence": 0.404
}]
}
403: Forbidden
If your api_key is not authorized to access the model.
{
"Message": "User is not authorized to access this resource"
}

Code Snippets

For your convenience, we've provided code snippets for calling this endpoint in various programming languages. If you need help integrating the inference API into your project don't hesitate to reach out.

All examples upload to an example dataset with a model-endpoint of xx-your-model--1. You can easily find your dataset's identifier by looking at the curl command shown in the Roboflow web interface after your model has finished training.

cURL
Python
Javascript
iOS
Android
Ruby
PHP
Go
.NET
Elixir
cURL

Linux or MacOS

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

base64 YOUR_IMAGE.jpg | curl -d @- \
"https://detect.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://infer.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.

Python

Uploading a Local Image

To install dependencies, pip install requests pillow

import requests
import base64
import io
from PIL import Image
# Load Image with PIL
image = Image.open("YOUR_IMAGE.jpg").convert("RGB")
# Convert to JPEG Buffer
buffered = io.BytesIO()
image.save(buffered, quality=90, format="JPEG")
# Base 64 Encode
img_str = base64.b64encode(buffered.getvalue())
img_str = img_str.decode("ascii")
# Construct the URL
upload_url = "".join([
"https://detect.roboflow.com/your-model/42",
"?api_key=YOUR_KEY",
"&name=YOUR_IMAGE.jpg"
])
# POST to the API
r = requests.post(upload_url, data=img_str, headers={
"Content-Type": "application/x-www-form-urlencoded"
})
# Output result
print(r.json())

Inferring on an Image Hosted Elsewhere via URL

import requests
import urllib.parse
# Construct the URL
img_url = "https://i.imgur.com/PEEvqPN.png"
upload_url = "".join([
"detect.roboflow.com/your-model/42",
"?api_key=YOUR_KEY",
"&image=" + urllib.parse.quote_plus(img_url)
])
# POST to the API
r = requests.post(upload_url)
# Output result
print(r.json())
Javascript

Node.js

We're using axios to perform the POST request in this example so first run npm install axios to install the dependency.

Inferring on a Local Image

const axios = require("axios");
const fs = require("fs");
const image = fs.readFileSync("YOUR_IMAGE.jpg", {
encoding: "base64"
});
axios({
method: "POST",
url: "https://detect.roboflow.com/your-model/42",
params: {
api_key: "YOUR_KEY"
},
data: image,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(function(response) {
console.log(response.data);
})
.catch(function(error) {
console.log(error.message);
});

Inferring on an Image Hosted Elsewhere via URL

const axios = require("axios");
axios({
method: "POST",
url: "https://detect.roboflow.com/your-model/42",
params: {
api_key: "YOUR_KEY",
image: "https://i.imgur.com/PEEvqPN.png"
}
})
.then(function(response) {
console.log(response.data);
})
.catch(function(error) {
console.log(error.message);
});

Web

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

iOS

We are adding code snippets as they are requested by users. If you'd like to integrate the inference API into your iOS app, please click below to record your upvote.

Swift

Click here to request a Swift snippet.

Objective C

Click here to request an Objective-C snippet.

Android

We are adding code snippets as they are requested by users. If you'd like to integrate the inference API into your Android app, please click below to record your upvote.

Kotlin

Click here to request a Kotlin snippet.

Java

Click here to request a Java snippet.

Ruby

We are adding code snippets as they are requested by users. If you'd like to integrate the inference API into your Ruby app, please click here to record your upvote.

PHP

We are adding code snippets as they are requested by users. If you'd like to integrate the inference API into your PHP app, please click here to record your upvote.

Go

We are adding code snippets as they are requested by users. If you'd like to integrate the inference API into your Go app, please click here to record your upvote.

.NET

We are adding code snippets as they are requested by users. If you'd like to integrate the inference API into your .NET app, please click here to record your upvote.

Elixir

We are adding code snippets as they are requested by users. If you'd like to integrate the inference API into your Elixir app, please click here to record your upvote.

Video Inference

To get predictions from your model on a video, split it into frames, perform inference on each frame, then composite the predictions back into a rendered video.

We have an open source video inference utility script that performs these steps with ffmpeg and the Roboflow Inference API:

Using Your Webcam

You can also pipe frames from your webcam to your Roboflow model for predictions. We've created a tutorial with example code to show you how:

Drawing a Box from the Inference API JSON Output

Note: to receive and image with predicted box output - just pass "format=image" as a query parameter. This section shows an example if you want to double check and parse your JSON output.

Let's suppose you've created an inference url to return JSON predictions like so:

parts = []
url_base = 'https://infer.roboflow.com/'
endpoint = '[YOUR-MODEL]'
access_token = '?access_token=[YOUR_TOKEN]'
format = '&format=json'
confidence = '&confidence=10'
stroke='&stroke=5'
parts.append(url_base)
parts.append(endpoint)
parts.append(access_token)
parts.append(format)
parts.append(confidence)
parts.append(stroke)
url = ''.join(parts)

And you've drawn an inference from your API like so:

import glob
import requests
import base64
from base64 import decodebytes
import io
from PIL import Image
import time
import cv2
from io import BytesIO
f = '[YOUR-IMAGE].jpg'
image = Image.open(f)
buffered = io.BytesIO()
image = image.convert("RGB")
image.save(buffered, quality=90, format="JPEG")
img_str = base64.b64encode(buffered.getvalue())
img_str = img_str.decode("ascii")
headers = {'accept': 'application/json'}
start = time.time()
r = requests.post(url, data=img_str, headers=headers)
print('post took ' + str(time.time() - start))
# print(r)
print(r.json())
preds = r.json()
detections = preds['predictions']

Then you can draw the box on your image like so:

from PIL import Image, ImageDraw, ImageFont
f = './[YOUR-IMAGE].jpg'
image = Image.open(f)
draw = ImageDraw.Draw(image)
font = ImageFont.load_default()
for box in detections:
color = "#4892EA"
x1 = box['x'] - box['width'] / 2
x2 = box['x'] + box['width'] / 2
y1 = box['y'] - box['height'] / 2
y2 = box['y'] + box['height'] / 2
draw.rectangle([
x1, y1, x2, y2
], outline=color, width=5)
if True:
text = box['class']
text_size = font.getsize(text)
# set button size + 10px margins
button_size = (text_size[0]+20, text_size[1]+20)
button_img = Image.new('RGBA', button_size, color)
# put text on button with 10px margins
button_draw = ImageDraw.Draw(button_img)
button_draw.text((10, 10), text, font=font, fill=(255,255,255,255))
# put button on source image in position (0, 0)
image.paste(button_img, (int(x1), int(y1)))
image.show()