This repository was archived by the owner on Mar 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
242 lines (202 loc) · 8.09 KB
/
script.js
File metadata and controls
242 lines (202 loc) · 8.09 KB
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
// DOM Elements
const image1Input = document.getElementById('image1');
const image2Input = document.getElementById('image2');
const preview1 = document.getElementById('preview1');
const preview2 = document.getElementById('preview2');
const analyzeBtn = document.getElementById('analyze-btn');
const loader = document.getElementById('loader');
const resultsContainer = document.getElementById('results-container');
const resultImage1 = document.getElementById('result-image1');
const resultImage2 = document.getElementById('result-image2');
const differencesList = document.getElementById('differences-list');
const image1Details = document.getElementById('image1-details');
const image2Details = document.getElementById('image2-details');
const jsonOutput = document.getElementById('json-output');
const copyJsonBtn = document.getElementById('copy-json-btn');
const tabBtns = document.querySelectorAll('.tab-btn');
const tabContents = document.querySelectorAll('.tab-content');
// Global variables to store image data
let image1Data = null;
let image2Data = null;
let analysisResults = null;
// Event listeners
image1Input.addEventListener('change', (e) => handleImageUpload(e, preview1, 1));
image2Input.addEventListener('change', (e) => handleImageUpload(e, preview2, 2));
analyzeBtn.addEventListener('click', analyzeImages);
copyJsonBtn.addEventListener('click', copyJsonToClipboard);
// Tab switching
tabBtns.forEach(btn => {
btn.addEventListener('click', () => {
const tabId = btn.getAttribute('data-tab');
// Remove active class from all buttons and content
tabBtns.forEach(btn => btn.classList.remove('active'));
tabContents.forEach(content => content.classList.remove('active'));
// Add active class to current button and content
btn.classList.add('active');
document.getElementById(`${tabId}-tab`).classList.add('active');
});
});
// Handle image upload
function handleImageUpload(event, previewElement, imageNumber) {
const file = event.target.files[0];
if (!file) return;
if (!file.type.startsWith('image/')) {
alert('Please select an image file.');
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const img = document.createElement('img');
img.src = e.target.result;
previewElement.innerHTML = '';
previewElement.appendChild(img);
if (imageNumber === 1) {
image1Data = e.target.result;
} else {
image2Data = e.target.result;
}
checkEnableAnalyzeButton();
};
reader.readAsDataURL(file);
}
// Check if both images are loaded to enable the analyze button
function checkEnableAnalyzeButton() {
if (image1Data && image2Data) {
analyzeBtn.disabled = false;
}
}
// Analyze images using Anthropic API
async function analyzeImages() {
// Show loader
loader.style.display = 'flex';
resultsContainer.style.display = 'none';
try {
// Call your backend service
const response = await fetch('/api/compare-base64', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
image1: image1Data,
image2: image2Data
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'An error occurred during analysis');
}
// Store the results
analysisResults = await response.json();
// Display the results
displayResults(analysisResults);
// Hide loader and show results
loader.style.display = 'none';
resultsContainer.style.display = 'block';
} catch (error) {
console.error('Error analyzing images:', error);
alert('An error occurred while analyzing the images: ' + error.message);
loader.style.display = 'none';
}
}
// Display the comparison results
function displayResults(results) {
// Display images in results section
resultImage1.innerHTML = preview1.innerHTML;
resultImage2.innerHTML = preview2.innerHTML;
// Display key differences
differencesList.innerHTML = '';
results.comparison.keyDifferences.forEach(diff => {
const li = document.createElement('li');
li.textContent = `${diff.attribute}: ${diff.difference}`;
differencesList.appendChild(li);
});
// Display image 1 details
displayImageDetails(results.comparison.image1, image1Details);
// Display image 2 details
displayImageDetails(results.comparison.image2, image2Details);
// Display JSON data
jsonOutput.textContent = JSON.stringify(results, null, 2);
}
// Display details for a single image
function displayImageDetails(imageData, container) {
container.innerHTML = '';
// Loop through each attribute
for (const [key, value] of Object.entries(imageData)) {
if (key !== 'objects' && key !== 'sceneType') { // Handle these specially
const category = document.createElement('div');
category.className = 'attribute-category';
const title = document.createElement('h4');
title.textContent = formatAttributeName(key);
category.appendChild(title);
// Add each detail within this attribute
for (const [detailKey, detailValue] of Object.entries(value)) {
const item = document.createElement('div');
item.className = 'attribute-item';
if (Array.isArray(detailValue)) {
item.innerHTML = `<span class="attribute-label">${formatAttributeName(detailKey)}:</span> ${detailValue.join(', ')}`;
} else {
item.innerHTML = `<span class="attribute-label">${formatAttributeName(detailKey)}:</span> ${detailValue}`;
}
category.appendChild(item);
}
container.appendChild(category);
} else if (key === 'objects' || key === 'sceneType') {
// Handle complex nested structures
const category = document.createElement('div');
category.className = 'attribute-category';
const title = document.createElement('h4');
title.textContent = formatAttributeName(key);
category.appendChild(title);
// Add primary and secondary
const primaryItem = document.createElement('div');
primaryItem.className = 'attribute-item';
primaryItem.innerHTML = `<span class="attribute-label">Primary:</span> ${value.primary}`;
category.appendChild(primaryItem);
if (value.secondary) {
const secondaryItem = document.createElement('div');
secondaryItem.className = 'attribute-item';
secondaryItem.innerHTML = `<span class="attribute-label">Secondary:</span> ${value.secondary}`;
category.appendChild(secondaryItem);
}
// Add specifics if available
if (value.specifics && value.specifics.length > 0) {
const specificsItem = document.createElement('div');
specificsItem.className = 'attribute-item';
specificsItem.innerHTML = `<span class="attribute-label">Specifics:</span> ${value.specifics.join(', ')}`;
category.appendChild(specificsItem);
}
// Add qualities if available
if (value.qualities && value.qualities.length > 0) {
const qualitiesItem = document.createElement('div');
qualitiesItem.className = 'attribute-item';
qualitiesItem.innerHTML = `<span class="attribute-label">Qualities:</span> ${value.qualities.join(', ')}`;
category.appendChild(qualitiesItem);
}
container.appendChild(category);
}
}
}
// Format attribute names for display
function formatAttributeName(name) {
return name
.replace(/([A-Z])/g, ' $1') // Add space before capital letters
.replace(/^./, str => str.toUpperCase()); // Capitalize first letter
}
// Copy JSON to clipboard
function copyJsonToClipboard() {
const jsonStr = JSON.stringify(analysisResults, null, 2);
navigator.clipboard.writeText(jsonStr)
.then(() => {
// Show success message
const originalText = copyJsonBtn.textContent;
copyJsonBtn.textContent = 'Copied!';
setTimeout(() => {
copyJsonBtn.textContent = originalText;
}, 2000);
})
.catch(err => {
console.error('Failed to copy JSON: ', err);
alert('Failed to copy JSON to clipboard.');
});
}