Roboflow Inference Python SDK

1.2.2 · active · verified Thu Apr 16

The Roboflow Inference Python SDK provides a simple interface to deploy and interact with computer vision models from Roboflow. It enables users to perform tasks like object detection, classification, and segmentation locally or via the Roboflow API, abstracting away complex machine learning and deployment details. The current version is 1.2.2, with frequent patch and minor releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Inference HTTP client, set your API key (preferably via environment variable), and perform object detection inference on an image from a URL using a specified Roboflow model ID.

import os
from inference_sdk import InferenceHTTPClient

# Set your Roboflow API Key as an environment variable:
# export ROBOFLOW_API_KEY="YOUR_API_KEY"
api_key = os.environ.get('ROBOFLOW_API_KEY', '')

if not api_key:
    print("Warning: ROBOFLOW_API_KEY environment variable not set. Inference might fail.")

# Initialize the InferenceHTTPClient
# Use "https://detect.roboflow.com" for hosted inference
# Use "http://localhost:9001" if you're running a local inference server
client = InferenceHTTPClient(api_url="https://detect.roboflow.com", api_key=api_key)

# Specify your Roboflow model ID (e.g., "my-project/1")
# Replace with a real public model or your own private model ID.
model_id = "lego-brick-detector/1" # Example public model

# Define the image source (can be a URL, local path, or base64 string)
image_url = "https://media.roboflow.com/example_input.jpg"

print(f"Attempting inference on {image_url} using model {model_id}...")

try:
    # Perform inference
    result = client.infer(image_url, model_id=model_id)

    # Print results
    print("Inference successful!")
    print(f"Image dimensions: {result.image.width}x{result.image.height}")
    if result.predictions:
        print(f"Found {len(result.predictions)} predictions:")
        for pred in result.predictions:
            print(f"  Class: {pred.class_name}, Confidence: {pred.confidence:.2f}, "
                  f"Box: x={pred.x:.1f}, y={pred.y:.1f}, w={pred.width:.1f}, h={pred.height:.1f}")
    else:
        print("No predictions found.")

except Exception as e:
    print(f"An error occurred during inference: {e}")
    print("Please ensure your ROBOFLOW_API_KEY is correct and the model ID is valid.")

view raw JSON →