@@ -20,20 +20,28 @@ video.addEventListener('play', () => {
2020 document . body . append ( canvas )
2121 const displaySize = { width : video . width , height : video . height }
2222 faceapi . matchDimensions ( canvas , displaySize )
23+
24+ let labeledFaceDescriptors
25+ ( async ( ) => {
26+ labeledFaceDescriptors = await loadLabeledImages ( )
27+ } ) ( )
28+
2329 setInterval ( async ( ) => {
2430 const detections = await faceapi . detectAllFaces ( video , new faceapi . TinyFaceDetectorOptions ( ) ) . withFaceLandmarks ( ) . withFaceDescriptors ( )
2531 const resizedDetections = faceapi . resizeResults ( detections , displaySize )
2632 canvas . getContext ( '2d' ) . clearRect ( 0 , 0 , canvas . width , canvas . height )
27- const labeledFaceDescriptors = await loadLabeledImages ( )
28- const faceMatcher = new faceapi . FaceMatcher ( labeledFaceDescriptors , 0.6 )
29- const results = resizedDetections . map ( d => faceMatcher . findBestMatch ( d . descriptor ) )
30- results . forEach ( ( result , i ) => {
31- const box = resizedDetections [ i ] . detection . box
32- const drawBox = new faceapi . draw . DrawBox ( box , { label : result . toString ( ) } )
33- drawBox . draw ( canvas )
34- } )
33+
34+ if ( labeledFaceDescriptors ) {
35+ const faceMatcher = new faceapi . FaceMatcher ( labeledFaceDescriptors , 0.6 )
36+ const results = resizedDetections . map ( d => faceMatcher . findBestMatch ( d . descriptor ) )
37+ results . forEach ( ( result , i ) => {
38+ const box = resizedDetections [ i ] . detection . box
39+ const drawBox = new faceapi . draw . DrawBox ( box , { label : result . toString ( ) } )
40+ drawBox . draw ( canvas )
41+ } )
42+ }
3543
36- } , 100 )
44+ } , 200 )
3745} )
3846
3947function loadLabeledImages ( ) {
0 commit comments