API Reference
This page provides detailed information about the Locus Python API.
Core Interface
The primary entry point is the Detector class. It can be initialized with various configuration parameters passed as keyword arguments.
import locus
# Initialize with keyword arguments
detector = locus.Detector(
families=[locus.TagFamily.AprilTag36h11, locus.TagFamily.ArUco4x4_50],
decimation=2,
threads=4,
)
batch = detector.detect(img)
locus.Detector
High-level detector.
Construction
Detector(profile="standard") — load a shipped JSON profile by name.
Detector(config=my_cfg) — use a pre-built :class:DetectorConfig.
Detector() — equivalent to profile="standard".
Per-call orchestration options (decimation, threads, families,
max_concurrent_frames) stay outside the profile because they describe
how the detector is invoked, not what it looks for.
threads sets the Rayon worker count. 0 or None (the default)
uses the global Rayon pool (RAYON_NUM_THREADS / core count); n > 0
builds one scoped n-thread pool at construction and runs every
detect / detect_concurrent call inside it, bounding this detector's
CPU footprint. Detection output is identical for every value.
max_concurrent_frames sizes the pool of frame contexts that
:meth:detect_concurrent leases (default 1); frames beyond it get a
temporary context each.
Source code in crates/locus-py/locus/__init__.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
Functions
__init__
__init__(profile: ProfileName | None = None, config: DetectorConfig | None = None, *, decimation: int | None = None, threads: int | None = None, families: list[TagFamily] | None = None, max_concurrent_frames: int | None = None) -> None
Source code in crates/locus-py/locus/__init__.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
detect
detect(img: ndarray, intrinsics: CameraIntrinsics | None = None, tag_size: float | None = None, debug_telemetry: bool = False, **kwargs) -> DetectionBatch
Detect tags in the image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input grayscale image (np.uint8). |
required |
intrinsics
|
CameraIntrinsics | None
|
Optional CameraIntrinsics for 3D pose estimation. |
None
|
tag_size
|
float | None
|
Optional physical tag size (meters). |
None
|
Returns:
| Type | Description |
|---|---|
DetectionBatch
|
A vectorized DetectionBatch object. |
Source code in crates/locus-py/locus/__init__.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
Concurrent Detection
Detector supports concurrent multi-frame processing via detect_concurrent. The internal pool size is set with max_concurrent_frames, either as a Detector keyword (locus.Detector(max_concurrent_frames=8)) or through the DetectorBuilder.
See the Concurrent Detection how-to for full usage examples.
DetectorBuilder
A fluent builder for Detector. It carries orchestration only — the configuration, tag families, decimation, threads and pool size. Detection settings live in DetectorConfig: edit one and pass it to with_config. build() re-validates the configuration through the Pydantic model and returns a locus.Detector.
import locus
cfg = locus.DetectorConfig.from_profile("standard")
cfg.decoder.min_contrast = 12.0
detector = (
locus.DetectorBuilder()
.with_config(cfg)
.with_family(locus.TagFamily.AprilTag36h11)
.with_threads(4)
.with_max_concurrent_frames(8) # Enable parallel batch processing
.build()
)
| Method | Description |
|---|---|
with_profile(name) |
Use a shipped profile ("standard", "grid", "high_accuracy"). |
with_config(config) |
Use a DetectorConfig (default: the standard profile). |
with_family(family) |
Add a tag family to detect (default: AprilTag36h11). |
with_decimation(n) |
Spatial decimation factor (default 1). |
with_threads(n) |
Rayon worker count for the detector (see Thread control). 0 (default) = the global Rayon pool. |
with_max_concurrent_frames(n) |
Pool size for detect_concurrent (default 1 = sequential). |
build() |
Validate the configuration and build the Detector. |
detect_concurrent(frames, *, intrinsics=None, tag_size=None) -> list[DetectionResult]
Detect tags in multiple frames concurrently using Rayon. Releases the GIL for the entire parallel section. Pool contexts are managed internally. Rejected-corner data and telemetry are not available via this method.
| Parameter | Type | Description |
|---|---|---|
frames |
list[np.ndarray] |
List of (H, W) uint8 grayscale frames. |
intrinsics |
CameraIntrinsics \| None |
Camera intrinsics for 3D pose estimation. |
tag_size |
float \| None |
Physical tag side length in metres. |
Thread control
threads (Detector(threads=n) / DetectorBuilder.with_threads(n)) sets the
number of Rayon workers the detector uses.
0— the default — leaves the pipeline on Rayon's global pool, sized byRAYON_NUM_THREADSor the machine's core count. No extra threads are spawned.n > 0builds one scoped Rayon pool of exactlynthreads at detector construction, and runsdetectanddetect_concurrentinside it. The detector's CPU footprint is then bounded bynregardless of what the rest of the process does with the global pool, which is what you want when several detectors (or a detector and an unrelated Rayon workload) share a machine. Fordetect_concurrentthe bound covers the frame-level fan-out and the intra-frame parallelism, so the two cannot over-subscribe each other.
The pool is built once per detector, never per frame. Detection output is
identical for every value of threads: each parallel stage writes to
disjoint, index-addressed chunks, so only latency and CPU usage change.
Fixed in 0.7.x
Before this release threads was accepted, stored and reported by
Detector.config() but never read — the pipeline always ran on the global
pool. Code that relied on threads=1 to keep the detector single-threaded
was in fact running on all cores; it now gets one thread and will be
correspondingly slower. Pass threads=0 (or omit it) for the previous
behaviour.
Configuration
Locus uses Pydantic for robust configuration validation.
locus.DetectorConfig
Bases: BaseModel
Nested detector configuration.
The three shipped profiles live in crates/locus-core/profiles/*.json
and are embedded into the Rust crate at compile time; the wheel reads
the exact same bytes through the FFI. Those JSON profiles are
authoritative. Load via :meth:from_profile for a shipped profile or
:meth:from_profile_json for a user-supplied JSON string.
Source code in crates/locus-py/locus/_config.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
Functions
from_profile
classmethod
from_profile(name: ProfileName) -> DetectorConfig
Load a shipped profile by name.
General-purpose profiles must stay on QuadExtractionPolicy::Static.
Adaptive routing lives in explicit opt-in profiles
(currently only high_accuracy).
Source code in crates/locus-py/locus/_config.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
from_profile_json
classmethod
from_profile_json(json_str: str) -> DetectorConfig
Load a user-supplied profile from a JSON string.
Source code in crates/locus-py/locus/_config.py
398 399 400 401 | |
Data Models
These classes represent the output and internal state of the detection pipeline.
locus.DetectionBatch
dataclass
Vectorized detection results.
This dataclass contains parallel NumPy arrays representing a batch of detections.
Source code in crates/locus-py/locus/__init__.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
Attributes
centers
property
centers: ndarray
Compute centers from corners: (N, 2)
locus.Pose
Geometry
locus.CameraIntrinsics
Camera intrinsic parameters with optional lens distortion.
Construct with just (fx, fy, cx, cy) for a rectified camera, or
pass distortion_model and dist_coeffs for a distorted camera:
# Pinhole (no distortion)
k = CameraIntrinsics(fx=800.0, fy=800.0, cx=400.0, cy=300.0)
# Brown-Conrady
k = CameraIntrinsics(
fx=800.0, fy=800.0, cx=400.0, cy=300.0,
distortion_model=DistortionModel.BrownConrady,
dist_coeffs=[-0.3, 0.1, 0.001, -0.002, 0.0],
)
# Kannala-Brandt fisheye
k = CameraIntrinsics(
fx=380.0, fy=380.0, cx=320.0, cy=240.0,
distortion_model=DistortionModel.KannalaBrandt,
dist_coeffs=[0.1, -0.01, 0.001, 0.0],
)
Attributes
dist_coeffs
property
writable
dist_coeffs: list[float]
Distortion coefficients corresponding to the chosen model.
BrownConrady:[k1, k2, p1, p2, k3](5 values)KannalaBrandt:[k1, k2, k3, k4](4 values)Pinhole: ignored (may be empty)
distortion_model
property
writable
distortion_model: DistortionModel
The distortion model to use. Defaults to [DistortionModel::Pinhole].
Board Pose Estimation
Locus supports board-level 6-DOF pose estimation for AprilGrid and ChAruco boards.
All board types enforce dictionary bounds at construction time — a ValueError is raised
if the board requires more marker IDs than the target TagFamily provides.
For the underlying algorithm, see Board-Level Pose Estimation.
AprilGrid
locus.AprilGrid(
rows: int,
cols: int,
spacing: float, # gap between markers (metres)
marker_length: float, # physical marker side length (metres)
family: TagFamily,
)
Immutable topology for an AprilTag grid board. spacing = square_length - marker_length.
Raises ValueError if rows * cols > family.max_id_count().
| Attribute | Type | Description |
|---|---|---|
rows |
int |
Number of tag rows. |
cols |
int |
Number of tag columns. |
CharucoBoard
locus.CharucoBoard(
rows: int,
cols: int,
square_length: float, # physical checkerboard square side (metres)
marker_length: float, # physical ArUco marker side (metres)
family: TagFamily,
)
Immutable topology for a ChAruco board. Markers occupy squares where (row + col) is even.
Saddle points are at the outer corners of each square ((rows-1)*(cols-1) total).
Raises ValueError if the required marker count exceeds family.max_id_count().
| Attribute | Type | Description |
|---|---|---|
rows |
int |
Number of square rows. |
cols |
int |
Number of square columns. |
BoardEstimator
Unified stateful estimator for both AprilGrid and ChAruco boards. All scratch buffers
are pre-allocated at construction; estimate() performs zero heap allocations.
Construction:
# AprilGrid board
estimator = locus.BoardEstimator(board: AprilGrid)
# ChAruco board
estimator = locus.BoardEstimator.from_charuco(board: CharucoBoard)
Estimation:
result: BoardEstimateResult = estimator.estimate(
detector: locus.Detector,
img: np.ndarray, # (H, W) uint8 grayscale
intrinsics: CameraIntrinsics,
)
BoardEstimateResult attributes:
| Attribute | Shape / Type | Description |
|---|---|---|
ids |
(N,) int32 |
Decoded tag IDs visible in this frame. |
corners |
(N, 4, 2) float32 |
Refined tag corner image coordinates. |
board_pose |
(7,) float64 or None |
[tx, ty, tz, qw, qx, qy, qz] (Hamilton, scalar first) in camera frame — None if insufficient observations. Pose origin is the board's geometric centre. |
board_cov |
(6, 6) float64 or None |
Body-frame pose covariance in \(\mathfrak{se}(3)\) tangent space, rotation first: [ω, v]. |
Enumerations
locus.TagFamily
Bases: IntEnum
Fiducial marker dictionary to decode.
Attributes
AprilTag16h5
class-attribute
instance-attribute
AprilTag16h5 = ...
AprilTag 16h5 (30 codes, 4x4 bits, minimum Hamming distance 5).
AprilTag36h11
class-attribute
instance-attribute
AprilTag36h11 = ...
AprilTag 36h11 (587 codes, 6x6 bits, minimum Hamming distance 11).
ArUco4x4_100
class-attribute
instance-attribute
ArUco4x4_100 = ...
ArUco DICT_4X4_100.
ArUco4x4_50
class-attribute
instance-attribute
ArUco4x4_50 = ...
ArUco DICT_4X4_50.
ArUco6x6_250
class-attribute
instance-attribute
ArUco6x6_250 = ...
ArUco DICT_6X6_250.
ArUcoMip36h12
class-attribute
instance-attribute
ArUcoMip36h12 = ...
ArUco MIP 36h12 (250 codes, 6x6 bits; DICT_ARUCO_MIP_36h12).
locus.CornerRefinementMode
Bases: IntEnum
Sub-pixel corner refinement applied during quad extraction.
Attributes
Erf
class-attribute
instance-attribute
Erf = ...
Fit each edge with a PSF-blurred step (ERF) model and intersect the edge lines.
Incompatible with EdLines extraction.
None_
class-attribute
instance-attribute
None_ = ...
Keep the extractor's corners (EdLines' Gauss-Newton sub-pixel corners, or
ContourRdp's contour vertices). Required with AdaptivePpb routing.
locus.SegmentationConnectivity
Bases: IntEnum
Pixel connectivity of the dark regions that segmentation groups into candidates.
Attributes
Eight
class-attribute
instance-attribute
Eight = ...
8-connectivity: pixels also connect diagonally. Joins borders thinner than a pixel on the diagonal, but fuses diagonally touching squares.
Four
class-attribute
instance-attribute
Four = ...
4-connectivity: pixels connect horizontally and vertically only. Keeps dark squares that touch at a corner (checkerboards, AprilGrid connectors) apart.