diff --git a/docs/WEB_DEMO.md b/docs/WEB_DEMO.md index 13517fd..daaf6c1 100644 --- a/docs/WEB_DEMO.md +++ b/docs/WEB_DEMO.md @@ -21,6 +21,8 @@ Seeed's older Grove Buzzer v1.1 page describes an onboard transistor. That descr Section 5 still accepts only person, bicycle, car, motorcycle, bus, and truck, with a score of at least 0.5, and only when the box center is inside the middle half of the picture (from 25% to 75% of both width and height). Boxes farther out are ignored. +The control named **Looks-close box size** adds one more gate. Its default is 20. A box sounds only when its width is at least that percent of the picture width, or its height is at least that percent of the picture height. Gray dashed boxes labeled "too small" passed the class, score, and center checks but failed this size check, so they do not beep. Raise the number to ignore more distant-looking boxes. Lower it if nearby objects stay silent. A large object that is actually far away can still pass. A small object that is actually close can still fail. The phone has no meter measurement for this. You do not reflash the board for this control. Reload the page in Bluefy so it does not keep the previous file. + For those boxes, image left and image right are the two sides of the unmirrored rear-camera frame. The page does not flip the video. A center x less than half the picture width sets the left bit. Greater than half sets the right bit. Exactly half sets both bits for that one box. Several boxes are combined, so a left box and a right box can sound together. That both-sides object pattern is a steady pulse. It is not the not-ready double blip. The not-ready double blip plays on both buzzers when the camera, model, or page is not ready, and D3 stays on. It means the pipeline is down. It does not mean objects are on both sides. Quiet buzzers are not an all-clear: watch D5. A slow blip means the link is alive. A fast pattern means the link timed out. diff --git a/web/app.js b/web/app.js index a8a9ba5..0062442 100644 --- a/web/app.js +++ b/web/app.js @@ -12,7 +12,7 @@ import { encodeCommand, } from "./protocol.js"; import { claimForMode } from "./state.js"; -import { sideWarnings, updateTracks } from "./tracking.js"; +import { DEFAULT_MIN_APPARENT_SIZE, sideWarnings, updateTracks } from "./tracking.js"; const TFJS_URL = "https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.22.0/dist/tf.min.js"; const COCO_URL = "https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd@2.2.3/dist/coco-ssd.min.js"; @@ -157,6 +157,23 @@ async function sendClaim() { } } +function minApparentSize() { + const control = document.querySelector("#size-threshold"); + const percent = control ? Number(control.value) : DEFAULT_MIN_APPARENT_SIZE * 100; + return Number.isFinite(percent) ? percent / 100 : DEFAULT_MIN_APPARENT_SIZE; +} + +function applySides(now) { + state.zoneHit = sideWarnings( + state.tracks, + video.videoWidth, + video.videoHeight, + now, + minApparentSize() + ); + state.sides = (state.zoneHit.left ? SIDE_LEFT : 0) | (state.zoneHit.right ? SIDE_RIGHT : 0); +} + function drawTracks() { const width = video.videoWidth; const height = video.videoHeight; @@ -167,18 +184,22 @@ function drawTracks() { overlay.height = height; const context = overlay.getContext("2d"); context.clearRect(0, 0, width, height); - context.strokeStyle = "#ffe14a"; - context.lineWidth = 3; + const sounded = new Set((state.zoneHit?.hits || []).map((hit) => hit.trackId)); + const excluded = new Set((state.zoneHit?.excluded || []).map((hit) => hit.trackId)); context.font = "20px sans-serif"; - context.fillStyle = "#ffe14a"; + context.lineWidth = 3; for (const track of state.tracks) { + const tooSmall = excluded.has(track.id); + context.strokeStyle = sounded.has(track.id) ? "#ffe14a" : tooSmall ? "#9aa0a6" : "#ffffff"; + context.setLineDash(tooSmall ? [8, 6] : []); context.strokeRect(track.bbox.x, track.bbox.y, track.bbox.w, track.bbox.h); - context.fillText(`${track.id} ${track.className} ${track.score.toFixed(2)}`, track.bbox.x, track.bbox.y + 22); + context.fillStyle = context.strokeStyle; + const note = tooSmall ? " too small" : ""; + context.fillText(`${track.id} ${track.className} ${track.score.toFixed(2)}${note}`, track.bbox.x, track.bbox.y + 22); } - const x = width * 0.25; - const y = height * 0.25; + context.setLineDash([]); context.strokeStyle = "#ffffff"; - context.strokeRect(x, y, width * 0.5, height * 0.5); + context.strokeRect(width * 0.25, height * 0.25, width * 0.5, height * 0.5); } let detectTimer = 0; @@ -201,8 +222,7 @@ async function detectOnce() { })); state.tracks = updateTracks(state.tracks, detections, now); state.lastFrameMs = now; - state.zoneHit = sideWarnings(state.tracks, video.videoWidth, video.videoHeight, now); - state.sides = (state.zoneHit.left ? SIDE_LEFT : 0) | (state.zoneHit.right ? SIDE_RIGHT : 0); + applySides(now); drawTracks(); const names = state.zoneHit.hits.map((hit) => `${hit.className} ${hit.side}`).join(", "); detectStatus.textContent = state.zoneHit.hits.length @@ -211,6 +231,7 @@ async function detectOnce() { } catch (error) { state.modelError = true; state.zoneHit = null; + state.sides = 0; setBox(modelStatus, `Model stopped: ${error.message}. No brightness substitute is used.`, "bad"); } } @@ -406,6 +427,15 @@ document.addEventListener("visibilitychange", () => { } }); +document.querySelector("#size-threshold").addEventListener("input", (event) => { + const percent = Number(event.target.value); + document.querySelector("#size-readout").textContent = + `Sound a counted box only when its width or height covers at least ${percent}% of the picture. This is not meters.`; + applySides(performance.now()); + drawTracks(); + sendClaim(); +}); + setInterval(sendClaim, HEARTBEAT_MS); manualEnabled(false); diagnosticsText(); diff --git a/web/index.html b/web/index.html index 45276c8..b9ebaaa 100644 --- a/web/index.html +++ b/web/index.html @@ -43,9 +43,13 @@

4. Bluetooth only

D5 is a link light on the board, not a third buzzer. A slow blip means packets are arriving. A fast pattern means the link timed out. Quiet buzzers are not an all-clear.

5. Camera, model, and Bluetooth together

+ +

Default is 20% of the picture width or height. This is not meters.

Combined warning is off.

-

The same classes and the same center half of the picture still decide whether anything counts. A counted box whose center is left of the picture's middle sounds the left buzzer. Right of the middle sounds the right buzzer. A center exactly on the middle line sounds both, for that one box. Boxes outside the center half are ignored. This is left and right in the camera picture. It is not a turn instruction and not a distance.

+

The same classes, scores, and center half still decide what can count. A counted box also has to look large: its width or height must be at least the percentage above. A gray dashed box says "too small" and does not beep. A big distant object can still pass. A small nearby object can still fail. This is not a measured distance. Image left still sounds D2 and image right still sounds D4. A box center exactly on the middle line sounds both. Boxes outside the center half stay ignored.

What you do, in order

    diff --git a/web/logic.test.mjs b/web/logic.test.mjs index e57e322..fd16c5a 100644 --- a/web/logic.test.mjs +++ b/web/logic.test.mjs @@ -109,6 +109,46 @@ test("center gate chooses left, right, both, or none", () => { assert.equal(none.right, false); }); +test("small centered boxes do not sound and large boxes keep their sides", () => { + const smallLeft = track(1, "person", 0.95, box(40, 40, 10, 10)); + const largeLeft = track(2, "person", 0.9, box(10, 10, 40, 50)); + const largeRight = track(3, "car", 0.8, box(55, 15, 30, 40)); + const quiet = sideWarnings([smallLeft], 100, 100, 0, 0.2); + assert.equal(quiet.left, false); + assert.equal(quiet.right, false); + assert.equal(quiet.excluded.length, 1); + assert.equal(quiet.excluded[0].side, "left"); + + const left = sideWarnings([largeLeft], 100, 100, 0, 0.2); + assert.equal(left.left, true); + assert.equal(left.right, false); + assert.equal(left.hits[0].side, "left"); + + const right = sideWarnings([largeRight], 100, 100, 0, 0.2); + assert.equal(right.left, false); + assert.equal(right.right, true); + + const both = sideWarnings([largeLeft, largeRight, smallLeft], 100, 100, 0, 0.2); + assert.equal(both.left, true); + assert.equal(both.right, true); + assert.equal(both.hits.length, 2); + assert.equal(both.excluded.length, 1); + + const lowered = sideWarnings([smallLeft], 100, 100, 0, 0.1); + assert.equal(lowered.left, true); + assert.equal(lowered.excluded.length, 0); + const raised = sideWarnings([largeLeft], 100, 100, 0, 0.6); + assert.equal(raised.left, false); + assert.equal(raised.excluded.length, 1); + + const lowScore = sideWarnings([track(9, "person", 0.4, box(10, 10, 50, 60))], 100, 100, 0, 0.2); + assert.equal(lowScore.hits.length, 0); + assert.equal(lowScore.excluded.length, 0); + const outside = sideWarnings([track(8, "bus", 0.9, box(0, 0, 40, 40))], 100, 100, 0, 0.2); + assert.equal(outside.left, false); + assert.equal(outside.excluded.length, 0); +}); + test("iou keeps an id and expiry drops it", () => { const first = updateTracks([], [{ className: "person", score: 0.9, bbox: box(0, 0, 10, 10) }], 1000); const second = updateTracks(first, [{ className: "person", score: 0.8, bbox: box(1, 1, 10, 10) }], 1100); diff --git a/web/tracking.js b/web/tracking.js index ddd0f25..6eb815c 100644 --- a/web/tracking.js +++ b/web/tracking.js @@ -3,6 +3,14 @@ export const IOU_THRESHOLD = 0.3; export const TRACK_MAX_AGE_MS = 500; export const SUPPORTED_CLASSES = ["person", "bicycle", "car", "motorcycle", "bus", "truck"]; export const MIN_SCORE = 0.5; +export const DEFAULT_MIN_APPARENT_SIZE = 0.2; + +export function apparentSize(bbox, width, height) { + if (width <= 0 || height <= 0) { + return 0; + } + return Math.max(bbox.w / width, bbox.h / height); +} export function iou(a, b) { const ax2 = a.x + a.w; @@ -93,8 +101,8 @@ export function centerZoneHit(tracks, width, height, nowMs) { return best; } -export function sideWarnings(tracks, width, height, nowMs) { - const result = { left: false, right: false, onDivider: false, hits: [] }; +export function sideWarnings(tracks, width, height, nowMs, minApparentSize = DEFAULT_MIN_APPARENT_SIZE) { + const result = { left: false, right: false, onDivider: false, hits: [], excluded: [] }; if (width <= 0 || height <= 0) { return result; } @@ -118,21 +126,32 @@ export function sideWarnings(tracks, width, height, nowMs) { let side = "left"; if (cx > midpoint) { side = "right"; - result.right = true; } else if (cx < midpoint) { - result.left = true; + side = "left"; } else { side = "divider"; - result.left = true; - result.right = true; - result.onDivider = true; } - result.hits.push({ + const seen = { trackId: track.id, className: track.className, score: track.score, side, - }); + apparentSize: apparentSize(track.bbox, width, height), + }; + if (seen.apparentSize < minApparentSize) { + result.excluded.push(seen); + continue; + } + if (side === "right") { + result.right = true; + } else if (side === "left") { + result.left = true; + } else { + result.left = true; + result.right = true; + result.onDivider = true; + } + result.hits.push(seen); } return result; }