feat(ocr): 新增本地 OCR sidecar
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>lw.PPOCR.C HTTP OCR Demo</title>
|
||||
<style>
|
||||
*{box-sizing:border-box}body{margin:0;background:#f3f6fa;color:#172033;font-family:"Microsoft YaHei",Arial,sans-serif}
|
||||
header{display:flex;justify-content:space-between;align-items:center;gap:16px;padding:18px 24px;background:#fff;border-bottom:1px solid #dbe3ee}
|
||||
h1{font-size:21px;margin:0}.toolbar{display:flex;align-items:center;gap:10px;flex-wrap:wrap}
|
||||
input{max-width:340px;padding:8px;border:1px solid #b9c6d8;border-radius:7px;background:#fff}
|
||||
button{height:38px;padding:0 18px;border:0;border-radius:7px;background:#087f5b;color:#fff;font-weight:700;cursor:pointer}
|
||||
button.secondary{background:#fff;color:#475467;border:1px solid #cbd5e1}
|
||||
button.secondary:hover:not(:disabled){background:#f8fafc}
|
||||
button:disabled{background:#9aa9ba;cursor:not-allowed}
|
||||
button.secondary:disabled{background:#f1f5f9;color:#98a2b3;border-color:#dbe3ee}
|
||||
main{display:grid;grid-template-columns:minmax(0,1fr) 390px;gap:18px;max-width:1320px;margin:auto;padding:20px}
|
||||
.panel{background:#fff;border:1px solid #dbe3ee;border-radius:10px;padding:13px;min-height:560px;box-shadow:0 3px 18px rgba(30,50,80,.05)}
|
||||
.canvas-wrap{position:relative;display:inline-block;max-width:100%;line-height:0}#preview{display:block;max-width:100%;height:auto}#overlay{position:absolute;inset:0;pointer-events:none}
|
||||
.meta{display:grid;grid-template-columns:72px 1fr;gap:8px;font-size:14px;margin-bottom:14px}.label{color:#64748b}
|
||||
#texts{margin:0;padding-left:26px;max-height:260px;overflow:auto;line-height:1.6}pre{white-space:pre-wrap;word-break:break-all;max-height:255px;overflow:auto;background:#f5f7fb;border-radius:7px;padding:10px;font-size:12px}
|
||||
footer{padding:0 20px 20px;text-align:center;color:#667085;font-size:13px}a{color:#087f5b}
|
||||
@media(max-width:900px){header{align-items:flex-start;flex-direction:column}main{grid-template-columns:1fr}.panel{min-height:360px}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header><h1>lw.PPOCR.C · HTTP OCR Demo</h1><div class="toolbar"><input id="file" type="file" accept="image/*"><button id="run" disabled>开始识别</button><button id="clear" class="secondary" type="button" disabled>清空</button></div></header>
|
||||
<main>
|
||||
<section class="panel"><div class="canvas-wrap"><img id="preview" alt="图片预览"><canvas id="overlay"></canvas></div></section>
|
||||
<aside class="panel"><div class="meta"><div class="label">状态</div><div id="status">请选择图片</div><div class="label">耗时</div><div id="elapsed">-</div><div class="label">结果数</div><div id="count">-</div><div class="label">请求ID</div><div id="request">-</div></div><ol id="texts"></ol><pre id="json"></pre></aside>
|
||||
</main>
|
||||
<footer>原生 C++ / cpp-httplib 跨平台示例 · <a href="https://github.com/lxw112190/lw.PPOCR.C" target="_blank" rel="noopener">github.com/lxw112190/lw.PPOCR.C</a></footer>
|
||||
<script>
|
||||
const file=document.getElementById('file'),run=document.getElementById('run'),clearBtn=document.getElementById('clear'),preview=document.getElementById('preview'),canvas=document.getElementById('overlay');
|
||||
const statusEl=document.getElementById('status'),elapsed=document.getElementById('elapsed'),count=document.getElementById('count'),requestEl=document.getElementById('request'),texts=document.getElementById('texts'),json=document.getElementById('json');
|
||||
let selected=null,last=[],previewUrl='',busy=false;
|
||||
function releasePreviewUrl(){if(previewUrl){URL.revokeObjectURL(previewUrl);previewUrl=''}}
|
||||
function clearRecognitionOutput(){last=[];clearOverlay();texts.innerHTML='';json.textContent='';elapsed.textContent=count.textContent=requestEl.textContent='-'}
|
||||
function updateControls(){file.disabled=busy;run.disabled=busy||!selected;clearBtn.disabled=busy||!selected}
|
||||
function resetDemo(){selected=null;file.value='';releasePreviewUrl();preview.removeAttribute('src');clearRecognitionOutput();statusEl.textContent='请选择图片';canvas.width=0;canvas.height=0;canvas.style.width='0px';canvas.style.height='0px';updateControls()}
|
||||
file.addEventListener('change',()=>{clearRecognitionOutput();releasePreviewUrl();selected=file.files[0]||null;if(!selected){preview.removeAttribute('src');statusEl.textContent='请选择图片';updateControls();return}previewUrl=URL.createObjectURL(selected);preview.src=previewUrl;statusEl.textContent='图片已加载';updateControls()});
|
||||
clearBtn.addEventListener('click',resetDemo);
|
||||
preview.addEventListener('load',()=>{resizeOverlay();draw(last)});window.addEventListener('resize',()=>{resizeOverlay();draw(last)});
|
||||
run.addEventListener('click',async()=>{if(!selected||!preview.naturalWidth||busy)return;busy=true;updateControls();statusEl.textContent='正在转换并识别...';clearOverlay();try{const started=performance.now();const ppm=imageToPpm();const response=await fetch('/api/ocr',{method:'POST',headers:{'Content-Type':'image/x-portable-pixmap'},body:ppm});const data=await response.json();if(!response.ok||!data.ok)throw new Error(data.error||`HTTP ${response.status}`);last=data.result||[];draw(last);statusEl.textContent='识别成功';elapsed.textContent=`${Number(data.timing_ms&&data.timing_ms.server_total||performance.now()-started).toFixed(1)} ms`;count.textContent=last.length;requestEl.textContent=data.request_id||'-';texts.innerHTML='';last.forEach(item=>{const li=document.createElement('li');li.textContent=`${item.text} [${Number(item.score).toFixed(3)}]`;texts.appendChild(li)});json.textContent=JSON.stringify(data,null,2)}catch(error){statusEl.textContent=error.message}finally{busy=false;updateControls()}});
|
||||
function imageToPpm(){const width=preview.naturalWidth,height=preview.naturalHeight,pixels=width*height;if(pixels>40000000)throw new Error('图片超过 4000 万像素');const source=document.createElement('canvas');source.width=width;source.height=height;const context=source.getContext('2d',{willReadFrequently:true});context.drawImage(preview,0,0);const rgba=context.getImageData(0,0,width,height).data;const header=`P6\n${width} ${height}\n255\n`,result=new Uint8Array(header.length+pixels*3);for(let i=0;i<header.length;i++)result[i]=header.charCodeAt(i);for(let sourceIndex=0,targetIndex=header.length;sourceIndex<rgba.length;sourceIndex+=4){result[targetIndex++]=rgba[sourceIndex];result[targetIndex++]=rgba[sourceIndex+1];result[targetIndex++]=rgba[sourceIndex+2]}return result}
|
||||
function resizeOverlay(){canvas.width=preview.clientWidth;canvas.height=preview.clientHeight;canvas.style.width=`${preview.clientWidth}px`;canvas.style.height=`${preview.clientHeight}px`}
|
||||
function clearOverlay(){resizeOverlay();canvas.getContext('2d').clearRect(0,0,canvas.width,canvas.height)}
|
||||
function draw(results){clearOverlay();if(!preview.naturalWidth||!preview.naturalHeight)return;const sx=canvas.width/preview.naturalWidth,sy=canvas.height/preview.naturalHeight,ctx=canvas.getContext('2d');ctx.lineWidth=2;ctx.strokeStyle='#e03131';ctx.fillStyle='#e03131';ctx.font='bold 13px Microsoft YaHei,Arial';results.forEach((r,i)=>{const p=[[r.x1*sx,r.y1*sy],[r.x2*sx,r.y2*sy],[r.x3*sx,r.y3*sy],[r.x4*sx,r.y4*sy]];ctx.beginPath();ctx.moveTo(p[0][0],p[0][1]);p.slice(1).forEach(v=>ctx.lineTo(v[0],v[1]));ctx.closePath();ctx.stroke();ctx.fillText(String(i+1),Math.max(1,Math.min(...p.map(v=>v[0]))),Math.max(13,Math.min(...p.map(v=>v[1]))-3))})}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user