|
74 | 74 | const fileInput = document.getElementById('imageInput'); |
75 | 75 | const promptInput = document.getElementById('promptInput'); |
76 | 76 | const resultDiv = document.getElementById('result'); |
| 77 | + const modelSelect = document.getElementById('modelSelect'); |
77 | 78 |
|
78 | | - if (!fileInput.files[0] || !promptInput.value) { |
79 | | - alert('Please select an image and enter a prompt.'); |
| 79 | + if (!promptInput.value) { |
| 80 | + alert('Please enter a prompt.'); |
80 | 81 | return; |
81 | 82 | } |
82 | 83 |
|
83 | 84 | resultDiv.innerHTML = 'Processing...'; |
84 | 85 |
|
85 | 86 | try { |
86 | | - const model = await getGenerativeModel({ model: "gemini-1.5-pro" }); |
87 | | - const compressedImage = await resizeAndCompressImage(fileInput.files[0]); |
88 | | - const imagePart = await fileToGenerativePart(compressedImage); |
| 87 | + const model = await getGenerativeModel({ model: modelSelect.value }); |
| 88 | + let content = [promptInput.value]; |
| 89 | + |
| 90 | + if (fileInput.files[0]) { |
| 91 | + const compressedImage = await resizeAndCompressImage(fileInput.files[0]); |
| 92 | + const imagePart = await fileToGenerativePart(compressedImage); |
| 93 | + content.push(imagePart); |
| 94 | + } |
89 | 95 |
|
90 | | - const result = await model.generateContent([promptInput.value, imagePart]); |
| 96 | + const result = await model.generateContent(content); |
91 | 97 | const response = await result.response; |
92 | 98 | const text = response.text(); |
93 | 99 |
|
94 | 100 | resultDiv.innerHTML = marked.parse(text); |
| 101 | + document.getElementById('imagePreview').style.display = 'none'; |
95 | 102 |
|
96 | | - // Extract coordinates from the response |
97 | | - const coordinates = extractCoordinates(text); |
98 | | - if (coordinates.length > 0) { |
99 | | - displayImageWithBoundingBoxes(compressedImage, coordinates); |
| 103 | + if (fileInput.files[0]) { |
| 104 | + // Extract coordinates from the response |
| 105 | + const coordinates = extractCoordinates(text); |
| 106 | + if (coordinates.length > 0) { |
| 107 | + displayImageWithBoundingBoxes(fileInput.files[0], coordinates); |
| 108 | + } |
100 | 109 | } |
101 | 110 | } catch (error) { |
102 | 111 | resultDiv.innerHTML = `Error: ${error.message}`; |
|
201 | 210 | reader.readAsDataURL(file); |
202 | 211 | } |
203 | 212 |
|
204 | | - // Attach event listener to the submit button |
| 213 | + function clearImage() { |
| 214 | + document.getElementById('imageInput').value = ''; |
| 215 | + document.getElementById('canvas').getContext('2d').clearRect(0, 0, canvas.width, canvas.height); |
| 216 | + document.getElementById('imagePreview').src = ''; |
| 217 | + document.getElementById('imagePreview').style.display = 'none'; |
| 218 | + } |
| 219 | + |
| 220 | + function previewImage(event) { |
| 221 | + const file = event.target.files[0]; |
| 222 | + if (file) { |
| 223 | + const reader = new FileReader(); |
| 224 | + reader.onload = function(e) { |
| 225 | + const img = document.getElementById('imagePreview'); |
| 226 | + img.src = e.target.result; |
| 227 | + img.style.display = 'block'; |
| 228 | + } |
| 229 | + reader.readAsDataURL(file); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + // Attach event listeners |
205 | 234 | document.getElementById('submitBtn').addEventListener('click', processImageAndPrompt); |
| 235 | + document.getElementById('clearImageBtn').addEventListener('click', clearImage); |
| 236 | + document.getElementById('imageInput').addEventListener('change', previewImage); |
206 | 237 | </script> |
207 | 238 | <style> |
208 | 239 | body { |
|
224 | 255 | max-width: 100%; |
225 | 256 | height: auto; |
226 | 257 | } |
| 258 | + #imagePreview { |
| 259 | + max-width: 100%; |
| 260 | + display: none; |
| 261 | + margin-top: 10px; |
| 262 | + } |
227 | 263 | </style> |
228 | 264 | </head> |
229 | 265 | <body> |
230 | 266 | <h1>Gemini API Image Bounding Box Visualization</h1> |
| 267 | + <select id="modelSelect"> |
| 268 | + <option value="gemini-1.5-pro">gemini-1.5-pro (default)</option> |
| 269 | + <option value="gemini-1.5-pro-exp-0827">gemini-1.5-pro-exp-0827</option> |
| 270 | + <option value="gemini-1.5-flash-exp-0827">gemini-1.5-flash-exp-0827</option> |
| 271 | + <option value="gemini-1.5-flash-8b-exp-0827">gemini-1.5-flash-8b-exp-0827</option> |
| 272 | + </select> |
231 | 273 | <input type="file" id="imageInput" accept="image/*"> |
| 274 | + <button id="clearImageBtn">Clear Image</button> |
232 | 275 | <textarea id="promptInput">Return bounding boxes as JSON arrays [ymin, xmin, ymax, xmax] |
233 | 276 | </textarea> |
234 | 277 | <button id="submitBtn">Process</button> |
235 | 278 | <div id="result"></div> |
236 | 279 | <div id="imageContainer"> |
| 280 | + <img id="imagePreview" alt="Image preview" /> |
237 | 281 | <canvas id="canvas"></canvas> |
238 | 282 | </div> |
239 | 283 | </body> |
|
0 commit comments