File size: 10,259 Bytes
aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 dd1b723 aefc326 |
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 321 322 323 324 325 |
import {
AutoProcessor,
AutoModelForImageTextToText,
TextStreamer,
} from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected]';
let processor = null;
let model = null;
let videoFile = null;
let frames = [];
let captions = [];
// DOM Elements
const uploadArea = document.getElementById('uploadArea');
const videoInput = document.getElementById('videoInput');
const videoSection = document.getElementById('videoSection');
const videoPlayer = document.getElementById('videoPlayer');
const frameCanvas = document.getElementById('frameCanvas');
const processBtn = document.getElementById('processBtn');
const progressSection = document.getElementById('progressSection');
const progressFill = document.getElementById('progressFill');
const progressText = document.getElementById('progressText');
const resultsSection = document.getElementById('resultsSection');
const framesList = document.getElementById('framesList');
const deviceSelect = document.getElementById('deviceSelect');
// Check WebGPU support
async function checkWebGPU() {
if (!navigator.gpu) {
deviceSelect.querySelector('option[value="webgpu"]').disabled = true;
deviceSelect.value = 'wasm';
}
}
// Initialize model
async function initializeModel() {
try {
progressText.textContent = 'Loading processor...';
progressFill.style.width = '30%';
const model_id = 'onnx-community/FastVLM-0.5B-ONNX';
processor = await AutoProcessor.from_pretrained(model_id);
progressText.textContent = 'Loading model (this may take a moment)...';
progressFill.style.width = '60%';
const device = deviceSelect.value === 'webgpu' ? 'webgpu' : 'wasm';
model = await AutoModelForImageTextToText.from_pretrained(model_id, {
device: device,
dtype: {
embed_tokens: 'fp16',
vision_encoder: 'q4',
decoder_model_merged: 'q4',
},
});
progressFill.style.width = '100%';
progressText.textContent = 'Model loaded successfully!';
return true;
} catch (error) {
console.error('Error initializing model:', error);
progressText.textContent = 'Error loading model. Please refresh and try again.';
return false;
}
}
// Upload handling
uploadArea.addEventListener('click', () => videoInput.click());
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
const files = e.dataTransfer.files;
if (files.length > 0 && files[0].type.startsWith('video/')) {
handleVideoFile(files[0]);
}
});
videoInput.addEventListener('change', (e) => {
if (e.target.files.length > 0) {
handleVideoFile(e.target.files[0]);
}
});
function handleVideoFile(file) {
if (file.size > 100 * 1024 * 1024) {
alert('File size exceeds 100MB limit');
return;
}
videoFile = file;
const url = URL.createObjectURL(file);
videoPlayer.src = url;
videoSection.classList.remove('hidden');
resultsSection.classList.add('hidden');
frames = [];
captions = [];
}
// Extract frames from video
async function extractFrames() {
const interval = parseInt(document.getElementById('frameInterval').value);
const ctx = frameCanvas.getContext('2d');
const duration = videoPlayer.duration;
frames = [];
for (let time = 0; time < duration; time += interval) {
videoPlayer.currentTime = time;
await new Promise(resolve => {
videoPlayer.onseeked = resolve;
});
frameCanvas.width = videoPlayer.videoWidth;
frameCanvas.height = videoPlayer.videoHeight;
ctx.drawImage(videoPlayer, 0, 0);
const blob = await new Promise(resolve => {
frameCanvas.toBlob(resolve, 'image/jpeg', 0.9);
});
frames.push({
time: time,
blob: blob,
dataUrl: await blobToDataUrl(blob)
});
}
return frames;
}
function blobToDataUrl(blob) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
}
// Generate caption for a frame
async function generateCaption(imageDataUrl, frameIndex, totalFrames) {
try {
progressText.textContent = `Processing frame ${frameIndex + 1} of ${totalFrames}...`;
progressFill.style.width = `${((frameIndex + 1) / totalFrames) * 100}%`;
const messages = [
{
role: 'user',
content: '<image>Describe this video frame in detail. What is happening in this scene?',
},
];
const prompt = processor.apply_chat_template(messages, {
add_generation_prompt: true,
});
// Create image element from data URL
const img = new Image();
img.src = imageDataUrl;
await new Promise(resolve => img.onload = resolve);
const inputs = await processor(img, prompt, {
add_special_tokens: false,
});
let streamedText = '';
const outputs = await model.generate({
...inputs,
max_new_tokens: 256,
do_sample: false,
streamer: new TextStreamer(processor.tokenizer, {
skip_prompt: true,
skip_special_tokens: false,
callback_function: (text) => {
streamedText += text;
},
}),
});
const decoded = processor.batch_decode(
outputs.slice(null, [inputs.input_ids.dims.at(-1), null]),
{ skip_special_tokens: true }
);
return decoded[0];
} catch (error) {
console.error('Error generating caption:', error);
return 'Error generating caption for this frame';
}
}
// Process video
processBtn.addEventListener('click', async () => {
if (!videoFile) return;
processBtn.disabled = true;
progressSection.classList.remove('hidden');
resultsSection.classList.add('hidden');
try {
// Initialize model if not already loaded
if (!model || !processor) {
const success = await initializeModel();
if (!success) {
processBtn.disabled = false;
return;
}
}
// Extract frames
progressText.textContent = 'Extracting frames...';
progressFill.style.width = '20%';
frames = await extractFrames();
// Generate captions
captions = [];
for (let i = 0; i < frames.length; i++) {
const caption = await generateCaption(frames[i].dataUrl, i, frames.length);
captions.push({
time: frames[i].time,
caption: caption,
thumbnail: frames[i].dataUrl
});
// Update results in real-time
displayResults();
resultsSection.classList.remove('hidden');
}
progressText.textContent = 'Processing complete!';
setTimeout(() => {
progressSection.classList.add('hidden');
}, 2000);
} catch (error) {
console.error('Processing error:', error);
progressText.textContent = 'Error processing video. Please try again.';
}
processBtn.disabled = false;
});
// Display results
function displayResults() {
framesList.innerHTML = '';
captions.forEach((item, index) => {
const frameCard = document.createElement('div');
frameCard.className = 'frame-card';
const time = formatTime(item.time);
frameCard.innerHTML = `
<div class="frame-thumbnail">
<img src="${item.thumbnail}" alt="Frame at ${time}">
<span class="frame-time">${time}</span>
</div>
<div class="frame-caption">
<p>${item.caption}</p>
</div>
`;
framesList.appendChild(frameCard);
});
}
function formatTime(seconds) {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
// Export functions
document.getElementById('exportJson').addEventListener('click', () => {
const data = JSON.stringify(captions, null, 2);
downloadFile(data, 'captions.json', 'application/json');
});
document.getElementById('exportSrt').addEventListener('click', () => {
let srt = '';
captions.forEach((item, index) => {
const startTime = formatSrtTime(item.time);
const endTime = formatSrtTime(item.time + 5);
srt += `${index + 1}\n${startTime} --> ${endTime}\n${item.caption}\n\n`;
});
downloadFile(srt, 'captions.srt', 'text/plain');
});
document.getElementById('exportTxt').addEventListener('click', () => {
let txt = '';
captions.forEach(item => {
txt += `[${formatTime(item.time)}] ${item.caption}\n\n`;
});
downloadFile(txt, 'captions.txt', 'text/plain');
});
function formatSrtTime(seconds) {
const hours = Math.floor(seconds / 3600);
const mins = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
const ms = Math.floor((seconds % 1) * 1000);
return `${hours.toString().padStart(2, '0')}:${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')},${ms.toString().padStart(3, '0')}`;
}
function downloadFile(content, filename, type) {
const blob = new Blob([content], { type });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
// Initialize
checkWebGPU(); |