6
0

annotated-image.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <template>
  2. <div class="annotated-image" ref="root">
  3. <template v-if="src">
  4. <options-bar :file="current"
  5. :interaction="interaction"
  6. @interaction="interaction = $event"
  7. :filter="filter"
  8. @filter="filter = $event"
  9. :label="label"
  10. @label="label = $event"
  11. :labels="labels"/>
  12. <div class="media">
  13. <!-- image -->
  14. <img v-if="current.type === 'image'"
  15. ref="media" :src="src" alt="media"
  16. v-on:load="change" v-on:loadedmetadata="change" v-on:loadeddata="change">
  17. <!-- video -->
  18. <template v-if="current.type === 'video'">
  19. <div class="video-player">
  20. <video ref="media" :src="src"
  21. v-on:load="change" v-on:loadedmetadata="change" v-on:loadeddata="change" v-on:canplay="change"
  22. v-on:timeupdate="videoProgress"/>
  23. </div>
  24. <video-control :file="current"
  25. :video="video"
  26. :results="results"
  27. @play="videoPlay"
  28. @jump="videoJump"/>
  29. </template>
  30. <!-- overlay -->
  31. <annotation-overlay :file="current"
  32. :position="overlayPosition"
  33. :size="image"
  34. :interaction="interaction"
  35. :filter="filter"
  36. :label="label"
  37. :video="video"
  38. :results="results"
  39. :labels="labels"/>
  40. </div>
  41. </template>
  42. </div>
  43. </template>
  44. <script>
  45. import AnnotationOverlay from "@/components/media/annotation-overlay";
  46. import VideoControl from "@/components/media/video-control";
  47. import OptionsBar from "@/components/media/options-bar";
  48. export default {
  49. name: "annotated-image",
  50. components: {OptionsBar, VideoControl, AnnotationOverlay},
  51. props: ['current'],
  52. mounted: function () {
  53. // add resize listener
  54. window.addEventListener('resize', this.resize);
  55. this.interval = setInterval(this.resize, 1000);
  56. this.resize();
  57. // add result listener
  58. this.$root.socket.on('create-result', this.createResult);
  59. this.$root.socket.on('edit-result', this.editResult);
  60. this.$root.socket.on('remove-result', this.removeResult);
  61. // add label listener
  62. this.getLabels();
  63. this.$root.socket.on('connect', this.getLabels);
  64. this.$root.socket.on('create-label', this.addLabelToList);
  65. this.$root.socket.on('remove-label', this.removeLabelFromList);
  66. this.$root.socket.on('edit-label', this.editLabelInList);
  67. // get model
  68. this.$root.socket.get(`/projects/${this.$root.project.id}/model`)
  69. .then(response => response.json())
  70. .then(model => {
  71. this.supported.labeledImages = model.supports.includes('labeled-images');
  72. this.supported.labeledBoundingBoxes = model.supports.includes('labeled-bounding-boxes');
  73. this.supported.boundingBoxes = this.supported.labeledBoundingBoxes
  74. || model.supports.includes('bounding-boxes');
  75. });
  76. },
  77. destroyed: function () {
  78. window.removeEventListener('resize', this.resize);
  79. clearInterval(this.interval);
  80. this.$root.socket.off('create-result', this.createResult);
  81. this.$root.socket.off('edit-result', this.editResult);
  82. this.$root.socket.off('remove-result', this.removeResult);
  83. this.$root.socket.off('connect', this.getLabels);
  84. this.$root.socket.off('create-label', this.addLabelToList);
  85. this.$root.socket.off('remove-label', this.removeLabelFromList);
  86. this.$root.socket.off('edit-label', this.editLabelInList);
  87. },
  88. data: function () {
  89. return {
  90. interval: false,
  91. container: {
  92. top: 0,
  93. left: 0,
  94. width: 0,
  95. height: 0,
  96. },
  97. image: {
  98. top: 0,
  99. left: 0,
  100. width: 0,
  101. height: 0
  102. },
  103. video: {
  104. frame: 0,
  105. play: false
  106. },
  107. supported: {
  108. labeledImages: false,
  109. boundingBoxes: false,
  110. labeledBoundingBoxes: false,
  111. },
  112. interaction: 'draw-box',
  113. filter: ['user', 'pipeline'],
  114. label: false,
  115. results: [],
  116. labels: []
  117. }
  118. },
  119. computed: {
  120. src: function () {
  121. if (!this.container.width || !this.container.height)
  122. return '';
  123. if (this.current.type === 'video')
  124. return this.$root.socket.media(this.current);
  125. const width = Math.ceil(this.container.width / 400) * 400;
  126. const height = Math.ceil(this.container.height / 400) * 400;
  127. return this.$root.socket.media(this.current, width, height);
  128. },
  129. overlayPosition: function () {
  130. return {
  131. top: (this.image.top - this.container.top) + 'px',
  132. left: (this.image.left - this.container.left) + 'px',
  133. width: this.image.width + 'px',
  134. height: this.image.height + 'px'
  135. }
  136. }
  137. },
  138. methods: {
  139. resize: function () {
  140. const rect = this.$refs.root.getBoundingClientRect();
  141. this.container.top = rect.top;
  142. this.container.left = rect.left;
  143. this.container.width = rect.width;
  144. this.container.height = rect.height;
  145. this.change();
  146. },
  147. change: function () {
  148. if (!this.$refs.media)
  149. return;
  150. const rect = this.$refs.media.getBoundingClientRect();
  151. this.image.top = rect.top;
  152. this.image.left = rect.left;
  153. this.image.width = rect.width;
  154. this.image.height = rect.height;
  155. },
  156. videoProgress: function (event) {
  157. this.video.frame = Math.floor(event.target.currentTime * this.current.fps);
  158. },
  159. videoPlay: function (value) {
  160. this.video.play = value;
  161. if (value)
  162. this.$refs.media.play();
  163. else
  164. this.$refs.media.pause();
  165. },
  166. videoJump: function (value) {
  167. // calculate difference
  168. value = Math.max(0, Math.min(this.current.frames, value));
  169. const diff = value - this.video.frame;
  170. // set timestamp
  171. this.$refs.media.currentTime += diff / this.current.fps;
  172. // get timestamp offset and correct if needed
  173. const currentFrame = this.$refs.media.currentTime * this.current.fps;
  174. if (diff < 0 && currentFrame < value) {
  175. this.$refs.media.currentTime += 0.5 / this.current.fps;
  176. } else if (diff > 0 && currentFrame > value + 0.5) {
  177. this.$refs.media.currentTime -= 0.5 / this.current.fps;
  178. }
  179. },
  180. createResult: function (result) {
  181. if (result['file_id'] !== this.current.id)
  182. return;
  183. for (let r of this.results)
  184. if (r.id === result.id)
  185. return;
  186. this.results.push(result);
  187. },
  188. removeResult: function (result) {
  189. for (let i = 0; i < this.results.length; i++) {
  190. if (this.results[i].id === result.id) {
  191. this.results.splice(i, 1);
  192. return;
  193. }
  194. }
  195. },
  196. editResult: function (result) {
  197. for (let i = 0; i < this.results.length; i++) {
  198. if (this.results[i].id === result.id) {
  199. this.$set(this.results, i, result);
  200. return;
  201. }
  202. }
  203. },
  204. getLabels: function () {
  205. this.$root.socket.get(`/projects/${this.$root.project.id}/labels`)
  206. .then(response => response.json())
  207. .then(labels => {
  208. this.labels = [];
  209. labels.forEach(this.addLabelToList);
  210. });
  211. },
  212. addLabelToList: function (label) {
  213. if (label['project_id'] !== this.$root.project.id)
  214. return;
  215. for (let l of this.labels)
  216. if (l.id === label.id)
  217. return;
  218. this.labels.push(label);
  219. },
  220. removeLabelFromList: function (label) {
  221. for (let i = 0; i < this.labels.length; i++) {
  222. if (this.labels[i].id === label.id) {
  223. this.labels.splice(i, 1);
  224. return;
  225. }
  226. }
  227. },
  228. editLabelInList: function (label) {
  229. for (let i = 0; i < this.labels.length; i++) {
  230. if (this.labels[i].id === label.id) {
  231. this.$set(this.labels, i, label);
  232. return;
  233. }
  234. }
  235. }
  236. },
  237. watch: {
  238. current: {
  239. immediate: true,
  240. handler: function (newVal) {
  241. this.video.play = false;
  242. this.video.frame = 0;
  243. this.$root.socket.get(`/data/${newVal.id}/results`)
  244. .then(response => response.json())
  245. .then(results => {
  246. this.results = results;
  247. })
  248. }
  249. }
  250. }
  251. }
  252. </script>
  253. <style scoped>
  254. .annotated-image {
  255. width: 100%;
  256. height: 100%;
  257. display: flex;
  258. flex-direction: row;
  259. justify-content: center;
  260. align-items: center;
  261. }
  262. .options-bar {
  263. height: 100%;
  264. }
  265. .media {
  266. width: 100%;
  267. height: 100%;
  268. flex-grow: 1;
  269. display: flex;
  270. flex-direction: column;
  271. justify-content: center;
  272. align-items: center;
  273. }
  274. .video-player {
  275. width: 100%;
  276. height: 100%;
  277. flex-grow: 1;
  278. overflow-y: auto;
  279. display: flex;
  280. justify-content: center;
  281. align-items: center;
  282. }
  283. img, video {
  284. max-width: 100%;
  285. max-height: 100%;
  286. }
  287. </style>