kodokan.recognizer#

Technique recognition for applications: top-k guesses with inspectable scores (#3).

kodokan.recognize holds the research tools (features, LOO evaluation, metrics). This module is the application facade over them:

Scores are a softmax over negative distances to each technique’s centroid, in the train-standardized (and, when scikit-learn is installed, LDA-projected) descriptor space, with distances in units of the typical within-technique spread (measured leave-one-out, as a held-out demo would see it). They rank techniques and sum to 1; they are not calibrated probabilities. The LDA projection is still fit on every training demo, so with few demos per technique the spread stays optimistic.

>>> rng = np.random.default_rng(0)
>>> X = np.vstack([rng.normal(c, 0.3, size=(5, 4)) for c in (0.0, 3.0, 6.0)])
>>> y = np.repeat(["seoi-nage", "o-goshi", "uchi-mata"], 5)
>>> rec = TechniqueRecognizer(method="centroid").fit(X, y)
>>> r = rec.recognize(np.full(4, 3.1), k=2)
>>> r.guesses[0].technique, len(r.guesses), round(sum(r.scores.values()), 6)
('o-goshi', 2, 1.0)

Module Attributes

Featurizer

(pose_seq, start_s, end_s) -> fixed-length descriptor of one throw window.

DFLT_MIN_PEAK_ACTIVITY

Smoothed motion energy (keypoint speed, px per analysed frame) a throw must reach; below it a "segment" is pose jitter, which quantile thresholds alone would accept.

Functions

loo_top_k_accuracy(X, y, *[, ks, groups, method])

Leave-one-out (or leave-one-group-out) top-k accuracy of TechniqueRecognizer.

motion_segments(pose_seq, *[, min_peak_activity])

Throw windows by motion energy (kodokan.segment.find_segments()), keeping only those whose peak energy reaches min_peak_activity (an absolute floor: the hysteresis thresholds are quantiles, so an idle buffer still has "segments").

pooled_demo_descriptor(pose_seq, start_s, ...)

The default Featurizer: per-frame demo features, temporal-pyramid pooled.

recognize_segments(pose_seq, recognizer, *)

Offline video mode: every detected throw in pose_seq, recognized.

Classes

Guess(technique, score)

One candidate technique and its score (higher is more likely).

LiveRecognizer(recognizer, *, fps[, k, ...])

Live mode: each throw a ThrowDetector reports, recognized once.

Recognition(guesses[, scores])

The top-k guesses, best first, and every technique's score.

TechniqueRecognizer(*[, method, temperature])

Fit on pooled descriptors; recognize with top-k guesses and the full score vector.

ThrowDetector(*, fps[, width, height, ...])

A rolling pose buffer that reports each throw once, after its segment closes.

kodokan.recognizer.DFLT_MIN_PEAK_ACTIVITY = 3.0#

Smoothed motion energy (keypoint speed, px per analysed frame) a throw must reach; below it a “segment” is pose jitter, which quantile thresholds alone would accept.

kodokan.recognizer.Featurizer#

(pose_seq, start_s, end_s) -> fixed-length descriptor of one throw window.

alias of Callable[[PoseSequence, float, float], ndarray]

class kodokan.recognizer.Guess(technique, score)[source]#

Bases: object

One candidate technique and its score (higher is more likely).

class kodokan.recognizer.LiveRecognizer(recognizer, *, fps, k=3, featurize=<function pooled_demo_descriptor>, **detector_kwargs)[source]#

Bases: object

Live mode: each throw a ThrowDetector reports, recognized once.

push() takes one frame ((P, 17, 3) keypoints) and returns (segment, recognition) for the throws that closed with it. Detector options (buffer_s, close_after_s, segmenter, width, height) pass through.

push(keypoints, frame_index=None)[source]#

Add one frame; return the throws that closed with it, recognized.

Return type:

list[tuple[Segment, Recognition]]

class kodokan.recognizer.Recognition(guesses, scores=dict())[source]#

Bases: object

The top-k guesses, best first, and every technique’s score.

score_of(technique)[source]#

The score of technique (0 for a technique the recognizer never saw).

Return type:

float

class kodokan.recognizer.TechniqueRecognizer(*, method='lda', temperature=1.0)[source]#

Bases: object

Fit on pooled descriptors; recognize with top-k guesses and the full score vector.

Parameters:
  • method (str) – "lda" (LDA projection when scikit-learn is installed, else plain standardized space) or "centroid" (standardized space only).

  • temperature (float) – softmax temperature; higher spreads the scores out.

fit(X, y)[source]#

Fit on pooled descriptors X (one row per demo) and technique labels y.

Return type:

TechniqueRecognizer

fit_demos(feature_seqs, labels, *, levels=(1, 2, 4))[source]#

Fit on per-frame feature sequences (e.g. kodokan.recognize.demo_feature()).

Return type:

TechniqueRecognizer

recognize(x, *, k=3)[source]#

Top-k guesses (best first) and the full score vector for one descriptor.

Return type:

Recognition

recognize_clip(pose_seq, start_s, end_s, *, k=3, featurize=<function pooled_demo_descriptor>)[source]#

Recognize the throw in pose_seq between start_s and end_s.

Return type:

Recognition

scores(x)[source]#

Every technique’s score for one pooled descriptor (they sum to 1).

Return type:

dict[str, float]

class kodokan.recognizer.ThrowDetector(*, fps, width=0, height=0, buffer_s=20.0, close_after_s=1.0, segmenter=<function motion_segments>)[source]#

Bases: object

A rolling pose buffer that reports each throw once, after its segment closes.

Push one frame at a time ((P, 17, 3) keypoints, as in PoseSequence). push() returns (buffer, segment) for the throws that closed with that frame. A segment counts as closed when it ends at least close_after_s before the newest frame; the buffer keeps the last buffer_s seconds of pushed frames (at fps frames a second: push every analysed frame, or pass the analysed rate as fps). A segment that mostly overlaps a throw already reported is that throw.

buffer()[source]#

The buffered frames as a PoseSequence (people padded with NaN).

Return type:

PoseSequence

push(keypoints, frame_index=None)[source]#

Add one frame; return (buffer, segment) for each throw it closed.

Return type:

list[tuple[PoseSequence, Segment]]

kodokan.recognizer.loo_top_k_accuracy(X, y, *, ks=(1, 3, 5), groups=None, method='lda')[source]#

Leave-one-out (or leave-one-group-out) top-k accuracy of TechniqueRecognizer.

The share of held-out demos whose true technique is among the recognizer’s k best guesses, for each k in ks: the number the top-k app (#3) shows, next to top-1 from kodokan.recognize.loo_pooled_predict(). Pass groups (e.g. the source clip of each demo) to hold out whole groups. A held-out demo whose technique is absent from its training fold counts as a miss.

>>> rng = np.random.default_rng(0)
>>> X = np.vstack([rng.normal(c, 1.0, size=(4, 3)) for c in (0.0, 1.0, 2.0)])
>>> y = np.repeat(["a", "b", "c"], 4)
>>> acc = loo_top_k_accuracy(X, y, ks=(1, 3), method="centroid")
>>> acc[3]
1.0
Return type:

dict[int, float]

kodokan.recognizer.motion_segments(pose_seq, *, min_peak_activity=3.0, **segment_kwargs)[source]#

Throw windows by motion energy (kodokan.segment.find_segments()), keeping only those whose peak energy reaches min_peak_activity (an absolute floor: the hysteresis thresholds are quantiles, so an idle buffer still has “segments”).

Return type:

list[Segment]

kodokan.recognizer.pooled_demo_descriptor(pose_seq, start_s, end_s, *, mode='tori_angles_pos', levels=(1, 2, 4))[source]#

The default Featurizer: per-frame demo features, temporal-pyramid pooled.

Return type:

ndarray

kodokan.recognizer.recognize_segments(pose_seq, recognizer, *, k=3, featurize=<function pooled_demo_descriptor>, segmenter=<function motion_segments>)[source]#

Offline video mode: every detected throw in pose_seq, recognized.

Return type:

list[tuple[Segment, Recognition]]