annotation-box.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 ? currentLabel.name : 'unknown label' }}</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. if (this.position.label in this.labels)
  78. return this.labels[this.position.label];
  79. else
  80. return false;
  81. },
  82. style: function () {
  83. const left = this.image.left;
  84. const top = this.image.top;
  85. const width = this.image.width;
  86. const height = this.image.height;
  87. return {
  88. left: (left + this.position.x * width) + 'px',
  89. top: (top + this.position.y * height) + 'px',
  90. width: (this.position.w * width) + 'px',
  91. height: (this.position.h * height) + 'px',
  92. backgroundColor: this.backgroundColor
  93. }
  94. }
  95. },
  96. methods: {
  97. confirmSelf: function () {
  98. this.updateSelf(this.position);
  99. },
  100. labelSelf: function (event) {
  101. const options = event.target.options;
  102. const option = options[options.selectedIndex];
  103. const value = option.value;
  104. if (value)
  105. this.position.label = value;
  106. else
  107. delete this.position.label;
  108. this.updateSelf(this.position);
  109. this.showLabelSelection = false;
  110. },
  111. deleteSelf: function () {
  112. this.socket.post(this.boxUrl, {delete: true});
  113. },
  114. moveSelf: function (event) {
  115. this.$emit('move', event, this.position, this.updateSelf);
  116. },
  117. resizeSelf: function (mode) {
  118. this.$emit('resize', this.position, mode, this.updateSelf);
  119. },
  120. updateSelf: function (value) {
  121. if ('label' in this.position && !('label' in value))
  122. value.label = this.position.label;
  123. this.socket.post(this.boxUrl, value);
  124. }
  125. }
  126. }
  127. </script>
  128. <style scoped>
  129. .annotation-box {
  130. position: absolute;
  131. display: grid;
  132. grid-template-rows: 10px auto 10px;
  133. grid-template-columns: 10px auto 10px;
  134. }
  135. .buttons {
  136. display: flex;
  137. justify-content: center;
  138. align-items: center;
  139. position: relative;
  140. }
  141. img {
  142. width: 2rem;
  143. filter: invert(1);
  144. opacity: 0.9;
  145. }
  146. .nn, .ss {
  147. cursor: ns-resize;
  148. }
  149. .ww, .ee {
  150. cursor: ew-resize;
  151. }
  152. .ne, .sw {
  153. cursor: nesw-resize;
  154. }
  155. .nw, .se {
  156. cursor: nwse-resize;
  157. }
  158. select {
  159. position: absolute;
  160. left: 0;
  161. top: 0;
  162. width: 100%;
  163. height: 100%;
  164. }
  165. .label-text {
  166. position: absolute;
  167. bottom: 0;
  168. color: whitesmoke;
  169. font-size: 80%;
  170. }
  171. </style>