perf_resize.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.fillGradient = HelpFunc.fillGradient;
  18. global.log = HelpFunc.log;
  19. global.decodeParams2Case = HelpFunc.decodeParams2Case;
  20. global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite;
  21. global.addKernelCase = HelpFunc.addKernelCase;
  22. global.cvSize = Base.getCvSize();
  23. } else {
  24. enableButton();
  25. cvSize = getCvSize();
  26. }
  27. let totalCaseNum, currentCaseId;
  28. const matTypesUpLinear = ['CV_8UC1', 'CV_8UC2', 'CV_8UC3', 'CV_8UC4'];
  29. const size1UpLinear = [cvSize.szVGA];
  30. const size2UpLinear = [cvSize.szqHD, cvSize.sz720p];
  31. const combiUpLinear = combine(matTypesUpLinear, size1UpLinear, size2UpLinear);
  32. const combiDownLinear = [
  33. ['CV_8UC1', cvSize.szVGA, cvSize.szQVGA],
  34. ['CV_8UC2', cvSize.szVGA, cvSize.szQVGA],
  35. ['CV_8UC3', cvSize.szVGA, cvSize.szQVGA],
  36. ['CV_8UC4', cvSize.szVGA, cvSize.szQVGA],
  37. ['CV_8UC1', cvSize.szqHD, cvSize.szVGA],
  38. ['CV_8UC2', cvSize.szqHD, cvSize.szVGA],
  39. ['CV_8UC3', cvSize.szqHD, cvSize.szVGA],
  40. ['CV_8UC4', cvSize.szqHD, cvSize.szVGA],
  41. ['CV_8UC1', cvSize.sz720p, cvSize.sz213x120],// face detection min_face_size = 20%
  42. ['CV_8UC2', cvSize.sz720p, cvSize.sz213x120],// face detection min_face_size = 20%
  43. ['CV_8UC3', cvSize.sz720p, cvSize.sz213x120],// face detection min_face_size = 20%
  44. ['CV_8UC4', cvSize.sz720p, cvSize.sz213x120],// face detection min_face_size = 20%
  45. ['CV_8UC1', cvSize.sz720p, cvSize.szVGA],
  46. ['CV_8UC2', cvSize.sz720p, cvSize.szVGA],
  47. ['CV_8UC3', cvSize.sz720p, cvSize.szVGA],
  48. ['CV_8UC4', cvSize.sz720p, cvSize.szVGA],
  49. ['CV_8UC1', cvSize.sz720p, cvSize.szQVGA],
  50. ['CV_8UC2', cvSize.sz720p, cvSize.szQVGA],
  51. ['CV_8UC3', cvSize.sz720p, cvSize.szQVGA],
  52. ['CV_8UC4', cvSize.sz720p, cvSize.szQVGA]
  53. ];
  54. const matTypesAreaFast = ['CV_8UC1', 'CV_8UC3', 'CV_8UC4', 'CV_16UC1', 'CV_16UC3', 'CV_16UC4'];
  55. const sizesAreaFast = [cvSize.szVGA, cvSize.szqHD, cvSize.sz720p, cvSize.sz1080p];
  56. const scalesAreaFast = [2];
  57. const combiAreaFast = combine(matTypesAreaFast, sizesAreaFast, scalesAreaFast);
  58. function addResizeCase(suite, type) {
  59. suite.add('resize', function() {
  60. if (type == "area") {
  61. cv.resize(src, dst, dst.size(), 0, 0, cv.INTER_AREA);
  62. } else {
  63. cv.resize(src, dst, to, 0, 0, cv.INTER_LINEAR_EXACT);
  64. }
  65. }, {
  66. 'setup': function() {
  67. let from = this.params.from;
  68. let to = this.params.to;
  69. let matType = cv[this.params.matType];
  70. let src = new cv.Mat(from, matType);
  71. let type = this.params.modeType;
  72. let dst;
  73. if (type == "area") {
  74. dst = new cv.Mat(from.height/scale, from.width/scale, matType);
  75. } else {
  76. dst = new cv.Mat(to, matType);
  77. fillGradient(cv, src);
  78. }
  79. },
  80. 'teardown': function() {
  81. src.delete();
  82. dst.delete();
  83. }
  84. });
  85. }
  86. function addResizeModeCase(suite, combination, type) {
  87. totalCaseNum += combination.length;
  88. for (let i = 0; i < combination.length; ++i) {
  89. let matType = combination[i][0];
  90. let from = combination[i][1];
  91. let params;
  92. if (type == "area") {
  93. let scale = combination[i][2];
  94. params = { from: from, scale: scale, matType: matType, modeType: type };
  95. } else {
  96. let to = combination[i][2];
  97. params = { from: from, to: to, matType: matType, modeType: type};
  98. }
  99. addKernelCase(suite, params, type, addResizeCase)
  100. }
  101. }
  102. function genBenchmarkCase(paramsContent) {
  103. let suite = new Benchmark.Suite;
  104. totalCaseNum = 0;
  105. currentCaseId = 0;
  106. if (/\(\w+,[\ ]*[0-9]+x[0-9]+,[\ ]*[0-9]+x[0-9]+\)/g.test(paramsContent.toString())) {
  107. let params = paramsContent.toString().match(/\(\w+,[\ ]*[0-9]+x[0-9]+,[\ ]*[0-9]+x[0-9]+\)/g)[0];
  108. let paramObjs = [];
  109. paramObjs.push({name:"matType", value:"", reg:["/CV\_[0-9]+[A-z][A-z][0-9]/"], index:0});
  110. paramObjs.push({name:"size1", value:"", reg:[""], index:1});
  111. paramObjs.push({name:"size2", value:"", reg:[""], index:2});
  112. let locationList = decodeParams2Case(params, paramObjs,combinations);
  113. for (let i = 0; i < locationList.length; i++){
  114. let first = locationList[i][0];
  115. let second = locationList[i][1];
  116. addResizeModeCase(suite, [combinations[first][second]], "linear");
  117. }
  118. } else {
  119. log("no filter or getting invalid params, run all the cases");
  120. addResizeModeCase(suite, combiUpLinear, "linear");
  121. addResizeModeCase(suite, combiDownLinear, "linear");
  122. }
  123. setBenchmarkSuite(suite, "resize", currentCaseId);
  124. log(`Running ${totalCaseNum} tests from Resize`);
  125. suite.run({ 'async': true }); // run the benchmark
  126. }
  127. // init
  128. let combinations = [combiUpLinear, combiDownLinear];//, combiAreaFast];
  129. // set test filter params
  130. if (isNodeJs) {
  131. const args = process.argv.slice(2);
  132. let paramsContent = '';
  133. if (/--test_param_filter=\(\w+,[\ ]*[0-9]+x[0-9]+,[\ ]*[0-9]+x[0-9]+\)/g.test(args.toString())) {
  134. paramsContent = args.toString().match(/\(\w+,[\ ]*[0-9]+x[0-9]+,[\ ]*[0-9]+x[0-9]+\)/g)[0];
  135. }
  136. genBenchmarkCase(paramsContent);
  137. } else {
  138. runButton.onclick = function() {
  139. let paramsContent = paramsElement.value;
  140. genBenchmarkCase(paramsContent);
  141. if (totalCaseNum !== 0) {
  142. disableButton();
  143. }
  144. }
  145. }
  146. };
  147. async function main() {
  148. if (cv instanceof Promise) {
  149. cv = await cv;
  150. perf();
  151. } else {
  152. cv.onRuntimeInitialized = perf;
  153. }
  154. }
  155. main();