perf_threshold.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. const isNodeJs = (typeof window) === 'undefined'? true : false;
  2. if (isNodeJs) {
  3. var Benchmark = require('benchmark');
  4. var cv = require('../../opencv');
  5. var HelpFunc = require('../perf_helpfunc');
  6. var Base = require('../base');
  7. } else {
  8. var paramsElement = document.getElementById('params');
  9. var runButton = document.getElementById('runButton');
  10. var logElement = document.getElementById('log');
  11. }
  12. function perf() {
  13. console.log('opencv.js loaded');
  14. if (isNodeJs) {
  15. global.cv = cv;
  16. global.combine = HelpFunc.combine;
  17. global.log = HelpFunc.log;
  18. global.decodeParams2Case = HelpFunc.decodeParams2Case;
  19. global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite;
  20. global.addKernelCase = HelpFunc.addKernelCase;
  21. global.cvSize = Base.getCvSize();
  22. } else {
  23. enableButton();
  24. cvSize = getCvSize();
  25. }
  26. let totalCaseNum, currentCaseId;
  27. const typicalMatSizes = [cvSize.szVGA, cvSize.sz720p, cvSize.sz1080p, cvSize.szODD];
  28. const matTypes = ['CV_8UC1', 'CV_16SC1', 'CV_32FC1', 'CV_64FC1'];
  29. const threshTypes = ['THRESH_BINARY', 'THRESH_BINARY_INV', 'THRESH_TRUNC', 'THRESH_TOZERO', 'THRESH_TOZERO_INV'];
  30. const combiSizeMatTypeThreshType = combine(typicalMatSizes, matTypes, threshTypes);
  31. const combiSizeOnly = combine(typicalMatSizes, ['CV_8UC1'], ['THRESH_BINARY|THRESH_OTSU']);
  32. function addThresholdCase(suite, type) {
  33. suite.add('threshold', function() {
  34. if (type == "sizeonly") {
  35. cv.threshold(src, dst, threshold, thresholdMax, cv.THRESH_BINARY|cv.THRESH_OTSU);
  36. } else {
  37. cv.threshold(src, dst, threshold, thresholdMax, threshType);
  38. }
  39. }, {
  40. 'setup': function() {
  41. let matSize = this.params.matSize;
  42. let type = this.params.modeType;
  43. let src, dst, matType, threshType;
  44. if (type == "sizeonly") {
  45. src = new cv.Mat(matSize, cv.CV_8UC1);
  46. dst = new cv.Mat(matSize, cv.CV_8UC1);
  47. } else {
  48. matType = cv[this.params.matType];
  49. threshType = cv[this.params.threshType];
  50. src = new cv.Mat(matSize, matType);
  51. dst = new cv.Mat(matSize, matType);
  52. }
  53. let threshold = 127.0;
  54. let thresholdMax = 210.0;
  55. let srcView = src.data;
  56. srcView[0] = 0;
  57. srcView[1] = 100;
  58. srcView[2] = 200;
  59. },
  60. 'teardown': function() {
  61. src.delete();
  62. dst.delete();
  63. }
  64. });
  65. }
  66. function addThresholdModecase(suite, combination, type) {
  67. totalCaseNum += combination.length;
  68. for (let i = 0; i < combination.length; ++i) {
  69. let matSize = combination[i][0];
  70. let matType = 'CV_8UC1';
  71. let threshType = 'THRESH_BINARY|THRESH_OTSU';
  72. if (type != "sizeonly") {
  73. matType = combination[i][1];
  74. threshType = combination[i][2];
  75. }
  76. let params = {matSize: matSize, matType: matType, threshType: threshType, modeType: type};
  77. addKernelCase(suite, params, type, addThresholdCase);
  78. }
  79. }
  80. function genBenchmarkCase(paramsContent) {
  81. let suite = new Benchmark.Suite;
  82. totalCaseNum = 0;
  83. currentCaseId = 0;
  84. let params = "";
  85. let paramObjs = [];
  86. paramObjs.push({name:"size", value:"", reg:[""], index:0});
  87. if (/\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*THRESH\_\w+\)/g.test(paramsContent.toString())) {
  88. params = paramsContent.toString().match(/\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*THRESH\_\w+\)/g)[0];
  89. paramObjs.push({name:"matType", value:"", reg:["/CV\_[0-9]+[A-z][A-z][0-9]/"], index:1});
  90. paramObjs.push({name:"threshType", value:"", reg:["/THRESH\_[A-z]+\_?[A-z]*/"], index:2});
  91. } else if (/[\ ]*[0-9]+x[0-9]+[\ ]*/g.test(paramsContent.toString())) {
  92. params = paramsContent.toString().match(/[\ ]*[0-9]+x[0-9]+[\ ]*/g)[0];
  93. paramObjs.push({name:"matType", value:"CV_8UC1", reg:[""], index:1});
  94. paramObjs.push({name:"threshType", value:"THRESH_BINARY|THRESH_OTSU", reg:[""], index:2});
  95. }
  96. if(params != ""){
  97. let locationList = decodeParams2Case(params, paramObjs,combinations);
  98. for (let i = 0; i < locationList.length; i++){
  99. let first = locationList[i][0];
  100. let second = locationList[i][1];
  101. if (first == 0) {
  102. addThresholdModecase(suite, [combinations[first][second]], "normal");
  103. } else {
  104. addThresholdModecase(suite, [combinations[first][second]], "sizeonly");
  105. }
  106. }
  107. } else {
  108. log("no filter or getting invalid params, run all the cases");
  109. addThresholdModecase(suite, combiSizeMatTypeThreshType, "normal");
  110. addThresholdModecase(suite, combiSizeOnly, "sizeonly");
  111. }
  112. setBenchmarkSuite(suite, "threshold", currentCaseId);
  113. log(`Running ${totalCaseNum} tests from Threshold`);
  114. suite.run({ 'async': true }); // run the benchmark
  115. }
  116. // init
  117. let combinations = [combiSizeMatTypeThreshType, combiSizeOnly];
  118. // set test filter params
  119. if (isNodeJs) {
  120. const args = process.argv.slice(2);
  121. let paramsContent = '';
  122. if (/--test_param_filter=\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*THRESH\_\w+\)/g.test(args.toString())) {
  123. paramsContent = args.toString().match(/\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*THRESH\_\w+\)/g)[0];
  124. } else if (/--test_param_filter=[\ ]*[0-9]+x[0-9]+[\ ]*/g.test(args.toString())) {
  125. paramsContent = args.toString().match(/[\ ]*[0-9]+x[0-9]+[\ ]*/g)[0];
  126. }
  127. genBenchmarkCase(paramsContent);
  128. } else {
  129. runButton.onclick = function() {
  130. let paramsContent = paramsElement.value;
  131. genBenchmarkCase(paramsContent);
  132. if (totalCaseNum !== 0) {
  133. disableButton();
  134. }
  135. }
  136. }
  137. };
  138. async function main() {
  139. if (cv instanceof Promise) {
  140. cv = await cv;
  141. perf();
  142. } else {
  143. cv.onRuntimeInitialized = perf;
  144. }
  145. }
  146. main();