From 8d232a63ad64dff30e6586304b42620e703d353d Mon Sep 17 00:00:00 2001 From: lionkun <871518554@qq.com> Date: Wed, 24 Mar 2021 16:34:22 +0800 Subject: [PATCH] fix the perf tests of OpenCV.js so that it can run on Node.js successfully --- modules/js/perf/base.js | 9 +++++++++ modules/js/perf/perf_helpfunc.js | 9 ++++++++- modules/js/perf/perf_imgproc/perf_blur.js | 5 ++++- modules/js/perf/perf_imgproc/perf_cvtcolor.js | 8 ++++++-- modules/js/perf/perf_imgproc/perf_dilate.js | 5 ++++- modules/js/perf/perf_imgproc/perf_erode.js | 5 ++++- modules/js/perf/perf_imgproc/perf_filter2D.js | 5 ++++- modules/js/perf/perf_imgproc/perf_gaussianBlur.js | 5 ++++- modules/js/perf/perf_imgproc/perf_medianBlur.js | 5 ++++- modules/js/perf/perf_imgproc/perf_pyrDown.js | 5 ++++- modules/js/perf/perf_imgproc/perf_remap.js | 5 ++++- modules/js/perf/perf_imgproc/perf_resize.js | 6 +++++- modules/js/perf/perf_imgproc/perf_scharr.js | 5 ++++- modules/js/perf/perf_imgproc/perf_sobel.js | 5 ++++- modules/js/perf/perf_imgproc/perf_threshold.js | 5 ++++- modules/js/perf/perf_imgproc/perf_warpAffine.js | 7 ++++++- modules/js/perf/perf_imgproc/perf_warpPerspective.js | 7 ++++++- 17 files changed, 84 insertions(+), 17 deletions(-) diff --git a/modules/js/perf/base.js b/modules/js/perf/base.js index 3948f21254..f44b8e7f20 100644 --- a/modules/js/perf/base.js +++ b/modules/js/perf/base.js @@ -1,5 +1,10 @@ if (typeof window === 'undefined') { var cv = require("../opencv"); + if (cv instanceof Promise) { + loadOpenCV(); + } else { + cv.onRuntimeInitialized = perf; + } } let gCvSize; @@ -24,6 +29,10 @@ function getCvSize() { return gCvSize; } +async function loadOpenCV() { + cv = await cv; +} + if (typeof window === 'undefined') { exports.getCvSize = getCvSize; } \ No newline at end of file diff --git a/modules/js/perf/perf_helpfunc.js b/modules/js/perf/perf_helpfunc.js index e42f4ad807..c2ad7f2e0f 100644 --- a/modules/js/perf/perf_helpfunc.js +++ b/modules/js/perf/perf_helpfunc.js @@ -1,3 +1,10 @@ +const isNodeJs = (typeof window) === 'undefined'? true : false; + +if(isNodeJs) { + var Base = require("./base"); + global.getCvSize = Base.getCvSize; +} + var fillGradient = function(cv, img, delta=5) { let ch = img.channels(); console.assert(!img.empty() && img.depth() == cv.CV_8U && ch <= 4); @@ -56,8 +63,8 @@ var smoothBorder = function(cv, img, color, delta=5) { var cvtStr2cvSize = function(strSize) { let size; - let cvSize = getCvSize(); + switch(strSize) { case "127,61": size = cvSize.szODD;break; case '320,240': size = cvSize.szQVGA;break; diff --git a/modules/js/perf/perf_imgproc/perf_blur.js b/modules/js/perf/perf_imgproc/perf_blur.js index 59712fb478..66c5f240e7 100644 --- a/modules/js/perf/perf_imgproc/perf_blur.js +++ b/modules/js/perf/perf_imgproc/perf_blur.js @@ -17,7 +17,10 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_cvtcolor.js b/modules/js/perf/perf_imgproc/perf_cvtcolor.js index b5007985cc..fbae5d1bca 100644 --- a/modules/js/perf/perf_imgproc/perf_cvtcolor.js +++ b/modules/js/perf/perf_imgproc/perf_cvtcolor.js @@ -17,7 +17,11 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.constructMode = HelpFunc.constructMode; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); @@ -25,7 +29,7 @@ function perf() { } let totalCaseNum, currentCaseId; - //extra color conversions supported implicitly + // extra color conversions supported implicitly { cv.CX_BGRA2HLS = cv.COLOR_COLORCVT_MAX + cv.COLOR_BGR2HLS, cv.CX_BGRA2HLS_FULL = cv.COLOR_COLORCVT_MAX + cv.COLOR_BGR2HLS_FULL, diff --git a/modules/js/perf/perf_imgproc/perf_dilate.js b/modules/js/perf/perf_imgproc/perf_dilate.js index c4e14c7be2..5b6cd01682 100644 --- a/modules/js/perf/perf_imgproc/perf_dilate.js +++ b/modules/js/perf/perf_imgproc/perf_dilate.js @@ -17,7 +17,10 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_erode.js b/modules/js/perf/perf_imgproc/perf_erode.js index 95aba6fa21..8915ead40f 100644 --- a/modules/js/perf/perf_imgproc/perf_erode.js +++ b/modules/js/perf/perf_imgproc/perf_erode.js @@ -17,7 +17,10 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_filter2D.js b/modules/js/perf/perf_imgproc/perf_filter2D.js index d92dc2b55a..4602befcbd 100644 --- a/modules/js/perf/perf_imgproc/perf_filter2D.js +++ b/modules/js/perf/perf_imgproc/perf_filter2D.js @@ -17,7 +17,10 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_gaussianBlur.js b/modules/js/perf/perf_imgproc/perf_gaussianBlur.js index 33c5401a7e..b59aa83b84 100644 --- a/modules/js/perf/perf_imgproc/perf_gaussianBlur.js +++ b/modules/js/perf/perf_imgproc/perf_gaussianBlur.js @@ -17,7 +17,10 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_medianBlur.js b/modules/js/perf/perf_imgproc/perf_medianBlur.js index 69b7ba3ead..333bc8424c 100644 --- a/modules/js/perf/perf_imgproc/perf_medianBlur.js +++ b/modules/js/perf/perf_imgproc/perf_medianBlur.js @@ -17,7 +17,10 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_pyrDown.js b/modules/js/perf/perf_imgproc/perf_pyrDown.js index a98b109ade..957ac7684d 100644 --- a/modules/js/perf/perf_imgproc/perf_pyrDown.js +++ b/modules/js/perf/perf_imgproc/perf_pyrDown.js @@ -17,7 +17,10 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_remap.js b/modules/js/perf/perf_imgproc/perf_remap.js index fe2e5d7541..1aa69ecef7 100644 --- a/modules/js/perf/perf_imgproc/perf_remap.js +++ b/modules/js/perf/perf_imgproc/perf_remap.js @@ -17,7 +17,10 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_resize.js b/modules/js/perf/perf_imgproc/perf_resize.js index 3eef30f0e3..5262d22489 100644 --- a/modules/js/perf/perf_imgproc/perf_resize.js +++ b/modules/js/perf/perf_imgproc/perf_resize.js @@ -17,7 +17,11 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.fillGradient = HelpFunc.fillGradient; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_scharr.js b/modules/js/perf/perf_imgproc/perf_scharr.js index a76a93078c..4726e76312 100644 --- a/modules/js/perf/perf_imgproc/perf_scharr.js +++ b/modules/js/perf/perf_imgproc/perf_scharr.js @@ -17,7 +17,10 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_sobel.js b/modules/js/perf/perf_imgproc/perf_sobel.js index b7064e852a..ddc09bb8f6 100644 --- a/modules/js/perf/perf_imgproc/perf_sobel.js +++ b/modules/js/perf/perf_imgproc/perf_sobel.js @@ -17,7 +17,10 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_threshold.js b/modules/js/perf/perf_imgproc/perf_threshold.js index 381ddaeade..629628748d 100644 --- a/modules/js/perf/perf_imgproc/perf_threshold.js +++ b/modules/js/perf/perf_imgproc/perf_threshold.js @@ -17,7 +17,10 @@ function perf() { if (isNodeJs) { global.cv = cv; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase; global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_warpAffine.js b/modules/js/perf/perf_imgproc/perf_warpAffine.js index c63cd60e61..dc3cf67af4 100644 --- a/modules/js/perf/perf_imgproc/perf_warpAffine.js +++ b/modules/js/perf/perf_imgproc/perf_warpAffine.js @@ -16,8 +16,13 @@ function perf() { console.log('opencv.js loaded'); if (isNodeJs) { global.cv = cv; + global.fillGradient = HelpFunc.fillGradient; + global.smoothBorder = HelpFunc.smoothBorder; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase global.cvSize = Base.getCvSize(); } else { enableButton(); diff --git a/modules/js/perf/perf_imgproc/perf_warpPerspective.js b/modules/js/perf/perf_imgproc/perf_warpPerspective.js index dcde2fb22c..252729e3f0 100644 --- a/modules/js/perf/perf_imgproc/perf_warpPerspective.js +++ b/modules/js/perf/perf_imgproc/perf_warpPerspective.js @@ -16,8 +16,13 @@ function perf() { console.log('opencv.js loaded'); if (isNodeJs) { global.cv = cv; + global.fillGradient = HelpFunc.fillGradient; + global.smoothBorder = HelpFunc.smoothBorder; global.combine = HelpFunc.combine; - global.cvtStr2cvSize = HelpFunc.cvtStr2cvSize; + global.log = HelpFunc.log; + global.decodeParams2Case = HelpFunc.decodeParams2Case; + global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite; + global.addKernelCase = HelpFunc.addKernelCase global.cvSize = Base.getCvSize(); } else { enableButton();