File size: 11,453 Bytes
11a5c45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3db8e08
 
 
11a5c45
3db8e08
11a5c45
 
 
3db8e08
11a5c45
 
3db8e08
11a5c45
3db8e08
11a5c45
 
3db8e08
11a5c45
 
 
 
 
 
 
3db8e08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11a5c45
 
3db8e08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11a5c45
 
3db8e08
11a5c45
 
 
 
 
 
3db8e08
 
 
 
 
 
 
 
 
11a5c45
 
 
 
 
 
3db8e08
11a5c45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3db8e08
 
 
 
 
 
 
 
11a5c45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3db8e08
 
 
11a5c45
3db8e08
11a5c45
 
 
 
 
 
 
 
 
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
// Global variables
let currentImage = null;
let analysisInProgress = false;

// DOM Elements
const dropZone = document.getElementById('dropZone');
const imageUpload = document.getElementById('imageUpload');
const previewImage = document.getElementById('previewImage');
const resultsSection = document.getElementById('resultsSection');
const loadingSection = document.getElementById('loadingSection');

// Measurement elements
const measurementElements = {
    bust: { value: document.getElementById('bustValue'), note: document.getElementById('bustNote') },
    underbust: { value: document.getElementById('underbustValue'), note: document.getElementById('underbustNote') },
    waist: { value: document.getElementById('waistValue'), note: document.getElementById('waistNote') },
    hips: { value: document.getElementById('hipsValue'), note: document.getElementById('hipsNote') },
    height: { value: document.getElementById('heightValue'), note: document.getElementById('heightNote') },
    weight: { value: document.getElementById('weightValue'), note: document.getElementById('weightNote') }
};

// Confidence elements
const confidenceValue = document.getElementById('confidenceValue');
const analysisNotes = document.getElementById('analysisNotes');

// Initialize the application
document.addEventListener('DOMContentLoaded', function() {
    initializeEventListeners();
});

function initializeEventListeners() {
    // File input change
    imageUpload.addEventListener('change', handleFileSelect);
    
    // Drag and drop events
    ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
        dropZone.addEventListener(eventName, preventDefaults, false);
    });
    
    ['dragenter', 'dragover'].forEach(eventName => {
        dropZone.addEventListener(eventName, highlight, false);
    });
    
    ['dragleave', 'drop'].forEach(eventName => {
        dropZone.addEventListener(eventName, unhighlight, false);
    });
    
    dropZone.addEventListener('drop', handleDrop, false);
}

function preventDefaults(e) {
    e.preventDefault();
    e.stopPropagation();
}

function highlight() {
    dropZone.classList.add('drag-over');
}

function unhighlight() {
    dropZone.classList.remove('drag-over');
}

function handleDrop(e) {
    const dt = e.dataTransfer;
    const files = dt.files;
    handleFiles(files);
}

function handleFileSelect(e) {
    const files = e.target.files;
    handleFiles(files);
}

function handleFiles(files) {
    if (analysisInProgress) return;
    
    const file = files[0];
    if (!file) return;
    
    // Check if file is an image
    if (!file.type.match('image.*')) {
        alert('Please upload an image file (JPG, PNG, WEBP)');
        return;
    }
    
    // Check file size (10MB max)
    if (file.size > 10 * 1024 * 1024) {
        alert('File size must be less than 10MB');
        return;
    }
    
    // Create preview
    const reader = new FileReader();
    reader.onload = function(e) {
        previewImage.src = e.target.result;
        currentImage = e.target.result;
        startAnalysis();
    };
    reader.readAsDataURL(file);
}
function startAnalysis() {
    if (!currentImage) return;
    
    analysisInProgress = true;
    
    // Show loading state
    resultsSection.classList.add('hidden');
    loadingSection.classList.remove('hidden');
    
    // Get reference values
    const referenceType = document.getElementById('referenceType').value;
    const referenceValue = parseFloat(document.getElementById('referenceValue').value) || null;
    const unitSystem = document.getElementById('unitSystem').value;
    const actualHeight = parseFloat(document.getElementById('actualHeight').value) || null;
    const heightUnit = document.getElementById('heightUnit').value;
    
    // Simulate AI analysis (in a real app, this would call an API)
    setTimeout(() => {
        performAnalysis(referenceType, referenceValue, unitSystem, actualHeight, heightUnit);
    }, 3000);
}
function performAnalysis(referenceType, referenceValue, unitSystem, actualHeight, heightUnit) {
    // This is a mock analysis - in a real application, this would use computer vision APIs
    const measurements = generateMockMeasurements(referenceType, referenceValue, unitSystem, actualHeight, heightUnit);
    
    // Update UI with results
    updateResultsUI(measurements, unitSystem);
    
    // Hide loading, show results
    loadingSection.classList.add('hidden');
    resultsSection.classList.remove('hidden');
    
    analysisInProgress = false;
}
function generateMockMeasurements(referenceType, referenceValue, unitSystem, actualHeight, heightUnit) {
    // Convert actual height to cm for internal calculations
    let baseHeightCm;
    if (actualHeight) {
        baseHeightCm = heightUnit === 'in' ? actualHeight * 2.54 : actualHeight;
    } else {
        baseHeightCm = 160 + Math.random() * 30; // 160-190cm if not provided
    }
    
    // Base measurements in cm for metric system
    let baseMeasurements = {
        bust: (85 + Math.random() * 20) * (baseHeightCm / 170),
        underbust: (70 + Math.random() * 15) * (baseHeightCm / 170),
        waist: (65 + Math.random() * 15) * (baseHeightCm / 170),
        hips: (90 + Math.random() * 20) * (baseHeightCm / 170),
        height: baseHeightCm,
        weight: (55 + Math.random() * 25) * (baseHeightCm / 170) / 1.7
    };
    
    // Adjust measurements if reference is provided
    if (referenceValue && referenceType) {
        // Convert reference value to cm if needed
        let referenceValueCm = referenceValue;
        if (unitSystem === 'imperial' && referenceType !== 'weight') {
            referenceValueCm = referenceValue * 2.54;
        } else if (unitSystem === 'imperial' && referenceType === 'weight') {
            referenceValueCm = referenceValue * 0.453592;
        }
        
        // Calculate scaling factor based on reference
        const scalingFactor = referenceValueCm / baseMeasurements[referenceType];
        
        // Apply scaling to all measurements
        Object.keys(baseMeasurements).forEach(key => {
            if (key !== 'weight') {
                baseMeasurements[key] *= scalingFactor;
            } else {
                baseMeasurements[key] *= scalingFactor;
            }
        });
    }
    
    // Convert to desired unit system for display
    const measurements = {};
    Object.keys(baseMeasurements).forEach(key => {
        let displayValue;
        let estimated = true;
        let confidence = 'medium';
        
        // Determine if this measurement is accurate based on reference
        if (referenceType === key) {
            estimated = false;
            confidence = 'high';
        } else if (referenceValue && referenceType) {
            confidence = 'high';
        }
        
        if (unitSystem === 'metric') {
            if (key === 'weight') {
                displayValue = Math.round(baseMeasurements[key]);
            } else {
                displayValue = Math.round(baseMeasurements[key]);
            }
        } else {
            if (key === 'weight') {
                displayValue = Math.round(baseMeasurements[key] * 2.20462);
            } else {
                displayValue = Math.round(baseMeasurements[key] / 2.54 * 10) / 10; // Round to 1 decimal for inches
            } else {
                displayValue = Math.round(baseMeasurements[key]);
            }
        }
        
        measurements[key] = {
            value: displayValue,
            estimated: estimated,
            confidence: confidence
        };
    });
    
    return measurements;
}
function updateResultsUI(measurements, unitSystem) {
    // Update each measurement
    Object.keys(measurements).forEach(key => {
        const measurement = measurements[key];
        const element = measurementElements[key];
        
        if (element) {
            // Update value with appropriate units
            let unit = '';
            if (key === 'weight') {
                unit = unitSystem === 'metric' ? 'kg' : 'lbs';
            } else {
                unit = unitSystem === 'metric' ? 'cm' : 'in';
            }
            
            element.value.textContent = `${measurement.value} ${unit}`;
            
            // Update note
            if (measurement.estimated) {
                element.note.textContent = 'Estimated value';
                element.note.className = 'text-sm text-amber-600';
            } else {
                element.note.textContent = 'Measured from reference';
                element.note.className = 'text-sm text-green-600';
            }
        }
    });
    
    // Update confidence level
    const confidenceLevels = Object.values(measurements).map(m => m.confidence);
    const avgConfidence = confidenceLevels.reduce((a, b) => {
        const values = { low: 1, medium: 2, high: 3 };
        return a + values[b];
    }, 0) / confidenceLevels.length;
    
    let confidenceText = '';
    let confidenceColor = '';
    
    if (avgConfidence >= 2.5) {
        confidenceText = 'High';
        confidenceColor = 'text-green-600';
    } else if (avgConfidence >= 1.5) {
        confidenceText = 'Medium';
        confidenceColor = 'text-amber-600';
    } else {
        confidenceText = 'Low';
        confidenceColor = 'text-red-600';
    }
    
    confidenceValue.textContent = confidenceText;
    confidenceValue.className = `font-semibold ${confidenceColor}`;
    
    // Update analysis notes
    let notes = [];
    
    const referenceType = document.getElementById('referenceType').value;
    const referenceValue = parseFloat(document.getElementById('referenceValue').value) || null;
    const actualHeight = parseFloat(document.getElementById('actualHeight').value) || null;
    
    if (!referenceValue && !actualHeight) {
        notes.push('No reference measurement provided. Measurements are estimated based on average proportions.');
    } else if (referenceValue && !actualHeight) {
        notes.push(`Using ${referenceType} measurement as reference. Height is estimated.`);
    } else if (actualHeight) {
        notes.push('Using provided height for proportion calculations.');
    }
    
    if (measurements.weight.estimated) {
        notes.push('Weight is estimated based on body proportions and typical BMI ranges.');
    }
    
    analysisNotes.querySelector('span').textContent = notes.join(' ');
}
function resetAnalysis() {
    // Reset file input
    imageUpload.value = '';
    
    // Reset preview
    previewImage.src = '';
    currentImage = null;
    
    // Reset measurement values
    Object.values(measurementElements).forEach(element => {
        element.value.textContent = '-- cm';
        element.note.textContent = '';
        element.note.className = 'text-sm text-gray-600';
    });
    
    // Reset reference inputs
    document.getElementById('referenceType').value = 'bust';
    document.getElementById('unitSystem').value = 'metric';
    document.getElementById('referenceValue').value = '';
    document.getElementById('actualHeight').value = '';
    document.getElementById('heightUnit').value = 'cm';
    
    // Hide results
    resultsSection.classList.add('hidden');
}
// Utility function to convert pixels to real measurements
function pixelsToCm(pixels, referencePixels, referenceCm) {
    if (!referencePixels || !referenceCm) return null;
    return (pixels / referencePixels) * referenceCm;
}