Skip to content

Commit e781c56

Browse files
adjusted detection and recognition example
1 parent 623052b commit e781c56

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

examples/views/detectAndRecognizeFaces.html

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,65 @@
2727
</div>
2828
<button
2929
class="waves-effect waves-light btn"
30-
onclick="onDecreaseThreshold()"
30+
onclick="onDecreaseMinConfidence()"
3131
>
3232
<i class="material-icons left">-</i>
3333
</button>
3434
<button
3535
class="waves-effect waves-light btn"
36-
onclick="onIncreaseThreshold()"
36+
onclick="onIncreaseMinConfidence()"
37+
>
38+
<i class="material-icons left">+</i>
39+
</button>
40+
<div class="row">
41+
<label for="maxDistance">Max Descriptor Distance:</label>
42+
<input disabled value="0.8" id="maxDistance" type="text" class="bold">
43+
</div>
44+
<button
45+
class="waves-effect waves-light btn"
46+
onclick="onDecreaseMaxDistance()"
47+
>
48+
<i class="material-icons left">-</i>
49+
</button>
50+
<button
51+
class="waves-effect waves-light btn"
52+
onclick="onIncreaseMaxDistance()"
3753
>
3854
<i class="material-icons left">+</i>
3955
</button>
4056
</div>
4157
</div>
4258

4359
<script>
44-
const threshold = 0.8
60+
let maxDistance = 0.8
4561
let minConfidence = 0.7
4662
let detectionNet, recognitionNet
4763
let trainDescriptorsByClass = []
4864

49-
function onIncreaseThreshold() {
65+
function onIncreaseMinConfidence() {
5066
minConfidence = Math.min(faceapi.round(minConfidence + 0.1), 1.0)
5167
$('#minConfidence').val(minConfidence)
5268
updateResults()
5369
}
5470

55-
function onDecreaseThreshold() {
71+
function onDecreaseMinConfidence() {
5672
minConfidence = Math.max(faceapi.round(minConfidence - 0.1), 0.1)
5773
$('#minConfidence').val(minConfidence)
5874
updateResults()
5975
}
6076

77+
function onIncreaseMaxDistance() {
78+
maxDistance = Math.min(faceapi.round(maxDistance + 0.1), 1.0)
79+
$('#maxDistance').val(maxDistance)
80+
updateResults()
81+
}
82+
83+
function onDecreaseMaxDistance() {
84+
maxDistance = Math.max(faceapi.round(maxDistance - 0.1), 0.1)
85+
$('#maxDistance').val(maxDistance)
86+
updateResults()
87+
}
88+
6189
async function updateResults() {
6290
const inputImgEl = $('#inputImg').get(0)
6391
const { width, height } = inputImgEl
@@ -79,9 +107,15 @@
79107

80108
descriptors.forEach((descriptor, i) => {
81109
const bestMatch = getBestMatch(trainDescriptorsByClass, descriptor)
82-
const text = `${bestMatch.distance < threshold ? bestMatch.className : 'unkown'} (${bestMatch.distance})`
83-
const { x, y } = detectionsForSize[i].box
84-
faceapi.drawText(canvas.getContext('2d'), x, y, text, faceapi.getDefaultDrawOptions())
110+
const text = `${bestMatch.distance < maxDistance ? bestMatch.className : 'unkown'} (${bestMatch.distance})`
111+
const { x, y, height: boxHeight } = detectionsForSize[i].box
112+
faceapi.drawText(
113+
canvas.getContext('2d'),
114+
x,
115+
y + boxHeight,
116+
text,
117+
Object.assign(faceapi.getDefaultDrawOptions(), { color: 'red', fontSize: 16 })
118+
)
85119
})
86120
}
87121

0 commit comments

Comments
 (0)