test_objdetect.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. var haarcascade_data = undefined;
  70. if (typeof module !== 'undefined' && module.exports) {
  71. // The environment is Node.js
  72. let fs = require("fs");
  73. haarcascade_data = fs.readFileSync("haarcascade_frontalface_default.xml");
  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. if (haarcascade_data) {
  94. cv.FS_createDataFile("/", "haarcascade_frontalface_default.xml", haarcascade_data, true, false, false);
  95. }
  96. let classifier = new cv.CascadeClassifier();
  97. const modelPath = '/haarcascade_frontalface_default.xml';
  98. assert.equal(classifier.empty(), true);
  99. classifier.load(modelPath);
  100. assert.equal(classifier.empty(), false);
  101. let image = cv.Mat.eye({height: 10, width: 10}, cv.CV_8UC3);
  102. let objects = new cv.RectVector();
  103. let numDetections = new cv.IntVector();
  104. const scaleFactor = 1.1;
  105. const minNeighbors = 3;
  106. const flags = 0;
  107. const minSize = {height: 0, width: 0};
  108. const maxSize = {height: 10, width: 10};
  109. classifier.detectMultiScale2(image, objects, numDetections, scaleFactor,
  110. minNeighbors, flags, minSize, maxSize);
  111. // test default parameters
  112. classifier.detectMultiScale2(image, objects, numDetections, scaleFactor,
  113. minNeighbors, flags, minSize);
  114. classifier.detectMultiScale2(image, objects, numDetections, scaleFactor,
  115. minNeighbors, flags);
  116. classifier.detectMultiScale2(image, objects, numDetections, scaleFactor,
  117. minNeighbors);
  118. classifier.detectMultiScale2(image, objects, numDetections, scaleFactor);
  119. classifier.delete();
  120. objects.delete();
  121. numDetections.delete();
  122. }
  123. // HOGDescriptor
  124. {
  125. let hog = new cv.HOGDescriptor();
  126. let mat = new cv.Mat({height: 10, width: 10}, cv.CV_8UC1);
  127. let descriptors = new cv.FloatVector();
  128. let locations = new cv.PointVector();
  129. assert.equal(hog.winSize.height, 128);
  130. assert.equal(hog.winSize.width, 64);
  131. assert.equal(hog.nbins, 9);
  132. assert.equal(hog.derivAperture, 1);
  133. assert.equal(hog.winSigma, -1);
  134. assert.equal(hog.histogramNormType, 0);
  135. assert.equal(hog.nlevels, 64);
  136. hog.nlevels = 32;
  137. assert.equal(hog.nlevels, 32);
  138. hog.delete();
  139. mat.delete();
  140. descriptors.delete();
  141. locations.delete();
  142. }
  143. });
  144. QUnit.test('QR code detect and decode', function (assert) {
  145. {
  146. const detector = new cv.QRCodeDetector();
  147. let mat = cv.Mat.ones(800, 600, cv.CV_8U);
  148. assert.ok(mat);
  149. // test detect
  150. let points = new cv.Mat();
  151. let qrCodeFound = detector.detect(mat, points);
  152. assert.equal(points.rows, 0)
  153. assert.equal(points.cols, 0)
  154. assert.equal(qrCodeFound, false);
  155. // test detectMult
  156. qrCodeFound = detector.detectMulti(mat, points);
  157. assert.equal(points.rows, 0)
  158. assert.equal(points.cols, 0)
  159. assert.equal(qrCodeFound, false);
  160. // test decode (with random numbers)
  161. let decodeTestPoints = cv.matFromArray(1, 4, cv.CV_32FC2, [10, 20, 30, 40, 60, 80, 90, 100]);
  162. let qrCodeContent = detector.decode(mat, decodeTestPoints);
  163. assert.equal(typeof qrCodeContent, 'string');
  164. assert.equal(qrCodeContent, '');
  165. //test detectAndDecode
  166. qrCodeContent = detector.detectAndDecode(mat);
  167. assert.equal(typeof qrCodeContent, 'string');
  168. assert.equal(qrCodeContent, '');
  169. // test decodeCurved
  170. qrCodeContent = detector.decodeCurved(mat, decodeTestPoints);
  171. assert.equal(typeof qrCodeContent, 'string');
  172. assert.equal(qrCodeContent, '');
  173. decodeTestPoints.delete();
  174. points.delete();
  175. mat.delete();
  176. }
  177. });
  178. QUnit.test('Aruco-based QR code detect', function (assert) {
  179. {
  180. let qrcode_params = new cv.QRCodeDetectorAruco_Params();
  181. let detector = new cv.QRCodeDetectorAruco();
  182. let mat = cv.Mat.ones(800, 600, cv.CV_8U);
  183. assert.ok(mat);
  184. detector.setDetectorParameters(qrcode_params);
  185. let points = new cv.Mat();
  186. let qrCodeFound = detector.detect(mat, points);
  187. assert.equal(points.rows, 0)
  188. assert.equal(points.cols, 0)
  189. assert.equal(qrCodeFound, false);
  190. qrcode_params.delete();
  191. detector.delete();
  192. points.delete();
  193. mat.delete();
  194. }
  195. });
  196. QUnit.test('Bar code detect', function (assert) {
  197. {
  198. let detector = new cv.barcode_BarcodeDetector();
  199. let mat = cv.Mat.ones(800, 600, cv.CV_8U);
  200. assert.ok(mat);
  201. let points = new cv.Mat();
  202. let codeFound = detector.detect(mat, points);
  203. assert.equal(points.rows, 0)
  204. assert.equal(points.cols, 0)
  205. assert.equal(codeFound, false);
  206. codeContent = detector.detectAndDecode(mat);
  207. assert.equal(typeof codeContent, 'string');
  208. assert.equal(codeContent, '');
  209. detector.delete();
  210. points.delete();
  211. mat.delete();
  212. }
  213. });
  214. QUnit.test('Aruco detector', function (assert) {
  215. {
  216. let dictionary = cv.getPredefinedDictionary(cv.DICT_4X4_50);
  217. let aruco_image = new cv.Mat();
  218. let detectorParameters = new cv.aruco_DetectorParameters();
  219. let refineParameters = new cv.aruco_RefineParameters(10, 3, true);
  220. let detector = new cv.aruco_ArucoDetector(dictionary, detectorParameters,refineParameters);
  221. let corners = new cv.MatVector();
  222. let ids = new cv.Mat();
  223. dictionary.generateImageMarker(10, 128, aruco_image);
  224. assert.ok(!aruco_image.empty());
  225. detector.detectMarkers(aruco_image, corners, ids);
  226. dictionary.delete();
  227. aruco_image.delete();
  228. detectorParameters.delete();
  229. refineParameters.delete();
  230. detector.delete();
  231. corners.delete();
  232. ids.delete();
  233. }
  234. });
  235. QUnit.test('Charuco detector', function (assert) {
  236. {
  237. let dictionary = new cv.getPredefinedDictionary(cv.DICT_4X4_50);
  238. let boardIds = new cv.Mat();
  239. let board = new cv.aruco_CharucoBoard(new cv.Size(3, 5), 64, 32, dictionary, boardIds);
  240. let charucoParameters = new cv.aruco_CharucoParameters();
  241. let detectorParameters = new cv.aruco_DetectorParameters();
  242. let refineParameters = new cv.aruco_RefineParameters(10, 3, true);
  243. let detector = new cv.aruco_CharucoDetector(board, charucoParameters, detectorParameters, refineParameters);
  244. let board_image = new cv.Mat();
  245. let corners = new cv.Mat();
  246. let ids = new cv.Mat();
  247. board.generateImage(new cv.Size(300, 500), board_image);
  248. assert.ok(!board_image.empty());
  249. let chess_corners = board.getChessboardCorners();
  250. detector.detectBoard(board_image, corners, ids);
  251. assert.ok(!corners.empty());
  252. assert.ok(!ids.empty());
  253. dictionary.delete();
  254. boardIds.delete();
  255. board.delete();
  256. board_image.delete();
  257. charucoParameters.delete();
  258. detectorParameters.delete();
  259. refineParameters.delete();
  260. detector.delete();
  261. corners.delete();
  262. ids.delete();
  263. chess_corners.delete();
  264. }
  265. });