Classification

Run inference on classification models hosted on Roboflow.

Infer on Local and Hosted Images

To install dependencies, pip install roboflow

from roboflow import Roboflow
rf = Roboflow(api_key="API_KEY")
project = rf.workspace().project("MODEL_ENDPOINT")
model = project.version(VERSION).model

# infer on a local image
print(model.predict("your_image.jpg").json())

# visualize your prediction
# model.predict("your_image.jpg").save("prediction.jpg")

# infer on an image hosted elsewhere
# print(model.predict("URL_OF_YOUR_IMAGE", hosted=True).json())

Response Object Formats

Single-Label Classification

The hosted API inference route returns a JSON object containing an array of predictions. Each prediction has the following properties:

  • time = total time, in seconds, to process the image and return predictions

  • image = an object that holds information about the image width and height

    • width the height of the predicted image

    • height = the height of the predicted image

  • predictions = collection of all predicted classes and their associated confidence values for the prediction

    • class = the label of the classification

    • confidence = the model's confidence that the image contains objects of the detected classification

  • top = highest confidence predicted class

  • confidence = highest predicted confidence score

  • image_path = path of the predicted image

  • prediction_type = the model type used to perform inference, ClassificationModel in this case

// an example JSON object
{
  "time": 0.19064618100037478,
  "image": {
    "width": 210,
    "height": 113
  },
  "predictions": [
    {
      "class": "real-image",
      "confidence": 0.7149
    },
    {
      "class": "illustration",
      "confidence": 0.2851
    }
  ],
  "top": "real-image",
  "confidence": 0.7149,
  "image_path": "/cropped-images-1.jpg",
  "prediction_type": "ClassificationModel"
}

API Reference

Using the Inference API

POST https://classify.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.

Path Parameters

Query Parameters

{
   "predictions":{
      "bird":{
         "confidence":0.5282308459281921
      },
      "cat":{
         "confidence":0.5069406032562256
      },
      "dog":{
         "confidence":0.49514248967170715
      }
   },
   "predicted_classes":[
      "bird",
      "cat"
   ]
}

Last updated