feat(ui): paste/import zone JSON in the calibrator + surface editor in nav

This commit is contained in:
jetson committed 2026-11-12 16:19:16 +07:00
1 parent 0fe59abb4a
commit 08a0e54065
8 files changed
+135 -3

No files matched your search

+2 -1
View File
@@ -36,4 +36,5 @@ Feedmill Recounter: AI video analysis for counting sacks/boxes in feedmill video
- Two presets ship: `default` (the `feat/interactive-roi-line` branch's `95c031b` recalibration, count line y=182 — the active preset) and `fcp-2026-10-15` (pre-recalibration, count line y=220 — matches what the deployed live counter (commit `7beb820`) counts with today, because its `predict.py` hardcodes the line at y=220 and does not read `zones.json`).
- The unload branch in `src/counting.py` is unreachable by design (parity with the deployed live counter, which runs the same gates — `7beb820` contains the gates commit `8448dcb`): `unloading` is always 0 and `net` equals the gross count. Do not "fix" it into a counted unload.
- Zone writes are localhost-only; remote operators use an SSH tunnel (`ssh -L 9050:127.0.0.1:9050 jetson@<host>`).
- `tools/record_rtsp.sh` records an RTSP stream to a file for live-vs-recounter parity runs; the recounter stays file-based (no RTSP input path).
- `tools/record_rtsp.sh` records an RTSP stream to a file for live-vs-recounter parity runs; the recounter stays file-based (no RTSP input path).
- The `/calibrate` editor and `tools/calibrate_zone.py` both accept a pasted JSON preset (Import / Export JSON panel), so zones can be shared over chat or email and applied by paste.
+2 -1
View File
@@ -50,7 +50,8 @@ version tags are cut in this repo — `main` is the release line).
- **Interactive zone editor** at `/calibrate`: drag the counting ROI, the truck
ROI and the count line over a frame of an uploaded video, then save. Zone
writes are localhost-only and atomic (`zones.json.bak` + temp file +
`os.replace`).
`os.replace`). The editor also accepts a pasted JSON preset (Import / Export
JSON panel) and can copy or download the active preset as JSON.
- **Zone write API**: `POST /api/zones/<preset>` and
`POST /api/zones/<preset>/reset` validate, save and restore a zone preset.
- `tools/record_rtsp.sh` records an RTSP stream to a file, so the live counter
+15
View File
@@ -260,3 +260,18 @@ one is written to a temp file, and the temp file replaces `zones.json`.
Zone changes apply to the **next** analysis; a running job keeps the zone it
started with. `tools/calibrate_zone.py` does the same job in an OpenCV window
on the box itself.
### Importing / exporting zone JSON
The calibrator also accepts a pasted JSON blob (useful for sharing zones over
chat or email). Click **Import / Export JSON**, paste a preset object, and press
**Import JSON**:
```json
{"truck_area":[[x,y],...],"counting_area":[[x,y],...],
"count_line":{"x_start":0,"x_end":0,"y":0},
"reference_resolution":[1280,720]}
```
**Copy current zones** puts the active preset on the clipboard; **Download JSON**
saves it to a file. The same shape is what `POST /api/zones/<preset>` accepts.
+91
View File
@@ -317,6 +317,88 @@
}
}
function parseZoneJson(text) {
let obj;
try { obj = JSON.parse(text); } catch (e) { throw new Error('Invalid JSON: ' + e.message); }
if (!obj || typeof obj !== 'object' || Array.isArray(obj)) throw new Error('Expected a JSON object');
const truck = obj.truck_area;
const counting = obj.counting_area;
const line = obj.count_line;
if (!Array.isArray(truck) || truck.length !== 4) throw new Error('truck_area must be an array of 4 [x,y] points');
if (!Array.isArray(counting) || counting.length < 3) throw new Error('counting_area must be an array of at least 3 [x,y] points');
if (!line || typeof line !== 'object') throw new Error('count_line must be an object {x_start, x_end, y}');
const xs = [Number(line.x_start), Number(line.x_end)];
const y = Number(line.y);
if (!xs.every(function (v) { return Number.isFinite(v); }) || !Number.isFinite(y)) throw new Error('count_line values must be finite numbers');
for (let i = 0; i < truck.length; i++) {
if (!Array.isArray(truck[i]) || truck[i].length !== 2) throw new Error('truck_area points must be [x,y]');
if (!truck[i].every(function (v) { return Number.isFinite(Number(v)); })) throw new Error('truck_area values must be finite numbers');
}
for (let i = 0; i < counting.length; i++) {
if (!Array.isArray(counting[i]) || counting[i].length !== 2) throw new Error('counting_area points must be [x,y]');
if (!counting[i].every(function (v) { return Number.isFinite(Number(v)); })) throw new Error('counting_area values must be finite numbers');
}
return {
truck_area: truck.map(function (p) { return [Number(p[0]), Number(p[1])]; }),
counting_area: counting.map(function (p) { return [Number(p[0]), Number(p[1])]; }),
count_line: { x_start: Math.min(xs[0], xs[1]), x_end: Math.max(xs[0], xs[1]), y: Math.round(y) },
reference_resolution: Array.isArray(obj.reference_resolution) ? obj.reference_resolution : [ZONE_SOURCE_W, ZONE_SOURCE_H]
};
}
function importJson() {
const ta = $('zone-json');
if (!ta) return;
try {
const preset = parseZoneJson(ta.value);
applyPreset(preset);
setStatus('Imported — drag handles or Save');
} catch (err) {
setStatus('Import failed: ' + err.message);
}
}
function exportJson() {
if (!zonesData) { setStatus('Nothing to export'); return; }
const xs = zonesData.counting_line.map(function (p) { return p[0]; });
const ys = zonesData.counting_line.map(function (p) { return p[1]; });
const obj = {
truck_area: zonesData.truck_area,
counting_area: zonesData.counting_area,
count_line: { x_start: Math.min.apply(null, xs), x_end: Math.max.apply(null, xs), y: Math.round(ys.reduce(function (a, b) { return a + b; }, 0) / ys.length) },
reference_resolution: [ZONE_SOURCE_W, ZONE_SOURCE_H]
};
const text = JSON.stringify(obj, null, 2);
const ta = $('zone-json');
if (ta) ta.value = text;
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(function () { setStatus('Copied to clipboard'); }, function () { setStatus('Copied to textarea'); });
} else {
setStatus('Copied to textarea');
}
}
function downloadJson() {
if (!zonesData) { setStatus('Nothing to download'); return; }
const xs = zonesData.counting_line.map(function (p) { return p[0]; });
const ys = zonesData.counting_line.map(function (p) { return p[1]; });
const obj = {
truck_area: zonesData.truck_area,
counting_area: zonesData.counting_area,
count_line: { x_start: Math.min.apply(null, xs), x_end: Math.max.apply(null, xs), y: Math.round(ys.reduce(function (a, b) { return a + b; }, 0) / ys.length) },
reference_resolution: [ZONE_SOURCE_W, ZONE_SOURCE_H]
};
const blob = new Blob([JSON.stringify(obj, null, 2)], { type: 'application/json' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = (currentPreset || 'zones') + '.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(a.href);
setStatus('Downloaded');
}
async function resetZones() {
if (!currentPreset) return;
try {
@@ -394,6 +476,12 @@
if (currentPreset) loadPreset(currentPreset);
});
$('zone-reset-btn').addEventListener('click', resetZones);
const importBtn = $('zone-import-btn');
if (importBtn) importBtn.addEventListener('click', importJson);
const copyBtn = $('zone-copy-btn');
if (copyBtn) copyBtn.addEventListener('click', exportJson);
const dlBtn = $('zone-download-btn');
if (dlBtn) dlBtn.addEventListener('click', downloadJson);
$('zone-vis-all-btn').addEventListener('click', toggleAllZoneVisibility);
$('zone-vis-counting').addEventListener('click', function () {
toggleZoneItemVisibility('counting');
@@ -412,6 +500,9 @@
setStatus('Frame failed to load (corrupt video, or the time is past the end)');
});
$('zone-backdrop').addEventListener('load', function () {
const el = $('zone-status');
const cur = (el && el.textContent) || '';
if (/fail|error/i.test(cur)) return; // don't clobber an error message
setStatus(zoneEditMode ? 'Editing: drag handles, then Save' : 'Locked');
});
$('time-slider').addEventListener('input', function () {
+1
View File
@@ -19,6 +19,7 @@
<nav class="site-nav" aria-label="Main navigation">
<a href="/">Upload</a>
<a href="/jobs">Jobs</a>
<a href="/calibrate">Calibrate</a>
</nav>
</div>
</header>
+13 -1
View File
@@ -5,7 +5,9 @@
<div class="calibrate-heading">
<h2>Zone Calibration</h2>
<p>Drag the counting area, truck area and count line over a frame of an uploaded video, then save.
Every coordinate is in the 1280&times;720 analysis space.</p>
Every coordinate is in the 1280&times;720 analysis space.
<a href="/" class="btn btn-ghost btn-sm" style="margin-left: var(--space-2);">&larr; Back to Upload</a>
</p>
</div>
<div class="calibrate-toolbar">
@@ -34,6 +36,16 @@
<button type="button" id="zone-vis-line" class="btn btn-secondary btn-sm" aria-pressed="false">Count line</button>
</div>
<details class="calibrate-json-panel">
<summary>Import / Export JSON</summary>
<textarea id="zone-json" rows="6" placeholder='{"truck_area":[[x,y],...],"counting_area":[[x,y],...],"count_line":{"x_start":0,"x_end":0,"y":0},"reference_resolution":[1280,720]}'></textarea>
<div class="calibrate-json-actions">
<button id="zone-import-btn" type="button" class="btn btn-secondary btn-sm">Import JSON</button>
<button id="zone-copy-btn" type="button" class="btn btn-ghost btn-sm">Copy current zones</button>
<button id="zone-download-btn" type="button" class="btn btn-ghost btn-sm">Download JSON</button>
</div>
</details>
<div class="calibrate-stage">
<img id="zone-backdrop" alt="Video frame under the zone overlay">
<svg id="zoneOverlay" viewBox="0 0 1280 720" preserveAspectRatio="xMidYMid meet">
+4
View File
@@ -429,6 +429,10 @@
<option value="">Loading presets...</option>
</select>
</div>
<p class="zone-hint" style="margin: var(--space-2) 0 0; font-size: var(--text-sm); color: var(--color-neutral-500);">
Annotated output draws the calibrated counting/truck polygons and the &plusmn;20 px band.
<a href="/calibrate">Open zone calibrator &rarr;</a>
</p>
</div>
</div>
+7
View File
@@ -949,6 +949,13 @@ def test_calibrate_page_renders(client):
resp = client.get("/calibrate")
assert resp.status_code == 200
assert b"zoneOverlay" in resp.data
assert b"zone-json" in resp.data
def test_index_page_links_to_calibrator(client):
resp = client.get("/")
assert resp.status_code == 200
assert b'/calibrate' in resp.data
def test_api_frame_rejects_a_bad_filename(client, tmp_path, monkeypatch):