Spaces:
Running
Running
Download app.js from polyutaslab/UrbanV2X-Viewer: direct link, hf CLI and curl.
- Browser
- Download file 5.84 kB
-
https://huggingface.co/spaces/polyutaslab/UrbanV2X-Viewer/resolve/main/app.js
- Command line
-
hf download hf://spaces/polyutaslab/UrbanV2X-Viewer/app.js
-
curl -L -o app.js https://huggingface.co/spaces/polyutaslab/UrbanV2X-Viewer/resolve/main/app.js
5.84 kB
| ; | |
| const $=id=>document.getElementById(id); | |
| const cameras=[{id:2,label:'Front · left'},{id:1,label:'Front · right'},{id:3,label:'Left · front'},{id:4,label:'Right · front'},{id:5,label:'Left · rear'},{id:0,label:'Right · rear'},{id:6,label:'Rear'}]; | |
| let manifest,sequence,frames=[],index=0,displayed=-1,displayedSequence=null,ticket=0,playing=false,timer=null; | |
| for(const c of cameras){const card=document.createElement('figure');card.className='card c'+c.id;card.innerHTML=`<button type="button" aria-label="Enlarge camera ${c.id}, ${c.label}" disabled><img id="cam${c.id}" alt="Camera ${c.id}: ${c.label}"></button><figcaption><span>${c.label}</span><span class="badge">CAM ${c.id}</span></figcaption>`;card.querySelector('button').onclick=()=>enlarge(c.id);$('board').appendChild(card);} | |
| function stop(){playing=false;clearTimeout(timer);$('play').textContent='▶ Play';} | |
| function asset(seq,row,id){return `previews/${seq}/${row.timestamp_us}_cam${id}.jpg`;} | |
| function loadImage(url){return new Promise((resolve,reject)=>{const im=new Image();im.onload=()=>resolve(im);im.onerror=()=>reject(new Error('Preview image could not be loaded.'));im.src=url;});} | |
| async function show(n){const run=++ticket;window.dispatchEvent(new Event('viewer-loading'));index=Math.max(0,Math.min(n,frames.length-1));const row=frames[index],seq=sequence,idx=index;$('slider').value=idx;$('status').textContent='Loading time-matched views…';$('board').classList.add('loading');try{const images=await Promise.all(cameras.map(c=>loadImage(asset(seq,row,c.id))));if(run!==ticket)return;images.forEach((im,j)=>{$('cam'+cameras[j].id).src=im.src;});displayed=idx;displayedSequence=seq;window.viewerFrame={seq,row,index:idx};if(window.loadCloudFrame)await window.loadCloudFrame(window.viewerFrame);else window.dispatchEvent(new CustomEvent('viewer-frame',{detail:window.viewerFrame}));if(run!==ticket)return;document.querySelectorAll('.card button').forEach(b=>b.disabled=false);$('frame').textContent=`Sample ${idx+1} / ${frames.length} · +${((row.timestamp_us-frames[0].timestamp_us)/1e6).toFixed(1)} s`;$('stamp').textContent=new Date(row.timestamp_us/1000).toISOString().replace('T',' ').replace('Z',' UTC')+' · '+row.timestamp_us+' µs';$('status').textContent='Time-matched previews · Camera 2 reference';$('board').classList.remove('loading');if(playing){if(idx===frames.length-1)stop();else timer=setTimeout(()=>show(idx+1),800);}}catch(err){if(run!==ticket)return;stop();$('board').classList.remove('loading');$('status').textContent='Unable to load this sample. Choose another frame or reload to retry.';}} | |
| function enlarge(id){if(displayed<0||!displayedSequence)return;stop();const row=manifest.sequences[displayedSequence].frames[displayed];$('detailTitle').textContent=`${displayedSequence} · CAM ${id} · ${cameras.find(c=>c.id===id).label}`;$('detailImg').src=asset(displayedSequence,row,id);$('original').href=`https://huggingface.co/datasets/polyutaslab/UrbanV2X/resolve/main/${displayedSequence}/camera/cam${id}/${row.source_file}`;$('detail').showModal();} | |
| $('close').onclick=()=>$('detail').close();$('detail').onclick=e=>{if(e.target===$('detail'))$('detail').close();}; | |
| document.querySelectorAll('.sensor').forEach(el=>{el.onclick=()=>enlarge(Number(el.dataset.cam));el.onkeydown=e=>{if(e.key==='Enter'||e.key===' '){e.preventDefault();enlarge(Number(el.dataset.cam));}};}); | |
| $('sequence').onchange=()=>{stop();sequence=$('sequence').value;frames=manifest.sequences[sequence].frames;displayed=-1;displayedSequence=null;document.querySelectorAll('.card button').forEach(b=>{b.disabled=true;b.querySelector('img').removeAttribute('src');});$('slider').max=frames.length-1;show(manifest.sequences[sequence].default_sample||0);}; | |
| $('prev').onclick=()=>{stop();show(index-1);};$('next').onclick=()=>{stop();show(index+1);};$('slider').oninput=()=>{stop();show(Number($('slider').value));};$('play').onclick=()=>{if(playing){stop();return;}playing=true;$('play').textContent='Ⅱ Pause';show(index===frames.length-1?0:index);}; | |
| document.addEventListener('keydown',e=>{if(['INPUT','SELECT','BUTTON'].includes(document.activeElement.tagName)||$('detail').open||!frames.length)return;if(e.key==='ArrowLeft'){stop();show(index-1);}if(e.key==='ArrowRight'){stop();show(index+1);}}); | |
| fetch('manifest.json').then(r=>{if(!r.ok)throw new Error('Index unavailable');return r.json();}).then(data=>{manifest=data;for(const name of Object.keys(data.sequences)){const opt=document.createElement('option');opt.value=name;opt.textContent=name;$('sequence').appendChild(opt);}document.querySelectorAll('.controlrow button, .controlrow input, #sequence').forEach(el=>el.disabled=false);const requested=new URLSearchParams(location.search).get('sequence');$('sequence').value=data.sequences[requested]?requested:(data.default_sequence||Object.keys(data.sequences)[0]);$('sequence').dispatchEvent(new Event('change'));buildSceneCards(data);}).catch(()=>{$('status').textContent='Preview index unavailable. Please reload in a moment.';}); | |
| function buildSceneCards(data){const host=document.getElementById('scene-cards');for(const name of ['hkstp_s_02','hkstp_s_01','hkstp_l_01']){const seq=data.sequences[name];if(!seq)continue;const row=seq.frames[seq.default_sample||0];const button=document.createElement('button');button.className='scene-card';button.innerHTML=`<img src="${asset(name,row,2)}" alt="Front camera preview of ${name}" loading="lazy"><div><strong>${name}</strong><span>${name==='hkstp_s_02'?'3 onboard LiDARs + rsi_1 + rsi_2 · Common ENU':'Seven cameras + native LiDAR views'}</span><b>Explore scene →</b></div>`;button.onclick=()=>{$('sequence').value=name;$('sequence').dispatchEvent(new Event('change'));document.querySelector('[data-view=lidar]').click();$('explorer').scrollIntoView({behavior:'smooth'});};host.append(button);}} | |