623 lines
25 KiB
JavaScript
623 lines
25 KiB
JavaScript
/* Interactive zone editor.
|
|
*
|
|
* The backdrop is a 16:9 frame (1280x720) and the SVG overlay uses a
|
|
* 1920x1080 viewBox, so coordinates are edited directly in FCP's 1920x1080
|
|
* zone space with uniform scaling and letterbox offset ("xMidYMid meet").
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
const ZONE_SOURCE_W = 1920;
|
|
const ZONE_SOURCE_H = 1080;
|
|
const ZONE_LINE_MARGIN = 30; // 20px trigger band in 720p == 30px in 1080p
|
|
|
|
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
|
|
// Area key -> visibility toggle key and colour.
|
|
const AREAS = [
|
|
{ key: 'counting_area', vis: 'counting', color: '#2563eb' },
|
|
{ key: 'truck_area', vis: 'truck', color: '#d97706' }
|
|
];
|
|
|
|
let zonesData = null;
|
|
let zoneEditMode = false;
|
|
let zoneDrag = null;
|
|
const zoneVisibility = { counting: false, truck: false, counting_line: false };
|
|
|
|
let currentPreset = '';
|
|
let frameTimer = null;
|
|
|
|
const $ = function (id) { return document.getElementById(id); };
|
|
|
|
function setStatus(text) {
|
|
$('zone-status').textContent = text;
|
|
}
|
|
|
|
function sourcePoint(ev) {
|
|
const svg = document.getElementById('zoneOverlay');
|
|
const r = svg.getBoundingClientRect();
|
|
const scale = Math.min(r.width / ZONE_SOURCE_W, r.height / ZONE_SOURCE_H);
|
|
const offsetX = (r.width - ZONE_SOURCE_W * scale) / 2;
|
|
const offsetY = (r.height - ZONE_SOURCE_H * scale) / 2;
|
|
const x = Math.round((ev.clientX - r.left - offsetX) / scale);
|
|
const y = Math.round((ev.clientY - r.top - offsetY) / scale);
|
|
return {
|
|
x: Math.max(0, Math.min(ZONE_SOURCE_W, x)),
|
|
y: Math.max(0, Math.min(ZONE_SOURCE_H, y))
|
|
};
|
|
}
|
|
|
|
function svgEl(name, attrs) {
|
|
const node = document.createElementNS(SVG_NS, name);
|
|
Object.keys(attrs).forEach(function (key) {
|
|
node.setAttribute(key, attrs[key]);
|
|
});
|
|
return node;
|
|
}
|
|
|
|
function areaByKey(key) {
|
|
for (let i = 0; i < AREAS.length; i++) {
|
|
if (AREAS[i].key === key) return AREAS[i];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function makeHandle(id, color, cx, cy, radius, onDown) {
|
|
const node = svgEl('circle', {
|
|
id: id, cx: cx, cy: cy, r: radius,
|
|
fill: color, stroke: '#ffffff', 'stroke-width': '2',
|
|
'vector-effect': 'non-scaling-stroke'
|
|
});
|
|
node.classList.add('zone-handle');
|
|
node.style.display = zoneEditMode ? '' : 'none';
|
|
node.style.pointerEvents = zoneEditMode ? 'all' : 'none';
|
|
node.addEventListener('mousedown', onDown);
|
|
return node;
|
|
}
|
|
|
|
function renderZones() {
|
|
const layer = $('zoneLayer');
|
|
layer.innerHTML = '';
|
|
syncVisibilityUI();
|
|
if (!zonesData) return;
|
|
|
|
AREAS.forEach(function (area) {
|
|
const pts = zonesData[area.key];
|
|
if (!zoneVisibility[area.vis] || !Array.isArray(pts) || pts.length < 3) return;
|
|
layer.appendChild(svgEl('polygon', {
|
|
id: 'zp-' + area.vis,
|
|
points: pts.map(function (p) { return p[0] + ',' + p[1]; }).join(' '),
|
|
fill: area.color + '22',
|
|
stroke: area.color,
|
|
'stroke-width': '3',
|
|
'vector-effect': 'non-scaling-stroke'
|
|
}));
|
|
pts.forEach(function (pt, i) {
|
|
layer.appendChild(makeHandle(
|
|
'zh-' + area.vis + '-' + i, area.color, pt[0], pt[1], 12,
|
|
function (ev) { startZoneDrag(ev, area.key, 'poly', i); }
|
|
));
|
|
});
|
|
});
|
|
|
|
const line = zonesData.counting_line;
|
|
if (zoneVisibility.counting_line && Array.isArray(line) && line.length === 2) {
|
|
const lineY = Math.round((line[0][1] + line[1][1]) / 2);
|
|
line[0][1] = lineY;
|
|
line[1][1] = lineY;
|
|
const xMin = Math.min(line[0][0], line[1][0]);
|
|
const xMax = Math.max(line[0][0], line[1][0]);
|
|
const bandTop = Math.max(0, lineY - ZONE_LINE_MARGIN);
|
|
const bandBottom = Math.min(ZONE_SOURCE_H, lineY + ZONE_LINE_MARGIN);
|
|
|
|
const band = svgEl('rect', {
|
|
id: 'zb-counting', x: xMin, y: bandTop,
|
|
width: Math.max(1, xMax - xMin), height: Math.max(1, bandBottom - bandTop),
|
|
fill: 'rgba(22, 163, 74, 0.16)', stroke: '#16a34a',
|
|
'stroke-width': '1.5', 'stroke-dasharray': '6,4',
|
|
'vector-effect': 'non-scaling-stroke'
|
|
});
|
|
band.style.pointerEvents = 'none';
|
|
layer.appendChild(band);
|
|
|
|
const stroke = svgEl('line', {
|
|
id: 'zl-counting', x1: line[0][0], y1: lineY, x2: line[1][0], y2: lineY,
|
|
stroke: '#16a34a', 'stroke-width': '4', 'vector-effect': 'non-scaling-stroke'
|
|
});
|
|
stroke.style.pointerEvents = 'none';
|
|
layer.appendChild(stroke);
|
|
|
|
line.forEach(function (pt, i) {
|
|
layer.appendChild(makeHandle(
|
|
'zh-line-' + i, '#16a34a', pt[0], lineY, 13,
|
|
function (ev) { startZoneDrag(ev, 'counting_line', 'line', i); }
|
|
));
|
|
});
|
|
}
|
|
}
|
|
|
|
function startZoneDrag(ev, key, kind, index) {
|
|
if (!zoneEditMode) return;
|
|
ev.preventDefault();
|
|
ev.stopPropagation();
|
|
zoneDrag = { key: key, kind: kind, index: index };
|
|
document.addEventListener('mousemove', onZoneDrag);
|
|
document.addEventListener('mouseup', endZoneDrag);
|
|
}
|
|
|
|
function onZoneDrag(ev) {
|
|
if (!zoneDrag) return;
|
|
const pt = sourcePoint(ev);
|
|
const info = zoneDrag;
|
|
if (info.kind === 'poly') {
|
|
zonesData[info.key][info.index] = [pt.x, pt.y];
|
|
const area = areaByKey(info.key);
|
|
const poly = document.getElementById('zp-' + area.vis);
|
|
if (poly) {
|
|
poly.setAttribute('points', zonesData[info.key].map(function (p) {
|
|
return p[0] + ',' + p[1];
|
|
}).join(' '));
|
|
}
|
|
const handle = document.getElementById('zh-' + area.vis + '-' + info.index);
|
|
if (handle) {
|
|
handle.setAttribute('cx', pt.x);
|
|
handle.setAttribute('cy', pt.y);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// The count line stays horizontal: the dragged endpoint sets x, and
|
|
// both endpoints take the dragged y.
|
|
const line = zonesData.counting_line;
|
|
line[info.index][0] = pt.x;
|
|
line[0][1] = pt.y;
|
|
line[1][1] = pt.y;
|
|
const x0 = line[0][0];
|
|
const x1 = line[1][0];
|
|
const bandTop = Math.max(0, pt.y - ZONE_LINE_MARGIN);
|
|
const bandBottom = Math.min(ZONE_SOURCE_H, pt.y + ZONE_LINE_MARGIN);
|
|
const band = document.getElementById('zb-counting');
|
|
if (band) {
|
|
band.setAttribute('x', Math.min(x0, x1));
|
|
band.setAttribute('y', bandTop);
|
|
band.setAttribute('width', Math.max(1, Math.abs(x1 - x0)));
|
|
band.setAttribute('height', Math.max(1, bandBottom - bandTop));
|
|
}
|
|
const stroke = document.getElementById('zl-counting');
|
|
if (stroke) {
|
|
stroke.setAttribute('x1', x0);
|
|
stroke.setAttribute('y1', pt.y);
|
|
stroke.setAttribute('x2', x1);
|
|
stroke.setAttribute('y2', pt.y);
|
|
}
|
|
['zh-line-0', 'zh-line-1'].forEach(function (id, i) {
|
|
const handle = document.getElementById(id);
|
|
if (!handle) return;
|
|
handle.setAttribute('cx', i === 0 ? x0 : x1);
|
|
handle.setAttribute('cy', pt.y);
|
|
});
|
|
}
|
|
|
|
function endZoneDrag() {
|
|
zoneDrag = null;
|
|
document.removeEventListener('mousemove', onZoneDrag);
|
|
document.removeEventListener('mouseup', endZoneDrag);
|
|
}
|
|
|
|
function syncVisibilityUI() {
|
|
const buttons = {
|
|
counting: 'zone-vis-counting',
|
|
truck: 'zone-vis-truck',
|
|
counting_line: 'zone-vis-line'
|
|
};
|
|
Object.keys(buttons).forEach(function (key) {
|
|
const btn = $(buttons[key]);
|
|
if (!btn) return;
|
|
const visible = Boolean(zoneVisibility[key]);
|
|
btn.setAttribute('aria-pressed', visible ? 'true' : 'false');
|
|
});
|
|
const all = Object.keys(buttons).every(function (key) { return zoneVisibility[key]; });
|
|
const global = $('zone-vis-all-btn');
|
|
if (global) {
|
|
global.setAttribute('aria-pressed', all ? 'true' : 'false');
|
|
global.textContent = all ? 'Hide All ROIs' : 'Show All ROIs';
|
|
}
|
|
}
|
|
|
|
function toggleZoneEdit() {
|
|
zoneEditMode = !zoneEditMode;
|
|
if (zoneEditMode && !zoneVisibility.counting && !zoneVisibility.truck
|
|
&& !zoneVisibility.counting_line) {
|
|
zoneVisibility.counting = true;
|
|
zoneVisibility.truck = true;
|
|
zoneVisibility.counting_line = true;
|
|
}
|
|
$('zone-edit-btn').textContent = zoneEditMode ? 'Lock Edit' : 'Unlock Edit';
|
|
setStatus(zoneEditMode ? 'Editing: drag handles, then Save' : 'Locked');
|
|
renderZones();
|
|
}
|
|
|
|
function toggleZoneItemVisibility(key) {
|
|
if (!(key in zoneVisibility)) return;
|
|
zoneVisibility[key] = !zoneVisibility[key];
|
|
renderZones();
|
|
}
|
|
|
|
function toggleAllZoneVisibility() {
|
|
const all = Object.keys(zoneVisibility).every(function (key) { return zoneVisibility[key]; });
|
|
Object.keys(zoneVisibility).forEach(function (key) {
|
|
zoneVisibility[key] = !all;
|
|
});
|
|
renderZones();
|
|
}
|
|
|
|
function buildCurrentPresetPayload() {
|
|
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]
|
|
};
|
|
if (Array.isArray(zonesData.palet) && zonesData.palet.length >= 3) {
|
|
obj.palet = zonesData.palet;
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
function isFiniteNumber(v) {
|
|
return typeof v === 'number' && Number.isFinite(v);
|
|
}
|
|
|
|
function resolveInputSpace(obj) {
|
|
if ('coord_space' in obj && obj.coord_space !== undefined && obj.coord_space !== null) {
|
|
if (typeof obj.coord_space !== 'string' || !obj.coord_space.trim()) {
|
|
throw new Error('coord_space must be formatted like "1920x1080"');
|
|
}
|
|
const parts = obj.coord_space.trim().toLowerCase().split('x');
|
|
if (parts.length === 2) {
|
|
const w = Number(parts[0]);
|
|
const h = Number(parts[1]);
|
|
if (parts[0].trim() && parts[1].trim() && Number.isFinite(w) && Number.isFinite(h) && w > 0 && h > 0) {
|
|
return [w, h];
|
|
}
|
|
}
|
|
throw new Error('coord_space must be formatted like "1920x1080"');
|
|
}
|
|
if ('reference_resolution' in obj && obj.reference_resolution !== undefined && obj.reference_resolution !== null) {
|
|
if (!Array.isArray(obj.reference_resolution) || obj.reference_resolution.length !== 2) {
|
|
throw new Error('reference_resolution must be [width, height]');
|
|
}
|
|
const rw = obj.reference_resolution[0];
|
|
const rh = obj.reference_resolution[1];
|
|
if (!isFiniteNumber(rw) || !isFiniteNumber(rh) || rw <= 0 || rh <= 0) {
|
|
throw new Error('reference_resolution values must be positive numbers');
|
|
}
|
|
return [rw, rh];
|
|
}
|
|
return [ZONE_SOURCE_W, ZONE_SOURCE_H];
|
|
}
|
|
|
|
function scalePresetToSource(preset, srcW, srcH) {
|
|
const sx = srcW ? (ZONE_SOURCE_W / srcW) : 1;
|
|
const sy = srcH ? (ZONE_SOURCE_H / srcH) : 1;
|
|
const scalePt = function (p) {
|
|
return (sx === 1 && sy === 1)
|
|
? [p[0], p[1]]
|
|
: [Math.round(p[0] * sx), Math.round(p[1] * sy)];
|
|
};
|
|
const scaleVal = function (v, sc) {
|
|
return sc === 1 ? v : Math.round(v * sc);
|
|
};
|
|
const line = (preset && preset.count_line) || {};
|
|
const out = {
|
|
truck_area: Array.isArray(preset.truck_area) ? preset.truck_area.map(scalePt) : [],
|
|
counting_area: Array.isArray(preset.counting_area) ? preset.counting_area.map(scalePt) : [],
|
|
count_line: {
|
|
x_start: scaleVal(isFiniteNumber(line.x_start) ? line.x_start : 0, sx),
|
|
x_end: scaleVal(isFiniteNumber(line.x_end) ? line.x_end : 0, sx),
|
|
y: scaleVal(isFiniteNumber(line.y) ? line.y : 0, sy)
|
|
},
|
|
reference_resolution: [ZONE_SOURCE_W, ZONE_SOURCE_H]
|
|
};
|
|
if (Array.isArray(preset.palet)) {
|
|
out.palet = preset.palet.map(scalePt);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function applyPreset(preset) {
|
|
const raw = preset || {};
|
|
let res = [ZONE_SOURCE_W, ZONE_SOURCE_H];
|
|
try {
|
|
res = resolveInputSpace(raw);
|
|
} catch (e) {
|
|
res = [ZONE_SOURCE_W, ZONE_SOURCE_H];
|
|
}
|
|
const normalized = scalePresetToSource(raw, res[0], res[1]);
|
|
const line = normalized.count_line;
|
|
zonesData = {
|
|
truck_area: normalized.truck_area,
|
|
counting_area: normalized.counting_area,
|
|
palet: Array.isArray(normalized.palet) ? normalized.palet : null,
|
|
counting_line: [
|
|
[line.x_start, line.y],
|
|
[line.x_end, line.y]
|
|
]
|
|
};
|
|
renderZones();
|
|
}
|
|
|
|
async function loadPreset(name) {
|
|
currentPreset = name;
|
|
try {
|
|
const res = await fetch('/api/zones/' + encodeURIComponent(name));
|
|
const data = await res.json();
|
|
if (!res.ok) {
|
|
setStatus('Load failed: ' + (data.error || res.status));
|
|
return;
|
|
}
|
|
applyPreset(data.preset || {});
|
|
setStatus(zoneEditMode ? 'Editing: drag handles, then Save' : 'Locked');
|
|
} catch (err) {
|
|
setStatus('Load failed');
|
|
}
|
|
}
|
|
|
|
async function saveZones() {
|
|
if (!zonesData || !currentPreset) {
|
|
setStatus('Nothing to save');
|
|
return;
|
|
}
|
|
const payload = { preset: buildCurrentPresetPayload() };
|
|
try {
|
|
const res = await fetch('/api/zones/' + encodeURIComponent(currentPreset), {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload)
|
|
});
|
|
const data = await res.json().catch(function () { return {}; });
|
|
if (!res.ok) {
|
|
setStatus('Save failed: ' + (data.error || res.status));
|
|
return;
|
|
}
|
|
await loadPreset(currentPreset);
|
|
setStatus('Saved to zones.json');
|
|
} catch (err) {
|
|
setStatus('Save failed');
|
|
}
|
|
}
|
|
|
|
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 res = resolveInputSpace(obj);
|
|
const truck = ('truck_area' in obj) ? obj.truck_area : obj.truck;
|
|
const counting = ('counting_area' in obj) ? obj.counting_area : obj.counting;
|
|
if (!Array.isArray(truck) || truck.length !== 4) throw new Error('truck_area/truck must be an array of 4 [x,y] points');
|
|
if (!Array.isArray(counting) || counting.length < 3) throw new Error('counting_area/counting must be an array of at least 3 [x,y] points');
|
|
|
|
let x0, x1, lineY;
|
|
if (obj.count_line && typeof obj.count_line === 'object' && !Array.isArray(obj.count_line)) {
|
|
x0 = obj.count_line.x_start;
|
|
x1 = obj.count_line.x_end;
|
|
lineY = obj.count_line.y;
|
|
} else if (Array.isArray(obj.counting_line) && obj.counting_line.length === 2
|
|
&& Array.isArray(obj.counting_line[0]) && obj.counting_line[0].length === 2
|
|
&& Array.isArray(obj.counting_line[1]) && obj.counting_line[1].length === 2) {
|
|
x0 = obj.counting_line[0][0];
|
|
x1 = obj.counting_line[1][0];
|
|
lineY = (obj.counting_line[0][1] + obj.counting_line[1][1]) / 2;
|
|
} else if ('line_y' in obj || 'line_x_start' in obj || 'line_x_end' in obj) {
|
|
x0 = obj.line_x_start;
|
|
x1 = obj.line_x_end;
|
|
lineY = obj.line_y;
|
|
} else {
|
|
throw new Error('count_line or line_y/line_x_start/line_x_end is required');
|
|
}
|
|
if (!isFiniteNumber(x0) || !isFiniteNumber(x1) || !isFiniteNumber(lineY)) {
|
|
throw new Error('count_line values must be finite numbers');
|
|
}
|
|
|
|
const checkPoly = function (pts, label) {
|
|
for (let i = 0; i < pts.length; i++) {
|
|
if (!Array.isArray(pts[i]) || pts[i].length !== 2) throw new Error(label + ' points must be [x,y]');
|
|
if (!pts[i].every(isFiniteNumber)) {
|
|
throw new Error(label + ' values must be finite numbers');
|
|
}
|
|
}
|
|
};
|
|
checkPoly(truck, 'truck_area');
|
|
checkPoly(counting, 'counting_area');
|
|
if (obj.palet !== undefined) {
|
|
if (!Array.isArray(obj.palet) || obj.palet.length < 3) throw new Error('palet must be at least 3 [x,y] points');
|
|
checkPoly(obj.palet, 'palet');
|
|
}
|
|
|
|
const unscaled = {
|
|
truck_area: truck,
|
|
counting_area: counting,
|
|
count_line: { x_start: Math.min(x0, x1), x_end: Math.max(x0, x1), y: Math.round(lineY) }
|
|
};
|
|
if (obj.palet !== undefined) unscaled.palet = obj.palet;
|
|
const parsed = scalePresetToSource(unscaled, res[0], res[1]);
|
|
parsed._source_resolution = res;
|
|
return parsed;
|
|
}
|
|
|
|
function importJson() {
|
|
const ta = $('zone-json');
|
|
if (!ta) return;
|
|
try {
|
|
const preset = parseZoneJson(ta.value);
|
|
const src = preset._source_resolution;
|
|
delete preset._source_resolution;
|
|
applyPreset(preset);
|
|
if (src && (src[0] !== ZONE_SOURCE_W || src[1] !== ZONE_SOURCE_H)) {
|
|
setStatus('Imported (scaled ' + src[0] + 'x' + src[1] + ' -> 1920x1080) — drag handles or Save');
|
|
} else {
|
|
setStatus('Imported — drag handles or Save');
|
|
}
|
|
} catch (err) {
|
|
setStatus('Import failed: ' + err.message);
|
|
}
|
|
}
|
|
|
|
function exportJson() {
|
|
if (!zonesData) { setStatus('Nothing to export'); return; }
|
|
const obj = buildCurrentPresetPayload();
|
|
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 obj = buildCurrentPresetPayload();
|
|
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 {
|
|
const res = await fetch('/api/zones/' + encodeURIComponent(currentPreset) + '/reset', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
const data = await res.json().catch(function () { return {}; });
|
|
if (!res.ok) {
|
|
setStatus('Reset failed: ' + (data.error || res.status));
|
|
return;
|
|
}
|
|
applyPreset(data.preset || {});
|
|
setStatus('Reset to the shipped default');
|
|
} catch (err) {
|
|
setStatus('Reset failed');
|
|
}
|
|
}
|
|
|
|
function loadFrame() {
|
|
const video = $('video-select').value;
|
|
if (!video) return;
|
|
const t = parseFloat($('time-slider').value) || 0;
|
|
$('zone-backdrop').src = '/api/frame?video=' + encodeURIComponent(video) + '&t=' + t;
|
|
}
|
|
|
|
async function loadVideos() {
|
|
const select = $('video-select');
|
|
try {
|
|
const res = await fetch('/api/videos');
|
|
const videos = await res.json();
|
|
select.innerHTML = '';
|
|
videos.forEach(function (video) {
|
|
const option = document.createElement('option');
|
|
option.value = video.filename;
|
|
option.textContent = video.filename;
|
|
select.appendChild(option);
|
|
});
|
|
if (!videos.length) {
|
|
setStatus('No videos uploaded yet');
|
|
return;
|
|
}
|
|
loadFrame();
|
|
} catch (err) {
|
|
setStatus('Could not load the video list');
|
|
}
|
|
}
|
|
|
|
async function loadPresets() {
|
|
const select = $('preset-select');
|
|
try {
|
|
const res = await fetch('/api/zones');
|
|
const presets = await res.json();
|
|
select.innerHTML = '';
|
|
presets.forEach(function (preset) {
|
|
const option = document.createElement('option');
|
|
option.value = preset.name;
|
|
option.textContent = preset.name;
|
|
select.appendChild(option);
|
|
});
|
|
if (!presets.length) {
|
|
setStatus('No zone presets found');
|
|
return;
|
|
}
|
|
await loadPreset(presets[0].name);
|
|
} catch (err) {
|
|
setStatus('Could not load the zone presets');
|
|
}
|
|
}
|
|
|
|
function init() {
|
|
$('zone-edit-btn').addEventListener('click', toggleZoneEdit);
|
|
$('zone-save-btn').addEventListener('click', saveZones);
|
|
$('zone-reload-btn').addEventListener('click', function () {
|
|
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');
|
|
});
|
|
$('zone-vis-truck').addEventListener('click', function () {
|
|
toggleZoneItemVisibility('truck');
|
|
});
|
|
$('zone-vis-line').addEventListener('click', function () {
|
|
toggleZoneItemVisibility('counting_line');
|
|
});
|
|
$('preset-select').addEventListener('change', function () {
|
|
loadPreset(this.value);
|
|
});
|
|
$('video-select').addEventListener('change', loadFrame);
|
|
$('zone-backdrop').addEventListener('error', function () {
|
|
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 () {
|
|
$('time-label').textContent = (parseFloat(this.value) || 0).toFixed(1) + ' s';
|
|
if (frameTimer) clearTimeout(frameTimer);
|
|
frameTimer = setTimeout(loadFrame, 150);
|
|
});
|
|
|
|
renderZones();
|
|
loadPresets();
|
|
loadVideos();
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
} else {
|
|
init();
|
|
}
|
|
})();
|