perf_helpfunc.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. const isNodeJs = (typeof window) === 'undefined'? true : false;
  2. if(isNodeJs) {
  3. var Base = require("./base");
  4. global.getCvSize = Base.getCvSize;
  5. }
  6. var fillGradient = function(cv, img, delta=5) {
  7. let ch = img.channels();
  8. console.assert(!img.empty() && img.depth() == cv.CV_8U && ch <= 4);
  9. let n = 255 / delta;
  10. for(let r = 0; r < img.rows; ++r) {
  11. let kR = r % (2*n);
  12. let valR = (kR<=n) ? delta*kR : delta*(2*n-kR);
  13. for(let c = 0; c < img.cols; ++c) {
  14. let kC = c % (2*n);
  15. let valC = (kC<=n) ? delta*kC : delta*(2*n-kC);
  16. let vals = [valR, valC, 200*r/img.rows, 255];
  17. let p = img.ptr(r, c);
  18. for(let i = 0; i < ch; ++i) p[i] = vals[i];
  19. }
  20. }
  21. }
  22. var smoothBorder = function(cv, img, color, delta=5) {
  23. let ch = img.channels();
  24. console.assert(!img.empty() && img.depth() == cv.CV_8U && ch <= 4);
  25. let n = 100/delta;
  26. let nR = Math.min(n, (img.rows+1)/2);
  27. let nC = Math.min(n, (img.cols+1)/2);
  28. let s = new cv.Scalar();
  29. for (let r = 0; r < nR; r++) {
  30. let k1 = r*delta/100.0, k2 = 1-k1;
  31. for(let c = 0; c < img.cols; c++) {
  32. let view = img.ptr(r, c);
  33. for(let i = 0; i < ch; i++) s[i] = view[i];
  34. for(let i = 0; i < ch; i++) view[i] = s[i]*k1 + color[i] * k2;
  35. }
  36. for(let c=0; c < img.cols; c++) {
  37. let view = img.ptr(img.rows-r-1, c);
  38. for(let i = 0; i < ch; i++) s[i] = view[i];
  39. for(let i = 0; i < ch; i++) view[i] = s[i]*k1 + color[i] * k2;
  40. }
  41. }
  42. for (let r = 0; r < img.rows; r++) {
  43. for(let c = 0; c < nC; c++) {
  44. let k1 = c*delta/100.0, k2 = 1-k1;
  45. let view = img.ptr(r, c);
  46. for(let i = 0; i < ch; i++) s[i] = view[i];
  47. for(let i = 0; i < ch; i++) view[i] = s[i]*k1 + color[i] * k2;
  48. }
  49. for(let c = 0; c < n; c++) {
  50. let k1 = c*delta/100.0, k2 = 1-k1;
  51. let view = img.ptr(r, img.cols-c-1);
  52. for(let i = 0; i < ch; i++) s[i] = view[i];
  53. for(let i = 0; i < ch; i++) view[i] = s[i]*k1 + color[i] * k2;
  54. }
  55. }
  56. }
  57. var cvtStr2cvSize = function(strSize) {
  58. let size;
  59. let cvSize = getCvSize();
  60. switch(strSize) {
  61. case "127,61": size = cvSize.szODD;break;
  62. case '320,240': size = cvSize.szQVGA;break;
  63. case '640,480': size = cvSize.szVGA;break;
  64. case '800,600': size = cvSize.szSVGA;break;
  65. case '960,540': size = cvSize.szqHD;break;
  66. case '1024,768': size = cvSize.szXGA;break;
  67. case '1280,720': size = cvSize.sz720p;break;
  68. case '1280,1024': size = cvSize.szSXGA;break;
  69. case '1920,1080': size = cvSize.sz1080p;break;
  70. case "130,60": size = cvSize.sz130x60;break;
  71. case '213,120': size = cvSize.sz213x120;break;
  72. default: console.error("unsupported size for this case");
  73. }
  74. return size;
  75. }
  76. var combine = function() {
  77. let result = [[]];
  78. for (let i = 0; i < arguments.length; ++i) {
  79. result = permute(result, arguments[i]);
  80. }
  81. return result;
  82. }
  83. function permute (source, target) {
  84. let result = [];
  85. for (let i = 0; i < source.length; ++i) {
  86. for (let j = 0; j < target.length; ++j) {
  87. let tmp = source[i].slice();
  88. tmp.push(target[j]);
  89. result.push(tmp);
  90. }
  91. }
  92. return result;
  93. }
  94. var constructMode = function (startStr, sChannel, dChannel) {
  95. let modeList = []
  96. for (let j in dChannel) {
  97. modeList.push(startStr+sChannel+"2"+dChannel[j])
  98. }
  99. return modeList;
  100. }
  101. var enableButton = function () {
  102. runButton.removeAttribute('disabled');
  103. runButton.setAttribute('class', 'btn btn-primary');
  104. runButton.innerHTML = 'Run';
  105. }
  106. var disableButton = function () {
  107. runButton.setAttribute("disabled", "disabled");
  108. runButton.setAttribute('class', 'btn btn-primary disabled');
  109. runButton.innerHTML = "Running";
  110. }
  111. var log = function (message) {
  112. console.log(message);
  113. if (!isNodeJs) {
  114. logElement.innerHTML += `\n${'\t' + message}`;
  115. }
  116. }
  117. var addKernelCase = function (suite, params, type, kernelFunc) {
  118. kernelFunc(suite, type);
  119. let index = suite.length - 1;
  120. suite[index].params = params;
  121. }
  122. function constructParamLog(params, kernel) {
  123. let paramLog = '';
  124. if (kernel == "cvtcolor") {
  125. let mode = params.mode;
  126. let size = params.size;
  127. paramLog = `params: (${parseInt(size[0])}x${parseInt(size[1])}, ${mode})`;
  128. } else if (kernel == "resize") {
  129. let matType = params.matType;
  130. let size1 = params.from;
  131. let size2 = params.to;
  132. paramLog = `params: (${matType},${parseInt(size1.width)}x${parseInt(size1.height)},`+
  133. `${parseInt(size2.width)}x${parseInt(size2.height)})`;
  134. } else if (kernel == "threshold") {
  135. let matSize = params.matSize;
  136. let matType = params.matType;
  137. let threshType = params.threshType;
  138. paramLog = `params: (${parseInt(matSize.width)}x${parseInt(matSize.height)},`+
  139. `${matType},${threshType})`;
  140. } else if (kernel == "sobel") {
  141. let size = params.size;
  142. let ddepth = params.ddepth;
  143. let dxdy = params.dxdy;
  144. let ksize = params.ksize;
  145. let borderType = params.borderType;
  146. paramLog = `params: (${parseInt(size[0])}x${parseInt(size[1])},`+
  147. `${ddepth},${dxdy},${borderType}, ksize:${ksize})`;
  148. } else if (kernel == "filter2d") {
  149. let size = params.size;
  150. let ksize = params.ksize;
  151. let borderMode = params.borderMode;
  152. paramLog = `params: (${parseInt(size.width)}x${parseInt(size.height)},`+
  153. `${ksize},${borderMode})`;
  154. } else if (kernel == "scharr") {
  155. let size = params.size;
  156. let ddepth = params.ddepth;
  157. let dxdy = params.dxdy;
  158. let borderType = params.borderType;
  159. paramLog = `params: (${parseInt(size[0])}x${parseInt(size[1])},`+
  160. `${ddepth},${dxdy},${borderType})`;
  161. } else if (kernel == "gaussianBlur" || kernel == "blur") {
  162. let size = params.size;
  163. let matType = params.matType;
  164. let borderType = params.borderType;
  165. let ksize = params.ksize;
  166. paramLog = `params: (${parseInt(size.width)}x${parseInt(size.height)},`+
  167. `${matType},${borderType}, ksize: (${ksize}x${ksize}))`;
  168. } else if (kernel == "medianBlur") {
  169. let size = params.size;
  170. let matType = params.matType;
  171. let ksize = params.ksize;
  172. paramLog = `params: (${parseInt(size.width)}x${parseInt(size.height)},`+
  173. `${matType}, ksize: ${ksize})`;
  174. } else if (kernel == "erode" || kernel == "dilate" || kernel == "pyrDown") {
  175. let size = params.size;
  176. let matType = params.matType;
  177. paramLog = `params: (${parseInt(size.width)}x${parseInt(size.height)},`+
  178. `${matType})`;
  179. } else if (kernel == "remap") {
  180. let size = params.size;
  181. let matType = params.matType;
  182. let mapType = params.mapType;
  183. let interType = params.interType;
  184. paramLog = `params: (${parseInt(size.width)}x${parseInt(size.height)},`+
  185. `${matType}, ${mapType}, ${interType})`;
  186. } else if (kernel == "warpAffine" || kernel == "warpPerspective") {
  187. let size = params.size;
  188. let interType = params.interType;
  189. let borderMode = params.borderMode;
  190. paramLog = `params: (${parseInt(size.width)}x${parseInt(size.height)},`+
  191. `${interType}, ${borderMode})`;
  192. }
  193. return paramLog;
  194. }
  195. var setBenchmarkSuite = function (suite, kernel, currentCaseId) {
  196. suite
  197. // add listeners
  198. .on('cycle', function(event) {
  199. ++currentCaseId;
  200. let params = event.target.params;
  201. paramLog = constructParamLog(params, kernel);
  202. log(`=== ${event.target.name} ${currentCaseId} ===`);
  203. log(paramLog);
  204. log('elapsed time:' +String(event.target.times.elapsed*1000)+' ms');
  205. log('mean time:' +String(event.target.stats.mean*1000)+' ms');
  206. log('stddev time:' +String(event.target.stats.deviation*1000)+' ms');
  207. log(String(event.target));
  208. })
  209. .on('error', function(event) { log(`test case ${event.target.name} failed`); })
  210. .on('complete', function(event) {
  211. log(`\n ###################################`)
  212. log(`Finished testing ${event.currentTarget.length} cases \n`);
  213. if (!isNodeJs) {
  214. runButton.removeAttribute('disabled');
  215. runButton.setAttribute('class', 'btn btn-primary');
  216. runButton.innerHTML = 'Run';
  217. }
  218. });
  219. }
  220. var decodeParams2Case = function(paramContent, paramsList, combinations) {
  221. let sizeString = (paramContent.match(/[0-9]+x[0-9]+/g) || []).toString();
  222. let sizes = (sizeString.match(/[0-9]+/g) || []);
  223. let paramSize = paramsList.length;
  224. let paramObjs = []
  225. let sizeCount = 0;
  226. for (let i = 0; i < paramSize; i++) {
  227. let param = paramsList[i];
  228. let paramName = param.name;
  229. let paramValue = param.value;
  230. let paramReg = param.reg;
  231. let paramIndex = param.index;
  232. if(paramValue != "") {
  233. paramObjs.push({name: paramName, value: paramValue, index: paramIndex});
  234. } else if (paramName.startsWith('size')) {
  235. let sizeStr = sizes.slice(sizeCount, sizeCount+2).toString();
  236. paramValue = cvtStr2cvSize(sizeStr);
  237. sizeCount += 2;
  238. paramObjs.push({name: paramName, value: paramValue, index: paramIndex});
  239. } else {
  240. for (let index in paramReg) {
  241. let reg = eval(paramReg[index]);
  242. if ('loc' in param) {
  243. paramValue = (paramContent.match(reg) || [])[param.loc].toString();
  244. } else {
  245. paramValue = (paramContent.match(reg) || []).toString();
  246. }
  247. if (paramValue != "") {
  248. paramObjs.push({name: paramName, value: paramValue, index: paramIndex});
  249. break;
  250. }
  251. }
  252. }
  253. }
  254. let location = [];
  255. for (let i = 0; i < combinations.length; ++i) {
  256. let combination = combinations[i];
  257. for (let j = 0; j < combination.length; ++j) {
  258. if (judgeCombin(combination[j], paramObjs)) {
  259. location.push([i,j]);
  260. }
  261. }
  262. }
  263. return location;
  264. }
  265. function judgeCombin(combination, paramObjs) {
  266. for (let i =0; i < paramObjs.length; i++) {
  267. if (paramObjs[i].value != combination[paramObjs[i].index]){
  268. return false;
  269. }
  270. }
  271. return true;
  272. }
  273. if (typeof window === 'undefined') {
  274. exports.enableButton = enableButton;
  275. exports.disableButton = disableButton;
  276. exports.fillGradient = fillGradient;
  277. exports.smoothBorder = smoothBorder;
  278. exports.cvtStr2cvSize = cvtStr2cvSize;
  279. exports.combine = combine;
  280. exports.constructMode = constructMode;
  281. exports.log = log;
  282. exports.decodeParams2Case = decodeParams2Case;
  283. exports.setBenchmarkSuite = setBenchmarkSuite;
  284. exports.addKernelCase = addKernelCase;
  285. }