From a03b81316782ae30038b288fd3568993fa1e3538 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Tue, 11 Jun 2024 12:01:51 +0300 Subject: [PATCH] Merge pull request #25732 from dkurt:opencv_js_tests_update Fix OpenCV.js tests #25732 ### Pull Request Readiness Checklist * Firefox tests passed See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake --- modules/js/test/init_cv.js | 14 ++++++++++++++ modules/js/test/test_calib3d.js | 5 ----- modules/js/test/test_core.js | 5 ----- modules/js/test/test_features2d.js | 5 ----- modules/js/test/test_imgproc.js | 5 ----- modules/js/test/test_mat.js | 5 ----- modules/js/test/test_objdetect.js | 10 +++++++--- modules/js/test/test_photo.js | 6 ------ modules/js/test/test_utils.js | 4 ---- modules/js/test/test_video.js | 5 ----- modules/js/test/tests.js | 10 +++++++--- platforms/js/build_js.py | 2 +- 12 files changed, 29 insertions(+), 47 deletions(-) create mode 100644 modules/js/test/init_cv.js diff --git a/modules/js/test/init_cv.js b/modules/js/test/init_cv.js new file mode 100644 index 0000000000..d053707407 --- /dev/null +++ b/modules/js/test/init_cv.js @@ -0,0 +1,14 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. + +if (cv instanceof Promise) { + QUnit.test("init_cv", (assert) => { + const done = assert.async(); + assert.ok(true); + cv.then((ready_cv) => { + cv = ready_cv; + done(); + }); + }); +} diff --git a/modules/js/test/test_calib3d.js b/modules/js/test/test_calib3d.js index b0001dbc67..157658df5e 100644 --- a/modules/js/test/test_calib3d.js +++ b/modules/js/test/test_calib3d.js @@ -2,11 +2,6 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. -if (typeof module !== 'undefined' && module.exports) { - // The environment is Node.js - var cv = require('./opencv.js'); // eslint-disable-line no-var -} - QUnit.module('Camera Calibration and 3D Reconstruction', {}); QUnit.test('constants', function(assert) { diff --git a/modules/js/test/test_core.js b/modules/js/test/test_core.js index 14d4ffe72b..bfd9d2cd0d 100644 --- a/modules/js/test/test_core.js +++ b/modules/js/test/test_core.js @@ -2,11 +2,6 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. -if (typeof module !== 'undefined' && module.exports) { - // The environment is Node.js - var cv = require('./opencv.js'); // eslint-disable-line no-var -} - QUnit.module('Core', {}); QUnit.test('test_LUT', function(assert) { diff --git a/modules/js/test/test_features2d.js b/modules/js/test/test_features2d.js index c5eb73a123..7844da3146 100644 --- a/modules/js/test/test_features2d.js +++ b/modules/js/test/test_features2d.js @@ -2,11 +2,6 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. -if (typeof module !== 'undefined' && module.exports) { - // The environment is Node.js - var cv = require('./opencv.js'); // eslint-disable-line no-var -} - function generateTestFrame(width, height) { let w = width || 200; let h = height || 200; diff --git a/modules/js/test/test_imgproc.js b/modules/js/test/test_imgproc.js index a0d2d937cc..65b3cb74af 100644 --- a/modules/js/test/test_imgproc.js +++ b/modules/js/test/test_imgproc.js @@ -68,11 +68,6 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // -if (typeof module !== 'undefined' && module.exports) { - // The environment is Node.js - var cv = require('./opencv.js'); // eslint-disable-line no-var -} - QUnit.module('Image Processing', {}); QUnit.test('test_imgProc', function(assert) { diff --git a/modules/js/test/test_mat.js b/modules/js/test/test_mat.js index fd3611cd2c..a1d5c98b17 100644 --- a/modules/js/test/test_mat.js +++ b/modules/js/test/test_mat.js @@ -68,11 +68,6 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // -if (typeof module !== 'undefined' && module.exports) { - // The environment is Node.js - var cv = require('./opencv.js'); // eslint-disable-line no-var -} - QUnit.module('CoreMat', {}); QUnit.test('test_mat_creation', function(assert) { diff --git a/modules/js/test/test_objdetect.js b/modules/js/test/test_objdetect.js index e7190db337..1339029b65 100644 --- a/modules/js/test/test_objdetect.js +++ b/modules/js/test/test_objdetect.js @@ -68,11 +68,11 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // +var haarcascade_data = undefined; if (typeof module !== 'undefined' && module.exports) { // The environment is Node.js - var cv = require('./opencv.js'); // eslint-disable-line no-var - cv.FS_createLazyFile('/', 'haarcascade_frontalface_default.xml', // eslint-disable-line new-cap - 'haarcascade_frontalface_default.xml', true, false); + let fs = require("fs"); + haarcascade_data = fs.readFileSync("haarcascade_frontalface_default.xml"); } QUnit.module('Object Detection', {}); @@ -99,6 +99,10 @@ QUnit.test('Cascade classification', function(assert) { // CascadeClassifier { + if (haarcascade_data) { + cv.FS_createDataFile("/", "haarcascade_frontalface_default.xml", haarcascade_data, true, false, false); + } + let classifier = new cv.CascadeClassifier(); const modelPath = '/haarcascade_frontalface_default.xml'; diff --git a/modules/js/test/test_photo.js b/modules/js/test/test_photo.js index 62f62173d0..f04e5071ba 100644 --- a/modules/js/test/test_photo.js +++ b/modules/js/test/test_photo.js @@ -40,12 +40,6 @@ // Author : Rijubrata Bhaumik, Intel Corporation. rijubrata.bhaumik[at]intel[dot]com -if (typeof module !== 'undefined' && module.exports) { - // The environment is Node.js - var cv = require('./opencv.js'); // eslint-disable-line no-var -} - - QUnit.module('Photo', {}); QUnit.test('test_photo', function(assert) { diff --git a/modules/js/test/test_utils.js b/modules/js/test/test_utils.js index 905ac3191a..03096aec74 100644 --- a/modules/js/test/test_utils.js +++ b/modules/js/test/test_utils.js @@ -67,10 +67,6 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // -if (typeof module !== 'undefined' && module.exports) { - // The environment is Node.js - var cv = require('./opencv.js'); // eslint-disable-line no-var -} QUnit.module('Utils', {}); QUnit.test('Test vectors', function(assert) { { diff --git a/modules/js/test/test_video.js b/modules/js/test/test_video.js index 3594c21ede..13fb3beca6 100644 --- a/modules/js/test/test_video.js +++ b/modules/js/test/test_video.js @@ -67,11 +67,6 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // -if (typeof module !== 'undefined' && module.exports) { - // The environment is Node.js - var cv = require('./opencv.js'); // eslint-disable-line no-var -} - QUnit.module('Video', {}); QUnit.test('Background Segmentation', function(assert) { // BackgroundSubtractorMOG2 diff --git a/modules/js/test/tests.js b/modules/js/test/tests.js index 74a4b87e45..b099fdb3c8 100644 --- a/modules/js/test/tests.js +++ b/modules/js/test/tests.js @@ -43,8 +43,9 @@ testrunner.options.maxBlockDuration = 20000; // cause opencv_js.js need time to testrunner.run( { - code: 'opencv.js', - tests: ['test_mat.js', + code: {path: "opencv.js", namespace: "cv"}, + tests: ['init_cv.js', + 'test_mat.js', 'test_utils.js', 'test_core.js', 'test_imgproc.js', @@ -57,7 +58,10 @@ testrunner.run( }, function(err, report) { console.log(report.failed + ' failed, ' + report.passed + ' passed'); - if (report.failed) { + if (report.failed || err) { + if (err) { + console.log(err); + } process.on('exit', function() { process.exit(1); }); diff --git a/platforms/js/build_js.py b/platforms/js/build_js.py index f0701212af..d1c15a9c76 100644 --- a/platforms/js/build_js.py +++ b/platforms/js/build_js.py @@ -192,7 +192,7 @@ class Builder: if self.options.simd: flags += "-msimd128 " if self.options.build_flags: - flags += self.options.build_flags + flags += self.options.build_flags + " " if self.options.webnn: flags += "-s USE_WEBNN=1 " flags += "-s EXPORTED_FUNCTIONS=\"['_malloc', '_free']\""