Image Classification API
For 1,000 images, you pay $0.25 with
fast, $0.50 withbalanced, $0.75 withprecise, or $0.40 withmatch(all prices).
No training required. Change labels in any HTTP API request.
classify |
match |
|
|---|---|---|
| Question it answers | Which of these labels fits the image best? | How well does each query fit the image, alone? |
| Text field | label, 2 to 256 per request |
query, 1 to 256 per request |
model |
fast, balanced or precise |
Do not send |
| Scores | Sum to 1 for each image | Independent, do not sum to 1 |
| Operation, per image | classifier.fast, classifier.balanced or classifier.precise |
classifier.match |
Classify
curl https://api.nullgate.club/classifier/classify \
-H "Authorization: Bearer $NULLGATE_API_KEY" \
-F model=balanced \
-F "label=a photo of a cat" \
-F "label=a photo of a dog" \
-F "label=a photo of a bird" \
-F [email protected]package main
import (
"context"
"fmt"
"log"
"os"
"github.com/nullgate-club/nullgate-sdk-go/v2"
"github.com/nullgate-club/nullgate-sdk-go/v2/api"
"github.com/nullgate-club/nullgate-sdk-go/v2/classifier"
)
func main() {
client, err := nullgate.New("", api.Options{})
if err != nil {
log.Fatal(err)
}
defer client.Close()
image, err := os.ReadFile("photo.jpg")
if err != nil {
log.Fatal(err)
}
result, err := client.Classifier.Classify(context.Background(), classifier.ClassifyOptions{
Images: [][]byte{image},
Labels: []string{"a photo of a cat", "a photo of a dog", "a photo of a bird"},
Model: classifier.Balanced,
})
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Results[0].Label, result.Results[0].Scores)
}{
"model": "balanced",
"results": [
{
"label": "a photo of a cat",
"scores": [0.97, 0.02, 0.01]
}
]
}Match
curl https://api.nullgate.club/classifier/match \
-H "Authorization: Bearer $NULLGATE_API_KEY" \
-F "query=a photo of a cat" \
-F "query=a photo of a dog" \
-F [email protected]package main
import (
"context"
"fmt"
"log"
"os"
"github.com/nullgate-club/nullgate-sdk-go/v2"
"github.com/nullgate-club/nullgate-sdk-go/v2/api"
"github.com/nullgate-club/nullgate-sdk-go/v2/classifier"
)
func main() {
client, err := nullgate.New("", api.Options{})
if err != nil {
log.Fatal(err)
}
defer client.Close()
image, err := os.ReadFile("photo.jpg")
if err != nil {
log.Fatal(err)
}
result, err := client.Classifier.Match(context.Background(), classifier.MatchOptions{
Images: [][]byte{image},
Queries: []string{"a photo of a cat", "a photo of a dog"},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Results[0].Scores)
}- Repeat
imageandqueryfor each value. - Send 1 to 256 unique, nonempty queries of up to 256 characters each. See image and body limits.
scoresfollow query order. The model scores the whole image and does not locate objects.
Example scores. In a probe of 39 public-domain photos, true queries scored 0.0214 to 0.8330. Three false queries per photo scored 0.0000000030 to 0.0021. Your scores may differ.
Classify request
Send multipart/form-data. Repeat image and label for each value. Other field names or content types, including JSON, return invalid_request.
| Field | Meaning |
|---|---|
model |
Optional text field. The tier: fast, balanced or precise. Defaults to balanced |
image |
File part with the bytes of one image file. The format is detected from the bytes. The file name and the part content type are ignored |
label |
Text field with one label in UTF-8, in English. Every image is compared with every label |
Do not gzip the request.
Classify result
| Field | Meaning |
|---|---|
model |
The tier that classified the images |
results |
One result per image, in the order of the image parts |
results[].label |
The label with the highest probability for this image |
results[].scores |
One probability per label, in the order of the label fields. They sum to 1 for each image |
Tiers
model |
Speed | Accuracy |
|---|---|---|
fast |
Fastest | Good for clearly different classes |
balanced |
Medium | Better on similar classes. The default |
precise |
Slowest | Best on similar classes and small details |
Limits
| Input | Limit |
|---|---|
| Images | 1 to 32 per request |
| Image format | JPEG, PNG or WebP |
| Image size | 4 MiB each |
| Image area | JPEG: 28 megapixels. PNG: 40 megapixels. WebP: 16 megapixels |
| Image shape | The longer side is at most 32 times the shorter side |
| Labels | 2 to 256 per request, all different, 1 to 256 characters each |
| Request body | 48 MiB, including multipart encoding |
Outside these limits, the API returns invalid_request. message names the field.
The API ignores EXIF orientation. Rotate photos before sending them.
Overload. If capacity stays full, the API returns HTTP 503 unavailable with Retry-After: 1. No charge. Wait one second before retrying.
Billing
Both endpoints charge per image at the listed rate. 10 images cost 10 operations. Label and query counts do not affect price.
Your balance must cover all images. Failed requests are free, even if only one image is invalid. See Prices.
Common mistakes
| You do | What happens | Fix |
|---|---|---|
Write vague labels, such as cat or ok |
The scores are close to each other and the best label is often wrong | Write each label as a short description of the image: a photo of a cat, a screenshot of a login form |
| Send one label | The request fails with invalid_request |
Send at least two labels. For a yes/no question, send both answers: a photo of a cat, a photo without a cat |
| Read each score as an independent answer | The scores sum to 1 across the labels you sent, so a wrong label still gets a high score when no label fits | Add a label for the other cases, such as a photo of something else, and compare the scores with each other |