js_style_transfer.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Style Transfer Example</title>
  6. <link href="js_example_style.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <h2>Style Transfer Example</h2>
  10. <p>
  11. This tutorial shows you how to write an style transfer example with OpenCV.js.<br>
  12. To try the example you should click the <b>modelFile</b> button(and <b>configFile</b> button if needed) to upload inference model.
  13. You can find the model URLs and parameters in the <a href="#appendix">model info</a> section.
  14. Then You should change the parameters in the first code snippet according to the uploaded model.
  15. Finally click <b>Try it</b> button to see the result. You can choose any other images.<br>
  16. </p>
  17. <div class="control"><button id="tryIt" disabled>Try it</button></div>
  18. <div>
  19. <table cellpadding="0" cellspacing="0" width="0" border="0">
  20. <tr>
  21. <td>
  22. <canvas id="canvasInput" width="400" height="400"></canvas>
  23. </td>
  24. <td>
  25. <canvas id="canvasOutput" style="visibility: hidden;" width="400" height="400"></canvas>
  26. </td>
  27. </tr>
  28. <tr>
  29. <td>
  30. <div class="caption">
  31. canvasInput <input type="file" id="fileInput" name="file" accept="image/*">
  32. </div>
  33. </td>
  34. <td>
  35. <p id='status' align="left"></p>
  36. </td>
  37. </tr>
  38. <tr>
  39. <td>
  40. <div class="caption">
  41. modelFile <input type="file" id="modelFile" name="file">
  42. </div>
  43. </td>
  44. </tr>
  45. <tr>
  46. <td>
  47. <div class="caption">
  48. configFile <input type="file" id="configFile">
  49. </div>
  50. </td>
  51. </tr>
  52. </table>
  53. </div>
  54. <div>
  55. <p class="err" id="errorMessage"></p>
  56. </div>
  57. <div>
  58. <h3>Help function</h3>
  59. <p>1.The parameters for model inference which you can modify to investigate more models.</p>
  60. <textarea class="code" rows="5" cols="100" id="codeEditor" spellcheck="false"></textarea>
  61. <p>2.Main loop in which will read the image from canvas and do inference once.</p>
  62. <textarea class="code" rows="15" cols="100" id="codeEditor1" spellcheck="false"></textarea>
  63. <p>3.Get blob from image as input for net, and standardize it with <b>mean</b> and <b>std</b>.</p>
  64. <textarea class="code" rows="17" cols="100" id="codeEditor2" spellcheck="false"></textarea>
  65. <p>4.Fetch model file and save to emscripten file system once click the input button.</p>
  66. <textarea class="code" rows="17" cols="100" id="codeEditor3" spellcheck="false"></textarea>
  67. <p>5.The post-processing, including scaling and reordering.</p>
  68. <textarea class="code" rows="21" cols="100" id="codeEditor4" spellcheck="false"></textarea>
  69. </div>
  70. <div id="appendix">
  71. <h2>Model Info:</h2>
  72. </div>
  73. <script src="utils.js" type="text/javascript"></script>
  74. <script src="js_dnn_example_helper.js" type="text/javascript"></script>
  75. <script id="codeSnippet" type="text/code-snippet">
  76. inputSize = [224, 224];
  77. mean = [104, 117, 123];
  78. std = 1;
  79. swapRB = false;
  80. </script>
  81. <script id="codeSnippet1" type="text/code-snippet">
  82. main = async function() {
  83. const input = getBlobFromImage(inputSize, mean, std, swapRB, 'canvasInput');
  84. let net = cv.readNet(configPath, modelPath);
  85. net.setInput(input);
  86. const start = performance.now();
  87. const result = net.forward();
  88. const time = performance.now()-start;
  89. const output = postProcess(result);
  90. updateResult(output, time);
  91. input.delete();
  92. net.delete();
  93. result.delete();
  94. }
  95. </script>
  96. <script id="codeSnippet4" type="text/code-snippet">
  97. postProcess = function(result) {
  98. const resultData = result.data32F;
  99. const C = result.matSize[1];
  100. const H = result.matSize[2];
  101. const W = result.matSize[3];
  102. const mean = [104, 117, 123];
  103. let normData = [];
  104. for (let h = 0; h < H; ++h) {
  105. for (let w = 0; w < W; ++w) {
  106. for (let c = 0; c < C; ++c) {
  107. normData.push(resultData[c*H*W + h*W + w] + mean[c]);
  108. }
  109. normData.push(255);
  110. }
  111. }
  112. let output = new cv.matFromArray(H, W, cv.CV_8UC4, normData);
  113. return output;
  114. }
  115. </script>
  116. <script type="text/javascript">
  117. let jsonUrl = "js_style_transfer_model_info.json";
  118. drawInfoTable(jsonUrl, 'appendix');
  119. let utils = new Utils('errorMessage');
  120. utils.loadCode('codeSnippet', 'codeEditor');
  121. utils.loadCode('codeSnippet1', 'codeEditor1');
  122. let getBlobFromImageCode = 'getBlobFromImage = ' + getBlobFromImage.toString();
  123. document.getElementById('codeEditor2').value = getBlobFromImageCode;
  124. let loadModelCode = 'loadModel = ' + loadModel.toString();
  125. document.getElementById('codeEditor3').value = loadModelCode;
  126. utils.loadCode('codeSnippet4', 'codeEditor4');
  127. let canvas = document.getElementById('canvasInput');
  128. let ctx = canvas.getContext('2d');
  129. let img = new Image();
  130. img.crossOrigin = 'anonymous';
  131. img.src = 'lena.png';
  132. img.onload = function() {
  133. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  134. };
  135. let tryIt = document.getElementById('tryIt');
  136. tryIt.addEventListener('click', () => {
  137. initStatus();
  138. document.getElementById('status').innerHTML = 'Running function main()...';
  139. utils.executeCode('codeEditor');
  140. utils.executeCode('codeEditor1');
  141. if (modelPath === "") {
  142. document.getElementById('status').innerHTML = 'Runing failed.';
  143. utils.printError('Please upload model file by clicking the button first.');
  144. } else {
  145. setTimeout(main, 1);
  146. }
  147. });
  148. let fileInput = document.getElementById('fileInput');
  149. fileInput.addEventListener('change', (e) => {
  150. initStatus();
  151. loadImageToCanvas(e, 'canvasInput');
  152. });
  153. let configPath = "";
  154. let configFile = document.getElementById('configFile');
  155. configFile.addEventListener('change', async (e) => {
  156. initStatus();
  157. configPath = await loadModel(e);
  158. document.getElementById('status').innerHTML = `The config file '${configPath}' is created successfully.`;
  159. });
  160. let modelPath = "";
  161. let modelFile = document.getElementById('modelFile');
  162. modelFile.addEventListener('change', async (e) => {
  163. initStatus();
  164. modelPath = await loadModel(e);
  165. document.getElementById('status').innerHTML = `The model file '${modelPath}' is created successfully.`;
  166. configPath = "";
  167. configFile.value = "";
  168. });
  169. utils.loadOpenCv(() => {
  170. tryIt.removeAttribute('disabled');
  171. });
  172. var main = async function() {};
  173. var postProcess = function(result) {};
  174. utils.executeCode('codeEditor1');
  175. utils.executeCode('codeEditor2');
  176. utils.executeCode('codeEditor3');
  177. utils.executeCode('codeEditor4');
  178. function updateResult(output, time) {
  179. try{
  180. let canvasOutput = document.getElementById('canvasOutput');
  181. canvasOutput.style.visibility = "visible";
  182. let resized = new cv.Mat(canvasOutput.width, canvasOutput.height, cv.CV_8UC4);
  183. cv.resize(output, resized, new cv.Size(canvasOutput.width, canvasOutput.height));
  184. cv.imshow('canvasOutput', resized);
  185. document.getElementById('status').innerHTML = `<b>Model:</b> ${modelPath}<br>
  186. <b>Inference time:</b> ${time.toFixed(2)} ms`;
  187. } catch(e) {
  188. console.log(e);
  189. }
  190. }
  191. function initStatus() {
  192. document.getElementById('status').innerHTML = '';
  193. document.getElementById('canvasOutput').style.visibility = "hidden";
  194. utils.clearError();
  195. }
  196. </script>
  197. </body>
  198. </html>