@@ -441,46 +441,110 @@ const faceMatcher = new faceapi.FaceMatcher(labeledDescriptors)
441
441
442
442
## Displaying Detection Results
443
443
444
- Drawing the detected faces into a canvas:
444
+ Preparing the overlay canvas:
445
445
446
446
``` 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
452
449
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)
456
451
```
457
452
458
- Drawing face landmarks into a canvas :
453
+ face-api.js predefines some highlevel drawing functions, which you can utilize :
459
454
460
455
``` 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 */
461
464
const detectionsWithLandmarks = await faceapi
462
465
.detectAllFaces (input)
463
466
.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)
464
473
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' ))
472
500
```
473
501
474
- Finally you can also draw boxes with custom text :
502
+ DrawBox drawing options :
475
503
476
504
``` 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!'
480
519
]
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
+ ```
481
529
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
+ }
484
548
```
485
549
486
550
<a name =" getting-started-face-detection-options " ></a >
0 commit comments