dom/camera/test/test_camera_hardware_face_detection.html

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=965420
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Bug 965420 - Test camera hardware API for face detection</title>
michael@0 8 <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
michael@0 9 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 10 <script type="text/javascript" src="camera_common.js"></script>
michael@0 11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 12 </head>
michael@0 13 <body>
michael@0 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=965420">Mozilla Bug 965420</a>
michael@0 15 <video id="viewfinder" width = "200" height = "200" autoplay></video>
michael@0 16 <img src="#" alt="This image is going to load" id="testimage"/>
michael@0 17
michael@0 18 <script class="testbody" type="text/javascript;version=1.7">
michael@0 19
michael@0 20 var whichCamera = navigator.mozCameras.getListOfCameras()[0];
michael@0 21 var initialConfig = {
michael@0 22 mode: 'picture',
michael@0 23 recorderProfile: 'cif',
michael@0 24 previewSize: {
michael@0 25 width: 352,
michael@0 26 height: 288
michael@0 27 }
michael@0 28 };
michael@0 29
michael@0 30 const PREF_FACEDETECTION_ENABLED = "camera.control.face_detection.enabled";
michael@0 31
michael@0 32 var cameraObj;
michael@0 33 var oldPref;
michael@0 34
michael@0 35 // Shorthand functions
michael@0 36 function end() {
michael@0 37 function reallyEnd() {
michael@0 38 CameraTest.end();
michael@0 39 }
michael@0 40 if (oldPref) {
michael@0 41 SpecialPowers.pushPrefEnv(
michael@0 42 {'set': [[PREF_FACEDETECTION_ENABLED, oldPref]]}, reallyEnd);
michael@0 43 } else {
michael@0 44 SpecialPowers.pushPrefEnv(
michael@0 45 {'clear': [[PREF_FACEDETECTION_ENABLED]]}, reallyEnd);
michael@0 46 }
michael@0 47 }
michael@0 48 function next() {
michael@0 49 CameraTest.next();
michael@0 50 }
michael@0 51
michael@0 52 function compareFaces(aFaces, expected)
michael@0 53 {
michael@0 54 ok(aFaces, "have detected faces object");
michael@0 55 ok(aFaces.length == expected.faces.length,
michael@0 56 "expected=" + expected.faces.length + ", got=" + aFaces.length);
michael@0 57 aFaces.forEach(function (face, index) {
michael@0 58 let result = compareFace(face, expected.faces[index]);
michael@0 59 ok(result === "ok", "face check: " + result);
michael@0 60 if (result !== "ok") {
michael@0 61 return false;
michael@0 62 }
michael@0 63 });
michael@0 64 return true;
michael@0 65 }
michael@0 66
michael@0 67 function compareFace(aFace, expected)
michael@0 68 {
michael@0 69 if (aFace.id != expected.id) {
michael@0 70 return "expected face.id=" + expected.id + ", got=" + aFace.id;
michael@0 71 }
michael@0 72 if (aFace.score != expected.score) {
michael@0 73 return "expected face.score=" + expected.score + ", got=" + aFace.score;
michael@0 74 }
michael@0 75 if (!aFace.bounds) {
michael@0 76 return "face.bounds is missing";
michael@0 77 }
michael@0 78 if (aFace.bounds.left != expected.bounds.left ||
michael@0 79 aFace.bounds.top != expected.bounds.top ||
michael@0 80 aFace.bounds.right != expected.bounds.right ||
michael@0 81 aFace.bounds.bottom != expected.bounds.bottom) {
michael@0 82 return "expected face.bounds=" + expected.bounds.toSource() +
michael@0 83 ", got=({left:" + aFace.bounds.left + ", top:" + aFace.bounds.top + ", right:" + aFace.bounds.right + ", bottom:" + aFace.bounds.bottom + "})";
michael@0 84 }
michael@0 85
michael@0 86 if (aFace.leftEye && !expected.leftEye) {
michael@0 87 return "expected null face.leftEye, got=({x:" + aFace.leftEye.x + ", y:" + aFace.leftEye.y + "})";
michael@0 88 }
michael@0 89 if (!aFace.leftEye && expected.leftEye) {
michael@0 90 return "expected face.leftEye=" + expected.leftEye.toSource() + ", got null leftEye";
michael@0 91 }
michael@0 92 if (aFace.leftEye && expected.leftEye &&
michael@0 93 (aFace.leftEye.x != expected.leftEye.x || aFace.leftEye.y != expected.leftEye.y)) {
michael@0 94 return "expected face.leftEye=" + expected.leftEye.toSource() +
michael@0 95 ", got=({x:" + aFace.leftEye.x + ", y:" + aFace.leftEye.y + "})";
michael@0 96 }
michael@0 97
michael@0 98 if (aFace.rightEye && !expected.rightEye) {
michael@0 99 return "expected null face.rightEye, got=({x:" + aFace.rightEye.x + ", y:" + aFace.rightEye.y + "})";
michael@0 100 }
michael@0 101 if (!aFace.rightEye && expected.rightEye) {
michael@0 102 return "expected face.rightEye=" + expected.rightEye.toSource() + ", got null rightEye";
michael@0 103 }
michael@0 104 if (aFace.rightEye && expected.rightEye &&
michael@0 105 (aFace.rightEye.x != expected.rightEye.x || aFace.rightEye.y != expected.rightEye.y)) {
michael@0 106 return "expected face.rightEye=" + expected.rightEye.toSource() +
michael@0 107 ", got=({x:" + aFace.rightEye.x + ", y:" + aFace.rightEye.y + "})";
michael@0 108 }
michael@0 109
michael@0 110 if (aFace.mouth && !expected.mouth) {
michael@0 111 return "expected null face.mouth, got=({x:" + aFace.mouth.x + ", y:" + aFace.mouth.y + "})";
michael@0 112 }
michael@0 113 if (!aFace.mouth && expected.mouth) {
michael@0 114 return "expected face.mouth=" + expected.mouth.toSource() + ", got null mouth";
michael@0 115 }
michael@0 116 if (aFace.mouth && expected.mouth &&
michael@0 117 (aFace.mouth.x != expected.mouth.x || aFace.mouth.y != expected.mouth.y)) {
michael@0 118 return "expected face.mouth=" + expected.mouth.toSource() +
michael@0 119 ", got=({x:" + aFace.mouth.x + ", y:" + aFace.mouth.y + "})";
michael@0 120 }
michael@0 121
michael@0 122 return "ok";
michael@0 123 }
michael@0 124
michael@0 125 var tests = [
michael@0 126 {
michael@0 127 key: "face-detection-detected-one-face",
michael@0 128 func: function testFaceDetectionFoundOneFace(camera) {
michael@0 129 var expected = {
michael@0 130 faces: [ {
michael@0 131 id: 1,
michael@0 132 score: 2,
michael@0 133 bounds: {
michael@0 134 left: 3,
michael@0 135 top: 4,
michael@0 136 right: 5,
michael@0 137 bottom: 6
michael@0 138 },
michael@0 139 leftEye: {
michael@0 140 x: 7,
michael@0 141 y: 8
michael@0 142 },
michael@0 143 rightEye: {
michael@0 144 x: 9,
michael@0 145 y: 10
michael@0 146 },
michael@0 147 mouth: {
michael@0 148 x: 11,
michael@0 149 y: 12
michael@0 150 }
michael@0 151 } ]
michael@0 152 };
michael@0 153 camera.onFacesDetected = function(aFaces) {
michael@0 154 ok(compareFaces(aFaces, expected),
michael@0 155 "onFaceDetected received the detected faces correctly");
michael@0 156 camera.stopFaceDetection();
michael@0 157 next();
michael@0 158 }
michael@0 159 camera.startFaceDetection();
michael@0 160 }
michael@0 161 },
michael@0 162 {
michael@0 163 key: "face-detection-detected-two-faces",
michael@0 164 func: function testFaceDetectionFoundTwoFace(camera) {
michael@0 165 var expected = {
michael@0 166 faces: [ {
michael@0 167 id: 1,
michael@0 168 score: 2,
michael@0 169 bounds: {
michael@0 170 left: 3,
michael@0 171 top: 4,
michael@0 172 right: 5,
michael@0 173 bottom: 6
michael@0 174 },
michael@0 175 leftEye: {
michael@0 176 x: 7,
michael@0 177 y: 8
michael@0 178 },
michael@0 179 rightEye: {
michael@0 180 x: 9,
michael@0 181 y: 10
michael@0 182 },
michael@0 183 mouth: {
michael@0 184 x: 11,
michael@0 185 y: 12
michael@0 186 }
michael@0 187 },
michael@0 188 {
michael@0 189 id: 13,
michael@0 190 score: 14,
michael@0 191 bounds: {
michael@0 192 left: 15,
michael@0 193 top: 16,
michael@0 194 right: 17,
michael@0 195 bottom: 18
michael@0 196 },
michael@0 197 leftEye: {
michael@0 198 x: 19,
michael@0 199 y: 20
michael@0 200 },
michael@0 201 rightEye: {
michael@0 202 x: 21,
michael@0 203 y: 22
michael@0 204 },
michael@0 205 mouth: {
michael@0 206 x: 23,
michael@0 207 y: 24
michael@0 208 }
michael@0 209 } ]
michael@0 210 };
michael@0 211 camera.onFacesDetected = function(aFaces) {
michael@0 212 ok(compareFaces(aFaces, expected),
michael@0 213 "onFaceDetected received the detected faces correctly");
michael@0 214 camera.stopFaceDetection();
michael@0 215 next();
michael@0 216 }
michael@0 217 camera.startFaceDetection();
michael@0 218 }
michael@0 219 },
michael@0 220 {
michael@0 221 key: "face-detection-detected-one-face-no-features",
michael@0 222 func: function (camera) {
michael@0 223 var expected = {
michael@0 224 faces: [ {
michael@0 225 id: 1,
michael@0 226 score: 100,
michael@0 227 bounds: {
michael@0 228 left: 3,
michael@0 229 top: 4,
michael@0 230 right: 5,
michael@0 231 bottom: 6
michael@0 232 },
michael@0 233 leftEye: null,
michael@0 234 rightEye: null,
michael@0 235 mouth: null
michael@0 236 } ]
michael@0 237 };
michael@0 238 camera.onFacesDetected = function(aFaces) {
michael@0 239 ok(compareFaces(aFaces, expected),
michael@0 240 "onFaceDetected received the detected faces correctly");
michael@0 241 camera.stopFaceDetection();
michael@0 242 next();
michael@0 243 }
michael@0 244 camera.startFaceDetection();
michael@0 245 }
michael@0 246 },
michael@0 247 {
michael@0 248 key: "face-detection-no-faces-detected",
michael@0 249 func: function (camera) {
michael@0 250 var expected = {
michael@0 251 faces: []
michael@0 252 };
michael@0 253 camera.onFacesDetected = function(aFaces) {
michael@0 254 ok(compareFaces(aFaces, expected),
michael@0 255 "onFaceDetected received the detected faces correctly");
michael@0 256 camera.stopFaceDetection();
michael@0 257 next();
michael@0 258 }
michael@0 259 camera.startFaceDetection();
michael@0 260 }
michael@0 261 },
michael@0 262 ];
michael@0 263
michael@0 264 var testGenerator = function() {
michael@0 265 for (var i = 0; i < tests.length; ++i ) {
michael@0 266 yield tests[i];
michael@0 267 }
michael@0 268 }();
michael@0 269
michael@0 270 window.addEventListener('beforeunload', function() {
michael@0 271 document.getElementById('viewfinder').mozSrcObject = null;
michael@0 272 if (cameraObj) {
michael@0 273 cameraObj.release();
michael@0 274 cameraObj = null;
michael@0 275 }
michael@0 276 });
michael@0 277
michael@0 278 // Must call CameraTest.begin() before any other async methods.
michael@0 279 CameraTest.begin("hardware", function(test) {
michael@0 280 // If the pref doesn't exist, this get will fail; catch it and continue.
michael@0 281 try {
michael@0 282 oldPref = SpecialPowers.getBoolPref(PREF_FACEDETECTION_ENABLED);
michael@0 283 } catch(e) { }
michael@0 284
michael@0 285 SpecialPowers.pushPrefEnv({'set': [[PREF_FACEDETECTION_ENABLED, true]]}, function() {
michael@0 286 var enabled;
michael@0 287 try {
michael@0 288 enabled = SpecialPowers.getBoolPref(PREF_FACEDETECTION_ENABLED);
michael@0 289 } catch(e) { }
michael@0 290 ok(enabled, PREF_FACEDETECTION_ENABLED + " is " + enabled);
michael@0 291
michael@0 292 function onSuccess(camera, config) {
michael@0 293 document.getElementById('viewfinder').mozSrcObject = camera;
michael@0 294 cameraObj = camera;
michael@0 295 CameraTest.next = function() {
michael@0 296 try {
michael@0 297 var t = testGenerator.next();
michael@0 298 test.set(t.key, t.func.bind(undefined, camera));
michael@0 299 } catch(e) {
michael@0 300 if (e instanceof StopIteration) {
michael@0 301 end();
michael@0 302 } else {
michael@0 303 throw e;
michael@0 304 }
michael@0 305 }
michael@0 306 };
michael@0 307 next();
michael@0 308 }
michael@0 309 function onError(error) {
michael@0 310 ok(false, "getCamera() failed with: " + error);
michael@0 311 end();
michael@0 312 }
michael@0 313 navigator.mozCameras.getCamera(whichCamera, initialConfig, onSuccess, onError);
michael@0 314 })
michael@0 315 });
michael@0 316
michael@0 317 </script>
michael@0 318 </body>
michael@0 319
michael@0 320 </html>

mercurial