Spaces:
Running
Running
File size: 5,694 Bytes
99aa0fa |
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 |
// Configuration
const HF_TOKEN = "YOUR_HUGGINGFACE_TOKEN"; // Get from https://huggingface.co/settings/tokens
const BACKEND_URL = "http://localhost:5000"; // Your Flask backend
// DOM Elements
let cameraStream = null;
let capturedImage = null;
// Initialize Hugging Face connection
async function checkHFConnection() {
try {
const response = await fetch(`${BACKEND_URL}/health`);
const data = await response.json();
document.getElementById('hfStatus').textContent = data.hf_status;
document.getElementById('hfStatus').style.color = data.hf_status === 'Available' ? 'green' : 'red';
} catch (error) {
document.getElementById('hfStatus').textContent = 'Unavailable';
document.getElementById('hfStatus').style.color = 'red';
}
}
// Camera Functions
async function startCamera() {
try {
cameraStream = await navigator.mediaDevices.getUserMedia({
video: {
facingMode: 'environment',
width: { ideal: 1280 },
height: { ideal: 720 }
}
});
const video = document.getElementById('cameraFeed');
video.srcObject = cameraStream;
document.getElementById('videoContainer').style.display = 'block';
// Hide upload option when camera is active
document.querySelector('input[type="file"]').style.display = 'none';
} catch (error) {
alert(`Camera Error: ${error.message}`);
}
}
function stopCamera() {
if (cameraStream) {
cameraStream.getTracks().forEach(track => track.stop());
cameraStream = null;
document.getElementById('videoContainer').style.display = 'none';
document.querySelector('input[type="file"]').style.display = 'inline-block';
}
}
function captureImage() {
const video = document.getElementById('cameraFeed');
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
canvas.toBlob(blob => {
capturedImage = blob;
displayCapturedImage(URL.createObjectURL(blob));
}, 'image/jpeg', 0.9);
}
// Handle file upload
document.getElementById('fileUpload').addEventListener('change', function(e) {
if (e.target.files[0]) {
capturedImage = e.target.files[0];
displayCapturedImage(URL.createObjectURL(capturedImage));
}
});
function displayCapturedImage(url) {
document.getElementById('previewImage').src = url;
document.getElementById('imagePreview').style.display = 'block';
stopCamera(); // Stop camera when image is captured/uploaded
}
// Main function to generate description
async function generateDescription() {
if (!capturedImage) {
alert('Please capture or upload an image first.');
return;
}
// Show results section
document.getElementById('results').style.display = 'block';
// Get user selections
const language = document.getElementById('language').value;
const vlmModel = document.getElementById('vlmModel').value;
const llamaModel = document.getElementById('llamaModel').value;
// Create FormData for the image and parameters
const formData = new FormData();
formData.append('image', capturedImage, 'captured.jpg');
formData.append('language', language);
formData.append('vlm_model', vlmModel);
formData.append('llama_model', llamaModel);
try {
// Send to backend
const response = await fetch(`${BACKEND_URL}/analyze`, {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const results = await response.json();
// Display results
document.getElementById('basicDescription').textContent = results.basic_description || 'No description generated';
document.getElementById('detailedAnalysis').textContent = results.detailed_analysis || 'No detailed analysis';
document.getElementById('translation').textContent = results.translation || 'No translation';
document.getElementById('jsonOutput').textContent = JSON.stringify(results, null, 2);
} catch (error) {
console.error('Error:', error);
document.getElementById('basicDescription').textContent = `Error: ${error.message}`;
document.getElementById('results').style.display = 'block';
}
}
// Enhanced object detection using region-based analysis[citation:10]
async function getDetailedObjectAnalysis(imageBlob, llamaModel) {
// This is a simplified approach - in production, you'd want to:
// 1. Use an object detector (like YOLO) to get bounding boxes[citation:10]
// 2. Crop image regions
// 3. Use BLIP/Llama to describe each region
// 4. Synthesize into comprehensive description
const formData = new FormData();
formData.append('image', imageBlob);
formData.append('llama_model', llamaModel);
try {
const response = await fetch(`${BACKEND_URL}/detailed-analysis`, {
method: 'POST',
body: formData
});
return await response.json();
} catch (error) {
return { error: error.message };
}
}
// Initialize
window.onload = function() {
checkHFConnection();
setInterval(checkHFConnection, 30000); // Check every 30 seconds
}; |