Python API¶
spintrack.track runs what spintrack run does and returns the records instead of writing files:
import spintrack
track = spintrack.track("examples/sample/config.toml") # the run, without the files
df = track.to_polars() # columns by name: forward_total, heading, ...
print(track.quality.n_dropped, df.select("forward_total", "heading").tail())
For a live camera, feed frames from your own capture code to a Tracker. The config must already describe the ball, the field of view and the camera position, so track a recorded clip of the rig first and use the config.toml the run writes.
from spintrack import Config, Tracker
cfg = Config.load("config.toml")
tracker = Tracker(cfg, width, height)
for gray, ts_ms in frames(): # your camera SDK: 2-D uint8 images, timestamps in ms
res = tracker.process_frame(gray, ts_ms)
if res is not None: # None: the frame could not be tracked
print(res.forward, res.side, res.turn) # rad this frame; side, turn: left +
Reference¶
Track the recording a config describes and return its records.
config is a config file, a Config or None (the defaults); src overrides its
video. The run is that of spintrack run, with the same refusals and the same
geometry found in the recording, minus the files.
frames opens the frames to track instead of the video, for a caller that decodes
them itself: it is called once, twice with two_pass, and the records count its
frames from 0. Nothing is found in the recording then, so the config must give the
ball, the field of view and the camera position.
Source code in python/spintrack/pipeline.py
What track returns: the records, as in tracks.parquet, and quality.
Source code in python/spintrack/pipeline.py
to_polars ¶
Bases: _Table
A rig and, through video, a recording of it.
Source code in python/spintrack/config.py
load
classmethod
¶
Read a .toml config, resolving its relative paths against its folder.
Source code in python/spintrack/config.py
save ¶
Write the config to path as TOML, under comments (lines from #).
Only keys that differ from the defaults are written unless full. Paths are
written relative to the file, unless the two share no folder but the root.
Source code in python/spintrack/config.py
Source code in python/spintrack/tracker.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 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 | |
ball_columns ¶
BALL_COLUMNS for every frame so far, as an (n, 4) array indexed by frame.
The ball's path is the follower's (CenterWatch.ball_path), smoothed over all
of its looks, so it is only final once the run ends. The offset is the window
circle's distance from it in ball radii, which is about the rotation, in rad,
that the misplacement adds to the frame's orientation. All NaN, and the ball
never seen, without a follower.
Source code in python/spintrack/tracker.py
prime_from ¶
Start from another tracker's map and illumination field.
Both trackers must have been built from the same config, so their initial
windows agree and the map needs no correction: the map lives in the window frame
at R = I, and a window re-fit in other carried it unchanged (see
TrackEngine.rebuild). The illumination field is fixed in the window, so it
comes from wherever other ended up and is resampled into this one.
When other watched the ball, this tracker's window follows the trajectory
other measured, planned from all of its looks at once (CenterWatch.replay),
instead of following the ball for itself.
Source code in python/spintrack/tracker.py
process_frame ¶
process_frame(gray: ndarray, ts_ms: float = -1.0, wall_ms: float | None = None) -> FrameResult | None
Track one grayscale frame (2-D uint8); None if the frame was dropped.
Source code in python/spintrack/tracker.py
refit_center ¶
Point the tracking window at a new ball center, keeping the surface map.
Returns the rotation Q from the old window frame to the new one, which the
caller must apply to any orientation it recorded in the old frame. The ignore
polygons are image-fixed (the animal has not moved), so they are not translated.
circle_px is the new center's pixel_circle center, when already known.
Source code in python/spintrack/tracker.py
save_map ¶
Write the current surface map as a spintrack .npz template.
Source code in python/spintrack/tracker.py
One tracked frame. Distances are in ball radii (radians of ball rotation).
The animal-frame properties follow spintrack.path: lab x forward, y left, z up, so
sideways motion and turning are positive to the left.
Source code in python/spintrack/tracker.py
heading
property
¶
Integrated heading in rad, [0, 2 pi), from the first tracked frame's.
The tracking options a config key, a CLI flag or a caller sets.