Skip to content

Commit eb6c7a1

Browse files
added description of new drawing api to README
1 parent 5a45ad4 commit eb6c7a1

File tree

1 file changed

+87
-23
lines changed

1 file changed

+87
-23
lines changed

README.md

Lines changed: 87 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -441,46 +441,110 @@ const faceMatcher = new faceapi.FaceMatcher(labeledDescriptors)
441441

442442
## Displaying Detection Results
443443

444-
Drawing the detected faces into a canvas:
444+
Preparing the overlay canvas:
445445

446446
``` javascript
447-
const detections = await faceapi.detectAllFaces(input)
448-
449-
// resize the detected boxes in case your displayed image has a different size then the original
450-
const detectionsForSize = faceapi.resizeResults(detections, { width: input.width, height: input.height })
451-
// draw them into a canvas
447+
const displaySize = { width: input.width, height: input.height }
448+
// resize the overlay canvas to the input dimensions
452449
const canvas = document.getElementById('overlay')
453-
canvas.width = input.width
454-
canvas.height = input.height
455-
faceapi.drawDetection(canvas, detectionsForSize, { withScore: true })
450+
faceapi.matchDimensions(canvas, displaySize)
456451
```
457452

458-
Drawing face landmarks into a canvas:
453+
face-api.js predefines some highlevel drawing functions, which you can utilize:
459454

460455
``` javascript
456+
/* Display detected face bounding boxes */
457+
const detections = await faceapi.detectAllFaces(input)
458+
// resize the detected boxes in case your displayed image has a different size than the original
459+
const resizedDetections = faceapi.resizeResults(detections, displaySize)
460+
// draw detections into the canvas
461+
faceapi.draw.drawDetections(canvas, resizedDetections)
462+
463+
/* Display face landmarks */
461464
const detectionsWithLandmarks = await faceapi
462465
.detectAllFaces(input)
463466
.withFaceLandmarks()
467+
// resize the detected boxes and landmarks in case your displayed image has a different size than the original
468+
const resizedResults = faceapi.resizeResults(detectionsWithLandmarks, displaySize)
469+
// draw detections into the canvas
470+
faceapi.draw.drawDetections(canvas, resizedDetections)
471+
// draw the landmarks into the canvas
472+
faceapi.draw.drawFaceLandmarks(canvas, resizedResults)
464473

465-
// resize the detected boxes and landmarks in case your displayed image has a different size then the original
466-
const detectionsWithLandmarksForSize = faceapi.resizeResults(detectionsWithLandmarks, { width: input.width, height: input.height })
467-
// draw them into a canvas
468-
const canvas = document.getElementById('overlay')
469-
canvas.width = input.width
470-
canvas.height = input.height
471-
faceapi.drawLandmarks(canvas, detectionsWithLandmarks, { drawLines: true })
474+
475+
/* Display face expression results */
476+
const detectionsWithExpressions = await faceapi
477+
.detectAllFaces(input)
478+
.withFaceLandmarks()
479+
.withFaceExpressions()
480+
// resize the detected boxes and landmarks in case your displayed image has a different size than the original
481+
const resizedResults = faceapi.resizeResults(detectionsWithExpressions, displaySize)
482+
// draw detections into the canvas
483+
faceapi.draw.drawDetections(canvas, resizedDetections)
484+
// draw a textbox displaying the face expressions with minimum probability into the canvas
485+
const minProbability = 0.05
486+
faceapi.draw.drawFaceExpressions(canvas, resizedResults, minProbability)
487+
```
488+
489+
You can also draw boxes with custom text ([DrawBox](https://github.com/justadudewhohacks/tfjs-image-recognition-base/blob/master/src/draw/DrawBox.ts)):
490+
491+
``` javascript
492+
const box = { x: 50, y: 50, width: 100, height: 100 }
493+
// see DrawBoxOptions below
494+
const drawOptions = {
495+
label: 'Hello I am a box!',
496+
lineWidth: 2
497+
}
498+
const drawBox = new faceapi.draw.DrawBox(box, drawOptions)
499+
drawBox.draw(document.getElementById('myCanvas'))
472500
```
473501

474-
Finally you can also draw boxes with custom text:
502+
DrawBox drawing options:
475503

476504
``` javascript
477-
const boxesWithText = [
478-
new faceapi.BoxWithText(new faceapi.Rect(x, y, width, height), text))
479-
new faceapi.BoxWithText(new faceapi.Rect(0, 0, 50, 50), 'some text'))
505+
export interface IDrawBoxOptions {
506+
boxColor?: string
507+
lineWidth?: number
508+
drawLabelOptions?: IDrawTextFieldOptions
509+
label?: string
510+
}
511+
```
512+
513+
Finally you can draw custom text fields ([DrawTextField](https://github.com/justadudewhohacks/tfjs-image-recognition-base/blob/master/src/draw/DrawTextField.ts)):
514+
515+
``` javascript
516+
const text = [
517+
'This is a textline!',
518+
'This is another textline!'
480519
]
520+
const anchor = { x: 200, y: 200 }
521+
// see DrawTextField below
522+
const drawOptions = {
523+
anchorPosition: 'TOP_LEFT',
524+
backgroundColor: 'rgba(0, 0, 0, 0.5)'
525+
}
526+
const drawBox = new faceapi.draw.DrawTextField(text, anchor, drawOptions)
527+
drawBox.draw(document.getElementById('myCanvas'))
528+
```
481529

482-
const canvas = document.getElementById('overlay')
483-
faceapi.drawDetection(canvas, boxesWithText)
530+
DrawTextField drawing options:
531+
532+
``` javascript
533+
export interface IDrawTextFieldOptions {
534+
anchorPosition?: AnchorPosition
535+
backgroundColor?: string
536+
fontColor?: string
537+
fontSize?: number
538+
fontStyle?: string
539+
padding?: number
540+
}
541+
542+
export enum AnchorPosition {
543+
TOP_LEFT = 'TOP_LEFT',
544+
TOP_RIGHT = 'TOP_RIGHT',
545+
BOTTOM_LEFT = 'BOTTOM_LEFT',
546+
BOTTOM_RIGHT = 'BOTTOM_RIGHT'
547+
}
484548
```
485549

486550
<a name="getting-started-face-detection-options"></a>

0 commit comments

Comments
 (0)