Skip to content

Commit 15376f1

Browse files
authored
1 parent 79898f0 commit 15376f1

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

gemini-bbox.html

+58-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import { marked } from "https://esm.run/marked";
1010

1111
function getApiKey() {
12-
let apiKey = localStorage.getItem("GEMINI_API_KEY");
13-
if (!apiKey) {
14-
apiKey = prompt("Please enter your Gemini API key:");
15-
if (apiKey) {
16-
localStorage.setItem("GEMINI_API_KEY", apiKey);
12+
let apiKey = localStorage.getItem("GEMINI_API_KEY");
13+
if (!apiKey) {
14+
apiKey = prompt("Please enter your Gemini API key:");
15+
if (apiKey) {
16+
localStorage.setItem("GEMINI_API_KEY", apiKey);
17+
}
1718
}
18-
}
19-
return apiKey;
19+
return apiKey;
2020
}
2121

2222
async function getGenerativeModel(params) {
@@ -83,6 +83,9 @@
8383

8484
resultDiv.innerHTML = 'Processing...';
8585

86+
// Clear previous bounding box images
87+
document.getElementById('boundingBoxImages').innerHTML = '';
88+
8689
try {
8790
const model = await getGenerativeModel({ model: modelSelect.value });
8891
let content = [promptInput.value];
@@ -98,7 +101,6 @@
98101
const text = response.text();
99102

100103
resultDiv.innerHTML = marked.parse(text);
101-
document.getElementById('imagePreview').style.display = 'none';
102104

103105
if (fileInput.files[0]) {
104106
// Extract coordinates from the response
@@ -155,6 +157,9 @@
155157

156158
// Draw bounding boxes
157159
const colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#00FFFF'];
160+
const boundingBoxImages = document.getElementById('boundingBoxImages');
161+
boundingBoxImages.innerHTML = ''; // Clear previous bounding box images
162+
158163
coordinates.forEach((box, index) => {
159164
const [ymin, xmin, ymax, xmax] = box.map(coord => coord / 1000);
160165

@@ -164,6 +169,31 @@
164169
ctx.strokeStyle = colors[index % colors.length];
165170
ctx.lineWidth = 5;
166171
ctx.strokeRect(xmin * image.width + 80, ymin * image.height + 20, width, height);
172+
173+
// Extract bounding box image
174+
const boundingBoxCanvas = document.createElement('canvas');
175+
boundingBoxCanvas.width = width;
176+
boundingBoxCanvas.height = height;
177+
const bbCtx = boundingBoxCanvas.getContext('2d');
178+
bbCtx.drawImage(image, xmin * image.width, ymin * image.height, width, height, 0, 0, width, height);
179+
180+
// Convert canvas to JPEG base64 data URI
181+
const dataURI = boundingBoxCanvas.toDataURL('image/jpeg');
182+
183+
const boundingBoxContainer = document.createElement('div');
184+
boundingBoxContainer.className = 'bounding-box-container';
185+
186+
const title = document.createElement('p');
187+
title.textContent = `Bounding Box: [${box.join(', ')}]`;
188+
boundingBoxContainer.appendChild(title);
189+
190+
const img = document.createElement('img');
191+
img.src = dataURI;
192+
img.style.width = `${Math.round(width)}px`;
193+
img.style.height = `${Math.round(height)}px`;
194+
boundingBoxContainer.appendChild(img);
195+
196+
boundingBoxImages.appendChild(boundingBoxContainer);
167197
});
168198

169199
// Draw axes and labels
@@ -215,6 +245,7 @@
215245
document.getElementById('canvas').getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
216246
document.getElementById('imagePreview').src = '';
217247
document.getElementById('imagePreview').style.display = 'none';
248+
document.getElementById('boundingBoxImages').innerHTML = '';
218249
}
219250

220251
function previewImage(event) {
@@ -260,6 +291,24 @@
260291
display: none;
261292
margin-top: 10px;
262293
}
294+
#boundingBoxImages {
295+
display: flex;
296+
flex-wrap: wrap;
297+
gap: 20px;
298+
margin-top: 20px;
299+
}
300+
.bounding-box-container {
301+
display: block;
302+
margin-bottom: 20px;
303+
}
304+
.bounding-box-container p {
305+
margin: 0 0 5px 0;
306+
font-weight: bold;
307+
}
308+
.bounding-box-container img {
309+
display: block;
310+
border: 1px solid #ccc;
311+
}
263312
</style>
264313
</head>
265314
<body>
@@ -279,5 +328,6 @@ <h1>Gemini API Image Bounding Box Visualization</h1>
279328
<img id="imagePreview" alt="Image preview" />
280329
<canvas id="canvas"></canvas>
281330
</div>
331+
<div id="boundingBoxImages"></div>
282332
</body>
283333
</html>

0 commit comments

Comments
 (0)