2022-04-11 17:45:24 +02:00
|
|
|
import { mergeDeep } from '../util/util';
|
2022-10-18 16:18:40 +02:00
|
|
|
import { getCanvasContext, rect, replace, labels } from './primitives';
|
2022-04-11 17:45:24 +02:00
|
|
|
import { options } from './options';
|
|
|
|
import type { ObjectResult } from '../result';
|
|
|
|
import type { AnyCanvas, DrawOptions } from '../exports';
|
|
|
|
|
|
|
|
/** draw detected objects */
|
2022-08-21 21:23:03 +02:00
|
|
|
export function object(inCanvas: AnyCanvas, result: ObjectResult[], drawOptions?: Partial<DrawOptions>) {
|
|
|
|
const localOptions: DrawOptions = mergeDeep(options, drawOptions);
|
2022-04-11 17:45:24 +02:00
|
|
|
if (!result || !inCanvas) return;
|
|
|
|
const ctx = getCanvasContext(inCanvas);
|
|
|
|
if (!ctx) return;
|
|
|
|
ctx.lineJoin = 'round';
|
|
|
|
ctx.font = localOptions.font;
|
|
|
|
for (const h of result) {
|
|
|
|
if (localOptions.drawBoxes) {
|
|
|
|
ctx.strokeStyle = localOptions.color;
|
|
|
|
ctx.fillStyle = localOptions.color;
|
|
|
|
rect(ctx, h.box[0], h.box[1], h.box[2], h.box[3], localOptions);
|
2022-10-18 16:18:40 +02:00
|
|
|
if (localOptions.drawLabels && (localOptions.objectLabels?.length > 0)) {
|
|
|
|
let l = localOptions.objectLabels.slice();
|
|
|
|
l = replace(l, '[label]', h.label);
|
|
|
|
l = replace(l, '[score]', 100 * h.score);
|
|
|
|
labels(ctx, l, h.box[0], h.box[1], localOptions);
|
2022-04-11 17:45:24 +02:00
|
|
|
}
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|