Roboflow Inference CLI

1.2.2 · active · verified Mon Apr 13

Roboflow Inference CLI is a command-line interface designed for deploying computer vision models to various devices and environments with minimal machine learning or deployment knowledge. It provides tools to run and manage a local inference server, process data with workflows, benchmark performance, make predictions, and deploy to the cloud. The library is currently at version 1.2.2 and sees active development with frequent releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to perform inference programmatically using the `inference_sdk.InferenceHTTPClient`. It assumes a local inference server is running (started via `inference server start` in the terminal, which requires Docker) or uses the Roboflow hosted API. It takes an image URL and a model ID, then prints the inference results. Ensure your `ROBOFLOW_API_KEY` is set as an environment variable.

import os
from inference_sdk import InferenceHTTPClient

# Ensure you have your Roboflow API key set as an environment variable or replace os.environ.get with your key.
# You can find your API key on the Roboflow dashboard.
ROBOFLOW_API_KEY = os.environ.get('ROBOFLOW_API_KEY', '')

if not ROBOFLOW_API_KEY:
    print("Warning: ROBOFLOW_API_KEY environment variable not set. Inference may fail.")
    # For a quick demo without a real key, you might use a dummy value
    # or skip this part if you are only running a local server without Roboflow API interaction.
    # For proper usage, always use a real key.

# 1. Start a local inference server (requires Docker to be running):
#    Run in your terminal: inference server start
#    This will typically start on http://localhost:9001

# 2. Initialize the InferenceHTTPClient
client = InferenceHTTPClient(
    api_url="http://localhost:9001",  # Or "https://serverless.roboflow.com" for hosted API
    api_key=ROBOFLOW_API_KEY,
)

# Example image URL for inference
image_url = "https://media.roboflow.com/inference/soccer.jpg"

# Replace with your actual model_id (e.g., 'your-project-name/your-model-version')
# You can find this on your Roboflow model's deploy tab.
model_id = "soccer-players-5fuqs/1"

# 3. Perform inference
try:
    print(f"Running inference on {image_url} with model {model_id}...")
    results = client.infer(image_url, model_id=model_id)
    print("Inference successful!")
    # Print first few predictions for brevity
    if results and 'predictions' in results and len(results['predictions']) > 0:
        print("First 3 predictions:")
        for i, pred in enumerate(results['predictions'][:3]):
            print(f"  - Class: {pred.get('class')}, Confidence: {pred.get('confidence'):.2f}")
    else:
        print("No predictions found or unexpected result format.")
except Exception as e:
    print(f"An error occurred during inference: {e}")
    print("Ensure the local inference server is running ('inference server start') and the model ID/API key are correct.")

view raw JSON →