My code :
const canvas = document.getElementById("output");
const ctx = canvas.getContext("2d");
const model = faceDetection.SupportedModels.MediaPipeFaceDetector;
const detectorConfig = {
runtime: 'tfjs',
};
faceDetection.createDetector(model, detectorConfig).then((e) => {
const estimationConfig = {flipHorizontal: false};
let image = new Image();
image.src = "imgs/1.png"
image.onload = () => {
canvas.width = image.width;
canvas.height = image.height;
e.estimateFaces(image, estimationConfig).then((faces) => {
console.log("faces ", faces)
dispatchEvent(new CustomEvent('run-visual', {detail: {
faces: faces,
image: image
}
}))
});
}
});
addEventListener('run-visual', (f) => {
console.log('DRAW', f.detail.faces[0].box.xMin )
ctx.drawImage(f.detail.image, f.detail.faces[0].box.xMin,
f.detail.faces[0].box.yMin , f.detail.faces[0].box.width, f.detail.faces[0].box.height, 0 , 0 , 200, 200)
})
My results:
{
"keypoints": [
{
"x": 465.3794479370117,
"y": 490.12847900390625,
"name": "rightEye"
},
{
"x": 623.3193206787109,
"y": 478.1827545166016,
"name": "leftEye"
},
{
"x": 614.3927001953125,
"y": 578.9810943603516,
"name": "noseTip"
},
{
"x": 597.7729034423828,
"y": 668.6562347412109,
"name": "mouthCenter"
},
{
"x": 273.1340026855469,
"y": 545.9391784667969,
"name": "rightEarTragion"
},
{
"x": 624.7901916503906,
"y": 505.5956268310547,
"name": "leftEarTragion"
}
],
"box": {
"xMin": 279.776611328125,
"xMax": 683.3624267578125,
"yMin": 378.20487976074224,
"yMax": 781.7937469482422,
"width": 403.5858154296875,
"height": 403.58886718749994
}
}
My html index
<!-- Copyright 2022 Google LLC. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1.0, user-scalable=no">
<style>
body {
margin: 0;
}
#stats {
position: relative;
width: 100%;
height: 80px;
}
#main {
position: relative;
margin: 0;
}
#canvas-wrapper {
position: relative;
border: solid red 1px;
}
</style>
</head>
<body>
<div id="main">
<div class="container">
<div class="canvas-wrapper">
OUTPUT:
<canvas id="output"></canvas>
<!-- <video id="video" playsinline style="
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
visibility: hidden;
width: auto;
height: auto;
">
</video> -->
</div>
</div>
</div>
</div>
</body>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/@mediapipe/face_detection"></script>
<script defer src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core"></script>
<script defer src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-converter"></script>
<!-- You must explicitly require a TF.js backend if you're not using the TF.js union bundle. -->
<script defer src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-webgl"></script>
<script defer src="https://cdn.jsdelivr.net/npm/@tensorflow-models/face-detection"></script>
<script defer src="index.js"></script>
</html>
Why for any picture i always got faces array with one item – one face detected…