Download src/lib/rolloutAutoScore.ts from Jingyi-Z/lerobotac-dataset-visualizer: direct link, hf CLI and curl.
- Browser
- Download file 23.7 kB
-
https://huggingface.co/spaces/Jingyi-Z/lerobotac-dataset-visualizer/resolve/main/src/lib/rolloutAutoScore.ts
- Command line
-
hf download hf://spaces/Jingyi-Z/lerobotac-dataset-visualizer/src/lib/rolloutAutoScore.ts
-
curl -L -o rolloutAutoScore.ts https://huggingface.co/spaces/Jingyi-Z/lerobotac-dataset-visualizer/resolve/main/src/lib/rolloutAutoScore.ts
23.7 kB
| // In-browser automatic rollout scoring — a TypeScript port of the offline | |
| // video scorer's heuristics, run on downscaled top-camera frames: | |
| // | |
| // ball red-hue HSV mask -> centroid + pixel area (area rises when the | |
| // ball is lifted toward the camera) | |
| // bowl circle Hough transform on the first frame's gradient edges | |
| // arm bright (near-white) mask proximity to the ball centroid | |
| // grip gripper joint trajectory (actuation toward closed) | |
| // | |
| // Stage decision (highest wins): S5 ball ends inside the bowl circle; | |
| // S3 lifted and passed near the bowl; S2 lifted; S1 arm reached the ball | |
| // (or the gripper actuated near it); S0 otherwise. S4 (release attempted) | |
| // is not separable automatically — reviewers upgrade S3 by hand. | |
| // | |
| // All functions are pure over RGBA buffers so they unit-test without a DOM. | |
| // Pixel thresholds are expressed at a 640 px reference width and scaled. | |
| export interface BowlCircle { | |
| x: number; | |
| y: number; | |
| r: number; | |
| score: number; // Hough votes, for confidence reporting | |
| } | |
| export interface FrameFeatures { | |
| t: number; // seconds, episode-relative | |
| ballX: number; | |
| ballY: number; | |
| ballArea: number; // red pixels at analysis scale (0 = not found) | |
| /** rms distance of the red pixels from their centroid. A ball is compact | |
| * (uniform disc: rms = 0.71 sqrt(area/pi)); scattered red pixels — arm | |
| * wiring, reflections — spread far wider, and their centroid is not a | |
| * ball position. */ | |
| ballSpread: number; | |
| armNearBall: boolean; | |
| } | |
| // ---- wrist-camera end check -------------------------------------------------- | |
| // The wrist camera rides on the gripper: it has no fixed frame, so it can | |
| // never judge WHERE the ball is on the table — that stays the top camera's | |
| // job. But it looks straight at the fingers, which is exactly where the top | |
| // view is blind at the end of an episode (the arm parks over the ball). In | |
| // the "carried, but not visible at the end" review branch the top view has | |
| // already ruled the final ball position outside the bowl circle, so a large | |
| // red component at the fingers in the last seconds proves the ball is still | |
| // WITH the gripper — never released into the bowl — and the review becomes | |
| // a scored failure. Positive evidence only: no red at the fingers keeps the | |
| // episode with a person. Calibrated on rollout_eval_A_71ep wrist frames: | |
| // ball-at-gripper components measure 288-3966 px at 320-wide with centroid | |
| // in the bottom fifth; the robot's own fixed red fleck never exceeds ~20 px. | |
| export const REVIEW_CARRIED_NOT_VISIBLE = | |
| "carried, but the ball is not visible at the end"; | |
| export const WRIST_BALL_MIN_AREA = 150; // px at 320-wide analysis | |
| export const WRIST_BALL_MIN_Y_FRAC = 0.6; // the fingers sit low in the frame | |
| export function wristSeesBallAtGripper( | |
| samples: FrameFeatures[], | |
| w: number, | |
| h: number, | |
| ): boolean { | |
| const minArea = WRIST_BALL_MIN_AREA * (w / 320) ** 2; | |
| return samples.some( | |
| (s) => s.ballArea >= minArea && s.ballY >= WRIST_BALL_MIN_Y_FRAC * h, | |
| ); | |
| } | |
| export interface AutoScoreResult { | |
| stage: "S0" | "S1" | "S2" | "S3" | "S4" | "S5"; | |
| suggestedFailure: string; | |
| evidence: string; // human-readable, goes into the notes field | |
| ballSeen: number; // frames with a VISIBLE ball (see visibleFraction) | |
| frames: number; | |
| // structured evidence (2026-09-17 bug report: the label and the evidence | |
| // must be checkable against each other, not free text only) | |
| lifted: boolean; | |
| /** lifted, or occluded inside an actuated gripper for a sustained | |
| * stretch — the carry evidence S5 requires */ | |
| carried: boolean; | |
| visibleFraction: number; // frames with a credible ball / all frames | |
| endVisible: boolean; // ball credibly visible in the last ~1 s | |
| bowlMoved: boolean; | |
| /** final ball distance to the FINAL bowl circle, in radii; null without | |
| * a bowl or a visible end position */ | |
| finalDistR: number | null; | |
| /** the scorer cannot decide this episode honestly: route to a person | |
| * instead of writing a score */ | |
| needsReview: boolean; | |
| reviewReason?: string; | |
| } | |
| // ---- pixel primitives -------------------------------------------------------- | |
| /** Red-ball mask test for one RGBA pixel (two red hue bands, saturated, | |
| * not too dark) — mirrors the offline scorer's HSV bands. */ | |
| export function isRedPixel(r: number, g: number, b: number): boolean { | |
| const mx = Math.max(r, g, b); | |
| const mn = Math.min(r, g, b); | |
| if (mx < 70) return false; // too dark | |
| const sat = mx === 0 ? 0 : (mx - mn) / mx; | |
| if (sat < 0.45) return false; // washed out | |
| // hue in [0,360): red wraps around 0 | |
| const d = mx - mn; | |
| let h: number; | |
| if (mx === r) h = (60 * ((g - b) / d) + 360) % 360; | |
| else if (mx === g) h = 60 * ((b - r) / d) + 120; | |
| else h = 60 * ((r - g) / d) + 240; | |
| return h < 14 || h > 340; | |
| } | |
| /** Bright, low-saturation pixel — the white arm body and gripper. */ | |
| export function isWhitePixel(r: number, g: number, b: number): boolean { | |
| const mx = Math.max(r, g, b); | |
| const mn = Math.min(r, g, b); | |
| return mx > 175 && mx - mn < 40; | |
| } | |
| /** Ball centroid + area and arm proximity for one frame. */ | |
| export function frameFeatures( | |
| rgba: Uint8ClampedArray, | |
| w: number, | |
| h: number, | |
| t: number, | |
| armRadiusPx: number, | |
| ): FrameFeatures { | |
| // red mask | |
| const mask = new Uint8Array(w * h); | |
| let total = 0; | |
| for (let y = 0; y < h; y++) { | |
| for (let x = 0; x < w; x++) { | |
| const i = (y * w + x) * 4; | |
| if (isRedPixel(rgba[i], rgba[i + 1], rgba[i + 2])) { | |
| mask[y * w + x] = 1; | |
| total++; | |
| } | |
| } | |
| } | |
| if (total < 6) { | |
| return { | |
| t, | |
| ballX: -1, | |
| ballY: -1, | |
| ballArea: 0, | |
| ballSpread: 0, | |
| armNearBall: false, | |
| }; | |
| } | |
| // the BALL is the largest 4-connected red component — stray red pixels | |
| // (table point markers, reflections) must not shift the centroid or | |
| // inflate the spread (a 20 px marker 190 px away once rejected every | |
| // frame of an episode through the global rms) | |
| const label = new Int32Array(w * h); // 0 = unlabeled | |
| const stack: number[] = []; | |
| let bestN = 0; | |
| let bestSx = 0; | |
| let bestSy = 0; | |
| let bestSxx = 0; | |
| let bestSyy = 0; | |
| let next = 0; | |
| for (let start = 0; start < w * h; start++) { | |
| if (!mask[start] || label[start]) continue; | |
| next++; | |
| let n = 0; | |
| let sx = 0; | |
| let sy = 0; | |
| let sxx = 0; | |
| let syy = 0; | |
| stack.push(start); | |
| label[start] = next; | |
| while (stack.length) { | |
| const p = stack.pop()!; | |
| const px = p % w; | |
| const py = (p - px) / w; | |
| n++; | |
| sx += px; | |
| sy += py; | |
| sxx += px * px; | |
| syy += py * py; | |
| if (px > 0 && mask[p - 1] && !label[p - 1]) { | |
| label[p - 1] = next; | |
| stack.push(p - 1); | |
| } | |
| if (px < w - 1 && mask[p + 1] && !label[p + 1]) { | |
| label[p + 1] = next; | |
| stack.push(p + 1); | |
| } | |
| if (py > 0 && mask[p - w] && !label[p - w]) { | |
| label[p - w] = next; | |
| stack.push(p - w); | |
| } | |
| if (py < h - 1 && mask[p + w] && !label[p + w]) { | |
| label[p + w] = next; | |
| stack.push(p + w); | |
| } | |
| } | |
| if (n > bestN) { | |
| bestN = n; | |
| bestSx = sx; | |
| bestSy = sy; | |
| bestSxx = sxx; | |
| bestSyy = syy; | |
| } | |
| } | |
| if (bestN < 6) { | |
| return { | |
| t, | |
| ballX: -1, | |
| ballY: -1, | |
| ballArea: 0, | |
| ballSpread: 0, | |
| armNearBall: false, | |
| }; | |
| } | |
| const bx = bestSx / bestN; | |
| const by = bestSy / bestN; | |
| const ballSpread = Math.sqrt( | |
| Math.max(0, bestSxx / bestN - bx * bx) + | |
| Math.max(0, bestSyy / bestN - by * by), | |
| ); | |
| // arm proximity: any white pixel within armRadiusPx of the ball centroid | |
| const r2 = armRadiusPx * armRadiusPx; | |
| const x0 = Math.max(0, Math.floor(bx - armRadiusPx)); | |
| const x1 = Math.min(w - 1, Math.ceil(bx + armRadiusPx)); | |
| const y0 = Math.max(0, Math.floor(by - armRadiusPx)); | |
| const y1 = Math.min(h - 1, Math.ceil(by + armRadiusPx)); | |
| let near = false; | |
| outer: for (let y = y0; y <= y1; y++) { | |
| for (let x = x0; x <= x1; x++) { | |
| const dx = x - bx; | |
| const dy = y - by; | |
| if (dx * dx + dy * dy > r2) continue; | |
| const i = (y * w + x) * 4; | |
| if (isWhitePixel(rgba[i], rgba[i + 1], rgba[i + 2])) { | |
| near = true; | |
| break outer; | |
| } | |
| } | |
| } | |
| return { | |
| t, | |
| ballX: bx, | |
| ballY: by, | |
| ballArea: bestN, | |
| ballSpread, | |
| armNearBall: near, | |
| }; | |
| } | |
| // ---- bowl detection ---------------------------------------------------------- | |
| /** Circle Hough on gradient edges. Radius range as a fraction of width | |
| * (defaults sized for a tabletop bowl seen from the top camera). Returns the | |
| * best circle, or null when the vote is too weak to trust. */ | |
| export function detectBowl( | |
| rgba: Uint8ClampedArray, | |
| w: number, | |
| h: number, | |
| rMinFrac = 0.055, | |
| rMaxFrac = 0.22, | |
| ): BowlCircle | null { | |
| // grayscale | |
| const gray = new Float32Array(w * h); | |
| for (let i = 0, p = 0; i < gray.length; i++, p += 4) { | |
| gray[i] = 0.299 * rgba[p] + 0.587 * rgba[p + 1] + 0.114 * rgba[p + 2]; | |
| } | |
| // Sobel | |
| const gx = new Float32Array(w * h); | |
| const gy = new Float32Array(w * h); | |
| const mag = new Float32Array(w * h); | |
| for (let y = 1; y < h - 1; y++) { | |
| for (let x = 1; x < w - 1; x++) { | |
| const i = y * w + x; | |
| const a = gray[i - w - 1], | |
| b = gray[i - w], | |
| c = gray[i - w + 1]; | |
| const d = gray[i - 1], | |
| f = gray[i + 1]; | |
| const g = gray[i + w - 1], | |
| hh = gray[i + w], | |
| k = gray[i + w + 1]; | |
| const vx = c + 2 * f + k - (a + 2 * d + g); | |
| const vy = g + 2 * hh + k - (a + 2 * b + c); | |
| gx[i] = vx; | |
| gy[i] = vy; | |
| mag[i] = Math.abs(vx) + Math.abs(vy); | |
| } | |
| } | |
| // edge threshold: keep the strongest ~4% of gradients | |
| const sorted = Float32Array.from(mag).sort(); | |
| const thr = Math.max(60, sorted[Math.floor(sorted.length * 0.96)]); | |
| const rMin = Math.max(4, Math.round(w * rMinFrac)); | |
| const rMax = Math.round(w * rMaxFrac); | |
| const rStep = 2; | |
| const nR = Math.floor((rMax - rMin) / rStep) + 1; | |
| const acc = new Int32Array(w * h * nR); | |
| for (let y = 1; y < h - 1; y++) { | |
| for (let x = 1; x < w - 1; x++) { | |
| const i = y * w + x; | |
| if (mag[i] < thr) continue; | |
| const m = Math.hypot(gx[i], gy[i]); | |
| if (m === 0) continue; | |
| const ux = gx[i] / m; | |
| const uy = gy[i] / m; | |
| for (let ri = 0; ri < nR; ri++) { | |
| const r = rMin + ri * rStep; | |
| // vote along the gradient in both directions | |
| for (const s of [1, -1]) { | |
| const cx = Math.round(x + s * ux * r); | |
| const cy = Math.round(y + s * uy * r); | |
| if (cx < 0 || cx >= w || cy < 0 || cy >= h) continue; | |
| acc[(cy * w + cx) * nR + ri]++; | |
| } | |
| } | |
| } | |
| } | |
| let best = 0; | |
| let bestIdx = -1; | |
| for (let i = 0; i < acc.length; i++) { | |
| if (acc[i] > best) { | |
| best = acc[i]; | |
| bestIdx = i; | |
| } | |
| } | |
| if (bestIdx < 0) return null; | |
| const ri0 = bestIdx % nR; | |
| const pix0 = (bestIdx - ri0) / nR; | |
| const bx = pix0 % w; | |
| const by = Math.floor(pix0 / w); | |
| // votes disperse over neighboring (x, y, r) bins from rounding — score and | |
| // refine over the 3x3x3 neighborhood of the raw argmax | |
| let sum = 0; | |
| let wx = 0; | |
| let wy = 0; | |
| let wr = 0; | |
| for (let dy = -1; dy <= 1; dy++) { | |
| for (let dx = -1; dx <= 1; dx++) { | |
| for (let dr = -1; dr <= 1; dr++) { | |
| const x = bx + dx; | |
| const y = by + dy; | |
| const ri = ri0 + dr; | |
| if (x < 0 || x >= w || y < 0 || y >= h || ri < 0 || ri >= nR) continue; | |
| const v = acc[(y * w + x) * nR + ri]; | |
| sum += v; | |
| wx += v * x; | |
| wy += v * y; | |
| wr += v * (rMin + ri * rStep); | |
| } | |
| } | |
| } | |
| const r = sum > 0 ? wr / sum : rMin + ri0 * rStep; | |
| // confidence: neighborhood votes should cover a reasonable share of the | |
| // circumference | |
| const minVotes = Math.max(16, Math.round(2 * Math.PI * r * 0.25)); | |
| if (sum < minVotes) return null; | |
| return { | |
| x: Math.round(wx / sum), | |
| y: Math.round(wy / sum), | |
| r: Math.round(r), | |
| score: sum, | |
| }; | |
| } | |
| // ---- gripper ----------------------------------------------------------------- | |
| /** True when the gripper trajectory actuates by more than 35% of its own | |
| * range away from its starting value (a grasp attempt) AND that range is | |
| * meaningful against the signal's own scale — plain servo noise around a | |
| * fixed position must not count. */ | |
| export function gripperActuated( | |
| gripper: { t: number[]; pos: number[] } | null, | |
| ): boolean { | |
| if (!gripper || gripper.pos.length < 3) return false; | |
| const lo = Math.min(...gripper.pos); | |
| const hi = Math.max(...gripper.pos); | |
| const range = hi - lo; | |
| const scale = Math.max(Math.abs(hi), Math.abs(lo)); | |
| if (range <= 1e-9 || scale <= 1e-9) return false; | |
| if (range < 0.15 * scale) return false; // noise around a held position | |
| const start = gripper.pos[0]; | |
| return gripper.pos.some((v) => Math.abs(v - start) > 0.35 * range); | |
| } | |
| // ---- stage decision ---------------------------------------------------------- | |
| const REF_W = 640; // thresholds below are in pixels at this width | |
| const VISIBLE_AREA_FRAC = 0.15; // of the episode's own full-view ball area | |
| const COMPACT_SPREAD = 2.0; // x sqrt(area/pi); a disc is ~0.71 | |
| const OCCLUDED_CARRY_MIN = 8; // samples (~2 s at 4 fps) inside the gripper | |
| const END_WINDOW_SAMPLES = 5; // ~1.25 s at 4 fps | |
| const LIFT_AREA_RATIO = 1.08; // ball area gain when lifted toward the camera | |
| const NEAR_BOWL_PX = 90; | |
| const IN_BOWL_R = 0.85; | |
| export function decideStages( | |
| frames: FrameFeatures[], | |
| bowl: BowlCircle | null, | |
| gripper: { t: number[]; pos: number[] } | null, | |
| analysisWidth: number, | |
| /** the bowl re-detected on the LAST frame — the bowl can be dragged | |
| * during an episode, and "ball in bowl" only means success against the | |
| * bowl's final position. null = not redetected (occluded). */ | |
| bowlEnd: BowlCircle | null = null, | |
| ): AutoScoreResult { | |
| const scale = analysisWidth / REF_W; | |
| const grip = gripperActuated(gripper); | |
| const detections = frames.filter((f) => f.ballArea > 0); | |
| // ---- visibility (2026-09-17 bug report, defect 2): a centroid is only | |
| // a BALL when the mask is a credible ball — large enough relative to the | |
| // episode's own full view of it, and compact. Stray red pixels (wiring, | |
| // reflections, the occluded ball's exposed sliver) fail one or both, and | |
| // such frames count as "ball not visible" instead of emitting a position. | |
| // the episode's "full view" ball size: median of the 8 largest | |
| // detections. A percentile over ALL detections collapses on episodes | |
| // that are mostly occluded slivers, which are exactly the ones the | |
| // visibility test exists for. | |
| const areasSorted = detections.map((f) => f.ballArea).sort((x, y) => y - x); | |
| const top = areasSorted.slice(0, 8); | |
| const fullArea = top.length ? top[Math.floor(top.length / 2)] : 0; | |
| const minVisibleArea = Math.max(10, VISIBLE_AREA_FRAC * fullArea); | |
| const isVisible = (f: FrameFeatures): boolean => | |
| f.ballArea >= minVisibleArea && | |
| f.ballSpread <= COMPACT_SPREAD * Math.sqrt(f.ballArea / Math.PI); | |
| const seen = frames.filter(isVisible); | |
| const visibleFraction = frames.length ? seen.length / frames.length : 0; | |
| const base: Omit<AutoScoreResult, "stage" | "suggestedFailure" | "evidence"> = | |
| { | |
| ballSeen: seen.length, | |
| frames: frames.length, | |
| lifted: false, | |
| carried: false, | |
| visibleFraction, | |
| endVisible: false, | |
| bowlMoved: false, | |
| finalDistR: null, | |
| needsReview: false, | |
| }; | |
| if (seen.length < 3) { | |
| return { | |
| ...base, | |
| stage: grip ? "S1" : "S0", | |
| suggestedFailure: grip ? "F3" : "F6", | |
| evidence: | |
| `ball not credibly tracked (${seen.length}/${frames.length} frames)` + | |
| (grip ? "; gripper actuated" : ""), | |
| }; | |
| } | |
| // baseline area: median of the first up-to-8 visible detections | |
| const head = seen | |
| .slice(0, 8) | |
| .map((f) => f.ballArea) | |
| .sort((x, y) => x - y); | |
| const baseArea = head[Math.floor(head.length / 2)]; | |
| // lifted: sustained area gain over >= 3 consecutive visible detections | |
| let liftRun = 0; | |
| let lifted = false; | |
| let minBowlDistLifted = Infinity; | |
| for (const f of seen) { | |
| if (f.ballArea > LIFT_AREA_RATIO * baseArea) { | |
| liftRun++; | |
| if (liftRun >= 3) { | |
| lifted = true; | |
| if (bowl) { | |
| const d = Math.hypot(f.ballX - bowl.x, f.ballY - bowl.y); | |
| if (d < minBowlDistLifted) minBowlDistLifted = d; | |
| } | |
| } | |
| } else { | |
| liftRun = 0; | |
| } | |
| } | |
| // occluded carry: the ball disappears into the actuated gripper for a | |
| // sustained stretch between visible bookends. On top-camera poses where | |
| // lifting adds no apparent size (the pi05_ft geometry), this is what a | |
| // real carry looks like: full view, then a long not-visible run while | |
| // the jaw is closed, then full view again somewhere else. | |
| let occludedCarry = false; | |
| if (grip && seen.length >= 2) { | |
| const firstVis = frames.indexOf(seen[0]); | |
| const lastVis = frames.lastIndexOf(seen[seen.length - 1]); | |
| let run = 0; | |
| for (let i = firstVis; i <= lastVis; i++) { | |
| if (!isVisible(frames[i])) { | |
| run++; | |
| if (run >= OCCLUDED_CARRY_MIN) { | |
| occludedCarry = true; | |
| break; | |
| } | |
| } else { | |
| run = 0; | |
| } | |
| } | |
| } | |
| const carried = lifted || occludedCarry; | |
| // end state: the ball must be credibly visible near the episode's end | |
| // for any "where did it end up" claim; its final position is the median | |
| // of the last visible detections | |
| const endWindowStart = frames.length - END_WINDOW_SAMPLES; | |
| const endVisible = | |
| frames.filter((f, i) => i >= endWindowStart && isVisible(f)).length >= 2; | |
| const tail = seen.slice(-5); | |
| const fx = tail.map((f) => f.ballX).sort((x, y) => x - y)[ | |
| Math.floor(tail.length / 2) | |
| ]; | |
| const fy = tail.map((f) => f.ballY).sort((x, y) => x - y)[ | |
| Math.floor(tail.length / 2) | |
| ]; | |
| // the bowl's FINAL position judges success; the start circle anchors | |
| // "started outside" and the near-bowl transport check | |
| const bowlFinal = bowlEnd ?? bowl; | |
| const bowlMoved = | |
| bowl != null && | |
| bowlEnd != null && | |
| Math.hypot(bowlEnd.x - bowl.x, bowlEnd.y - bowl.y) > 0.75 * bowl.r; | |
| const finalDistR = | |
| bowlFinal != null && endVisible | |
| ? Math.hypot(fx - bowlFinal.x, fy - bowlFinal.y) / bowlFinal.r | |
| : null; | |
| const endInBowl = finalDistR != null && finalDistR < IN_BOWL_R; | |
| const headPos = seen.slice(0, 5); | |
| const sx = headPos.map((f) => f.ballX).sort((x, y) => x - y)[ | |
| Math.floor(headPos.length / 2) | |
| ]; | |
| const sy = headPos.map((f) => f.ballY).sort((x, y) => x - y)[ | |
| Math.floor(headPos.length / 2) | |
| ]; | |
| const startedOutsideBowl = | |
| bowl != null && Math.hypot(sx - bowl.x, sy - bowl.y) > 1.5 * bowl.r; | |
| const nearBowl = lifted && minBowlDistLifted < NEAR_BOWL_PX * scale; | |
| const reached = seen.some((f) => f.armNearBall) || grip; | |
| // ---- the ladder (defect 1): S5 needs the outcome AND the carry, each | |
| // from its own instrument. No carry evidence caps the stage at S1. | |
| let stage: AutoScoreResult["stage"]; | |
| let fail: string; | |
| let needsReview = false; | |
| let reviewReason: string | undefined; | |
| if (endInBowl && startedOutsideBowl && carried && endVisible) { | |
| stage = "S5"; | |
| fail = "F0"; | |
| if (bowlMoved) { | |
| // in the (moved) bowl at the end, but the scene was disturbed: | |
| // a person decides whether this counts | |
| needsReview = true; | |
| reviewReason = "in the bowl, but the bowl was displaced"; | |
| } | |
| } else if (endInBowl && !carried) { | |
| // the outcome test says success but no instrument saw a carry: | |
| // pushed/rolled in, or an occlusion artifact — not a score | |
| stage = "S1"; | |
| fail = "F3"; | |
| needsReview = true; | |
| reviewReason = "ball ends in the bowl without carry evidence"; | |
| } else if (carried && !endVisible) { | |
| // carried somewhere the tracker cannot certify | |
| stage = "S3"; | |
| fail = "F4"; | |
| needsReview = true; | |
| reviewReason = REVIEW_CARRIED_NOT_VISIBLE; | |
| } else if (!endVisible && grip && visibleFraction < 0.9) { | |
| // the gripper acted and the ball never credibly reappeared: the | |
| // outcome cannot be read off the video (squeezed under the jaw, | |
| // rolled out of view) — a person decides | |
| stage = lifted ? "S3" : "S1"; | |
| fail = lifted ? "F4" : "F3"; | |
| needsReview = true; | |
| reviewReason = "ball not visible at the end of the episode"; | |
| } else if (bowlMoved && (lifted || reached)) { | |
| stage = lifted ? "S3" : "S1"; | |
| fail = "F7"; | |
| needsReview = true; | |
| reviewReason = "the bowl was displaced"; | |
| } else if (nearBowl) { | |
| stage = "S3"; | |
| fail = "F5"; | |
| } else if (lifted) { | |
| stage = "S2"; | |
| fail = "F4"; | |
| } else if (reached) { | |
| stage = "S1"; | |
| fail = grip ? "F3" : "F2"; | |
| } else { | |
| stage = "S0"; | |
| fail = "F6"; | |
| } | |
| const bits = [ | |
| `ball visible ${Math.round(100 * visibleFraction)}% (${seen.length}/${frames.length})`, | |
| bowl | |
| ? `bowl (${bowl.x},${bowl.y}) r${bowl.r}` | |
| : "bowl not found (S3/S5 unavailable)", | |
| lifted | |
| ? `lifted (area > ${LIFT_AREA_RATIO}x base)` | |
| : occludedCarry | |
| ? "carried occluded (long not-visible run in a closed gripper)" | |
| : "no carry evidence", | |
| endVisible ? "end visible" : "end NOT visible", | |
| ]; | |
| if (finalDistR != null) bits.push(`final dist ${finalDistR.toFixed(2)}r`); | |
| if (bowlMoved && bowl && bowlEnd) { | |
| bits.push( | |
| `bowl MOVED (${bowl.x},${bowl.y}) -> (${bowlEnd.x},${bowlEnd.y})`, | |
| ); | |
| } else if (bowl && !bowlEnd) { | |
| bits.push("end-frame bowl not redetected"); | |
| } | |
| if (grip) bits.push("gripper actuated"); | |
| if (needsReview) bits.push(`NEEDS REVIEW: ${reviewReason}`); | |
| return { | |
| ...base, | |
| stage, | |
| suggestedFailure: fail, | |
| evidence: bits.join("; "), | |
| lifted, | |
| carried, | |
| endVisible, | |
| bowlMoved, | |
| finalDistR, | |
| needsReview, | |
| reviewReason, | |
| }; | |
| } | |
| // ---- CSV import -------------------------------------------------------------- | |
| export interface ImportedReview { | |
| episode: number; | |
| stage: string; | |
| scene?: string; | |
| failure?: string; | |
| notes?: string; | |
| } | |
| /** Parse an offline scorer CSV. Header must name an episode column and a | |
| * stage column; scene/failure/notes are optional. Rows with an unknown | |
| * stage are skipped. Returns rows plus a skipped count. */ | |
| export function parseAutoLabelCsv(text: string): { | |
| rows: ImportedReview[]; | |
| skipped: number; | |
| } { | |
| const lines = text.split(/\r?\n/).filter((l) => l.trim().length > 0); | |
| if (lines.length < 2) return { rows: [], skipped: 0 }; | |
| const header = lines[0] | |
| .replace(/^/, "") | |
| .split(",") | |
| .map((c) => c.trim().toLowerCase()); | |
| const iEp = header.findIndex((c) => /^(episode|episode_index|ep)$/.test(c)); | |
| const iStage = header.findIndex((c) => /stage/.test(c)); | |
| if (iEp < 0 || iStage < 0) return { rows: [], skipped: lines.length - 1 }; | |
| const iScene = header.findIndex((c) => /scene/.test(c)); | |
| const iFail = header.findIndex((c) => /fail/.test(c)); | |
| const iNotes = header.findIndex((c) => /note/.test(c)); | |
| const rows: ImportedReview[] = []; | |
| let skipped = 0; | |
| for (let i = 1; i < lines.length; i++) { | |
| const cols = lines[i].split(","); | |
| const ep = Number(cols[iEp]); | |
| const stageRaw = (cols[iStage] ?? "").trim(); | |
| const m = /^S([0-5])/i.exec(stageRaw); | |
| if (!Number.isInteger(ep) || ep < 0 || !m) { | |
| skipped++; | |
| continue; | |
| } | |
| const failRaw = iFail >= 0 ? (cols[iFail] ?? "").trim() : ""; | |
| const fm = /^F([0-8])/i.exec(failRaw); | |
| rows.push({ | |
| episode: ep, | |
| stage: `S${m[1]}`, | |
| scene: iScene >= 0 ? (cols[iScene] ?? "").trim() || undefined : undefined, | |
| failure: fm ? `F${fm[1]}` : undefined, | |
| notes: iNotes >= 0 ? (cols[iNotes] ?? "").trim() || undefined : undefined, | |
| }); | |
| } | |
| return { rows, skipped }; | |
| } | |