Commit 52858047 by vincent

use PredictAgeAndGenderTask in ageAndGenderRecognition example + align face in…

use PredictAgeAndGenderTask in ageAndGenderRecognition example + align face in faceExpressionRecognition example
parent 7c7cf2a8
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
<head> <head>
<script src="face-api.js"></script> <script src="face-api.js"></script>
<script src="js/commons.js"></script> <script src="js/commons.js"></script>
<script src="js/drawing.js"></script>
<script src="js/faceDetectionControls.js"></script> <script src="js/faceDetectionControls.js"></script>
<script src="js/imageSelectionControls.js"></script> <script src="js/imageSelectionControls.js"></script>
<link rel="stylesheet" href="styles.css"> <link rel="stylesheet" href="styles.css">
...@@ -139,8 +138,6 @@ ...@@ -139,8 +138,6 @@
<script> <script>
window.net = new faceapi.AgeGenderNet()
async function updateResults() { async function updateResults() {
if (!isFaceDetectionModelLoaded()) { if (!isFaceDetectionModelLoaded()) {
return return
...@@ -149,24 +146,37 @@ ...@@ -149,24 +146,37 @@
const inputImgEl = $('#inputImg').get(0) const inputImgEl = $('#inputImg').get(0)
const options = getFaceDetectorOptions() const options = getFaceDetectorOptions()
const result = await faceapi.detectSingleFace(inputImgEl, options) const results = await faceapi.detectAllFaces(inputImgEl, options)
// compute face landmarks to align faces for better accuracy
if (!result) return .withFaceLandmarks()
.withAgeAndGender()
const face = (await faceapi.extractFaces(inputImgEl, [result]))[0]
const { age, gender, genderProbability } = await window.net.predictAgeAndGender(face) const canvas = $('#overlay').get(0)
faceapi.matchDimensions(canvas, inputImgEl)
console.log('age', age)
console.log('gender', gender, genderProbability) const resizedResults = faceapi.resizeResults(results, inputImgEl)
faceapi.draw.drawDetections(canvas, resizedResults)
resizedResults.forEach(result => {
const { age, gender, genderProbability } = result
new faceapi.draw.DrawTextField(
[
`${faceapi.round(age, 0)} years`,
`${gender} (${faceapi.round(genderProbability)})`
],
result.detection.box.bottomLeft
).draw(canvas)
})
} }
async function run() { async function run() {
// load face detection and face expression recognition models // load face detection and face expression recognition models
await changeFaceDetector(SSD_MOBILENETV1) await changeFaceDetector(SSD_MOBILENETV1)
await faceapi.loadFaceLandmarkModel('/')
// TODO
const weights = await faceapi.fetchNetWeights('tmp/age_gender.weights') const weights = await faceapi.fetchNetWeights('tmp/age_gender.weights')
console.log(weights.length) await faceapi.nets.ageGenderNet.load(weights)
await window.net.load(weights)
// start processing image // start processing image
updateResults() updateResults()
......
...@@ -148,7 +148,10 @@ ...@@ -148,7 +148,10 @@
const inputImgEl = $('#inputImg').get(0) const inputImgEl = $('#inputImg').get(0)
const options = getFaceDetectorOptions() const options = getFaceDetectorOptions()
const results = await faceapi.detectAllFaces(inputImgEl, options).withFaceExpressions() const results = await faceapi.detectAllFaces(inputImgEl, options)
// compute face landmarks to align faces for better accuracy
.withFaceLandmarks()
.withFaceExpressions()
const canvas = $('#overlay').get(0) const canvas = $('#overlay').get(0)
faceapi.matchDimensions(canvas, inputImgEl) faceapi.matchDimensions(canvas, inputImgEl)
...@@ -161,7 +164,9 @@ ...@@ -161,7 +164,9 @@
async function run() { async function run() {
// load face detection and face expression recognition models // load face detection and face expression recognition models
// and load face landmark model for face alignment
await changeFaceDetector(SSD_MOBILENETV1) await changeFaceDetector(SSD_MOBILENETV1)
await faceapi.loadFaceLandmarkModel('/')
await faceapi.loadFaceExpressionModel('/') await faceapi.loadFaceExpressionModel('/')
// start processing image // start processing image
updateResults() updateResults()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment