Skip to content
imaged

Image classification

Run an image model and read its highest-scoring classes.

Use your own .slab model and a runtime license. Model files carry their input sizes, preprocessing, labels, and backend preferences.

classify.py

Python setup →

Run Python inside a signed macOS app with a customer runtime build, or use a development build.

"""Classify an image and print the five most likely classes.

usage: python classify.py MODEL.slab IMAGE LICENSE_FILE
"""

import sys

import slab

model, image, license_file = sys.argv[1:4]

cfg = slab.Config(cache_dir="/tmp/slab_cache", license=open(license_file).read())
session = slab.Session(cfg, model, warmup=True)

out = session.run(slab.Input(frame=slab.Frame.open(image)))
print(f"{out.backend_used.name}, {out.latency_ms:.2f} ms")
for c in out.top_k(5):
    print(f"  {c.label or c.class_id}: {c.score:.4f}")