yolo-helpers
Version:
Helper functions to use models converted from YOLO in browser and Node.js
43 lines (42 loc) • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.drawBox = drawBox;
function drawBox(args) {
let { context, label } = args;
let lineWidth = args.lineWidth ?? 5;
let borderColor = args.borderColor ?? 'red';
let left = args.x - args.width / 2;
let top = args.y - args.height / 2;
context.lineWidth = lineWidth;
context.strokeStyle = borderColor;
context.strokeRect(left, top, args.width, args.height);
if (label) {
let textColor = label.fontColor ?? 'white';
let backgroundColor = label.backgroundColor ?? 'transparent';
let font = label.font ?? 'normal 900 14px Arial, sans-serif';
let text = label.text;
context.font = font;
let metrics = context.measureText(text);
let width = metrics.width;
let height = metrics.fontBoundingBoxAscent || metrics.actualBoundingBoxAscent || 25;
if (top - lineWidth - height >= 0) {
// draw background of text label
if (backgroundColor !== 'transparent') {
context.fillStyle = backgroundColor;
context.fillRect(left, top - lineWidth - height, width, height + lineWidth);
}
}
else {
top += height + lineWidth;
left += lineWidth;
// draw background of text label
if (backgroundColor !== 'transparent') {
context.fillStyle = backgroundColor;
context.fillRect(left, top - lineWidth - height, width, height + lineWidth);
}
}
// draw the text label
context.fillStyle = textColor;
context.fillText(text, left, top - lineWidth);
}
}