1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < script src ="face-api.js "> </ script >
5
+ < script src ="js/commons.js "> </ script >
6
+ < script src ="js/faceDetectionControls.js "> </ script >
7
+ < script src ="js/imageSelectionControls.js "> </ script >
8
+ < link rel ="stylesheet " href ="styles.css ">
9
+ < link rel ="stylesheet " href ="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.css ">
10
+ < script type ="text/javascript " src ="https://code.jquery.com/jquery-2.1.1.min.js "> </ script >
11
+ < script src ="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js "> </ script >
12
+ </ head >
13
+ < body >
14
+ < div id ="navbar "> </ div >
15
+ < div class ="center-content page-container ">
16
+
17
+ < div class ="progress " id ="loader ">
18
+ < div class ="indeterminate "> </ div >
19
+ </ div >
20
+ < div style ="position: relative " class ="margin ">
21
+ < img id ="inputImg " src ="" style ="max-width: 800px; " />
22
+ < canvas id ="overlay " />
23
+ </ div >
24
+
25
+ < div class ="row side-by-side ">
26
+ <!-- image_selection_control -->
27
+ < div id ="selectList "> </ div >
28
+ < div class ="row ">
29
+ < label for ="imgUrlInput "> Get image from URL:</ label >
30
+ < input id ="imgUrlInput " type ="text " class ="bold ">
31
+ </ div >
32
+ < button
33
+ class ="waves-effect waves-light btn "
34
+ onclick ="loadImageFromUrl() "
35
+ >
36
+ Ok
37
+ </ button >
38
+ <!-- image_selection_control -->
39
+ </ div >
40
+
41
+ < div class ="row side-by-side ">
42
+
43
+ <!-- face_detector_selection_control -->
44
+ < div id ="face_detector_selection_control " class ="row input-field " style ="margin-right: 20px; ">
45
+ < select id ="selectFaceDetector ">
46
+ < option value ="ssd_mobilenetv1 "> SSD Mobilenet V1</ option >
47
+ < option value ="tiny_face_detector "> Tiny Face Detector</ option >
48
+ < option value ="mtcnn "> MTCNN</ option >
49
+ </ select >
50
+ < label > Select Face Detector</ label >
51
+ </ div >
52
+ <!-- face_detector_selection_control -->
53
+
54
+ </ div >
55
+
56
+ <!-- ssd_mobilenetv1_controls -->
57
+ < span id ="ssd_mobilenetv1_controls ">
58
+ < div class ="row side-by-side ">
59
+ < div class ="row ">
60
+ < label for ="minConfidence "> Min Confidence:</ label >
61
+ < input disabled value ="0.5 " id ="minConfidence " type ="text " class ="bold ">
62
+ </ div >
63
+ < button
64
+ class ="waves-effect waves-light btn "
65
+ onclick ="onDecreaseMinConfidence() "
66
+ >
67
+ < i class ="material-icons left "> -</ i >
68
+ </ button >
69
+ < button
70
+ class ="waves-effect waves-light btn "
71
+ onclick ="onIncreaseMinConfidence() "
72
+ >
73
+ < i class ="material-icons left "> +</ i >
74
+ </ button >
75
+ </ div >
76
+ </ span >
77
+ <!-- ssd_mobilenetv1_controls -->
78
+
79
+ <!-- tiny_face_detector_controls -->
80
+ < span id ="tiny_face_detector_controls ">
81
+ < div class ="row side-by-side ">
82
+ < div class ="row input-field " style ="margin-right: 20px; ">
83
+ < select id ="inputSize ">
84
+ < option value ="" disabled selected > Input Size:</ option >
85
+ < option value ="160 "> 160 x 160</ option >
86
+ < option value ="224 "> 224 x 224</ option >
87
+ < option value ="320 "> 320 x 320</ option >
88
+ < option value ="416 "> 416 x 416</ option >
89
+ < option value ="512 "> 512 x 512</ option >
90
+ < option value ="608 "> 608 x 608</ option >
91
+ </ select >
92
+ < label > Input Size</ label >
93
+ </ div >
94
+ < div class ="row ">
95
+ < label for ="scoreThreshold "> Score Threshold:</ label >
96
+ < input disabled value ="0.5 " id ="scoreThreshold " type ="text " class ="bold ">
97
+ </ div >
98
+ < button
99
+ class ="waves-effect waves-light btn "
100
+ onclick ="onDecreaseScoreThreshold() "
101
+ >
102
+ < i class ="material-icons left "> -</ i >
103
+ </ button >
104
+ < button
105
+ class ="waves-effect waves-light btn "
106
+ onclick ="onIncreaseScoreThreshold() "
107
+ >
108
+ < i class ="material-icons left "> +</ i >
109
+ </ button >
110
+ </ div >
111
+ </ span >
112
+ <!-- tiny_face_detector_controls -->
113
+
114
+ <!-- mtcnn_controls -->
115
+ < span id ="mtcnn_controls ">
116
+ < div class ="row side-by-side ">
117
+ < div class ="row ">
118
+ < label for ="minFaceSize "> Minimum Face Size:</ label >
119
+ < input disabled value ="20 " id ="minFaceSize " type ="text " class ="bold ">
120
+ </ div >
121
+ < button
122
+ class ="waves-effect waves-light btn "
123
+ onclick ="onDecreaseMinFaceSize() "
124
+ >
125
+ < i class ="material-icons left "> -</ i >
126
+ </ button >
127
+ < button
128
+ class ="waves-effect waves-light btn "
129
+ onclick ="onIncreaseMinFaceSize() "
130
+ >
131
+ < i class ="material-icons left "> +</ i >
132
+ </ button >
133
+ </ div >
134
+ </ span >
135
+ <!-- mtcnn_controls -->
136
+
137
+ </ body >
138
+
139
+ < script >
140
+
141
+ async function updateResults ( ) {
142
+ if ( ! isFaceDetectionModelLoaded ( ) ) {
143
+ return
144
+ }
145
+
146
+ const inputImgEl = $ ( '#inputImg' ) . get ( 0 )
147
+ const options = getFaceDetectorOptions ( )
148
+
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
+ } )
170
+ }
171
+
172
+ async function run ( ) {
173
+ // load face detection and age and gender recognition models
174
+ // and load face landmark model for face alignment
175
+ await changeFaceDetector ( SSD_MOBILENETV1 )
176
+ await faceapi . loadFaceLandmarkModel ( '/' )
177
+ await faceapi . nets . ageGenderNet . load ( '/' )
178
+
179
+ // start processing image
180
+ updateResults ( )
181
+ }
182
+
183
+ $ ( document ) . ready ( function ( ) {
184
+ renderNavBar ( '#navbar' , 'age_and_gender_recognition' )
185
+ initImageSelectionControls ( 'happy.jpg' , true )
186
+ initFaceDetectionControls ( )
187
+ run ( )
188
+ } )
189
+ </ script >
190
+ </ body >
191
+ </ html >
0 commit comments