detection-HTMLImageElement.https.html (3790B)
1 <!DOCTYPE html> 2 <script src="/resources/testharness.js"></script> 3 <script src="/resources/testharnessreport.js"></script> 4 <script src="resources/shapedetection-helpers.js"></script> 5 <body> 6 <img id="img" src="/images/green-16x16.png"/> 7 </body> 8 <script> 9 10 // These tests verify that a Detector's detect() works on an HTMLImageElement. 11 const imageElementTests = 12 [ 13 { 14 createDetector: () => { return new FaceDetector(); }, 15 mockTestName: "FaceDetectionTest", 16 detectionResultTest: FaceDetectorDetectionResultTest, 17 name: "Face - detect(HTMLImageElement)" 18 }, 19 { 20 createDetector: () => { return new BarcodeDetector(); }, 21 mockTestName: "BarcodeDetectionTest", 22 detectionResultTest: BarcodeDetectorDetectionResultTest, 23 name: "Barcode - detect(HTMLImageElement)", 24 }, 25 { 26 createDetector: () => { return new TextDetector(); }, 27 mockTestName: "TextDetectionTest", 28 detectionResultTest: TextDetectorDetectionResultTest, 29 name: "Text - detect(HTMLImageElement)" 30 } 31 ]; 32 33 for (let imageElementTest of imageElementTests) { 34 detection_test(imageElementTest.mockTestName, async (t, detectionTest) => { 35 const img = document.getElementById("img"); 36 37 const detector = imageElementTest.createDetector(); 38 const detectionResult = await detector.detect(img); 39 imageElementTest.detectionResultTest(detectionResult, detectionTest); 40 }, imageElementTest.name); 41 } 42 43 function FaceDetectorDetectionResultTest(detectionResult, mockTest) { 44 const imageReceivedByMock = 45 mockTest.MockFaceDetectionProvider().getFrameData(); 46 assert_equals(imageReceivedByMock.byteLength, 1024, "Image length"); 47 const GREEN_PIXEL = 0xFF00FF00; 48 assert_equals(imageReceivedByMock[0], GREEN_PIXEL, "Pixel color"); 49 assert_equals(detectionResult.length, 3, "Number of faces"); 50 assert_equals(detectionResult[0].landmarks.length, 2, "Number of landmarks"); 51 assert_object_equals(detectionResult[0].landmarks[0], 52 {type : 'eye', locations : [{x : 4.0, y : 5.0}]}, 53 "landmark #1"); 54 assert_equals(detectionResult[0].landmarks[1].locations.length, 8, 55 "Number of locations along eye"); 56 assert_object_equals(detectionResult[1].landmarks[0], 57 {type : 'nose', locations : [{x : 100.0, y : 50.0}]}, 58 "landmark #2"); 59 assert_equals(detectionResult[1].landmarks[1].locations.length, 9, 60 "Number of locations along nose"); 61 } 62 63 function BarcodeDetectorDetectionResultTest(detectionResult, mockTest) { 64 assert_equals(detectionResult.length, 2, "Number of barcodes"); 65 assert_equals(detectionResult[0].rawValue, "cats", "barcode 1"); 66 assert_equals(detectionResult[0].format, "qr_code", "barcode 1 format"); 67 assert_equals(detectionResult[1].rawValue, "dogs", "barcode 2"); 68 assert_equals(detectionResult[1].format, "code_128", "barcode 2 format"); 69 } 70 71 function TextDetectorDetectionResultTest(detectionResult, mockTest) { 72 assert_equals(detectionResult.length, 2, "Number of textBlocks"); 73 assert_equals(detectionResult[0].rawValue, "cats", "textBlock 1"); 74 assert_equals(detectionResult[1].rawValue, "dogs", "textBlock 2"); 75 for (let i = 0; i < detectionResult.length; i++) { 76 assert_equals(detectionResult[i].boundingBox.x, detectionResult[i].cornerPoints[0].x); 77 assert_equals(detectionResult[i].boundingBox.y, detectionResult[i].cornerPoints[0].y); 78 assert_equals(detectionResult[i].boundingBox.width, 79 detectionResult[i].cornerPoints[2].x - detectionResult[i].cornerPoints[3].x); 80 assert_equals(detectionResult[i].boundingBox.height, 81 detectionResult[i].cornerPoints[2].y - detectionResult[i].cornerPoints[1].y); 82 } 83 84 } 85 86 </script>