Files
d405-capture/d405_ui.html
T
liyang a038da124a 时间戳治理 + 工作距离/健康度指示 + 低分辨率提醒
- 禁用 librealsense global_time_enabled,时间戳改走相机硬件时钟(hardware_clock),
  消除软件对时造成的 ~150ms 回跳(旧录制 424×240@60 第 575 帧实测 −149ms)
- intrinsics.json 逐帧记录彩色/深度双流硬件时间戳、帧号、主机到达时刻,
  以及本段丢帧数与时间戳回跳数;README 补字段表与坑
- /api/status 增加中心距离 dist_cm(中央 1/3 区域深度中位数)与 drops/backjumps
- UI 状态栏:中心距离按 D405 最佳 7–50cm 变色提示;录制健康度实时显示;
  分辨率低于 848×480 时提示指尖/指缝细节风险;首次连接同步服务端流参数
- 实测 424×240@60:600 帧与 3595 帧两段均零丢帧零回跳,间隔恒定 16.697ms,
  彩色-深度时间戳差 <0.33ms

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-15 17:10:15 +08:00

298 lines
11 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>D405 采集台</title>
<style>
:root{
--bg:#101418; --panel:#1a2027; --line:#2a323c; --txt:#dde3ea; --dim:#8a95a3;
--acc:#3d9df0; --rec:#e0453a;
}
*{box-sizing:border-box}
body{margin:0;background:var(--bg);color:var(--txt);
font:14px/1.5 system-ui,"Noto Sans CJK SC",sans-serif}
header{display:flex;align-items:center;gap:14px;padding:10px 18px;
border-bottom:1px solid var(--line)}
header h1{font-size:16px;margin:0;font-weight:600}
#conn{font-size:12px;color:var(--dim)}
#conn.bad{color:var(--rec)}
main{display:flex;gap:14px;padding:14px;align-items:flex-start;flex-wrap:wrap}
#viewwrap{flex:1 1 640px;min-width:320px}
#view{width:100%;display:block;background:#000;border:1px solid var(--line);
border-radius:8px;min-height:200px}
#overlay{display:flex;gap:16px;margin-top:6px;font-size:13px;color:var(--dim);flex-wrap:wrap}
#overlay .ok{color:#4cc38a}
#overlay .warn{color:#e8a33d}
#overlay .bad{color:var(--rec);font-weight:600}
.hint.warn{color:#e8a33d}
aside{width:300px;flex:0 0 300px;display:flex;flex-direction:column;gap:12px}
.card{background:var(--panel);border:1px solid var(--line);border-radius:10px;
padding:12px 14px}
.card h2{margin:0 0 10px;font-size:13px;color:var(--dim);font-weight:600;
text-transform:none;letter-spacing:.5px}
.row{display:flex;align-items:center;justify-content:space-between;
gap:10px;margin:8px 0}
.row label{color:var(--txt)}
select,input[type=number]{background:#12171d;color:var(--txt);
border:1px solid var(--line);border-radius:6px;padding:5px 8px;width:130px}
input[type=range]{width:130px;accent-color:var(--acc)}
input[type=checkbox]{width:16px;height:16px;accent-color:var(--acc)}
.val{color:var(--dim);font-size:12px;min-width:38px;text-align:right}
button{border:0;border-radius:8px;padding:10px 0;width:100%;font-size:16px;
font-weight:600;cursor:pointer;transition:background .15s}
#recbtn{background:var(--acc);color:#fff}
#recbtn.rec{background:var(--rec)}
#recbtn:disabled{background:#333;color:#777;cursor:not-allowed}
#applybtn{background:#2a323c;color:var(--txt);font-size:14px;padding:8px 0}
#applybtn.dirty{background:#c98a1b;color:#fff}
#recinfo{margin-top:8px;font-size:13px;color:var(--dim);min-height:20px}
#recinfo b{color:var(--txt)}
#saved{font-size:12px;color:var(--dim);margin-top:4px;word-break:break-all}
#msg{font-size:12px;color:var(--rec);min-height:16px;margin-top:6px}
.hint{font-size:11px;color:var(--dim);margin-top:2px}
</style>
</head>
<body>
<header>
<h1>📷 D405 采集台</h1>
<span id="conn">连接中…</span>
</header>
<main>
<div id="viewwrap">
<img id="view" alt="live">
<div id="overlay">
<span id="ov_mode">—</span><span id="ov_fps">—</span><span id="ov_depth">—</span>
<span id="ov_dist">—</span><span id="ov_health"></span>
</div>
</div>
<aside>
<div class="card">
<h2>录制</h2>
<button id="recbtn" onclick="toggleRec()">● 开始录制</button>
<div class="row">
<label>录制时长(秒)</label>
<input type="number" id="duration" min="0" max="86400" step="1" value="0"
style="width:80px">
</div>
<div class="hint">0 = 手动停止;填 N 秒到时自动停止保存</div>
<div class="row">
<label>保存位置</label>
<input type="text" id="out_dir" style="width:170px"
placeholder="默认 hardware/captures">
</div>
<div class="hint">目录不存在会自动创建,~ 可用;每段录制在其下建时间戳子目录</div>
<div class="row">
<label>保存深度</label>
<input type="checkbox" id="save_depth" checked>
</div>
<div id="recinfo"></div>
<div id="saved"></div>
</div>
<div class="card">
<h2>视频流(改动需应用,会重启流)</h2>
<div class="row">
<label>分辨率</label>
<select id="res">
<option value="1280x720">1280×720</option>
<option value="848x480" selected>848×480</option>
<option value="640x360">640×360</option>
<option value="424x240">424×240</option>
</select>
</div>
<div class="row">
<label>帧率</label>
<select id="fps">
<option>5</option><option>15</option><option selected>30</option>
<option>60</option><option>90</option>
</select>
</div>
<div class="hint">1280×720 最高 30fps;60/90fps 需 848×480 及以下</div>
<div class="hint" id="res_hint"></div>
<button id="applybtn" onclick="applyStream()">应用视频流设置</button>
</div>
<div class="card">
<h2>预览与深度显示(即时生效)</h2>
<div class="row">
<label>预览画质</label>
<input type="range" id="quality" min="30" max="95" step="5" value="80">
<span class="val" id="quality_v">80</span>
</div>
<div class="row">
<label>深度范围</label>
<select id="depth_max">
<option value="0.5">0 ~ 0.5 m</option>
<option value="1" selected>0 ~ 1 m</option>
<option value="2">0 ~ 2 m</option>
<option value="4">0 ~ 4 m</option>
</select>
</div>
<div class="row">
<label>预览布局</label>
<select id="layout">
<option value="side" selected>彩色 | 深度</option>
<option value="color">仅彩色</option>
<option value="depth">仅深度</option>
</select>
</div>
</div>
<div class="card">
<h2>传感器(即时生效)</h2>
<div class="row">
<label>自动曝光</label>
<input type="checkbox" id="auto_exp" checked>
</div>
<div class="row">
<label>手动曝光 µs</label>
<input type="range" id="exposure" min="100" max="30000" step="100" value="8000" disabled>
<span class="val" id="exposure_v">8000</span>
</div>
<div class="row">
<label>增益</label>
<input type="range" id="gain" min="16" max="248" step="8" value="64" disabled>
<span class="val" id="gain_v">64</span>
</div>
</div>
<div id="msg"></div>
</aside>
</main>
<script>
const $ = id => document.getElementById(id);
let st = {on:false}, streamDirty = false, synced = false;
// ---- 自愈式拉帧预览:/frame.jpg 服务端等新帧才返回,断了自动重连 ----
const sleep = ms => new Promise(r=>setTimeout(r,ms));
let lastUrl = null;
(async function frameLoop(){
for(;;){
try{
const r = await fetch('/frame.jpg', {cache:'no-store'});
if(!r.ok) throw 0;
const url = URL.createObjectURL(await r.blob());
$('view').src = url;
if(lastUrl) URL.revokeObjectURL(lastUrl);
lastUrl = url;
}catch(e){
await sleep(700); // 服务端重启视频流期间在此重试
}
}
})();
async function api(path, body){
const r = await fetch(path, body===undefined ? {} :
{method:'POST', headers:{'Content-Type':'application/json'},
body:JSON.stringify(body)});
return r.json();
}
// ---- 即时生效项 ----
function pushLive(){
api('/api/config', {
jpeg_quality:+$('quality').value,
depth_max:+$('depth_max').value,
layout:$('layout').value,
auto_exposure:$('auto_exp').checked,
exposure:+$('exposure').value,
gain:+$('gain').value,
}).catch(()=>{});
}
['depth_max','layout'].forEach(id=>$(id).onchange=pushLive);
$('quality').oninput=()=>{$('quality_v').textContent=$('quality').value};
$('quality').onchange=pushLive;
$('exposure').oninput=()=>{$('exposure_v').textContent=$('exposure').value};
$('exposure').onchange=pushLive;
$('gain').oninput=()=>{$('gain_v').textContent=$('gain').value};
$('gain').onchange=pushLive;
$('auto_exp').onchange=()=>{
const manual=!$('auto_exp').checked;
$('exposure').disabled=!manual; $('gain').disabled=!manual;
pushLive();
};
// ---- 视频流设置(需重启)----
// 低分辨率提醒:手腕/手掌定位够用,指尖、指缝、遮挡边界需要更高分辨率
function resHint(){
const w=+$('res').value.split('x')[0];
const h=$('res_hint');
h.textContent = w<848 ? '⚠ 低于 848×480:手腕/手掌整体定位可用,指尖、指缝和遮挡边界易出错' : '';
h.className = w<848 ? 'hint warn' : 'hint';
}
$('res').onchange=$('fps').onchange=()=>{
resHint();
streamDirty=true;
$('applybtn').classList.add('dirty');
$('applybtn').textContent='应用视频流设置(未生效)';
};
async function applyStream(){
const [w,h]=$('res').value.split('x').map(Number);
$('applybtn').textContent='重启视频流中…';
const r=await api('/api/config',{width:w,height:h,fps:+$('fps').value});
streamDirty=false;
$('applybtn').classList.remove('dirty');
$('applybtn').textContent='应用视频流设置';
$('msg').textContent=r.error||'';
}
// ---- 录制 ----
async function toggleRec(){
if(st.on){ await api('/api/stop',{}); }
else{
const r=await api('/api/start',{
duration:+$('duration').value||0,
out_dir:$('out_dir').value.trim(),
save_depth:$('save_depth').checked});
$('msg').textContent=r.error||'';
}
poll();
}
// ---- 状态轮询 ----
async function poll(){
try{
st=await api('/api/status');
$('conn').textContent='已连接 · '+st.device;
$('conn').className='';
}catch(e){
$('conn').textContent='连接断开';
$('conn').className='bad';
return;
}
const b=$('recbtn');
b.disabled=!st.stream_ok;
if(st.on){
b.textContent='■ 停止并保存'; b.classList.add('rec');
let t=`录制中:<b>${st.frames}</b> 帧 · ${st.elapsed.toFixed(0)}s`;
if(st.duration>0) t+=` / ${st.duration}s(自动停止)`;
$('recinfo').innerHTML=t;
}else{
b.textContent='● 开始录制'; b.classList.remove('rec');
$('recinfo').innerHTML='';
}
$('saved').textContent=st.last_saved?('上次保存:'+st.last_saved):'';
$('ov_mode').textContent=`${st.width}×${st.height}@${st.fps}`;
$('ov_fps').textContent=`实际 ${st.fps_actual.toFixed(1)} fps`;
$('ov_depth').textContent=`深度有效 ${st.depth_fill.toFixed(0)}%`;
if(!synced && !streamDirty){ // 首次连上:下拉框跟服务端实际配置对齐
$('res').value=`${st.width}x${st.height}`; $('fps').value=String(st.fps);
resHint(); synced=true;
}
// 工作距离:画面中心 1/3 区域深度中位数;D405 官方最佳 7–50cm
const od=$('ov_dist'), d=st.dist_cm||0;
if(!d){ od.textContent='中心距离 —'; od.className=''; }
else if(d<7){ od.textContent=`中心距离 ${d.toFixed(0)}cm ⚠ 太近(<7cm)`; od.className='bad'; }
else if(d>50){ od.textContent=`中心距离 ${d.toFixed(0)}cm ⚠ 太远(>50cm)`; od.className='bad'; }
else { od.textContent=`中心距离 ${d.toFixed(0)}cm ✓ 7–50cm`; od.className='ok'; }
// 时间戳健康度:录制中实时;停止后保留上一段的统计
const oh=$('ov_health');
if(st.on || st.frames>0){
const bad=(st.drops>0)||(st.backjumps>0);
oh.textContent=`${st.on?'':'上段 '}丢帧 ${st.drops} · 时间戳回跳 ${st.backjumps}`+(bad?' ⚠':' ✓');
oh.className=bad?'bad':'ok';
}else{ oh.textContent=''; oh.className=''; }
if(st.error) $('msg').textContent=st.error;
}
setInterval(poll,1000); poll();
</script>
</body>
</html>