File size: 11,477 Bytes
b4f9490
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<template>

  <div class="zoom-view">

    <div v-if="hasAnnotations" class="zoom-image-container">

      <canvas 

        ref="zoomCanvas" 

        class="zoom-canvas"

        :width="zoomWidth"

        :height="zoomHeight"

      ></canvas>

      <div class="zoom-info">

        <span>Frame {{ currentFrameNumber }}</span>

        <span v-if="zoomRegion">{{ Math.round(zoomRegion.width) }}x{{ Math.round(zoomRegion.height) }}px</span>

        <span v-if="zoomRegion?.type === 'points' && selectedAnnotations.length > 1">

          {{ selectedAnnotations.length }} points

        </span>

      </div>

    </div>

    <div v-else class="no-annotations">

      <p>Aucune annotation sur cette frame</p>

    </div>

  </div>

</template>



<script>

import { useAnnotationStore } from '@/stores/annotationStore'

import { useVideoStore } from '@/stores/videoStore'

import { computed, ref, watch, nextTick, onMounted } from 'vue'



export default {

  name: 'ZoomView',

  

  mounted() {

    // Forcer le rafraîchissement quand le composant est monté

    this.$nextTick(() => {

      setTimeout(() => {

        this.updateZoomImage()

      }, 100) // Petit délai pour s'assurer que la vidéo est prête

    })

  },

  

  setup() {

    const annotationStore = useAnnotationStore()

    const videoStore = useVideoStore()

    const zoomCanvas = ref(null)

    

    // Dimensions du canvas de zoom

    const zoomWidth = 200

    const zoomHeight = 300



    const getCurrentFrameNumber = () => {

      const frameRate = annotationStore.currentSession?.frameRate || 30

      return Math.round(videoStore.currentTime * frameRate)

    }



    const currentFrameNumber = computed(() => getCurrentFrameNumber())



    const selectedAnnotations = computed(() => {

      const currentFrame = getCurrentFrameNumber()

      const frameAnnotations = annotationStore.getAnnotationsForFrame(currentFrame) || []

      return frameAnnotations.filter(

        annotation => annotation && annotation.objectId === annotationStore.selectedObjectId

      )

    })



    const hasAnnotations = computed(() => {

      return selectedAnnotations.value.length > 0

    })



    const zoomRegion = computed(() => {

      const annotations = selectedAnnotations.value

      if (!annotations.length) return null



      // Séparer rectangles et points

      const rectangles = annotations.filter(a => a.type === 'rectangle')

      const points = annotations.filter(a => a.type === 'point')



      if (rectangles.length > 0) {

        // Utiliser le premier rectangle trouvé

        const rect = rectangles[0]

        return {

          x: rect.x,

          y: rect.y,

          width: rect.width,

          height: rect.height,

          type: 'rectangle'

        }

      } else if (points.length > 0) {

        // Calculer le centre moyen des points

        const avgX = points.reduce((sum, p) => sum + p.x, 0) / points.length

        const avgY = points.reduce((sum, p) => sum + p.y, 0) / points.length

        

        if (points.length === 1) {

          // Pour un seul point, utiliser une taille fixe raisonnable

          const fixedSize = 120

          return {

            x: avgX - fixedSize,

            y: avgY - fixedSize,

            width: fixedSize * 2,

            height: fixedSize * 2,

            type: 'points',

            centerX: avgX,

            centerY: avgY

          }

        } else {

          // Pour plusieurs points, calculer la bounding box englobante

          const minX = Math.min(...points.map(p => p.x))

          const maxX = Math.max(...points.map(p => p.x))

          const minY = Math.min(...points.map(p => p.y))

          const maxY = Math.max(...points.map(p => p.y))

          

          // Calculer les dimensions nécessaires

          const pointsWidth = maxX - minX

          const pointsHeight = maxY - minY

          

          // Ajouter une marge (minimum 60px de chaque côté)

          const marginX = Math.max(60, pointsWidth * 0.3)

          const marginY = Math.max(60, pointsHeight * 0.3)

          

          // Calculer les dimensions finales

          const finalWidth = pointsWidth + marginX * 2

          const finalHeight = pointsHeight + marginY * 2

          

          return {

            x: minX - marginX,

            y: minY - marginY,

            width: finalWidth,

            height: finalHeight,

            type: 'points',

            centerX: avgX,

            centerY: avgY,

            pointsBounds: {

              minX, maxX, minY, maxY,

              pointsWidth, pointsHeight

            }

          }

        }

      }



      return null

    })



    const drawAnnotationsOnZoom = (ctx, sourceX, sourceY, sourceWidth, sourceHeight) => {

      const annotations = selectedAnnotations.value

      if (!annotations.length) return



      // Calculer le facteur d'échelle entre la source et le canvas

      const scaleX = zoomWidth / sourceWidth

      const scaleY = zoomHeight / sourceHeight



      annotations.forEach(annotation => {

        if (annotation.type === 'rectangle') {

          // Calculer la position du rectangle dans le canvas zoomé

          const rectX = (annotation.x - sourceX) * scaleX

          const rectY = (annotation.y - sourceY) * scaleY

          const rectWidth = annotation.width * scaleX

          const rectHeight = annotation.height * scaleY



          // Ne dessiner que si le rectangle est visible dans la zone

          if (rectX < zoomWidth && rectY < zoomHeight && 

              rectX + rectWidth > 0 && rectY + rectHeight > 0) {

            

            // Dessiner le rectangle

            ctx.strokeStyle = '#00ff00' // Vert pour les rectangles

            ctx.lineWidth = 2

            ctx.setLineDash([5, 3]) // Trait pointillé

            ctx.strokeRect(rectX, rectY, rectWidth, rectHeight)

            ctx.setLineDash([]) // Remettre trait plein

          }



        } else if (annotation.type === 'point') {

          // Calculer la position du point dans le canvas zoomé

          const pointX = (annotation.x - sourceX) * scaleX

          const pointY = (annotation.y - sourceY) * scaleY



          // Ne dessiner que si le point est visible dans la zone

          if (pointX >= 0 && pointX <= zoomWidth && pointY >= 0 && pointY <= zoomHeight) {

            

            // Couleur selon le type de point

            const pointColor = annotation.pointType === 'positive' ? '#00ff00' : '#ff0000'

            

            // Dessiner le cercle du point

            ctx.fillStyle = pointColor

            ctx.strokeStyle = '#ffffff'

            ctx.lineWidth = 2

            ctx.beginPath()

            ctx.arc(pointX, pointY, 6, 0, 2 * Math.PI)

            ctx.fill()

            ctx.stroke()



            // Dessiner le symbole + ou -

            ctx.strokeStyle = '#ffffff'

            ctx.lineWidth = 2

            ctx.beginPath()

            if (annotation.pointType === 'positive') {

              // Dessiner +

              ctx.moveTo(pointX - 3, pointY)

              ctx.lineTo(pointX + 3, pointY)

              ctx.moveTo(pointX, pointY - 3)

              ctx.lineTo(pointX, pointY + 3)

            } else {

              // Dessiner -

              ctx.moveTo(pointX - 3, pointY)

              ctx.lineTo(pointX + 3, pointY)

            }

            ctx.stroke()

          }

        }

      })



      // Si c'est une vue centrée sur des points, dessiner une croix de repère au centre

      if (zoomRegion.value?.type === 'points') {

        ctx.strokeStyle = '#ffff00' // Jaune pour le centre

        ctx.lineWidth = 1

        ctx.setLineDash([3, 3])

        ctx.beginPath()

        const centerX = zoomWidth / 2

        const centerY = zoomHeight / 2

        ctx.moveTo(centerX - 15, centerY)

        ctx.lineTo(centerX + 15, centerY)

        ctx.moveTo(centerX, centerY - 15)

        ctx.lineTo(centerX, centerY + 15)

        ctx.stroke()

        ctx.setLineDash([])

      }

    }



    const updateZoomImage = async () => {

      if (!zoomCanvas.value || !zoomRegion.value) return



      // Trouver l'élément vidéo

      const videoElement = document.querySelector('video')

      if (!videoElement) return



      const canvas = zoomCanvas.value

      const ctx = canvas.getContext('2d')

      

      // Effacer le canvas

      ctx.clearRect(0, 0, zoomWidth, zoomHeight)



      try {

        // Calculer les coordonnées source dans la vidéo

        const sourceX = Math.max(0, zoomRegion.value.x)

        const sourceY = Math.max(0, zoomRegion.value.y)

        const sourceWidth = Math.min(zoomRegion.value.width, videoElement.videoWidth - sourceX)

        const sourceHeight = Math.min(zoomRegion.value.height, videoElement.videoHeight - sourceY)



        // S'assurer que les dimensions sont valides

        if (sourceWidth <= 0 || sourceHeight <= 0) return



        // Dessiner la région zoomée sur le canvas

        ctx.drawImage(

          videoElement,

          sourceX, sourceY, sourceWidth, sourceHeight,  // Source (région de la vidéo)

          0, 0, zoomWidth, zoomHeight                    // Destination (canvas)

        )



        // Dessiner les annotations sur l'image zoomée

        drawAnnotationsOnZoom(ctx, sourceX, sourceY, sourceWidth, sourceHeight)



      } catch (error) {

        console.error('Erreur lors de la capture du zoom:', error)

        // Afficher un message d'erreur sur le canvas

        ctx.fillStyle = '#666'

        ctx.fillRect(0, 0, zoomWidth, zoomHeight)

        ctx.fillStyle = '#fff'

        ctx.font = '14px Arial'

        ctx.textAlign = 'center'

        ctx.fillText('Erreur de capture', zoomWidth / 2, zoomHeight / 2)

      }

    }



    // Watcher pour mettre à jour l'image quand les annotations changent

    watch([selectedAnnotations, currentFrameNumber], () => {

      nextTick(() => {

        updateZoomImage()

      })

    }, { deep: true })



    // Watcher pour mettre à jour quand le temps de la vidéo change

    watch(() => videoStore.currentTime, () => {

      nextTick(() => {

        updateZoomImage()

      })

    })



    // Hook onMounted pour forcer le rafraîchissement au montage

    onMounted(() => {

      nextTick(() => {

        setTimeout(() => {

          updateZoomImage()

        }, 200) // Délai plus long pour s'assurer que tout est prêt

      })

    })



    return {

      selectedAnnotations,

      hasAnnotations,

      zoomRegion,

      currentFrameNumber,

      zoomCanvas,

      zoomWidth,

      zoomHeight,

      updateZoomImage

    }

  }

}

</script>



<style scoped>

.zoom-view {

  height: 100%;

  padding: 10px;

  color: white;

  overflow: auto;

  display: flex;

  flex-direction: column;

  align-items: center;

  justify-content: center;

}



.zoom-image-container {

  display: flex;

  flex-direction: column;

  align-items: center;

  gap: 8px;

}



.zoom-canvas {

  border: 1px solid #555;

  border-radius: 4px;

  background: #000;

}



.zoom-info {

  display: flex;

  gap: 12px;

  font-size: 0.8rem;

  color: #ccc;

}



.no-annotations {

  text-align: center;

  color: #888;

  font-style: italic;

  display: flex;

  align-items: center;

  justify-content: center;

  height: 100%;

}



.no-annotations p {

  margin: 0;

}

</style>