Run this model with HF Jobs

Run CodeEliott/lsf-alphabet on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs to fine-tune https://huggingface.co/CodeEliott/lsf-alphabet. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/CodeEliott/lsf-alphabet on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
lsf-alphabet / demo.js
CodeEliott's picture
Open LSF alphabet model v0.1
32c0bc0 verified
Raw History Blame Contribute Delete
4.41 kB
// Live demo of the open LSF alphabet model. Webcam -> MediaPipe hand points -> 60 numbers -> model -> letter.
// Everything runs in the browser; only the tracker (MediaPipe, from Google's servers) and model/letters.json load.
import { FilesetResolver, HandLandmarker }
from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.14/vision_bundle.mjs";
const $ = id => document.getElementById(id);
const model = await (await fetch("model/letters.json")).json();
const SMOOTH = 8; // average the last 8 pictures: one shaky picture doesn't flip the letter
const BONES = [[0, 1, 2, 3, 4], [0, 5, 6, 7, 8], [5, 9, 10, 11, 12], [9, 13, 14, 15, 16], [13, 17], [0, 17, 18, 19, 20]];
// The signing hand: the only hand seen, or the higher one (the other one rests lower)
const signingHand = hands => hands.length ? hands.reduce((a, b) => (b[0].y < a[0].y ? b : a)) : null;
// The hand as 60 numbers: points 1..20 minus the wrist, x made square with the picture (aspect = width / height),
// divided by the hand's size in 3D (wrist to middle knuckle), x flipped for left-handed signers.
export function features(hand, aspect, leftHanded = false) {
const w = hand[0], m = hand[9], sx = leftHanded ? -1 : 1;
const size = Math.hypot((m.x - w.x) * aspect, m.y - w.y, (m.z - w.z) * aspect) || 1e-6;
const out = [];
for (let i = 1; i < 21; i++) {
const p = hand[i];
out.push(sx * (p.x - w.x) * aspect / size, (p.y - w.y) / size, (p.z - w.z) * aspect / size);
}
return out;
}
// The model: scale, layers with ReLU, softmax. Returns one probability per letter (model.letters).
export function predict(x) {
let z = x.map((v, i) => (v - model.mean[i]) / model.scale[i]);
model.layers.forEach(({ w, b }, n) => {
z = b.map((bj, j) => z.reduce((s, zi, i) => s + zi * w[i][j], bj));
if (n < model.layers.length - 1) z = z.map(v => Math.max(0, v));
});
const top = Math.max(...z), e = z.map(v => Math.exp(v - top)), sum = e.reduce((a, b) => a + b, 0);
return e.map(v => v / sum);
}
function draw(hand) {
const c = $("draw"), v = $("video"), ctx = c.getContext("2d");
c.width = v.videoWidth; c.height = v.videoHeight;
ctx.clearRect(0, 0, c.width, c.height);
if (!hand) return;
ctx.strokeStyle = "#5b8def"; ctx.lineWidth = 4; ctx.lineCap = "round";
for (const bone of BONES) {
ctx.beginPath();
bone.forEach((i, n) => ctx[n ? "lineTo" : "moveTo"](hand[i].x * c.width, hand[i].y * c.height));
ctx.stroke();
}
}
function show(p) {
if (!p) { $("letter").textContent = "·"; $("sure").textContent = "Montre ta main"; $("bars").textContent = ""; return; }
const top = model.letters.map((l, i) => [l, p[i]]).sort((a, b) => b[1] - a[1]).slice(0, 3);
$("letter").textContent = top[0][0].toUpperCase();
$("sure").textContent = top[0][1] > 0.6 ? "sûr" : top[0][1] > 0.35 ? "plutôt" : "hésite";
$("bars").innerHTML = top.map(([l, v]) => `<div class="bar"><span>${l.toUpperCase()}</span><i style="width:${Math.round(v * 200)}px"></i>${Math.round(v * 100)}%</div>`).join("");
}
async function start() {
$("start").hidden = true;
$("sure").textContent = "Chargement du suivi des mains…";
const video = $("video");
video.srcObject = await navigator.mediaDevices.getUserMedia({ video: { facingMode: "user", width: 640, height: 480 } });
await video.play();
const vision = await FilesetResolver.forVisionTasks("https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.14/wasm");
const tracker = await HandLandmarker.createFromOptions(vision, { runningMode: "VIDEO", numHands: 2,
baseOptions: { modelAssetPath: "https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task" } });
const recent = [];
const loop = () => {
const hand = signingHand(tracker.detectForVideo(video, performance.now()).landmarks);
draw(hand);
if (hand) {
recent.push(predict(features(hand, video.videoWidth / video.videoHeight, $("left").checked)));
if (recent.length > SMOOTH) recent.shift();
show(recent[0].map((_, i) => recent.reduce((s, p) => s + p[i], 0) / recent.length));
} else {
recent.length = 0;
show(null);
}
requestAnimationFrame(loop);
};
loop();
}
$("sure").textContent = "";
$("start").hidden = false;
$("start").onclick = () => start().catch(e => { $("sure").textContent = "Caméra impossible : " + e.message; $("start").hidden = false; });