File: test_camera_hardware_face_detection.html

package info (click to toggle)
iceweasel 31.8.0esr-1~deb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,373,164 kB
  • sloc: cpp: 3,717,015; ansic: 1,797,386; python: 206,412; java: 180,622; asm: 133,557; xml: 89,501; sh: 72,014; perl: 22,087; makefile: 21,970; objc: 4,014; yacc: 1,995; pascal: 1,292; lex: 950; exp: 449; lisp: 228; awk: 211; php: 113; sed: 43; csh: 31; ada: 16; ruby: 3
file content (320 lines) | stat: -rw-r--r-- 9,055 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=965420
-->
<head>
  <title>Bug 965420 - Test camera hardware API for face detection</title>
  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="camera_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=965420">Mozilla Bug 965420</a>
  <video id="viewfinder" width = "200" height = "200" autoplay></video>
  <img src="#" alt="This image is going to load" id="testimage"/>

<script class="testbody" type="text/javascript;version=1.7">

var whichCamera = navigator.mozCameras.getListOfCameras()[0];
var initialConfig = {
  mode: 'picture',
  recorderProfile: 'cif',
  previewSize: {
    width: 352,
    height: 288
  }
};

const PREF_FACEDETECTION_ENABLED = "camera.control.face_detection.enabled";

var cameraObj;
var oldPref;

// Shorthand functions
function end() {
  function reallyEnd() {
    CameraTest.end();
  }
  if (oldPref) {
    SpecialPowers.pushPrefEnv(
      {'set': [[PREF_FACEDETECTION_ENABLED, oldPref]]}, reallyEnd);
  } else {
    SpecialPowers.pushPrefEnv(
      {'clear': [[PREF_FACEDETECTION_ENABLED]]}, reallyEnd);
  }
}
function next() {
  CameraTest.next();
}

function compareFaces(aFaces, expected)
{
  ok(aFaces, "have detected faces object");
  ok(aFaces.length == expected.faces.length,
    "expected=" + expected.faces.length + ", got=" + aFaces.length);
  aFaces.forEach(function (face, index) {
    let result = compareFace(face, expected.faces[index]);
    ok(result === "ok", "face check: " + result);
    if (result !== "ok") {
      return false;
    }
  });
  return true;
}

function compareFace(aFace, expected)
{
  if (aFace.id != expected.id) {
    return "expected face.id=" + expected.id + ", got=" + aFace.id;
  }
  if (aFace.score != expected.score) {
    return "expected face.score=" + expected.score + ", got=" + aFace.score;
  }
  if (!aFace.bounds) {
    return "face.bounds is missing";
  }
  if (aFace.bounds.left != expected.bounds.left ||
      aFace.bounds.top != expected.bounds.top ||
      aFace.bounds.right != expected.bounds.right ||
      aFace.bounds.bottom != expected.bounds.bottom) {
    return "expected face.bounds=" + expected.bounds.toSource() +
      ", got=({left:" + aFace.bounds.left + ", top:" + aFace.bounds.top + ", right:" + aFace.bounds.right + ", bottom:" + aFace.bounds.bottom + "})";
  }

  if (aFace.leftEye && !expected.leftEye) {
    return "expected null face.leftEye, got=({x:" + aFace.leftEye.x + ", y:" + aFace.leftEye.y + "})";
  }
  if (!aFace.leftEye && expected.leftEye) {
    return "expected face.leftEye=" + expected.leftEye.toSource() + ", got null leftEye";
  }
  if (aFace.leftEye && expected.leftEye &&
      (aFace.leftEye.x != expected.leftEye.x || aFace.leftEye.y != expected.leftEye.y)) {
    return "expected face.leftEye=" + expected.leftEye.toSource() +
      ", got=({x:" + aFace.leftEye.x + ", y:" + aFace.leftEye.y + "})";
  }

  if (aFace.rightEye && !expected.rightEye) {
    return "expected null face.rightEye, got=({x:" + aFace.rightEye.x + ", y:" + aFace.rightEye.y + "})";
  }
  if (!aFace.rightEye && expected.rightEye) {
    return "expected face.rightEye=" + expected.rightEye.toSource() + ", got null rightEye";
  }
  if (aFace.rightEye && expected.rightEye &&
      (aFace.rightEye.x != expected.rightEye.x || aFace.rightEye.y != expected.rightEye.y)) {
    return "expected face.rightEye=" + expected.rightEye.toSource() +
      ", got=({x:" + aFace.rightEye.x + ", y:" + aFace.rightEye.y + "})";
  }

  if (aFace.mouth && !expected.mouth) {
    return "expected null face.mouth, got=({x:" + aFace.mouth.x + ", y:" + aFace.mouth.y + "})";
  }
  if (!aFace.mouth && expected.mouth) {
    return "expected face.mouth=" + expected.mouth.toSource() + ", got null mouth";
  }
  if (aFace.mouth && expected.mouth &&
      (aFace.mouth.x != expected.mouth.x || aFace.mouth.y != expected.mouth.y)) {
    return "expected face.mouth=" + expected.mouth.toSource() +
      ", got=({x:" + aFace.mouth.x + ", y:" + aFace.mouth.y + "})";
  }

  return "ok";
}

var tests = [
  {
    key: "face-detection-detected-one-face",
    func: function testFaceDetectionFoundOneFace(camera) {
      var expected = {
        faces: [ {
          id:       1,
          score:    2,
          bounds: {
            left:   3,
            top:    4,
            right:  5,
            bottom: 6
          },
          leftEye: {
            x:      7,
            y:      8
          },
          rightEye: {
            x:      9,
            y:      10
          },
          mouth: {
            x:      11,
            y:      12
          }
        } ]
      };
      camera.onFacesDetected = function(aFaces) {
        ok(compareFaces(aFaces, expected),
          "onFaceDetected received the detected faces correctly");
        camera.stopFaceDetection();
        next();
      }
      camera.startFaceDetection();
    }
  },
  {
    key: "face-detection-detected-two-faces",
    func: function testFaceDetectionFoundTwoFace(camera) {
      var expected = {
        faces: [ {
          id:       1,
          score:    2,
          bounds: {
            left:   3,
            top:    4,
            right:  5,
            bottom: 6
          },
          leftEye: {
            x:      7,
            y:      8
          },
          rightEye: {
            x:      9,
            y:      10
          },
          mouth: {
            x:      11,
            y:      12
          }
        },
        {
          id:       13,
          score:    14,
          bounds: {
            left:   15,
            top:    16,
            right:  17,
            bottom: 18
          },
          leftEye: {
            x:      19,
            y:      20
          },
          rightEye: {
            x:      21,
            y:      22
          },
          mouth: {
            x:      23,
            y:      24
          }
        } ]
      };
      camera.onFacesDetected = function(aFaces) {
        ok(compareFaces(aFaces, expected),
          "onFaceDetected received the detected faces correctly");
        camera.stopFaceDetection();
        next();
      }
      camera.startFaceDetection();
    }
  },
  {
    key: "face-detection-detected-one-face-no-features",
    func: function (camera) {
      var expected = {
        faces: [ {
          id:       1,
          score:    100,
          bounds: {
            left:   3,
            top:    4,
            right:  5,
            bottom: 6
          },
          leftEye:  null,
          rightEye: null,
          mouth:    null
        } ]
      };
      camera.onFacesDetected = function(aFaces) {
        ok(compareFaces(aFaces, expected),
          "onFaceDetected received the detected faces correctly");
        camera.stopFaceDetection();
        next();
      }
      camera.startFaceDetection();
    }
  },
  {
    key: "face-detection-no-faces-detected",
    func: function (camera) {
      var expected = {
        faces: []
      };
      camera.onFacesDetected = function(aFaces) {
        ok(compareFaces(aFaces, expected),
          "onFaceDetected received the detected faces correctly");
        camera.stopFaceDetection();
        next();
      }
      camera.startFaceDetection();
    }
  },
];

var testGenerator = function() {
  for (var i = 0; i < tests.length; ++i ) {
    yield tests[i];
  }
}();

window.addEventListener('beforeunload', function() {
  document.getElementById('viewfinder').mozSrcObject = null;
  if (cameraObj) {
    cameraObj.release();
    cameraObj = null;
  }
});

// Must call CameraTest.begin() before any other async methods.
CameraTest.begin("hardware", function(test) {
  // If the pref doesn't exist, this get will fail; catch it and continue.
  try {
    oldPref = SpecialPowers.getBoolPref(PREF_FACEDETECTION_ENABLED);
  } catch(e) { }

  SpecialPowers.pushPrefEnv({'set': [[PREF_FACEDETECTION_ENABLED, true]]}, function() {
    var enabled;
    try {
      enabled = SpecialPowers.getBoolPref(PREF_FACEDETECTION_ENABLED);
    } catch(e) { }
    ok(enabled, PREF_FACEDETECTION_ENABLED + " is " + enabled);

    function onSuccess(camera, config) {
      document.getElementById('viewfinder').mozSrcObject = camera;
      cameraObj = camera;
      CameraTest.next = function() {
        try {
          var t = testGenerator.next();
          test.set(t.key, t.func.bind(undefined, camera));
        } catch(e) {
          if (e instanceof StopIteration) {
            end();
          } else {
            throw e;
          }
        }
      };
      next();
    }
    function onError(error) {
      ok(false, "getCamera() failed with: " + error);
      end();
    }
    navigator.mozCameras.getCamera(whichCamera, initialConfig, onSuccess, onError);
  })
});

</script>
</body>

</html>