hands_solution_packed_assets_loader.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. var Module = typeof createMediapipeSolutionsPackedAssets !== 'undefined' ? createMediapipeSolutionsPackedAssets : {};
  2. if (!Module.expectedDataFileDownloads) {
  3. Module.expectedDataFileDownloads = 0;
  4. }
  5. Module.expectedDataFileDownloads++;
  6. (function() {
  7. // When running as a pthread, FS operations are proxied to the main thread, so we don't need to
  8. // fetch the .data bundle on the worker
  9. if (Module['ENVIRONMENT_IS_PTHREAD']) return;
  10. var loadPackage = function(metadata) {
  11. var PACKAGE_PATH = '';
  12. if (typeof window === 'object') {
  13. PACKAGE_PATH = window['encodeURIComponent'](window.location.pathname.toString().substring(0, window.location.pathname.toString().lastIndexOf('/')) + '/');
  14. } else if (typeof process === 'undefined' && typeof location !== 'undefined') {
  15. // web worker
  16. PACKAGE_PATH = encodeURIComponent(location.pathname.toString().substring(0, location.pathname.toString().lastIndexOf('/')) + '/');
  17. }
  18. var PACKAGE_NAME = 'blaze-out/k8-opt/genfiles/third_party/mediapipe/web/solutions/hands/hands_solution_packed_assets.data';
  19. var REMOTE_PACKAGE_BASE = 'hands_solution_packed_assets.data';
  20. if (typeof Module['locateFilePackage'] === 'function' && !Module['locateFile']) {
  21. Module['locateFile'] = Module['locateFilePackage'];
  22. err('warning: you defined Module.locateFilePackage, that has been renamed to Module.locateFile (using your locateFilePackage for now)');
  23. }
  24. var REMOTE_PACKAGE_NAME = Module['locateFile'] ? Module['locateFile'](REMOTE_PACKAGE_BASE, '') : REMOTE_PACKAGE_BASE;
  25. var REMOTE_PACKAGE_SIZE = metadata['remote_package_size'];
  26. var PACKAGE_UUID = metadata['package_uuid'];
  27. function fetchRemotePackage(packageName, packageSize, callback, errback) {
  28. if (typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string') {
  29. require('fs').readFile(packageName, function(err, contents) {
  30. if (err) {
  31. errback(err);
  32. } else {
  33. callback(contents.buffer);
  34. }
  35. });
  36. return;
  37. }
  38. var xhr = new XMLHttpRequest();
  39. xhr.open('GET', packageName, true);
  40. xhr.responseType = 'arraybuffer';
  41. xhr.onprogress = function(event) {
  42. var url = packageName;
  43. var size = packageSize;
  44. if (event.total) size = event.total;
  45. if (event.loaded) {
  46. if (!xhr.addedTotal) {
  47. xhr.addedTotal = true;
  48. if (!Module.dataFileDownloads) Module.dataFileDownloads = {};
  49. Module.dataFileDownloads[url] = {
  50. loaded: event.loaded,
  51. total: size
  52. };
  53. } else {
  54. Module.dataFileDownloads[url].loaded = event.loaded;
  55. }
  56. var total = 0;
  57. var loaded = 0;
  58. var num = 0;
  59. for (var download in Module.dataFileDownloads) {
  60. var data = Module.dataFileDownloads[download];
  61. total += data.total;
  62. loaded += data.loaded;
  63. num++;
  64. }
  65. total = Math.ceil(total * Module.expectedDataFileDownloads/num);
  66. if (Module['setStatus']) Module['setStatus']('Downloading data... (' + loaded + '/' + total + ')');
  67. } else if (!Module.dataFileDownloads) {
  68. if (Module['setStatus']) Module['setStatus']('Downloading data...');
  69. }
  70. };
  71. xhr.onerror = function(event) {
  72. throw new Error("NetworkError for: " + packageName);
  73. }
  74. xhr.onload = function(event) {
  75. if (xhr.status == 200 || xhr.status == 304 || xhr.status == 206 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0
  76. var packageData = xhr.response;
  77. callback(packageData);
  78. } else {
  79. throw new Error(xhr.statusText + " : " + xhr.responseURL);
  80. }
  81. };
  82. xhr.send(null);
  83. };
  84. function handleError(error) {
  85. console.error('package error:', error);
  86. };
  87. var fetchedCallback = null;
  88. var fetched = Module['getPreloadedPackage'] ? Module['getPreloadedPackage'](REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE) : null;
  89. if (!fetched) fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE, function(data) {
  90. if (fetchedCallback) {
  91. fetchedCallback(data);
  92. fetchedCallback = null;
  93. } else {
  94. fetched = data;
  95. }
  96. }, handleError);
  97. function runWithFS() {
  98. function assert(check, msg) {
  99. if (!check) throw msg + new Error().stack;
  100. }
  101. Module['FS_createPath']("/", "third_party", true, true);
  102. Module['FS_createPath']("/third_party", "mediapipe", true, true);
  103. Module['FS_createPath']("/third_party/mediapipe", "modules", true, true);
  104. Module['FS_createPath']("/third_party/mediapipe/modules", "palm_detection", true, true);
  105. Module['FS_createPath']("/third_party/mediapipe/modules", "hand_landmark", true, true);
  106. /** @constructor */
  107. function DataRequest(start, end, audio) {
  108. this.start = start;
  109. this.end = end;
  110. this.audio = audio;
  111. }
  112. DataRequest.prototype = {
  113. requests: {},
  114. open: function(mode, name) {
  115. this.name = name;
  116. this.requests[name] = this;
  117. Module['addRunDependency']('fp ' + this.name);
  118. },
  119. send: function() {},
  120. onload: function() {
  121. var byteArray = this.byteArray.subarray(this.start, this.end);
  122. this.finish(byteArray);
  123. },
  124. finish: function(byteArray) {
  125. var that = this;
  126. Module['FS_createPreloadedFile'](this.name, null, byteArray, true, true, function() {
  127. Module['removeRunDependency']('fp ' + that.name);
  128. }, function() {
  129. if (that.audio) {
  130. Module['removeRunDependency']('fp ' + that.name); // workaround for chromium bug 124926 (still no audio with this, but at least we don't hang)
  131. } else {
  132. err('Preloading file ' + that.name + ' failed');
  133. }
  134. }, false, true); // canOwn this data in the filesystem, it is a slide into the heap that will never change
  135. this.requests[this.name] = null;
  136. }
  137. };
  138. var files = metadata['files'];
  139. for (var i = 0; i < files.length; ++i) {
  140. new DataRequest(files[i]['start'], files[i]['end'], files[i]['audio'] || 0).open('GET', files[i]['filename']);
  141. }
  142. function processPackageData(arrayBuffer) {
  143. assert(arrayBuffer, 'Loading data file failed.');
  144. assert(arrayBuffer instanceof ArrayBuffer, 'bad input to processPackageData');
  145. var byteArray = new Uint8Array(arrayBuffer);
  146. var curr;
  147. // Reuse the bytearray from the XHR as the source for file reads.
  148. DataRequest.prototype.byteArray = byteArray;
  149. var files = metadata['files'];
  150. for (var i = 0; i < files.length; ++i) {
  151. DataRequest.prototype.requests[files[i].filename].onload();
  152. } Module['removeRunDependency']('datafile_blaze-out/k8-opt/genfiles/third_party/mediapipe/web/solutions/hands/hands_solution_packed_assets.data');
  153. };
  154. Module['addRunDependency']('datafile_blaze-out/k8-opt/genfiles/third_party/mediapipe/web/solutions/hands/hands_solution_packed_assets.data');
  155. if (!Module.preloadResults) Module.preloadResults = {};
  156. Module.preloadResults[PACKAGE_NAME] = {fromCache: false};
  157. if (fetched) {
  158. processPackageData(fetched);
  159. fetched = null;
  160. } else {
  161. fetchedCallback = processPackageData;
  162. }
  163. }
  164. if (Module['calledRun']) {
  165. runWithFS();
  166. } else {
  167. if (!Module['preRun']) Module['preRun'] = [];
  168. Module["preRun"].push(runWithFS); // FS is not initialized yet, wait for it
  169. }
  170. }
  171. loadPackage({"files": [{"filename": "/third_party/mediapipe/modules/palm_detection/palm_detection_lite.tflite", "start": 0, "end": 1985440}, {"filename": "/third_party/mediapipe/modules/palm_detection/palm_detection_full.tflite", "start": 1985440, "end": 4326720}, {"filename": "/third_party/mediapipe/modules/hand_landmark/handedness.txt", "start": 4326720, "end": 4326731}], "remote_package_size": 4326731, "package_uuid": "f2b11ac8-0259-41a9-b49f-d0fb8d91f87d"});
  172. })();