annotation-overlay.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <div class="annotation-overlay" :style="position"
  3. @touchstart="press" @touchmove="track" @touchend="release"
  4. @mousedown="press" @mousemove="track" @mouseup="release"
  5. @dragstart.stop>
  6. <annotation-box v-for="box in boundingBoxes"
  7. v-bind:key="box.identifier"
  8. :box="box"
  9. :position="box.data"
  10. :draggable="interaction === 'move-box'"
  11. :deletable="interaction === 'remove-box'"
  12. :taggable="interaction === 'label-box' ? label : false"
  13. :confirmable="interaction === 'confirm-box'"
  14. :labels="labels"
  15. @move="move"
  16. @resize="resize"/>
  17. <annotation-box v-if="current"
  18. :position="current"/>
  19. <div v-if="imageLabelResult"
  20. class="image-label"
  21. :class="{
  22. user: imageLabelResult.origin === 'user',
  23. pipeline: imageLabelResult.origin === 'pipeline'
  24. }">
  25. {{ imageLabelObject.name }}
  26. </div>
  27. </div>
  28. </template>
  29. <script>
  30. import AnnotationBox from "@/components/media/annotation-box";
  31. export default {
  32. name: "annotation-overlay",
  33. components: {AnnotationBox},
  34. props: ['file', 'position', 'size', 'interaction', 'filter', 'label', 'video', 'results', 'labels'],
  35. data: function () {
  36. return {
  37. callback: false,
  38. start: false,
  39. fixed: false,
  40. current: false,
  41. mousedown: false
  42. }
  43. },
  44. computed: {
  45. boundingBoxes: function () {
  46. return this.results.filter(b => {
  47. return b.type === 'bounding-box' && this.filter.includes(b.origin)
  48. && (!('frame' in b.data) || b.data.frame === this.video.frame)
  49. });
  50. },
  51. imageLabelResult: function () {
  52. for (let result of this.results)
  53. if (result.type === 'labeled-image')
  54. return result;
  55. return false;
  56. },
  57. imageLabelObject: function () {
  58. if (!this.imageLabelResult)
  59. return false;
  60. for (let label of this.labels)
  61. if (label.identifier === this.imageLabelResult.label)
  62. return label;
  63. return 'unknown label';
  64. }
  65. },
  66. methods: {
  67. getEventCoordinates: function (event) {
  68. if (!('clientX' in event)) {
  69. if ('touches' in event && event.touches.length > 0)
  70. event = event.touches[0];
  71. else if ('changedTouches' in event && event.changedTouches.length > 0)
  72. event = event.changedTouches[0];
  73. }
  74. return {
  75. x: Math.max(0, Math.min(1, (event.clientX - this.size.left) / this.size.width)),
  76. y: Math.max(0, Math.min(1, (event.clientY - this.size.top) / this.size.height))
  77. }
  78. },
  79. buildRectangle: function (start, end) {
  80. let lx, hx, ly, hy;
  81. if (this.fixed && 'offsetX' in this.fixed && 'offsetY' in this.fixed) {
  82. lx = Math.max(end.x + this.fixed.offsetX, 0);
  83. hx = Math.min(end.x + this.fixed.offsetX + this.fixed.w, 1);
  84. ly = Math.max(end.y + this.fixed.offsetY, 0);
  85. hy = Math.min(end.y + this.fixed.offsetY + this.fixed.h, 1);
  86. } else {
  87. lx = this.fixed && 'lx' in this.fixed ? this.fixed.lx : Math.max(Math.min(start.x, end.x), 0);
  88. hx = this.fixed && 'hx' in this.fixed ? this.fixed.hx : Math.min(Math.max(start.x, end.x), 1);
  89. ly = this.fixed && 'ly' in this.fixed ? this.fixed.ly : Math.max(Math.min(start.y, end.y), 0);
  90. hy = this.fixed && 'hy' in this.fixed ? this.fixed.hy : Math.min(Math.max(start.y, end.y), 1);
  91. }
  92. if (this.file.type === 'image') {
  93. return {
  94. x: lx,
  95. y: ly,
  96. w: hx - lx,
  97. h: hy - ly
  98. }
  99. } else {
  100. return {
  101. x: lx,
  102. y: ly,
  103. w: hx - lx,
  104. h: hy - ly,
  105. frame: this.video.frame
  106. }
  107. }
  108. },
  109. press: function (event) {
  110. this.mousedown = true;
  111. if (this.interaction === 'draw-box') {
  112. this.start = this.getEventCoordinates(event);
  113. }
  114. if (this.interaction === 'extreme-clicking') {
  115. const coordinates = this.getEventCoordinates(event);
  116. if (!this.start) {
  117. this.start = coordinates;
  118. } else if (!this.current) {
  119. this.current = this.buildRectangle(this.start, coordinates);
  120. } else {
  121. this.current = this.buildRectangle({
  122. x: Math.min(coordinates.x, this.current.x),
  123. y: Math.min(coordinates.y, this.current.y)
  124. }, {
  125. x: Math.max(coordinates.x, this.current.x + this.current.w),
  126. y: Math.max(coordinates.y, this.current.y + this.current.h)
  127. });
  128. }
  129. }
  130. if (this.interaction === 'label-box') {
  131. this.$root.socket.post(`/data/${this.file.identifier}/results`, {
  132. type: 'labeled-image',
  133. label: this.label.identifier
  134. });
  135. }
  136. if (this.interaction === 'confirm-box') {
  137. if (this.imageLabelResult) {
  138. this.$root.socket.post(`/results/${this.imageLabelResult.identifier}/confirm`, {
  139. confirm: true
  140. });
  141. }
  142. }
  143. if (this.interaction === 'remove-box') {
  144. if (this.imageLabelResult) {
  145. this.$root.socket.post(`/results/${this.imageLabelResult.identifier}/remove`, {
  146. remove: true
  147. });
  148. }
  149. }
  150. },
  151. track: function (event) {
  152. if (this.interaction === 'extreme-clicking') {
  153. if (this.mousedown)
  154. this.press(event);
  155. return;
  156. }
  157. if (this.start) {
  158. const coordinates = this.getEventCoordinates(event);
  159. this.current = this.buildRectangle(this.start, coordinates);
  160. }
  161. },
  162. release: function () {
  163. this.mousedown = false;
  164. if (this.interaction === 'extreme-clicking')
  165. return;
  166. if (this.current) {
  167. if (this.callback)
  168. this.callback(this.current);
  169. else
  170. // TODO then / error
  171. this.$root.socket.post(`/data/${this.file.identifier}/results`, {
  172. type: 'bounding-box',
  173. data: this.current
  174. });
  175. }
  176. this.callback = false;
  177. this.start = false;
  178. this.fixed = false;
  179. this.current = false;
  180. },
  181. move: function (event, position, callback) {
  182. if (this.interaction !== 'move-box')
  183. return;
  184. this.callback = callback;
  185. const coordinates = this.getEventCoordinates(event);
  186. this.start = position;
  187. this.fixed = {
  188. w: position.w,
  189. h: position.h,
  190. offsetX: position.x - coordinates.x,
  191. offsetY: position.y - coordinates.y
  192. };
  193. },
  194. resize: function (mode, position, callback) {
  195. if (this.interaction !== 'move-box')
  196. return;
  197. this.callback = callback;
  198. switch (mode) {
  199. case 'nw':
  200. this.start = {x: position.x + position.w, y: position.y + position.h};
  201. this.fixed = false;
  202. break;
  203. case 'ne':
  204. this.start = {x: position.x, y: position.y + position.h};
  205. this.fixed = false;
  206. break;
  207. case 'sw':
  208. this.start = {x: position.x + position.w, y: position.y};
  209. this.fixed = false;
  210. break;
  211. case 'se':
  212. this.start = {x: position.x, y: position.y};
  213. this.fixed = false;
  214. break;
  215. case 'nn':
  216. this.start = {x: position.x, y: position.y + position.h};
  217. this.fixed = {lx: position.x, hx: position.x + position.w};
  218. break;
  219. case 'ww':
  220. this.start = {x: position.x + position.w, y: position.y};
  221. this.fixed = {ly: position.y, hy: position.y + position.h};
  222. break;
  223. case 'ee':
  224. this.start = {x: position.x, y: position.y};
  225. this.fixed = {ly: position.y, hy: position.y + position.h};
  226. break;
  227. case 'ss':
  228. this.start = {x: position.x, y: position.y};
  229. this.fixed = {lx: position.x, hx: position.x + position.w};
  230. break;
  231. }
  232. }
  233. },
  234. watch: {
  235. interaction: function (newVal, oldVal) {
  236. if (oldVal === 'extreme-clicking')
  237. this.release();
  238. }
  239. }
  240. }
  241. </script>
  242. <style scoped>
  243. .annotation-overlay {
  244. position: absolute;
  245. }
  246. .image-label {
  247. position: absolute;
  248. right: 0;
  249. bottom: 0;
  250. padding: 0.7rem 1rem;
  251. color: whitesmoke;
  252. border-top-left-radius: 1rem;
  253. }
  254. .image-label.user {
  255. background-color: rgba(255, 0, 0, 0.4);
  256. }
  257. .image-label.pipeline {
  258. background-color: rgba(0, 0, 255, 0.4);
  259. }
  260. </style>