File size: 14,601 Bytes
965e09e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ec1256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
965e09e
 
 
 
 
 
 
7ec1256
965e09e
 
 
 
 
 
7ec1256
965e09e
 
 
 
7ec1256
 
 
 
 
 
 
 
 
 
 
965e09e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ec1256
 
 
 
 
 
 
965e09e
dc9bbcb
 
 
 
 
 
 
965e09e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0ceca0
d08b3d5
 
 
 
 
 
 
 
 
 
965e09e
 
 
 
 
 
d08b3d5
965e09e
d08b3d5
 
 
965e09e
 
 
 
 
d08b3d5
965e09e
 
 
 
 
 
 
 
 
d08b3d5
965e09e
 
 
b0ceca0
7ec1256
b0ceca0
 
 
 
 
7ec1256
 
965e09e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Virtual Fashion Try-On</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- Cropper.js CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.13/cropper.min.css" rel="stylesheet">
</head>

<body class="bg-gray-900 text-white min-h-screen flex flex-col items-center py-10">

    <h1 class="text-4xl font-bold text-blue-400 mb-10">Virtual Fashion Try-On</h1>

    <div class="flex flex-col md:flex-row gap-10 w-full max-w-6xl">

        <!-- LEFT: Input Form -->
        <form id="tryon-form" action="/" method="post" enctype="multipart/form-data" class="w-full md:w-1/2 bg-gray-800 rounded-2xl shadow-lg p-8 space-y-6">

            <div class="grid grid-cols-1 gap-8">
                <!-- Person Image Upload -->
                <div>
                    <div class="flex justify-between items-center mb-2">
                        <h2 class="text-lg font-semibold">Upload your photo</h2>
                        {% if cached_person %}
                        <button type="button" onclick="changePerson()" class="bg-yellow-500 hover:bg-yellow-600 text-white text-sm px-3 py-1 rounded-lg transition">
                            Change Person
                        </button>
                        {% endif %}
                    </div>
                    
                    {% if cached_person %}
                    <!-- Show cached person image -->
                    <div class="border-2 border-green-500 rounded-xl p-4 bg-green-900/20">
                        <div class="flex flex-col items-center">
                            <div class="flex items-center gap-2 mb-2">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-green-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                                </svg>
                                <p class="text-green-400 text-sm font-medium">Person image cached</p>
                            </div>
                            <img src="/uploads/person.jpg" alt="Cached Person" class="max-h-32 rounded-lg border border-gray-600">
                            <p class="text-gray-400 text-xs mt-2">Coordinates saved - no need to re-upload!</p>
                        </div>
                    </div>
                    {% else %}
                    <label for="person_image" class="flex flex-col items-center justify-center border-2 border-dashed border-gray-600 rounded-xl p-6 hover:bg-gray-700 cursor-pointer">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10 text-gray-400 mb-2" fill="none"
                            viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M7 16v4m0 0h10m-10 0v-4m0 0h10m-10 0V5m0 0h10m-10 0H5m14 0h-2" />
                        </svg>
                        <p class="text-gray-400">Drag & drop or click to upload</p>
                        <input id="person_image" type="file" name="person_image" class="hidden" 
                            onchange="showFileName('person_image', 'person_filename', 'person_preview')">
                    </label>
                    <p id="person_filename" class="text-green-400 text-sm mt-2 text-center"></p>
                    <div class="mt-3 flex justify-center">
                        <img id="person_preview" class="hidden max-h-32 rounded-lg border border-gray-600">
                    </div>
                    {% endif %}
                </div>

                <!-- Garment Image Upload with Cropper -->
                <div>
                    <div class="flex justify-between items-center mb-2">
                        <h2 class="text-lg font-semibold">Upload garment image</h2>
                        {% if cached_person %}
                        <div class="flex items-center gap-1 text-xs text-green-400">
                            <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
                            </svg>
                            <span>Fast mode</span>
                        </div>
                        {% endif %}
                    </div>
                    <label for="tshirt_image" class="flex flex-col items-center justify-center border-2 border-dashed border-gray-600 rounded-xl p-6 hover:bg-gray-700 cursor-pointer">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10 text-gray-400 mb-2" fill="none"
                            viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M7 16v4m0 0h10m-10 0v-4m0 0h10m-10 0V5m0 0h10m-10 0H5m14 0h-2" />
                        </svg>
                        <p class="text-gray-400">Drag & drop or click to upload</p>
                        <input id="tshirt_image" type="file" name="tshirt_image" class="hidden" required>
                    </label>
                    <p id="tshirt_filename" class="text-green-400 text-sm mt-2 text-center"></p>

                    <!-- Cropping Container -->
                    <div class="mt-3 flex justify-center">
                        <img id="tshirt_preview" class="hidden max-h-64 rounded-lg border border-gray-600">
                    </div>
                </div>
            </div>

            <!-- Submit Button -->
            <div class="flex justify-center">
                <button type="submit" class="bg-pink-500 hover:bg-pink-600 text-white font-semibold py-3 px-8 rounded-xl shadow-md transition">
                    πŸš€ Perform Virtual Try-On
                </button>
            </div>

        </form>

        <!-- RIGHT: Output -->
        <div class="w-full md:w-1/2 bg-gray-800 rounded-2xl shadow-lg p-8 flex items-center justify-center text-center">
            {% if result_img %}
            <div>
                <h2 class="text-2xl font-bold mb-6 text-center">πŸŽ‰ Your Virtual Try-On Result</h2>
                {% if processing_time %}
                <div class="text-center mb-4">
                    <span class="bg-blue-500 text-white px-3 py-1 rounded-full text-sm">
                        ⚑ Processed in {{ processing_time }}
                    </span>
                </div>
                {% endif %}
                <div class="flex justify-center mb-6">
                    <img id="result-image" src="{{ result_img }}" alt="Result Image" class="rounded-xl" 
                         onerror="this.style.display='none'; this.nextElementSibling.style.display='block';">
                    <div style="display:none;" class="text-red-500 p-4 border border-red-500 rounded-xl">
                        <p>❌ Image failed to load</p>
                        <p class="text-sm">Please try again or check the server logs</p>
                        <p class="text-xs">Image URL: {{ result_img }}</p>
                    </div>
                </div>
                <div class="flex justify-center">
                    <button onclick="downloadImage()" class="bg-green-500 hover:bg-green-600 text-white font-semibold py-3 px-6 rounded-xl shadow-md transition flex items-center gap-2">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24"
                            stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                        </svg>
                        Download Image
                    </button>
                </div>
            </div>
            {% else %}
            <div id="output-container">
                <div id="loading-spinner" style="display: none;" class="flex flex-col items-center">
                    <svg class="animate-spin h-16 w-16 text-pink-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4">
                        </circle>
                        <path class="opacity-75" fill="currentColor"
                            d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 
                            12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    <p class="mt-4 text-lg">Processing... Please wait.</p>
                </div>
                <div id="placeholder-text">
                    <h2 class="text-2xl font-bold text-center text-gray-500">Your result will appear here</h2>
                </div>
            </div>
            {% endif %}
        </div>

    </div>

        <!-- Global Full-screen Loader Overlay -->
    <div id="global-loader" style="display:none;" class="fixed inset-0 z-50 bg-black/70 flex items-center justify-center">
        <div class="flex flex-col items-center">
            <svg class="animate-spin h-16 w-16 text-pink-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
            </svg>
            <p class="mt-4 text-lg">Processing... Please wait.</p>
        </div>
    </div>

    <!-- Cropper.js -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.13/cropper.min.js"></script>

    <script>
        let cropper;

        // Global loader on every submit
        document.getElementById('tryon-form').addEventListener('submit', function(e) {
            const overlay = document.getElementById('global-loader');
            if (overlay) overlay.style.display = 'flex';

            // If garment cropper is active β†’ replace original file with cropped version
            if (cropper) {
                e.preventDefault();

                cropper.getCroppedCanvas().toBlob((blob) => {
                    const file = new File([blob], "cropped_garment.png", { type: "image/png" });

                    // Replace original input
                    const dataTransfer = new DataTransfer();
                    dataTransfer.items.add(file);
                    document.getElementById('tshirt_image').files = dataTransfer.files;

                    // Now submit form
                    e.target.submit();
                });
                return;
            }
        });

        // Change Person: full refresh via POST + server redirect
        function changePerson() {
            const form = document.createElement('form');
            form.method = 'POST';
            form.action = '/change_person';
            document.body.appendChild(form);
            form.submit();
        }

        // Show file name + preview (person only)
        function showFileName(inputId, filenameId, previewId) {
            const input = document.getElementById(inputId);
            const filename = document.getElementById(filenameId);
            const preview = document.getElementById(previewId);

            if (input.files.length > 0) {
                const file = input.files[0];
                filename.textContent = "βœ”οΈ " + file.name + " uploaded";

                const reader = new FileReader();
                reader.onload = function(e) {
                    preview.src = e.target.result;
                    preview.classList.remove("hidden");
                };
                reader.readAsDataURL(file);
            } else {
                filename.textContent = "";
                preview.classList.add("hidden");
            }
        }

        // Garment upload with Cropper
        document.getElementById('tshirt_image').addEventListener('change', function() {
            const input = this;
            const filename = document.getElementById('tshirt_filename');
            const preview = document.getElementById('tshirt_preview');

            if (input.files.length > 0) {
                const file = input.files[0];
                filename.textContent = "βœ”οΈ " + file.name + " uploaded";

                const reader = new FileReader();
                reader.onload = function(e) {
                    preview.src = e.target.result;
                    preview.classList.remove("hidden");

                    // Destroy old cropper if exists
                    if (cropper) {
                        cropper.destroy();
                    }

                    // Initialize Cropper.js with square ratio
                    cropper = new Cropper(preview, {
                        aspectRatio: 1,
                        viewMode: 1,
                        autoCropArea: 1,
                    });
                };
                reader.readAsDataURL(file);
            } else {
                filename.textContent = "";
                preview.classList.add("hidden");
                if (cropper) {
                    cropper.destroy();
                    cropper = null;
                }
            }
        });

        // Download result
        function downloadImage() {
            const img = document.getElementById('result-image');
            const canvas = document.createElement('canvas');
            const ctx = canvas.getContext('2d');
            canvas.width = img.naturalWidth;
            canvas.height = img.naturalHeight;
            ctx.drawImage(img, 0, 0);
            canvas.toBlob(function(blob) {
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = 'virtual-try-on-result.png';
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                URL.revokeObjectURL(url);
            }, 'image/png');
        }
    </script>

</body>

</html>