File size: 10,556 Bytes
c9595c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Labélisation d'Images avec SAM</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
    <style>
        /* General Styles */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        header {
            background-color: #4CAF50;
            padding: 15px 0;
            text-align: center;
            color: white;
            font-size: 24px;
            font-weight: bold;
        }

        section {
            margin: 20px auto;
            max-width: 1200px;
            padding: 20px;
            background: white;
            box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        .upload-section {
            text-align: center;
        }

        .upload-section input[type="file"] {
            margin: 10px 0;
        }

        .image-container {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            justify-content: center;
        }

        .image-item {
            width: 150px;
            height: 150px;
            overflow: hidden;
            border: 2px solid #ddd;
            border-radius: 8px;
            cursor: pointer;
            transition: transform 0.3s ease, border-color 0.3s ease;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #fff;
        }

        .image-item:hover {
            border-color: #4CAF50;
            transform: scale(1.05);
        }

        .image-item img {
            max-width: 100%;
            max-height: 100%;
            object-fit: cover;
        }

        canvas {
            border: 2px solid #ddd;
            border-radius: 8px;
            margin: 20px auto;
            display: block;
        }

        .class-management {
            text-align: center;
            margin-bottom: 20px;
        }

        .class-management input[type="text"] {
            padding: 8px;
            font-size: 16px;
            width: 300px;
            margin-right: 10px;
        }

        .class-list {
            display: flex;
            justify-content: center;
            flex-wrap: wrap;
            gap: 10px;
            list-style: none;
            padding: 0;
        }

        .class-item {
            padding: 5px 15px;
            border-radius: 20px;
            background-color: #f4f4f4;
            border: 1px solid #ccc;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .class-item:hover {
            background-color: #ddd;
        }

        .class-item.active {
            background-color: #4CAF50;
            color: white;
            border-color: #45a049;
        }

        .controls {
            text-align: center;
            margin-top: 20px;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border-radius: 5px;
            transition: background-color 0.3s ease;
            margin: 0 10px;
        }

        button:hover {
            background-color: #45a049;
        }

        button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }

        .result-section img {
            max-width: 100%;
            margin: 20px auto;
            display: block;
            border: 2px solid #4CAF50;
            border-radius: 8px;
        }
    </style>
</head>
<body>
    <header>Labélisation d'Images avec SAM</header>

    <!-- Section 1: Téléchargement d'images -->
    <section class="upload-section">
        <h2>Télécharger vos images</h2>
        <form method="post" enctype="multipart/form-data">
            <input type="file" id="image" name="images" accept="image/*" multiple required>
            <br>
            <button type="submit">Télécharger</button>
        </form>
    </section>

    {% if uploaded_images %}
    <!-- Section 2: Galerie des images téléchargées -->
    <section>
        <h2>Images téléchargées</h2>
        <div class="image-container">
            {% for image in uploaded_images %}
            <div class="image-item" onclick="loadImage('{{ image }}')">
                <img src="{{ url_for('static', filename='uploads/' + image) }}" alt="{{ image }}">
            </div>
            {% endfor %}
        </div>
    </section>

    <!-- Section 3: Zone de travail -->
    <section>
        <canvas id="image-canvas"></canvas>
    </section>

    <!-- Section 4: Gestion des classes -->
    <section class="class-management">
        <h3>Ajouter une classe</h3>
        <input type="text" id="class-name" placeholder="Nom de la classe">
        <button id="add-class">Ajouter</button>
        <ul id="class-list" class="class-list"></ul>
    </section>

    <!-- Section 5: Contrôles -->
    <section class="controls">
        <button id="finish-button" disabled>Terminer l'annotation</button>
        <button id="segment-button" disabled>Lancer la segmentation</button>
    </section>
    {% endif %}

    <script>
        let selectedImage = null; // Image actuellement sélectionnée
        let annotations = {}; // Stocke les annotations de chaque image (clé : image, valeur : points)
        let currentClass = null; // Classe actuellement sélectionnée
        const finishButton = document.getElementById('finish-button');
        const segmentButton = document.getElementById('segment-button');

        function loadImage(imageName) {
            if (!imageName) {
                alert("Veuillez sélectionner une image !");
                return;
            }

            selectedImage = imageName; // Stockez le nom de l'image sélectionnée
            console.log("Image sélectionnée :", selectedImage);

            // Initialiser les annotations pour cette image si elles n'existent pas
            if (!annotations[selectedImage]) {
                annotations[selectedImage] = [];
            }

            const img = new Image();
            img.src = `/static/uploads/${imageName}`;
            img.onload = () => {
                const canvas = document.getElementById('image-canvas');
                const ctx = canvas.getContext('2d');
                canvas.width = img.width;
                canvas.height = img.height;
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                ctx.drawImage(img, 0, 0);

                // Dessiner les points existants pour cette image
                annotations[selectedImage].forEach(point => {
                    drawPoint(ctx, point.x, point.y, point.class);
                });
            };

            finishButton.disabled = false;
        }

        function drawPoint(ctx, x, y, pointClass) {
            ctx.fillStyle = pointClass === 'arbre' ? 'green' : 'red';
            ctx.beginPath();
            ctx.arc(x, y, 5, 0, 2 * Math.PI);
            ctx.fill();
        }

        document.getElementById('add-class').addEventListener('click', () => {
            const className = document.getElementById('class-name').value.trim();
            if (!className) {
                alert("Veuillez entrer un nom de classe !");
                return;
            }
            const li = document.createElement('li');
            li.textContent = className;
            li.classList.add('class-item');
            li.onclick = () => {
                document.querySelectorAll('.class-item').forEach(item => item.classList.remove('active'));
                li.classList.add('active');
                currentClass = className;
                console.log("Classe sélectionnée :", currentClass);
            };
            document.getElementById('class-list').appendChild(li);
            document.getElementById('class-name').value = '';
        });

        const canvas = document.getElementById('image-canvas');
        const ctx = canvas.getContext('2d');

        canvas.addEventListener('click', (event) => {
            if (!currentClass) {
                alert("Veuillez sélectionner une classe avant d'ajouter des points !");
                return;
            }

            if (!selectedImage) {
                alert("Veuillez sélectionner une image avant d'ajouter des points !");
                return;
            }

            const rect = canvas.getBoundingClientRect();
            const x = event.clientX - rect.left;
            const y = event.clientY - rect.top;

            const newPoint = { x, y, class: currentClass };
            annotations[selectedImage].push(newPoint);
            console.log(`Point ajouté pour ${selectedImage}:`, newPoint);

            drawPoint(ctx, x, y, currentClass);
        });

        finishButton.addEventListener('click', () => {
            if (!selectedImage) {
                alert("Veuillez sélectionner une image !");
                return;
            }

            console.log(`Annotation terminée pour ${selectedImage}.`);
            alert(`Annotation pour ${selectedImage} terminée !`);

            finishButton.disabled = true;

            // Vérifiez si toutes les annotations sont terminées
            if (Object.keys(annotations).length > 0) {
                segmentButton.disabled = false;
            }
        });

        segmentButton.addEventListener('click', () => {
            const dataToSend = Object.keys(annotations).map(imageName => ({
                image_name: imageName,
                points: annotations[imageName]
            }));

            console.log("Données envoyées :", dataToSend);

            fetch('/segment', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(dataToSend)
            })
            .then(response => response.json())
            .then(data => {
                console.log("Réponse du backend :", data);
                if (data.success) {
                    alert("Segmentation réussie !");
                } else {
                    alert("Erreur : " + data.error);
                }
            })
            .catch(err => console.error('Erreur lors de la segmentation :', err));
        });
    </script>
</body>
</html>