annotation-box.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <div class="annotation-box" :style="style" @mousedown.stop.prevent="moveSelf" @touchstart.stop.prevent="moveSelf">
  3. <div class="nw" @mousedown.stop.prevent="resizeSelf('nw')" @touchstart.stop.prevent="resizeSelf('nw')"></div>
  4. <div class="nn" @mousedown.stop.prevent="resizeSelf('nn')" @touchstart.stop.prevent="resizeSelf('nn')"></div>
  5. <div class="ne" @mousedown.stop.prevent="resizeSelf('ne')" @touchstart.stop.prevent="resizeSelf('ne')"></div>
  6. <div class="ww" @mousedown.stop.prevent="resizeSelf('ww')" @touchstart.stop.prevent="resizeSelf('ww')"></div>
  7. <div class="buttons">
  8. <template v-if="!immutable">
  9. <div v-if="position.label" class="label-text">{{ currentLabel.name }}</div>
  10. <img v-if="confirmationButton"
  11. alt="confirm" src="@/assets/icons/check.svg"
  12. @click.stop.prevent="confirmSelf"
  13. @touchstart.stop.prevent="confirmSelf"
  14. @mousedown.stop.prevent>
  15. <img v-if="labelButton"
  16. alt="label" src="@/assets/icons/tag.svg"
  17. @click.stop.prevent="showLabelSelection = true"
  18. @touchstart.stop.prevent="showLabelSelection = true"
  19. @mousedown.stop.prevent>
  20. <img alt="delete" src="@/assets/icons/trash.svg"
  21. @click.stop.prevent="deleteSelf"
  22. @touchstart.stop.prevent="deleteSelf"
  23. @mousedown.stop.prevent>
  24. </template>
  25. </div>
  26. <select v-if="!immutable && showLabelSelection"
  27. @touchstart.stop @mousedown.stop
  28. @change="labelSelf"
  29. @focusout="showLabelSelection = false">
  30. <option value="">None</option>
  31. <option v-for="label in labelList"
  32. :key="label.id"
  33. :value="label.id"
  34. :selected="label === currentLabel">
  35. {{ label.name }}
  36. </option>
  37. </select>
  38. <div class="ee" @mousedown.stop.prevent="resizeSelf('ee')" @touchstart.stop.prevent="resizeSelf('ee')"></div>
  39. <div class="sw" @mousedown.stop.prevent="resizeSelf('sw')" @touchstart.stop.prevent="resizeSelf('sw')"></div>
  40. <div class="ss" @mousedown.stop.prevent="resizeSelf('ss')" @touchstart.stop.prevent="resizeSelf('ss')"></div>
  41. <div class="se" @mousedown.stop.prevent="resizeSelf('se')" @touchstart.stop.prevent="resizeSelf('se')"></div>
  42. </div>
  43. </template>
  44. <script>
  45. export default {
  46. name: "annotation-box",
  47. props: ['type', 'image', 'position', 'socket', 'boxUrl', 'labels', 'supports'],
  48. data: function () {
  49. return {
  50. showLabelSelection: false
  51. }
  52. },
  53. computed: {
  54. immutable: function () {
  55. return !this.type;
  56. },
  57. backgroundColor: function () {
  58. switch (this.type) {
  59. case 'user':
  60. return 'rgba(255, 0, 0, 0.3)';
  61. case 'pipeline':
  62. return 'rgba(0, 0, 255, 0.3)';
  63. default:
  64. return 'rgba(255, 255, 255, 0.3)';
  65. }
  66. },
  67. confirmationButton: function () {
  68. return this.type === 'pipeline';
  69. },
  70. labelButton: function () {
  71. return this.supports.includes('labeled-bounding-boxes') && this.type !== 'pipeline';
  72. },
  73. labelList: function () {
  74. return Object.keys(this.labels).map(key => this.labels[key]);
  75. },
  76. currentLabel: function () {
  77. return this.labels[this.position.label];
  78. },
  79. style: function () {
  80. const left = this.image.left;
  81. const top = this.image.top;
  82. const width = this.image.width;
  83. const height = this.image.height;
  84. return {
  85. left: (left + this.position.x * width) + 'px',
  86. top: (top + this.position.y * height) + 'px',
  87. width: (this.position.w * width) + 'px',
  88. height: (this.position.h * height) + 'px',
  89. backgroundColor: this.backgroundColor
  90. }
  91. }
  92. },
  93. methods: {
  94. confirmSelf: function () {
  95. this.updateSelf(this.position);
  96. },
  97. labelSelf: function (event) {
  98. const options = event.target.options;
  99. const option = options[options.selectedIndex];
  100. const value = option.value;
  101. if (value)
  102. this.position.label = value;
  103. else
  104. delete this.position.label;
  105. this.updateSelf(this.position);
  106. this.showLabelSelection = false;
  107. },
  108. deleteSelf: function () {
  109. this.socket.post(this.boxUrl, {delete: true});
  110. },
  111. moveSelf: function (event) {
  112. this.$emit('move', event, this.position, this.updateSelf);
  113. },
  114. resizeSelf: function (mode) {
  115. this.$emit('resize', this.position, mode, this.updateSelf);
  116. },
  117. updateSelf: function (value) {
  118. if ('label' in this.position && !('label' in value))
  119. value.label = this.position.label;
  120. this.socket.post(this.boxUrl, value);
  121. }
  122. }
  123. }
  124. </script>
  125. <style scoped>
  126. .annotation-box {
  127. position: absolute;
  128. display: grid;
  129. grid-template-rows: 10px auto 10px;
  130. grid-template-columns: 10px auto 10px;
  131. }
  132. .buttons {
  133. display: flex;
  134. justify-content: center;
  135. align-items: center;
  136. position: relative;
  137. }
  138. img {
  139. width: 2rem;
  140. filter: invert(1);
  141. opacity: 0.9;
  142. }
  143. .nn, .ss {
  144. cursor: ns-resize;
  145. }
  146. .ww, .ee {
  147. cursor: ew-resize;
  148. }
  149. .ne, .sw {
  150. cursor: nesw-resize;
  151. }
  152. .nw, .se {
  153. cursor: nwse-resize;
  154. }
  155. select {
  156. position: absolute;
  157. left: 0;
  158. top: 0;
  159. width: 100%;
  160. height: 100%;
  161. }
  162. .label-text {
  163. position: absolute;
  164. bottom: 0;
  165. color: whitesmoke;
  166. font-size: 80%;
  167. }
  168. </style>