To visualize face detection results, you must first prepare an overlay canvas that matches the dimensions of your input image or video. Use faceapi.matchDimensions to sync the canvas size with the input.
Once detections are made, use faceapi.resizeResults to scale the detection coordinates to match the displayed dimensions of the canvas before drawing.
Common drawing tasks include:
- Bounding Boxes: Use
faceapi.draw.drawDetections(canvas, resizedResults). - Landmarks: Use
faceapi.draw.drawFaceLandmarks(canvas, resizedResults). - Expressions: Use
faceapi.draw.drawFaceExpressions(canvas, resizedResults, minProbability) where minProbability is the threshold for displaying an expression.
const displaySize = { width: input.width, height: input.height }
// resize the overlay canvas to the input dimensions
const canvas = document.getElementById('overlay')
faceapi.matchDimensions(canvas, displaySize)
/* Example: Displaying detections and landmarks */
const detectionsWithLandmarks = await faceapi
.detectAllFaces(input)
.withFaceLandmarks()
// resize the detected boxes and landmarks to match the display size
const resizedResults = faceapi.resizeResults(detectionsWithLandmarks, displaySize)
// draw to canvas
faceapi.draw.drawDetections(canvas, resizedResults)
faceapi.draw.drawFaceLandmarks(canvas, resizedResults)