test_objdetect.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // //////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //
  41. // //////////////////////////////////////////////////////////////////////////////////////
  42. // Author: Sajjad Taheri, University of California, Irvine. sajjadt[at]uci[dot]edu
  43. //
  44. // LICENSE AGREEMENT
  45. // Copyright (c) 2015 The Regents of the University of California (Regents)
  46. //
  47. // Redistribution and use in source and binary forms, with or without
  48. // modification, are permitted provided that the following conditions are met:
  49. // 1. Redistributions of source code must retain the above copyright
  50. // notice, this list of conditions and the following disclaimer.
  51. // 2. Redistributions in binary form must reproduce the above copyright
  52. // notice, this list of conditions and the following disclaimer in the
  53. // documentation and/or other materials provided with the distribution.
  54. // 3. Neither the name of the University nor the
  55. // names of its contributors may be used to endorse or promote products
  56. // derived from this software without specific prior written permission.
  57. //
  58. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY
  59. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  60. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  61. // DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
  62. // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  63. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  64. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  65. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  66. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  67. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  68. //
  69. if (typeof module !== 'undefined' && module.exports) {
  70. // The environment is Node.js
  71. var cv = require('./opencv.js'); // eslint-disable-line no-var
  72. cv.FS_createLazyFile('/', 'haarcascade_frontalface_default.xml', // eslint-disable-line new-cap
  73. 'haarcascade_frontalface_default.xml', true, false);
  74. }
  75. QUnit.module('Object Detection', {});
  76. QUnit.test('Cascade classification', function(assert) {
  77. // Group rectangle
  78. {
  79. let rectList = new cv.RectVector();
  80. let weights = new cv.IntVector();
  81. let groupThreshold = 1;
  82. const eps = 0.2;
  83. let rect1 = new cv.Rect(1, 2, 3, 4);
  84. let rect2 = new cv.Rect(1, 4, 2, 3);
  85. rectList.push_back(rect1);
  86. rectList.push_back(rect2);
  87. cv.groupRectangles(rectList, weights, groupThreshold, eps);
  88. rectList.delete();
  89. weights.delete();
  90. }
  91. // CascadeClassifier
  92. {
  93. let classifier = new cv.CascadeClassifier();
  94. const modelPath = '/haarcascade_frontalface_default.xml';
  95. assert.equal(classifier.empty(), true);
  96. classifier.load(modelPath);
  97. assert.equal(classifier.empty(), false);
  98. let image = cv.Mat.eye({height: 10, width: 10}, cv.CV_8UC3);
  99. let objects = new cv.RectVector();
  100. let numDetections = new cv.IntVector();
  101. const scaleFactor = 1.1;
  102. const minNeighbors = 3;
  103. const flags = 0;
  104. const minSize = {height: 0, width: 0};
  105. const maxSize = {height: 10, width: 10};
  106. classifier.detectMultiScale2(image, objects, numDetections, scaleFactor,
  107. minNeighbors, flags, minSize, maxSize);
  108. // test default parameters
  109. classifier.detectMultiScale2(image, objects, numDetections, scaleFactor,
  110. minNeighbors, flags, minSize);
  111. classifier.detectMultiScale2(image, objects, numDetections, scaleFactor,
  112. minNeighbors, flags);
  113. classifier.detectMultiScale2(image, objects, numDetections, scaleFactor,
  114. minNeighbors);
  115. classifier.detectMultiScale2(image, objects, numDetections, scaleFactor);
  116. classifier.delete();
  117. objects.delete();
  118. numDetections.delete();
  119. }
  120. // HOGDescriptor
  121. {
  122. let hog = new cv.HOGDescriptor();
  123. let mat = new cv.Mat({height: 10, width: 10}, cv.CV_8UC1);
  124. let descriptors = new cv.FloatVector();
  125. let locations = new cv.PointVector();
  126. assert.equal(hog.winSize.height, 128);
  127. assert.equal(hog.winSize.width, 64);
  128. assert.equal(hog.nbins, 9);
  129. assert.equal(hog.derivAperture, 1);
  130. assert.equal(hog.winSigma, -1);
  131. assert.equal(hog.histogramNormType, 0);
  132. assert.equal(hog.nlevels, 64);
  133. hog.nlevels = 32;
  134. assert.equal(hog.nlevels, 32);
  135. hog.delete();
  136. mat.delete();
  137. descriptors.delete();
  138. locations.delete();
  139. }
  140. });
  141. QUnit.test('QR code detect and decode', function (assert) {
  142. {
  143. const detector = new cv.QRCodeDetector();
  144. let mat = cv.Mat.ones(800, 600, cv.CV_8U);
  145. assert.ok(mat);
  146. // test detect
  147. let points = new cv.Mat();
  148. let qrCodeFound = detector.detect(mat, points);
  149. assert.equal(points.rows, 0)
  150. assert.equal(points.cols, 0)
  151. assert.equal(qrCodeFound, false);
  152. // test detectMult
  153. qrCodeFound = detector.detectMulti(mat, points);
  154. assert.equal(points.rows, 0)
  155. assert.equal(points.cols, 0)
  156. assert.equal(qrCodeFound, false);
  157. // test decode (with random numbers)
  158. let decodeTestPoints = cv.matFromArray(1, 4, cv.CV_32FC2, [10, 20, 30, 40, 60, 80, 90, 100]);
  159. let qrCodeContent = detector.decode(mat, decodeTestPoints);
  160. assert.equal(typeof qrCodeContent, 'string');
  161. assert.equal(qrCodeContent, '');
  162. //test detectAndDecode
  163. qrCodeContent = detector.detectAndDecode(mat);
  164. assert.equal(typeof qrCodeContent, 'string');
  165. assert.equal(qrCodeContent, '');
  166. // test decodeCurved
  167. qrCodeContent = detector.decodeCurved(mat, decodeTestPoints);
  168. assert.equal(typeof qrCodeContent, 'string');
  169. assert.equal(qrCodeContent, '');
  170. decodeTestPoints.delete();
  171. points.delete();
  172. mat.delete();
  173. }
  174. });