Spaces:
Running
Running
File size: 2,032 Bytes
e176226 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import { describe, it, expect } from 'vitest';
import { normalizeBox, scoreZones, decide, smooth, sampleTimes, fitSize } from '../src/lib.js';
const th = { person: 0.2, dish: 0.15 };
describe('lib', () => {
it('normalizeBox handles negative drags and clamps', () => {
expect(normalizeBox([0.5, 0.5, -0.2, -0.3]).map((v) => +v.toFixed(3))).toEqual([0.3, 0.2, 0.2, 0.3]);
expect(normalizeBox([0.9, 0.9, 0.5, 0.5]).map((v) => +v.toFixed(3))).toEqual([0.9, 0.9, 0.1, 0.1]);
});
it('scoreZones uses detection centers and max score', () => {
const dets = [
{ label: 'a person', score: 0.3, box: { xmin: 10, ymin: 10, xmax: 30, ymax: 30 } },
{ label: 'a plate', score: 0.4, box: { xmin: 60, ymin: 60, xmax: 80, ymax: 80 } },
{ label: 'a plate', score: 0.2, box: { xmin: 65, ymin: 65, xmax: 75, ymax: 75 } },
];
const zones = [{ name: 'A', box: [0, 0, 0.5, 0.5] }, { name: 'B', box: [0.5, 0.5, 0.5, 0.5] }];
const r = scoreZones(dets, zones, 100, 100, ['a person'], ['a plate']);
expect(r[0]).toMatchObject({ person: 0.3, dish: 0 });
expect(r[1]).toMatchObject({ person: 0, dish: 0.4 });
expect(r[1].dishes.length).toBe(2);
});
it('decide precedence', () => {
expect(decide({ person: 0.5, dish: 0.9 }, th)).toBe('ocupada');
expect(decide({ person: 0.1, dish: 0.3 }, th)).toBe('limpiar');
expect(decide({ person: 0.1, dish: 0.1 }, th)).toBe('libre');
});
it('smooth needs k equal readings', () => {
let s = smooth(null, 'ocupada');
s = smooth(s, 'limpiar'); expect(s.shown).toBe('ocupada');
s = smooth(s, 'limpiar'); expect(s.shown).toBe('limpiar'); expect(s.changed).toBe(true);
s = smooth(s, 'libre'); s = smooth(s, 'limpiar'); expect(s.shown).toBe('limpiar');
});
it('sampleTimes and fitSize', () => {
expect(sampleTimes(5, 2, 10)).toEqual([0, 2, 4]);
expect(sampleTimes(100, 1, 3)).toEqual([0, 1, 2]);
expect(fitSize(2560, 1440, 1280)).toEqual([1280, 720]);
expect(fitSize(800, 600, 1280)).toEqual([800, 600]);
});
});
|