Skip to content

Commit 5285804

Browse files
use PredictAgeAndGenderTask in ageAndGenderRecognition example + align face in faceExpressionRecognition example
1 parent 7c7cf2a commit 5285804

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

examples/examples-browser/views/ageAndGenderRecognition.html

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<head>
44
<script src="face-api.js"></script>
55
<script src="js/commons.js"></script>
6-
<script src="js/drawing.js"></script>
76
<script src="js/faceDetectionControls.js"></script>
87
<script src="js/imageSelectionControls.js"></script>
98
<link rel="stylesheet" href="styles.css">
@@ -139,8 +138,6 @@
139138

140139
<script>
141140

142-
window.net = new faceapi.AgeGenderNet()
143-
144141
async function updateResults() {
145142
if (!isFaceDetectionModelLoaded()) {
146143
return
@@ -149,24 +146,37 @@
149146
const inputImgEl = $('#inputImg').get(0)
150147
const options = getFaceDetectorOptions()
151148

152-
const result = await faceapi.detectSingleFace(inputImgEl, options)
153-
154-
if (!result) return
155-
156-
const face = (await faceapi.extractFaces(inputImgEl, [result]))[0]
157-
const { age, gender, genderProbability } = await window.net.predictAgeAndGender(face)
158-
159-
console.log('age', age)
160-
console.log('gender', gender, genderProbability)
149+
const results = await faceapi.detectAllFaces(inputImgEl, options)
150+
// compute face landmarks to align faces for better accuracy
151+
.withFaceLandmarks()
152+
.withAgeAndGender()
153+
154+
const canvas = $('#overlay').get(0)
155+
faceapi.matchDimensions(canvas, inputImgEl)
156+
157+
const resizedResults = faceapi.resizeResults(results, inputImgEl)
158+
faceapi.draw.drawDetections(canvas, resizedResults)
159+
160+
resizedResults.forEach(result => {
161+
const { age, gender, genderProbability } = result
162+
new faceapi.draw.DrawTextField(
163+
[
164+
`${faceapi.round(age, 0)} years`,
165+
`${gender} (${faceapi.round(genderProbability)})`
166+
],
167+
result.detection.box.bottomLeft
168+
).draw(canvas)
169+
})
161170
}
162171

163172
async function run() {
164173
// load face detection and face expression recognition models
165174
await changeFaceDetector(SSD_MOBILENETV1)
175+
await faceapi.loadFaceLandmarkModel('/')
166176

177+
// TODO
167178
const weights = await faceapi.fetchNetWeights('tmp/age_gender.weights')
168-
console.log(weights.length)
169-
await window.net.load(weights)
179+
await faceapi.nets.ageGenderNet.load(weights)
170180

171181
// start processing image
172182
updateResults()

examples/examples-browser/views/faceExpressionRecognition.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@
148148
const inputImgEl = $('#inputImg').get(0)
149149
const options = getFaceDetectorOptions()
150150

151-
const results = await faceapi.detectAllFaces(inputImgEl, options).withFaceExpressions()
151+
const results = await faceapi.detectAllFaces(inputImgEl, options)
152+
// compute face landmarks to align faces for better accuracy
153+
.withFaceLandmarks()
154+
.withFaceExpressions()
152155

153156
const canvas = $('#overlay').get(0)
154157
faceapi.matchDimensions(canvas, inputImgEl)
@@ -161,7 +164,9 @@
161164

162165
async function run() {
163166
// load face detection and face expression recognition models
167+
// and load face landmark model for face alignment
164168
await changeFaceDetector(SSD_MOBILENETV1)
169+
await faceapi.loadFaceLandmarkModel('/')
165170
await faceapi.loadFaceExpressionModel('/')
166171
// start processing image
167172
updateResults()

0 commit comments

Comments
 (0)