Zum Inhalt springen

Ultralytics HUB Inference API

Mit der Ultralytics HUB Inference API kannst du Inferenzen über unsere REST API durchführen, ohne die Ultralytics YOLO Umgebung lokal installieren und einrichten zu müssen.

Ultralytics HUB-Screenshot der Registerkarte "Deploy" auf der Seite "Modell" mit einem Pfeil, der auf die Karte " Ultralytics Inference API" zeigt


Pass auf: Ultralytics HUB Inference API Komplettlösung

Python

Um mit Python auf die Ultralytics HUB Inference API zuzugreifen, verwende den folgenden Code:

import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())

Hinweis

Ersetze MODEL_ID mit der gewünschten Modell-ID, API_KEY mit deinem aktuellen API-Schlüssel, und path/to/image.jpg mit dem Pfad zu dem Bild, für das du die Inferenz durchführen möchtest.

cURL

Um mit cURL auf die Ultralytics HUB Inference API zuzugreifen, verwende den folgenden Code:

curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"

Hinweis

Ersetze MODEL_ID mit der gewünschten Modell-ID, API_KEY mit deinem aktuellen API-Schlüssel, und path/to/image.jpg mit dem Pfad zu dem Bild, für das du die Inferenz durchführen möchtest.

Argumente

In der folgenden Tabelle findest du eine vollständige Liste der verfügbaren Schlussfolgerungsargumente.

Argument Standard Typ Beschreibung
image image Bilddatei, die für die Inferenz verwendet werden soll.
url str URL des Bildes, wenn keine Datei übergeben wird.
size 640 int Größe des Eingabebildes, gültiger Bereich ist 32 - 1280 bildpunkte.
confidence 0.25 float Konfidenzschwelle für Vorhersagen, gültiger Bereich 0.01 - 1.0.
iou 0.45 float Schwellenwert für Schnittmenge über Union (IoU), gültiger Bereich 0.0 - 0.95.

Antwort

Die Ultralytics HUB Inference API gibt eine JSON-Antwort zurück.

Klassifizierung

Klassifizierungsmodell

from ultralytics import YOLO

# Load model
model = YOLO("yolov8n-cls.pt")

# Run inference
results = model("image.jpg")

# Print image.jpg results in JSON format
print(results[0].tojson())
curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"
import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  success: true,
  message: "Inference complete.",
  data: [
    {
      class: 0,
      name: "person",
      confidence: 0.92
    }
  ]
}

Erkennung

Erkennungsmodell

from ultralytics import YOLO

# Load model
model = YOLO("yolov8n.pt")

# Run inference
results = model("image.jpg")

# Print image.jpg results in JSON format
print(results[0].tojson())
curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"
import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  success: true,
  message: "Inference complete.",
  data: [
    {
      class: 0,
      name: "person",
      confidence: 0.92,
      width: 0.4893378019332886,
      height: 0.7437513470649719,
      xcenter: 0.4434437155723572,
      ycenter: 0.5198975801467896
    }
  ]
}

OBB

OBB-Modell

from ultralytics import YOLO

# Load model
model = YOLO("yolov8n-obb.pt")

# Run inference
results = model("image.jpg")

# Print image.jpg results in JSON format
print(results[0].tojson())
curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"
import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  success: true,
  message: "Inference complete.",
  data: [
    {
      class: 0,
      name: "person",
      confidence: 0.92,
      obb: [
        0.669310450553894,
        0.6247171759605408,
        0.9847468137741089,
        ...
      ]
    }
  ]
}

Segmentierung

Segmentierungsmodell

from ultralytics import YOLO

# Load model
model = YOLO("yolov8n-seg.pt")

# Run inference
results = model("image.jpg")

# Print image.jpg results in JSON format
print(results[0].tojson())
curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"
import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  success: true,
  message: "Inference complete.",
  data: [
    {
      class: 0,
      name: "person",
      confidence: 0.92,
      segment: [0.44140625, 0.15625, 0.439453125, ...]
    }
  ]
}

Pose

Pose Modell

from ultralytics import YOLO

# Load model
model = YOLO("yolov8n-pose.pt")

# Run inference
results = model("image.jpg")

# Print image.jpg results in JSON format
print(results[0].tojson())
curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
    -H "x-api-key: API_KEY" \
    -F "image=@/path/to/image.jpg" \
    -F "size=640" \
    -F "confidence=0.25" \
    -F "iou=0.45"
import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())
{
  success: true,
  message: "Inference complete.",
  data: [
    {
      class: 0,
      name: "person",
      confidence: 0.92,
      keypoints: [
        0.5290805697441101,
        0.20698919892311096,
        1.0,
        0.5263055562973022,
        0.19584226608276367,
        1.0,
        0.5094948410987854,
        0.19120082259178162,
        1.0,
        ...
      ]
    }
  ]
}


Erstellt 2024-01-23, Aktualisiert 2024-06-22
Autoren: glenn-jocher (9), sergiuwaxmann (2), RizwanMunawar (1), priytosh-tripathi (1)

Kommentare