Skylark Edge
Skylark Edge

Python API

Every class and method in the skylark_edge package — downloading and loading models, running them on images and video frames, choosing the hardware, and the result format.

from skylark_edge import Edge, SkylarkEdgeError

The package has two classes: Edge, this machine's access to your organization's models, and Model, one model loaded and ready to run.

Edge

edge = Edge(license_key=None, *, home=None)
ArgumentDefault
license_keySKYLARK_LICENSE_KEY, then the activation fileYour organization's license key.
homeSKYLARK_HOME, then your user data directoryWhere models and the license state are kept.

Creating an Edge doesn't contact Skylark. Methods that do are marked online below; every other method works without a connection.

The default home is ~/.local/share/skylark-edge on Linux, ~/Library/Application Support/ai.Skylark-Labs.skylark-edge on macOS and %APPDATA%\Skylark Labs\skylark-edge\data on Windows. Set home when several users or services on one machine should share downloads.

edge.models()

Online. The models your license can download and Skylark Edge runs, and the version of each already on this machine.

>>> edge.models()
[{"name": "cctv_detection", "version": "2.1.0", "size": 82662400, "downloaded": "2.1.0"},
 {"name": "drone_detection", "version": "1.4.0", "size": 104857600, "downloaded": None}]

It also renews the license check, so calling it restarts the 30 offline days.

edge.download(name, progress=None)

Online. Downloads a model, or updates it to the latest version, and returns {"name", "version", "size"}. If the latest version is already here it returns at once.

edge.download("cctv_detection", progress=lambda done, total: print(done, total))

progress(done, total) is called as the file arrives; total is None if the size isn't known in advance. An interrupted download resumes where it stopped. After an update, the previous version is deleted.

edge.load(name, *, provider="auto", threads=None)

Decrypts a downloaded model in memory and returns a Model. Works offline for up to 30 days after the last license check; when online, it renews the check once a day.

Argument
providerWhich hardware runs the model. See Choosing the hardware.
threadsHow many CPU threads the model may use. Default: ONNX Runtime's choice, usually one per core.

edge.status()

What is on this machine and how long it may run offline. Never contacts Skylark.

>>> edge.status()
{"home": "/home/ops/.local/share/skylark-edge", "hasLicenseKey": True,
 "verifiedAt": 1761552000, "offlineUntil": 1764144000,
 "models": [{"name": "cctv_detection", "version": "2.1.0", "size": 82662428}]}

verifiedAt and offlineUntil are Unix times in seconds; 0 means the license has never been checked on this machine.

edge.purge()

Deletes every downloaded model and the saved license state from this machine.

Model

model = edge.load("cctv_detection")
Attribute
model.nameThe model's name.
model.classesIts class names, indexed by classId.
model.input_sizeThe network's input, (width, height).
model.confidenceThe model's own threshold. Nothing below it is returned.

model.predict(source, *, bgr=None)

Runs the model on one image and returns its results. source can be:

source
A path"street.jpg" or a pathlib.Path. JPEG, PNG, BMP or WebP.
BytesThe contents of an image file, as read from disk or received over the network.
A PIL imageAny mode; it is converted to RGB.
A NumPy arrayheight × width × 3, uint8. Taken as BGR, the order OpenCV uses. Pass bgr=False for an RGB array.

JPEG orientation tags are applied, so a photo comes out the right way up, as it would in an image viewer.

A Model can be shared between threads. Calls on one model run one at a time; to run images in parallel, load the model once per thread, which uses that much more memory.

Results

{
  "model": "cctv_detection",
  "image": { "width": 1920, "height": 1080 },
  "detections": [
    {
      "class": "person",
      "classId": 0,
      "confidence": 0.91,
      "bboxPx": [812.4, 301.0, 948.7, 702.2],
      "bbox": [0.4231, 0.2787, 0.4941, 0.6502]
    }
  ],
  "counts": { "person": 1 },
  "timing": { "decodeMs": 9.8, "inferMs": 212.5 }
}
Field
detectionsEvery detection of every class, at or above the model's threshold, with duplicate boxes of the same object removed.
class, classIdThe class name, and its index in model.classes.
confidence0 to 1.
bboxPx[x1, y1, x2, y2], top-left and bottom-right corners, in the pixels of the image you passed.
bboxThe same box divided by the image's width and height, so 0 to 1.
countsDetections per class.
timingMilliseconds spent reading the image and running the model.

These are the same fields as a detection from the Skylark SDK, with two differences. Edge's bboxPx is always in your image's own pixels, and Edge adds nothing to the detector's output: there is no plate, no flags and no crowd or congestion block. Filter on class and confidence for what you need:

people = [d for d in result["detections"] if d["class"] == "person" and d["confidence"] >= 0.5]

Choosing the hardware

provider decides where a model runs:

providerRuns onNeeds
"auto"An NVIDIA GPU if CUDA is available, else a DirectML GPU on Windows, else the CPUNothing; the default
"cpu"The CPUNothing
"cuda"An NVIDIA GPUonnxruntime-gpu and CUDA 12; Linux or Windows
"directml"Any DirectX 12 GPUonnxruntime-directml; Windows
"coreml"Apple's Neural Engine and GPUmacOS. Not every model runs on it; if one fails to load, use "cpu"

With "auto", a GPU that is missing or fails to start falls back to the CPU. Naming a provider instead makes load() fail if that provider can't run, so you know which hardware you got. "auto" never chooses CoreML.

Video and cameras

predict() takes one image at a time, so a video is a loop over its frames. With OpenCV, frames are already BGR arrays:

import cv2
from skylark_edge import Edge

model = Edge().load("cctv_detection")
cap = cv2.VideoCapture("rtsp://192.168.1.20/stream1")   # or a file, or 0 for a webcam

while True:
    ok, frame = cap.read()
    if not ok:
        break
    result = model.predict(frame)
    for d in result["detections"]:
        x1, y1, x2, y2 = map(int, d["bboxPx"])
        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
        cv2.putText(frame, f'{d["class"]} {d["confidence"]:.2f}', (x1, y1 - 6),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
    cv2.imshow("skylark-edge", frame)
    if cv2.waitKey(1) == 27:   # Esc
        break

If the model is slower than the camera, frames queue up and the picture falls behind. Run the model on every few frames instead (if n % 5 == 0), or read in one thread and predict in another, always on the newest frame.

Errors

Everything the package raises is a SkylarkEdgeError. Switch on its code, never on the message: the codes are stable, the wording is not.

try:
    model = edge.load("cctv_detection")
except SkylarkEdgeError as e:
    if e.code == "not_downloaded":
        edge.download("cctv_detection")
        model = edge.load("cctv_detection")
    else:
        raise

Every code and what to do about it is in Errors.