6
0

annotated-image.vue 11 KB

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