perf_warpAffine.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.fillGradient = HelpFunc.fillGradient;
  17. global.smoothBorder = HelpFunc.smoothBorder;
  18. global.combine = HelpFunc.combine;
  19. global.log = HelpFunc.log;
  20. global.decodeParams2Case = HelpFunc.decodeParams2Case;
  21. global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite;
  22. global.addKernelCase = HelpFunc.addKernelCase
  23. global.cvSize = Base.getCvSize();
  24. } else {
  25. enableButton();
  26. cvSize = getCvSize();
  27. }
  28. let totalCaseNum, currentCaseId;
  29. const WarpAffineSize = [cvSize.szVGA, cvSize.sz720p, cvSize.sz1080p];
  30. const InterType = ["INTER_NEAREST", "INTER_LINEAR"];
  31. const BorderMode = ["BORDER_CONSTANT", "BORDER_REPLICATE"]
  32. const combiWarpAffine = combine(WarpAffineSize, InterType, BorderMode);
  33. function addWarpAffineCase(suite, type) {
  34. suite.add('warpAffine', function() {
  35. cv.warpAffine(src, dst, warpMat, sz, interType, borderMode, borderColor);
  36. }, {
  37. 'setup': function() {
  38. let sz = this.params.size;
  39. let interType = cv[this.params.interType];
  40. let borderMode = cv[this.params.borderMode];
  41. let srcSize = new cv.Size(512, 512);
  42. let borderColor = new cv.Scalar.all(150);
  43. let src = new cv.Mat(srcSize, cv.CV_8UC4);
  44. let dst = new cv.Mat(sz, cv.CV_8UC4);
  45. fillGradient(cv, src);
  46. if (borderMode == cv.BORDER_CONSTANT) {
  47. smoothBorder(cv, src, borderMode, 1);
  48. }
  49. let point = new cv.Point(src.cols/2.0, src.rows/2.0);
  50. let warpMat = cv.getRotationMatrix2D(point, 30.0, 2.2);
  51. },
  52. 'teardown': function() {
  53. src.delete();
  54. dst.delete();
  55. warpMat.delete();
  56. }
  57. });
  58. }
  59. function addWarpAffineModeCase(suite, combination, type) {
  60. totalCaseNum += combination.length;
  61. for (let i = 0; i < combination.length; ++i) {
  62. let size = combination[i][0];
  63. let interType = combination[i][1];
  64. let borderMode = combination[i][2];
  65. let params = {size: size, interType:interType, borderMode:borderMode};
  66. addKernelCase(suite, params, type, addWarpAffineCase);
  67. }
  68. }
  69. function genBenchmarkCase(paramsContent) {
  70. let suite = new Benchmark.Suite;
  71. totalCaseNum = 0;
  72. currentCaseId = 0;
  73. if (/\([0-9]+x[0-9]+,[\ ]*INTER\_\w+,[\ ]*BORDER\_\w+\)/g.test(paramsContent.toString())) {
  74. let params = paramsContent.toString().match(/\([0-9]+x[0-9]+,[\ ]*INTER\_\w+,[\ ]*BORDER\_\w+\)/g)[0];
  75. let paramObjs = [];
  76. paramObjs.push({name:"size", value:"", reg:[""], index:0});
  77. paramObjs.push({name:"interType", value: "", reg:["/INTER\_\\w+/"], index:1});
  78. paramObjs.push({name:"borderMode", value: "", reg:["/BORDER\_\\w+/"], index:2});
  79. let locationList = decodeParams2Case(params, paramObjs, warpAffineCombinations);
  80. for (let i = 0; i < locationList.length; i++){
  81. let first = locationList[i][0];
  82. let second = locationList[i][1];
  83. addWarpAffineModeCase(suite, [warpAffineCombinations[first][second]], first);
  84. }
  85. } else {
  86. log("no filter or getting invalid params, run all the cases");
  87. addWarpAffineModeCase(suite, combiWarpAffine, 0);
  88. }
  89. setBenchmarkSuite(suite, "warpAffine", currentCaseId);
  90. log(`Running ${totalCaseNum} tests from warpAffine`);
  91. suite.run({ 'async': true }); // run the benchmark
  92. }
  93. let warpAffineCombinations = [combiWarpAffine];
  94. if (isNodeJs) {
  95. const args = process.argv.slice(2);
  96. let paramsContent = '';
  97. if (/--test_param_filter=\([0-9]+x[0-9]+,[\ ]*INTER\_\w+,[\ ]*BORDER\_\w+\)/g.test(args.toString())) {
  98. paramsContent = args.toString().match(/\([0-9]+x[0-9]+,[\ ]*INTER\_\w+,[\ ]*BORDER\_\w+\)/g)[0];
  99. }
  100. genBenchmarkCase(paramsContent);
  101. } else {
  102. runButton.onclick = function() {
  103. let paramsContent = paramsElement.value;
  104. genBenchmarkCase(paramsContent);
  105. if (totalCaseNum !== 0) {
  106. disableButton();
  107. }
  108. }
  109. }
  110. };
  111. async function main() {
  112. if (cv instanceof Promise) {
  113. cv = await cv;
  114. perf();
  115. } else {
  116. cv.onRuntimeInitialized = perf;
  117. }
  118. }
  119. main();