Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. (function(window, document) {
  2. 'use strict';
  3.  
  4. var Tracker = function Tracker(cockpit) {
  5. var tracker = this;
  6. console.log('Loading Tracker plugin');
  7. this.cockpit = cockpit;
  8.  
  9. this.frameWidth = 640;
  10. this.frameHeight = 360;
  11.  
  12. this.newPyramid = new jsfeat.pyramid_t(3);
  13. this.oldPyramid = new jsfeat.pyramid_t(3);
  14. this.point_status = new Uint8Array(1);
  15. this.oldXY = new Float32Array(2);
  16. this.newXY = new Float32Array(2);
  17. this.oldRoundedX = 0;
  18. this.oldRoundedY = 0;
  19.  
  20. this.canvas = document.querySelector('#dronestream canvas');
  21. if (! this.canvas) {
  22. console.error('Did not find required dronestream canvas');
  23. return;
  24. }
  25. console.log('found canvas, width/height:', this.canvas.clientWidth, this.canvas.clientHeight);
  26.  
  27. // add click-handler
  28. $('#cockpit').append('<div id="tracker"></div>');
  29. this.div = $('#tracker').get(0);
  30. this.div.addEventListener('click', function(event) {
  31. tracker.setTrackingCoords(event.offsetX, event.offsetY);
  32. });
  33.  
  34. this.enabled = false;
  35. this.observers = {};
  36. this.locked = false;
  37.  
  38. $('#cockpit').append('<img id="tracker-crosshairs" src="/plugin/tracker/img/sniper.png">');
  39. this.crosshairs = $('#tracker-crosshairs').get(0);
  40. this.crosshairs.style.display = 'none';
  41.  
  42. this.on('points', function (data) {
  43. tracker.crosshairs.style.left = (data[0].x - 83) + 'px';
  44. tracker.crosshairs.style.top = (data[0].y - 83 ) + 'px';
  45. console.log("crosshairs",data[0]);
  46.  
  47.  
  48. if (data[0].x < 663){
  49. console.log('counterclockwise');
  50. //this.cockpit.socket.emit("/pilot/" + "move", {
  51. //action : "counterclockwise", speed: ".1"
  52. //client.counterclockwise(.1);
  53.  
  54. }
  55.  
  56.  
  57. if (data[0].x > 703){
  58. console.log('clockwise');
  59. //client.clockwise(.1);
  60.  
  61.  
  62. }
  63.  
  64.  
  65.  
  66.  
  67. tracker.crosshairs.style.display = 'block';
  68. });
  69.  
  70. this.on('locked', function () {
  71. console.log('target acquired');
  72. });
  73.  
  74. this.on('lost', function () {
  75. console.log('target lost');
  76. tracker.crosshairs.style.display = 'none';
  77. tracker.disable();
  78. });
  79. };
  80.  
  81. Tracker.prototype.prepareTrackingBuffer = function () {
  82. this.newPyramid.allocate(
  83. this.frameWidth,
  84. this.frameHeight,
  85. jsfeat.U8_t | jsfeat.C1_t
  86. );
  87. this.oldPyramid.allocate(
  88. this.frameWidth,
  89. this.frameHeight,
  90. jsfeat.U8_t | jsfeat.C1_t
  91. );
  92. };
  93.  
  94. Tracker.prototype.update = function (frameBuffer) {
  95. var tmpXY,
  96. tmpPyramid,
  97. roundedX,
  98. roundedY;
  99.  
  100. if (true !== this.enabled) {
  101. return;
  102. }
  103. tmpXY = this.newXY;
  104. this.newXY = this.oldXY;
  105. this.oldXY = tmpXY;
  106.  
  107. tmpPyramid = this.newPyramid;
  108. this.newPyramid = this.oldPyramid;
  109. this.oldPyramid = tmpPyramid;
  110.  
  111. this.trackFlow(frameBuffer);
  112.  
  113. if (this.point_status[0] == 1) {
  114. roundedX = Math.round(
  115. this.newXY[0] * this.canvas.clientWidth / this.frameWidth
  116. );
  117. roundedY = Math.round(
  118. this.newXY[1] * this.canvas.clientHeight / this.frameHeight
  119. );
  120. if (
  121. (! this.locked) ||
  122. (roundedX !== this.oldRoundedX) ||
  123. (roundedY !== this.oldRoundedY)
  124. ) {
  125. this.oldRoundedX = roundedX;
  126. this.oldRoundedY = roundedY;
  127. this.emit('points', [{
  128. x: roundedX,
  129. y: roundedY
  130. }]);
  131. }
  132. if (! this.locked) {
  133. this.emit('locked');
  134. }
  135. this.locked = true;
  136. } else {
  137. if (this.locked) {
  138. this.emit('lost');
  139. }
  140. this.locked = false;
  141. }
  142. this.emit('done');
  143. };
  144.  
  145. Tracker.prototype.setTrackingCoords = function (x, y) {
  146. this.locked = false;
  147.  
  148. // translate from (stretched) canvas to framebuffer dimensions:
  149. this.newXY[0] = x * this.frameWidth / this.canvas.clientWidth;
  150. this.newXY[1] = y * this.frameHeight / this.canvas.clientHeight;
  151. // console.log('New tracking coords:', [x,y], this.newXY);
  152. this.enable();
  153. };
  154.  
  155. Tracker.prototype.trackFlow = function (frameBuffer) {
  156. this.newPyramid.data[0].data.set(frameBuffer);
  157.  
  158. jsfeat.imgproc.equalize_histogram(
  159. this.newPyramid.data[0].data,
  160. this.newPyramid.data[0].data
  161. );
  162.  
  163. this.newPyramid.build(this.newPyramid.data[0], true);
  164.  
  165. jsfeat.optical_flow_lk.track(
  166. this.oldPyramid, this.newPyramid,
  167. this.oldXY, this.newXY,
  168. 1,
  169. 50, // win_size
  170. 30, // max_iterations
  171. this.point_status,
  172. 0.01, // epsilon,
  173. 0.001 // min_eigen
  174. );
  175. };
  176.  
  177. Tracker.prototype.enable = function () {
  178. var tracker = this;
  179. if (this.enabled) {
  180. return;
  181. }
  182. this.enabled = true;
  183.  
  184. if (! this.cockpit.videostream) {
  185. console.error('The Tracker plugin depends on plugin video-stream');
  186. return;
  187. }
  188. this.prepareTrackingBuffer();
  189.  
  190. this.hookNextFrame();
  191. this.on('done', this.hookNextFrame.bind(this));
  192. };
  193.  
  194. Tracker.prototype.disable = function () {
  195. this.enabled = false;
  196. };
  197.  
  198. Tracker.prototype.on = function (event, callback) {
  199. var i = 0, handler;
  200. if (!this.observers[event]) {
  201. this.observers[event] = [];
  202. }
  203. this.observers[event].push(callback);
  204. };
  205.  
  206. Tracker.prototype.emit = function (event, data) {
  207. var i = 0, handler;
  208. if (this.observers[event]) {
  209. for (i = 0; handler = this.observers[event][i]; ++i) {
  210. handler(data);
  211. }
  212. }
  213. };
  214.  
  215. Tracker.prototype.hookNextFrame = function () {
  216. this.cockpit.videostream.onNextFrame(this.update.bind(this));
  217.  
  218. };
  219.  
  220. window.Cockpit.plugins.push(Tracker);
  221.  
  222. }(window, document));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement