File size: 4,595 Bytes
3216a1f | 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | /**
* canvas.js - Draws bounding boxes + labels on video frames.
*
* Renders face detection results (bounding boxes, name labels,
* confidence percentages) as an overlay on the recognition video feed.
*/
class FaceCanvas {
constructor(canvasElement, videoElement) {
this.canvas = canvasElement;
this.video = videoElement;
this.ctx = canvasElement.getContext('2d');
}
/**
* Sync canvas dimensions with the video element.
*/
resize() {
this.canvas.width = this.video.videoWidth || this.video.clientWidth;
this.canvas.height = this.video.videoHeight || this.video.clientHeight;
}
/**
* Clear the canvas.
*/
clear() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
/**
* Draw face detection results on the canvas.
* @param {Array} faces - Array of face result objects with box, label, confidence.
*/
drawResults(faces) {
this.resize();
this.clear();
if (!faces || faces.length === 0) return;
// Calculate scale factors (video may be displayed at different size)
const scaleX = this.canvas.width / (this.video.videoWidth || 1);
const scaleY = this.canvas.height / (this.video.videoHeight || 1);
for (const face of faces) {
const [x1, y1, x2, y2] = face.box;
const label = face.label;
const confidence = face.confidence;
const isRecognized = label !== 'Unknown';
// Mirror the x coordinates (video is mirrored with scaleX(-1))
const mirroredX1 = this.canvas.width - (x2 * scaleX);
const mirroredX2 = this.canvas.width - (x1 * scaleX);
const scaledY1 = y1 * scaleY;
const scaledY2 = y2 * scaleY;
const boxWidth = mirroredX2 - mirroredX1;
const boxHeight = scaledY2 - scaledY1;
// Colors
const boxColor = isRecognized ? '#22c55e' : '#ef4444';
const bgColor = isRecognized ? 'rgba(34,197,94,0.15)' : 'rgba(239,68,68,0.15)';
// Draw bounding box
this.ctx.strokeStyle = boxColor;
this.ctx.lineWidth = 2;
this.ctx.strokeRect(mirroredX1, scaledY1, boxWidth, boxHeight);
// Subtle fill
this.ctx.fillStyle = bgColor;
this.ctx.fillRect(mirroredX1, scaledY1, boxWidth, boxHeight);
// Corner accents
const cornerLen = Math.min(20, boxWidth * 0.25, boxHeight * 0.25);
this.ctx.strokeStyle = boxColor;
this.ctx.lineWidth = 3;
// Top-left
this.ctx.beginPath();
this.ctx.moveTo(mirroredX1, scaledY1 + cornerLen);
this.ctx.lineTo(mirroredX1, scaledY1);
this.ctx.lineTo(mirroredX1 + cornerLen, scaledY1);
this.ctx.stroke();
// Top-right
this.ctx.beginPath();
this.ctx.moveTo(mirroredX2 - cornerLen, scaledY1);
this.ctx.lineTo(mirroredX2, scaledY1);
this.ctx.lineTo(mirroredX2, scaledY1 + cornerLen);
this.ctx.stroke();
// Bottom-left
this.ctx.beginPath();
this.ctx.moveTo(mirroredX1, scaledY2 - cornerLen);
this.ctx.lineTo(mirroredX1, scaledY2);
this.ctx.lineTo(mirroredX1 + cornerLen, scaledY2);
this.ctx.stroke();
// Bottom-right
this.ctx.beginPath();
this.ctx.moveTo(mirroredX2 - cornerLen, scaledY2);
this.ctx.lineTo(mirroredX2, scaledY2);
this.ctx.lineTo(mirroredX2, scaledY2 - cornerLen);
this.ctx.stroke();
// Label background
const labelText = isRecognized
? `${label} ${(confidence * 100).toFixed(1)}%`
: `Unknown ${(confidence * 100).toFixed(1)}%`;
this.ctx.font = '600 13px Inter, sans-serif';
const textMetrics = this.ctx.measureText(labelText);
const labelPad = 8;
const labelHeight = 24;
const labelWidth = textMetrics.width + labelPad * 2;
this.ctx.fillStyle = 'rgba(0,0,0,0.75)';
this.ctx.beginPath();
this.ctx.roundRect(mirroredX1, scaledY1 - labelHeight - 4, labelWidth, labelHeight, 6);
this.ctx.fill();
// Label text
this.ctx.fillStyle = boxColor;
this.ctx.fillText(labelText, mirroredX1 + labelPad, scaledY1 - 10);
}
}
}
// Export globally
window.FaceCanvas = FaceCanvas;
|