How to fix a jittery AI model without retraining
A real-time AI model that is right 95% of the time still misfires several times a second. Four simple checks, with code, that fix it without retraining.
A classifier that is right most of the time can still be unusable. In a real-time system it makes a prediction every frame, and a model that is 95 percent accurate per frame is wrong several times a second. Users don’t experience accuracy. They experience the flicker, the false trigger, the action that fires twice.
The usual reflex is to improve the model: more data, more training, a bigger network. Sometimes that is right. Often the faster fix is a small deterministic layer between the model and the user, which decides when a prediction is allowed to become an action. I used this pattern to make Corporate Signal Translator, a browser app that recognises hand gestures, usable without retraining anything.
This piece explains the pattern step by step, with code you can adapt.
The shape of the problem
Every frame, the model returns a probability for each class. Three things go wrong.
- Low-confidence guesses. Between deliberate gestures, the hand is in transition and the model still has to pick something.
- Flicker. During a held gesture, one or two frames out of thirty come back as a neighbouring class.
- Repeats. A held gesture is a stream of identical predictions, and you want one action, not thirty.
Each failure has a cheap, explainable fix, and they compose in a fixed order.
Step 1: a confidence floor
Treat anything below a threshold as “no gesture”. This removes most transition frames.
const CONFIDENCE_FLOOR = 0.6;
function topLabel(probs: Record<string, number>): string | null {
let best: string | null = null;
let bestP = 0;
for (const [label, p] of Object.entries(probs)) {
if (p > bestP) { best = label; bestP = p; }
}
return bestP >= CONFIDENCE_FLOOR ? best : null;
}
Set the floor by looking at real predictions, not by intuition. Too high and deliberate gestures get dropped; too low and transitions leak through.
Step 2: overrides for known confusions
Some errors are systematic. In my case, a fist with the thumb slightly visible was read as a thumbs up. A geometric rule caught it: for a real thumbs up, the thumb tip is much further from the wrist than any other fingertip.
function overrideThumbsUp(
label: string,
otherTips: number[], // distance of each other fingertip from the wrist
thumbTip: number, // distance of the thumb tip from the wrist
): string {
if (label !== 'THUMBS_UP') return label;
const others = Math.max(...otherTips);
return thumbTip > 1.3 * others ? label : 'CLOSED_FIST';
}
An override like this is only worth writing for a confusion you have actually observed, and it should be specific to one pair of classes. If you find yourself writing dozens, the model needs work after all.
Step 3: a unanimous window
Keep the last N labels and accept a gesture only when all of them agree. Unanimity is stricter than a majority vote, which is the point: one stray frame resets the count instead of being outvoted.
const WINDOW = 8;
const recent: (string | null)[] = [];
function stable(label: string | null): string | null {
recent.push(label);
if (recent.length > WINDOW) recent.shift();
if (recent.length < WINDOW || label === null) return null;
return recent.every((l) => l === label) ? label : null;
}
The cost is latency. At 30 frames per second, eight frames is about a quarter of a second before anything happens. That was acceptable for gestures. For something like a game controller it would not be, and a shorter window with a higher confidence floor would be the better trade.
Step 4: an intent lock and deduplication
Once a gesture fires, ignore everything for a short period, and never fire the same gesture twice in a row.
const LOCK_MS = 2500;
let lockedUntil = 0;
let current: string | null = null;
function accept(label: string | null, now: number): string | null {
if (now < lockedUntil || label === null || label === current) return null;
current = label;
lockedUntil = now + LOCK_MS;
recent.length = 0; // start the next window clean
return label;
}
Finally, reset everything when the input disappears. In a gesture app that means when the hand leaves the frame, which also gives users a natural way to repeat a gesture on purpose.
Putting it together
The order matters. Cheap rejections come first, so the expensive checks run less often.
- Confidence floorDrop low-confidence frames
- LockIgnore input right after an action
- OverridesFix known confusions with explicit rules
- Unanimous windowRequire N agreeing frames
- DeduplicateNever repeat the current action
Testing it
The decision layer is ordinary code, which means it can be tested without a camera or a model. Feed it synthetic sequences of labels and timestamps and assert on what comes out: a single stray frame must not fire, eight agreeing frames must fire once, a second gesture inside the lock must be ignored. My project has 45 tests of this kind.
One caveat from my own code: the tests run against a JavaScript copy of the logic rather than importing the production TypeScript module, which means the two can drift apart. Import the real thing.
When not to do this
A decision layer hides errors from the user. It does not remove them from the model. If the model is wrong on a whole class, no amount of voting fixes it, and you need better data. The pattern works when the model is right in aggregate and wrong in bursts. That turns out to be most real-time classifiers.