Compare commits
6
Commits
61245b1c65
...
fbe78d362f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbe78d362f | ||
|
|
fe1c6d106f | ||
|
|
a86b748a6f | ||
|
|
d3128ba21c | ||
|
|
19564fb65b | ||
|
|
deb622095f |
+1
-1
@@ -264,7 +264,7 @@ auth.router ← /login /logout /api/login /api/login_restore /api/passwor
|
||||
auth.middleware ← 其余请求需登录(cookie 优先,Basic 兜底)
|
||||
preview-proxy.earlyGuard
|
||||
express.static(public)
|
||||
/api/sessions ×3 ← sessions / messages / files
|
||||
/api/sessions ×4 ← sessions / messages / files / skill-run(通用执行面)
|
||||
/api/skills
|
||||
/api/config ← requireAdmin
|
||||
/api/platforms
|
||||
|
||||
@@ -852,20 +852,57 @@ footer { position: relative; flex: none; padding: 8px 12px calc(10px + env(safe-
|
||||
/* ================= 全屏查看器(搬自 index;图片单点放大+缩放,消息双击放大) ================= */
|
||||
#img-viewer { position: fixed; inset: 0; z-index: 150; background: rgba(0,0,0,0.93); display: flex; align-items: center; justify-content: center; overflow: hidden; }
|
||||
#img-viewer.hidden { display: none; }
|
||||
#img-viewer-img { max-width: 92vw; max-height: 88vh; touch-action: none; cursor: grab; user-select: none; -webkit-user-drag: none; }
|
||||
#img-viewer-img { max-width: 100vw; max-height: 88vh; touch-action: none; cursor: grab; user-select: none; -webkit-user-drag: none; }
|
||||
#fs-viewer { position: fixed; inset: 0; z-index: 130; background: rgba(8,10,20,0.88); padding: 40px 14px 24px; overflow: auto; /* 缩放后支持横纵滚动 */ }
|
||||
#fs-viewer.hidden { display: none; }
|
||||
.fs-panel { max-width: 860px; margin: 0 auto; background: var(--page); border-radius: 14px; padding: 18px 20px; font-size: 15.5px; }
|
||||
.fs-close {
|
||||
position: fixed; top: 12px; right: 14px; z-index: 131; width: 34px; height: 34px;
|
||||
border: none; border-radius: 50%; background: rgba(255,255,255,0.18); color: #fff;
|
||||
/* 顶部操作区:默认右上角悬浮(消息全屏用);文件预览走 fs-bar 模式时独立成一行 */
|
||||
.fs-actions { position: fixed; top: calc(2px + env(safe-area-inset-top, 0px)); right: 14px; z-index: 131; display: flex; align-items: center; gap: 8px; }
|
||||
.fs-name { display: none; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 600; color: var(--ink); }
|
||||
.fs-btn {
|
||||
flex: none; display: flex; align-items: center; justify-content: center;
|
||||
width: 34px; height: 34px; border: none; border-radius: 50%;
|
||||
background: rgba(0,0,0,0.42); color: #fff; box-shadow: 0 1px 5px rgba(0,0,0,0.22);
|
||||
font-size: 15px; line-height: 1; cursor: pointer;
|
||||
}
|
||||
/* 源码切换 eye 钮:与关闭钮同款,居其左;仅当前内容有源文本时显示(setFsSource 控显隐) */
|
||||
.fs-eye { right: 58px; display: flex; align-items: center; justify-content: center; }
|
||||
.fs-eye svg { width: 17px; height: 17px; }
|
||||
.fs-eye.on { background: rgba(255,255,255,0.85); color: #333; }
|
||||
/* 源码面板:与 #fs-body 互斥显示(toggleFsSrc 切换);不参与双指缩放逻辑 */
|
||||
.fs-btn svg { width: 17px; height: 17px; }
|
||||
.fs-btn.on { background: rgba(255,255,255,0.85); color: #333; }
|
||||
|
||||
/* 文件预览沉浸模式:40px 操作条 + 全宽内容。内容从条下开始,不会叠在页面自己的顶部按钮上;
|
||||
宽度一点不让(手机横向最贵),只牺牲一条操作栏的高度。 */
|
||||
#fs-viewer.fs-bar { display: flex; flex-direction: column; padding: 0; overflow: auto; background: var(--page); }
|
||||
#fs-viewer.fs-bar .fs-actions {
|
||||
position: sticky; top: 0; z-index: 1;
|
||||
flex: none; width: 100%;
|
||||
height: calc(40px + env(safe-area-inset-top, 0px));
|
||||
padding: env(safe-area-inset-top, 0px) 10px 0 14px;
|
||||
justify-content: flex-end; gap: 6px;
|
||||
background: var(--page); border-bottom: 1px solid var(--border);
|
||||
}
|
||||
#fs-viewer.fs-bar .fs-name { display: block; flex: 1; }
|
||||
#fs-viewer.fs-bar .fs-btn { width: 32px; height: 32px; border-radius: 8px; background: transparent; color: var(--ink); box-shadow: none; }
|
||||
#fs-viewer.fs-bar .fs-btn:active { background: var(--surface); }
|
||||
#fs-viewer.fs-bar .fs-btn.on { background: var(--active); color: #fff; }
|
||||
#fs-viewer.fs-bar .fs-panel { flex: 1; min-height: 0; width: 100%; max-width: none; margin: 0; padding: 0; border-radius: 0; background: var(--page); }
|
||||
#fs-viewer.fs-bar #fs-body { position: relative; overflow: auto; }
|
||||
#fs-viewer.fs-bar #fs-body .fs-html-frame { height: 100%; min-height: 0; border-radius: 0; }
|
||||
/* 文本/源码直排:去掉气泡卡片的内外边距,只留 12px 阅读边距 */
|
||||
#fs-viewer.fs-bar #fs-body .bubble-ai,
|
||||
#fs-viewer.fs-bar #fs-body .bubble-user {
|
||||
max-width: 100%; margin: 0; border-radius: 0; box-shadow: none; background: var(--page);
|
||||
padding: 14px 12px calc(28px + env(safe-area-inset-bottom, 0px)); font-size: 15.5px;
|
||||
}
|
||||
#fs-viewer.fs-bar #fs-src-pane { overflow: auto; }
|
||||
#fs-viewer.fs-bar #fs-src-code { max-height: none; border: 0; border-radius: 0; padding: 12px 14px calc(28px + env(safe-area-inset-bottom, 0px)); }
|
||||
#fs-viewer.fs-bar .fs-img-frame { max-width: 100%; max-height: 100%; border-radius: 0; margin: 0 auto; }
|
||||
.fs-close {
|
||||
/* 顶到视口最上沿(避开刘海),不再压住预览面板顶部 */
|
||||
position: fixed; top: calc(2px + env(safe-area-inset-top, 0px)); right: 14px; z-index: 131; width: 34px; height: 34px;
|
||||
border: none; border-radius: 50%; background: rgba(0,0,0,0.42); color: #fff;
|
||||
box-shadow: 0 1px 5px rgba(0,0,0,0.22);
|
||||
font-size: 15px; line-height: 1; cursor: pointer;
|
||||
}
|
||||
/* 源码面板:与 #fs-body 互斥显示(toggleFsSrc 切换);bar 模式下占满操作条以下区域 */
|
||||
.fs-src-panel { padding: 14px 16px; }
|
||||
#fs-src-code {
|
||||
margin: 0; white-space: pre-wrap; word-break: break-word;
|
||||
|
||||
@@ -476,11 +476,20 @@
|
||||
|
||||
<!-- 消息全屏查看器(双击气泡/工具卡打开;支持双指缩放) -->
|
||||
<div id="fs-viewer" class="hidden" onclick="if(event.target===this)closeFullscreen()">
|
||||
<button class="fs-close" onclick="closeFullscreen()">✕</button>
|
||||
<!-- 源码切换钮:仅当当前内容有源文本时显隐(toggleFsSrc 在渲染视图/源码面板间互切) -->
|
||||
<button class="fs-close fs-eye hidden" id="fs-src-btn" title="显示源码" aria-label="显示源码">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path><circle cx="12" cy="12" r="3"></circle></svg>
|
||||
</button>
|
||||
<!-- 顶部操作区:文件预览(fs-bar 模式)时独立成一行,内容从行下开始,不遮挡页面自己的顶部按钮;
|
||||
消息全屏时退化为右上角悬浮按钮簇。文件名仅栏模式显示。 -->
|
||||
<div class="fs-actions">
|
||||
<span class="fs-name" id="fs-file-name"></span>
|
||||
<!-- 源码切换钮:仅当当前内容有源文本时显隐(toggleFsSrc 在渲染视图/源码面板间互切) -->
|
||||
<button class="fs-btn hidden" id="fs-src-btn" title="显示源码" aria-label="显示源码">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path><circle cx="12" cy="12" r="3"></circle></svg>
|
||||
</button>
|
||||
<!-- 复制源码:显隐与 eye 一致(有源文本才出现);HTML/SVG/MD/代码通用 -->
|
||||
<button class="fs-btn hidden" id="fs-copy-btn" title="复制源码" aria-label="复制源码">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>
|
||||
</button>
|
||||
<button class="fs-btn" onclick="closeFullscreen()" title="关闭" aria-label="关闭">✕</button>
|
||||
</div>
|
||||
<div class="fs-panel" id="fs-body"></div>
|
||||
<div class="fs-panel fs-src-panel hidden" id="fs-src-pane"><pre id="fs-src-code"></pre></div>
|
||||
</div>
|
||||
|
||||
@@ -107,12 +107,21 @@ function pvCloseDoc() {
|
||||
}
|
||||
|
||||
// 移动端全屏承载:把节点塞进 #fs-viewer 并展开
|
||||
function pvOverlayShow(node) {
|
||||
// opts.bar → 文件预览沉浸模式:顶部独立操作条,内容全宽占满剩余空间
|
||||
// opts.name → 操作条左侧的文件名(仅 bar 模式可见)
|
||||
// opts.source → 源文本,交给 eye / 复制钮(未传则两者隐藏)
|
||||
function pvOverlayShow(node, opts) {
|
||||
opts = opts || {};
|
||||
const viewer = document.getElementById('fs-viewer');
|
||||
const body = document.getElementById('fs-body');
|
||||
viewer.classList.toggle('fs-bar', !!opts.bar);
|
||||
const nm = document.getElementById('fs-file-name');
|
||||
nm.textContent = opts.name ? String(opts.name) : '';
|
||||
nm.title = nm.textContent;
|
||||
body.innerHTML = ''; body.style.zoom = '';
|
||||
if (typeof setFsSource === 'function') setFsSource(null);
|
||||
if (typeof setFsSource === 'function') setFsSource(opts.source == null ? null : opts.source);
|
||||
body.appendChild(node);
|
||||
document.getElementById('fs-viewer').classList.remove('hidden');
|
||||
viewer.classList.remove('hidden');
|
||||
}
|
||||
|
||||
// ---------- office:iframe 直挂预览网关(HEAD 预探顺便触发冷槽复活) ----------
|
||||
@@ -128,7 +137,7 @@ async function pvOpenOffice(name, sizeText) {
|
||||
document.getElementById('pv-body').appendChild(load); // 开窗即刻可见,冷启动转圈等首帧
|
||||
} else {
|
||||
load = pvLoadingBox(); load.classList.add('inline');
|
||||
pvOverlayShow(load);
|
||||
pvOverlayShow(load, { bar: true, name: name });
|
||||
if (!pvState.mobilePush) { try { history.pushState({ pvOffice: 1 }, ''); pvState.mobilePush = true; } catch (_) {} }
|
||||
}
|
||||
let head = null;
|
||||
@@ -179,7 +188,7 @@ async function openFilePreview(name, sizeText) {
|
||||
pvClearBody(); pvHeader(name, sizeText);
|
||||
document.getElementById('pv-body').appendChild(node);
|
||||
pvToolbar(tb || tbAll);
|
||||
} else pvOverlayShow(node);
|
||||
} else pvOverlayShow(node, { bar: true, name: name });
|
||||
};
|
||||
if (kind === 'none') {
|
||||
const low = String(name).toLowerCase();
|
||||
@@ -312,7 +321,7 @@ async function pvTextNode(name, sizeText, r) {
|
||||
return holder;
|
||||
}
|
||||
|
||||
// 移动端非 office:保持原全屏行为(#fs-viewer)
|
||||
// 移动端非 office:统一走 #fs-viewer 沉浸式(顶部操作条 + 全宽内容)
|
||||
async function pvRenderLegacy(name, r, my) {
|
||||
if (/\.html?$/i.test(name)) {
|
||||
const html = await r.text();
|
||||
@@ -320,8 +329,7 @@ async function pvRenderLegacy(name, r, my) {
|
||||
iframe.className = 'fs-html-frame';
|
||||
iframe.setAttribute('sandbox', 'allow-same-origin allow-scripts');
|
||||
iframe.srcdoc = rewriteHtmlDocSrcs(html, name);
|
||||
pvOverlayShow(iframe);
|
||||
setFsSource(html); // 源码钮可看页面原码
|
||||
pvOverlayShow(iframe, { bar: true, name: name, source: html }); // 全宽沉浸,源码可看/可复制
|
||||
if (my !== pvSeq) return;
|
||||
} else if (/\.pdf$/i.test(name)) {
|
||||
const u = URL.createObjectURL(await r.blob());
|
||||
@@ -331,7 +339,17 @@ async function pvRenderLegacy(name, r, my) {
|
||||
frame.className = 'fs-html-frame';
|
||||
frame.title = name;
|
||||
frame.src = u;
|
||||
pvOverlayShow(frame);
|
||||
pvOverlayShow(frame, { bar: true, name: name });
|
||||
} else if (/\.svg$/i.test(name)) {
|
||||
// SVG 当图片显示(<img> 不执行内嵌脚本),同时保留源码给 eye/复制钮
|
||||
const text = await r.text();
|
||||
const u = URL.createObjectURL(new Blob([text], { type: 'image/svg+xml;charset=utf-8' }));
|
||||
if (state.lastImgBlobUrl) { try { URL.revokeObjectURL(state.lastImgBlobUrl); } catch (_) {} }
|
||||
state.lastImgBlobUrl = u;
|
||||
const im = document.createElement('img');
|
||||
im.className = 'fs-img-frame'; im.src = u; im.alt = String(name);
|
||||
im.addEventListener('click', () => openImageViewer(u, String(name).split('/').pop()));
|
||||
pvOverlayShow(im, { bar: true, name: name, source: text });
|
||||
} else if (PV_IMG.test(name)) {
|
||||
const u = URL.createObjectURL(await r.blob());
|
||||
if (state.lastImgBlobUrl) { try { URL.revokeObjectURL(state.lastImgBlobUrl); } catch (_) {} }
|
||||
@@ -352,7 +370,7 @@ async function pvRenderLegacy(name, r, my) {
|
||||
wrap.className = 'msg other';
|
||||
wrap.innerHTML = '<div class="' + cls + '">' + html + '</div>';
|
||||
decorateCodePre(wrap);
|
||||
openFullscreen(wrap.firstChild, text); // 文件原文供 eye 钮看源码
|
||||
pvOverlayShow(wrap.firstChild, { bar: true, name: name, source: text }); // 全宽沉浸,eye/复制看原文
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -527,6 +527,8 @@
|
||||
btn.classList.remove('on');
|
||||
btn.title = '显示源码';
|
||||
btn.setAttribute('aria-label', '显示源码');
|
||||
// 复制钮与 eye 同显隐:有源码才可复制
|
||||
$('fs-copy-btn').classList.toggle('hidden', fsSrcText == null);
|
||||
}
|
||||
function toggleFsSrc() {
|
||||
if (fsSrcText == null) return;
|
||||
@@ -541,8 +543,15 @@
|
||||
btn.setAttribute('aria-label', lbl);
|
||||
}
|
||||
$('fs-src-btn').addEventListener('click', toggleFsSrc);
|
||||
// 复制源码:HTML/SVG/MD/代码通用,拿到的就是 eye 面板里那份文本
|
||||
$('fs-copy-btn').addEventListener('click', async () => {
|
||||
if (fsSrcText == null) return;
|
||||
const ok = await copyText(fsSrcText);
|
||||
toast(ok ? '已复制' : '复制失败');
|
||||
});
|
||||
|
||||
function openFullscreen(el, src) {
|
||||
$('fs-viewer').classList.remove('fs-bar'); // 消息全屏沿用悬浮按钮,清掉文件预览的栏模式
|
||||
const body = $('fs-body');
|
||||
body.innerHTML = '';
|
||||
body.style.zoom = '';
|
||||
@@ -566,6 +575,7 @@
|
||||
}
|
||||
function closeFullscreen() {
|
||||
setFsSource(null); // 顺手收起源码面板/隐藏 eye 钮,下轮打开不残留
|
||||
$('fs-viewer').classList.remove('fs-bar');
|
||||
$('fs-viewer').classList.add('hidden');
|
||||
$('fs-viewer').dispatchEvent(new Event('fs:close'));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
// 通用技能执行面:POST /api/sessions/:id/skills/:name/run
|
||||
//
|
||||
// 与 /api/db 的定位对称:data-page 给了页面「通用数据面」,这里补上「通用执行面」。
|
||||
// 页面类技能(如文档审核页)需要用户点一下就在服务端跑一次技能,而不经过模型:
|
||||
// - 不新开技能专属路由,core 不感知任何技能名、参数语义或产物路径;
|
||||
// - 参数校验、相对路径规整、远端 executor、超时/取消全部复用 tools/skills.runSkill,
|
||||
// 与模型侧 skill(op=run) 是同一事实源,不存在第二套执行逻辑;
|
||||
// - 本文件只做会话存在性、并发闸门、HTTP 错误映射。
|
||||
//
|
||||
// 认证:挂在 server.js 的 auth.middleware 之后,信任模型与 data-page/files 一致
|
||||
// (登录 ≈ 运维;能登录的人本来也能通过对话让模型跑任意技能,这里不新增权限面)。
|
||||
// 刻意不加 CORS:跨站 application/json POST 会被预检挡下。
|
||||
//
|
||||
// 并发:同一 (session, skill) 同时只允许一个请求;页面重复点按钮得到 409 而不是双跑。
|
||||
// 单实例内存 Set 即可(仓库整体就是单进程内存模型,重启即清空)。
|
||||
//
|
||||
// 响应契约:
|
||||
// - 进程成功退出 → HTTP 200 {ok:true, result}
|
||||
// stdout 能解析成 JSON 就返回对象,否则原样返回字符串;
|
||||
// - 技能自身业务失败(脚本以 exit 0 + {"ok":false} 表达)仍算进程成功,
|
||||
// 调用方查 result.ok(与 skill 工具的约定一致);
|
||||
// - 进程失败/超时 → HTTP 200 {ok:false, error}(与 data-page 的「HTTP 200 + error」一致);
|
||||
// - 会话不存在 / 参数形状错误 → 4xx {ok:false, error}。
|
||||
'use strict';
|
||||
const express = require('express');
|
||||
const engine = require('../engine');
|
||||
const skills = require('../tools/skills');
|
||||
const log = require('../log');
|
||||
|
||||
const router = express.Router();
|
||||
const inflight = new Set();
|
||||
|
||||
function sendErr(res, code, msg) {
|
||||
res.status(code).json({ ok: false, error: msg });
|
||||
}
|
||||
|
||||
router.post('/:id/skills/:name/run', async (req, res) => {
|
||||
const sessionId = req.params.id;
|
||||
const name = String(req.params.name || '').trim();
|
||||
if (!engine.getSession(sessionId)) return sendErr(res, 404, '会话不存在');
|
||||
if (!name) return sendErr(res, 400, '技能名不能为空');
|
||||
|
||||
const body = req.body || {};
|
||||
const args = body.args === undefined ? {} : body.args;
|
||||
if (args === null || typeof args !== 'object' || Array.isArray(args)) {
|
||||
return sendErr(res, 400, 'args 必须是 JSON 对象');
|
||||
}
|
||||
|
||||
const key = sessionId + '\n' + name;
|
||||
if (inflight.has(key)) return sendErr(res, 409, '该技能正在执行,请稍候');
|
||||
inflight.add(key);
|
||||
const t0 = Date.now();
|
||||
try {
|
||||
const ctx = {
|
||||
sessionId,
|
||||
taskDir: engine.taskDirOf(sessionId),
|
||||
cfg: engine.getConfig(),
|
||||
};
|
||||
const out = await skills.runSkill(ctx, name, args);
|
||||
let result = out;
|
||||
try { result = JSON.parse(out); } catch (_) { /* 非 JSON 技能:原样返回字符串 */ }
|
||||
log.info('skills', sessionId + ' HTTP 运行 ' + name + ' 完成(' + (Date.now() - t0) + 'ms)');
|
||||
res.json({ ok: true, result });
|
||||
} catch (err) {
|
||||
const msg = String((err && err.message) || err || '技能执行失败');
|
||||
if (err && err.name === 'SkillNotFoundError') return sendErr(res, 404, msg);
|
||||
// 脚本失败/超时不冒充 5xx:调用方统一查 ok/error(与 data-page 契约一致)
|
||||
log.warn('skills', sessionId + ' HTTP 运行 ' + name + ' 失败(' + (Date.now() - t0) + 'ms):' + msg.split('\n')[0]);
|
||||
res.json({ ok: false, error: msg });
|
||||
} finally {
|
||||
inflight.delete(key);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -67,6 +67,9 @@ app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use('/api/sessions', require('./routes/sessions'));
|
||||
app.use('/api/sessions', require('./routes/messages'));
|
||||
app.use('/api/sessions', require('./routes/files'));
|
||||
// 通用技能执行面(页面类技能用,不经模型):POST /api/sessions/:id/skills/:name/run。
|
||||
// 与 /api/db 对称;执行核心仍复用 tools/skills.runSkill,core 不感知具体技能。
|
||||
app.use('/api/sessions', require('./routes/skill-run'));
|
||||
app.use('/api/skills', require('./routes/skills'));
|
||||
// 系统配置含全部用户(脱敏密码)与平台密钥,且可整份改写 YAML:仅管理员可访问。
|
||||
app.use('/api/config', auth.requireAdmin, require('./routes/config'));
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
# 审核页内部协议(维护者文档)
|
||||
|
||||
> 本文件刻意不在 SKILL.md 里引用,避免模型顺着指针来读;只有维护者需要看。
|
||||
|
||||
`SKILL.md` 是给 AI 的调用契约;本文件记录审核页/重生成/数据表的实现细节。**AI 日常调用
|
||||
不需要读这里**,`skill get` 也只返回 SKILL.md。
|
||||
|
||||
## 文件与目录
|
||||
|
||||
```
|
||||
skills/document-photo-to-pdf/
|
||||
SKILL.md ← AI 契约(只有 images/output)
|
||||
REVIEW.md ← 本文件
|
||||
run.py ← 入口
|
||||
doc_deskew/ ← 检测/几何/出页/检验
|
||||
review_store.py ← data.db 的 doc_review_* 表
|
||||
review_page.py ← 模板副本 + 缩略图发布
|
||||
web/index.html ← 审核页模板的单一事实源
|
||||
|
||||
tasks/<session-id>/
|
||||
.app/doc-review/
|
||||
index.html ← 每次发布覆盖的模板副本(不含数据注入)
|
||||
<batch_id>/p1.jpg ... ← 本批缩略图(长边 1200,JPEG)
|
||||
data.db ← doc_review_batch / doc_review_page / doc_review_job
|
||||
<output>.pdf
|
||||
<output>.state.json ← state v2,供恢复自动/调试
|
||||
```
|
||||
|
||||
core 只托管会话 `.app/`,不托管 skill 目录,所以模板必须复制一份到会话;源仍只有
|
||||
`web/index.html` 一份,副本不是第二份源码。
|
||||
|
||||
## 对 AI 隐藏的参数
|
||||
|
||||
`run.py` 仍接受以下参数,但不出现在 SKILL.md / input_schema 里;审核页和 apply 接口使用:
|
||||
|
||||
| 参数 | 用途 |
|
||||
|---|---|
|
||||
| `paper` | a4/letter/16k/b5/数字比例/auto |
|
||||
| `orientation` | auto/none/portrait/landscape |
|
||||
| `enhance` | original/normal/strong/bw |
|
||||
| `dpi` | standard/high/72~600 |
|
||||
| `review` | `always`(默认)/`auto`(仅有 needs 时发布)/`off`(自动化用) |
|
||||
| `batch_id` | 重生成:从 data.db 读源图/输出/编辑,覆盖同名 PDF |
|
||||
| `edits` | `review/1` 对象或 JSON 路径;优先级高于显式参数 |
|
||||
| `state` | 上一份 state.json;仅用于恢复自动/调试 |
|
||||
|
||||
优先级:`edits.batch` > 显式参数 > 默认值。
|
||||
|
||||
## data.db 表
|
||||
|
||||
所有表以 `doc_review_` 前缀,避免和用户业务表混淆。技能自己 `CREATE TABLE IF NOT EXISTS`;
|
||||
`busy_timeout=5000`,WAL 由 core 建库时设置,技能侧 best-effort。
|
||||
|
||||
### doc_review_batch
|
||||
|
||||
```sql
|
||||
id TEXT PRIMARY KEY, -- drYYYYMMDD-HHMMSS-xxxx,不含点/斜杠
|
||||
output TEXT, -- 相对会话目录的 PDF 路径
|
||||
paper TEXT, enhance TEXT, dpi TEXT, orientation TEXT,
|
||||
status TEXT, -- running/ready/review/error
|
||||
summary TEXT, warnings TEXT,-- warnings 是 JSON 数组
|
||||
schema_version INTEGER,
|
||||
created_at TEXT, updated_at TEXT
|
||||
```
|
||||
|
||||
### doc_review_page
|
||||
|
||||
```sql
|
||||
batch_id TEXT, idx INTEGER, -- idx 永远是原始 images 的 1-based 下标
|
||||
source TEXT, -- 相对会话目录的原图路径
|
||||
source_w INTEGER, source_h INTEGER,
|
||||
w INTEGER, h INTEGER, -- 缩略图尺寸(页面画布用)
|
||||
auto_corners TEXT, -- 原图像素,自动基线
|
||||
auto_corners_norm TEXT, -- 归一化,页面“恢复自动”用
|
||||
corners TEXT, -- 归一化,当前角点(未旋转)
|
||||
auto_rotate INTEGER, -- 自动方向基线(0/90)
|
||||
rotate INTEGER, -- 当前应用旋转(用户覆盖后是绝对值)
|
||||
deleted INTEGER, sort INTEGER,
|
||||
status TEXT, reason TEXT, thumb TEXT, error TEXT,
|
||||
PRIMARY KEY(batch_id, idx)
|
||||
```
|
||||
|
||||
### doc_review_job
|
||||
|
||||
```sql
|
||||
batch_id TEXT PRIMARY KEY,
|
||||
edits TEXT, -- review/1 完整快照
|
||||
status TEXT, -- queued/running/done/error
|
||||
message TEXT,
|
||||
requested_at TEXT, started_at TEXT, finished_at TEXT
|
||||
```
|
||||
|
||||
## 审核页如何工作
|
||||
|
||||
1. 页面从 `/app/<task_id>/doc-review/` 加载;无 `?b=` 时查 `doc_review_batch` 最新一行。
|
||||
2. 通过 `POST /api/db/<task_id>/query` 读 `doc_review_batch` / `doc_review_page`。
|
||||
3. 用户拖角/旋转/删页/排序/改四项参数,只改浏览器内状态并写 localStorage 草稿。
|
||||
4. 点「重新生成 PDF」:
|
||||
- `INSERT ... ON CONFLICT` 写 `doc_review_job`(`status=queued`,`edits` 是完整快照);
|
||||
- 调通用执行接口
|
||||
`POST /api/sessions/<task_id>/skills/document-photo-to-pdf/run`,body `{args:{batch_id}}`;
|
||||
- 技能读 job → 应用 edits → 覆盖同名 PDF → 更新 batch/page/job;
|
||||
- 页面重新读 data.db,刷新警告、状态、PDF 链接。
|
||||
5. 全程不经过模型,也不需要用户复制 JSON。
|
||||
|
||||
通用接口实现在 `app/server/routes/skill-run.js`:core 只做会话/并发/错误映射,执行仍走
|
||||
`tools/skills.runSkill`,不感知技能名或参数语义。
|
||||
|
||||
## 页面交互约定
|
||||
|
||||
- 顶部标题条(白底,参考 H5 全屏预览):左侧 PDF 名,右侧下载 PDF,最右 X 返回会话。
|
||||
- 重新生成 PDF 不在顶栏:放在照片区底部(stage 与预览各一个,按当前视图显示;桌面只用
|
||||
stage),仅在存在未保存修改时出现。
|
||||
- 底部只保留翻页和当前页名,不再放操作按钮。
|
||||
- 页码不占顶栏:`1/n` 浮在图片右上角(裁切区、预览区各一个,按当前视图显示);
|
||||
左上角常驻当前模式标签(裁切区「手动定位」、预览区「校正预览」)。
|
||||
移动端一次只显示一个面板,看到哪个标签就是当前模式;视图按钮固定叫「切换」。
|
||||
移动端顶栏按钮带文字而非只靠图标:视图按钮写“点下去会去哪”(当前在预览→手动定位,
|
||||
在当前裁切→校正预览),另三个是旋转 / 重置 / 设置。
|
||||
- 裁切区/预览区支持左右滑动翻页:左滑下一页、右滑上一页;仅触摸/笔生效,拖角优先,
|
||||
按钮点击不触发。
|
||||
- 是否 dirty 用 `collect()` 与加载时的基线 `S.savedEdits` 比较;拖角、旋转、删页/排序、
|
||||
改四项参数都会触发。
|
||||
- 重新生成成功后重读 DB、清本地草稿、把新状态设为基线,按钮自动消失;失败则保留以便重试。
|
||||
- X 返回 `/?s=<task_id>`;独立页面里也适用。
|
||||
- 手机默认进校正预览,点左上角切到裁切。
|
||||
|
||||
## edits / 旋转语义
|
||||
|
||||
```json
|
||||
{"schema":"review/1",
|
||||
"batch":{"paper":"a4","enhance":"normal","dpi":"standard","orientation":"auto"},
|
||||
"pages":[{"i":3,"c":[[0.03,0.05],[0.97,0.04],[0.96,0.95],[0.04,0.96]],"r":90}],
|
||||
"order":[1,2,3],"deleted":[]}
|
||||
```
|
||||
|
||||
- `i` 是原始 images 下标,不随页序变化。
|
||||
- `c` 是 exif_transpose 后原图的归一化坐标,顺序左上/右上/右下/左下。
|
||||
- `r` 是该页最终顺时针旋转(0/90/180/270),覆盖自动方向;页面总会为“人工页”带上
|
||||
`r`(包括 0),服务端用 `user_r is not None` 判断是否覆盖。
|
||||
- `pages` 只放与自动基线不同的页;未改的页由服务端重新检测/评级,避免把 fallback 页
|
||||
误升成 ready。
|
||||
- `order` 缺省按 images 原序;`deleted` 的页不进出件。
|
||||
- 人工旋转 90/270 时,`orientation=auto` 下该页画布按旋转后的有效方向走,避免把横向内容
|
||||
压进竖向画布;`portrait`/`landscape` 强制方向时仍按强制值。
|
||||
|
||||
## state v2
|
||||
|
||||
每次成功出件写 `<output>.state.json`:
|
||||
|
||||
```json
|
||||
{"schema":"document-photo-to-pdf/2",
|
||||
"batch":{"doc_type":null,"paper":"auto","enhance":"normal","dpi":150,"orientation":"auto"},
|
||||
"pages":[{"index":1,"source":"p1.jpg","source_size":[1400,1700],
|
||||
"auto_corners":[[...]],"corners_norm":[[...]],
|
||||
"rotate":0,"rotate_source":"auto","status":"ready","reason":null}]}
|
||||
```
|
||||
|
||||
`rotate` 是自动基线(不是用户覆盖值);用户覆盖值在 data.db 的 `rotate` 列。state 只用于
|
||||
恢复自动/调试,审核页不读它。
|
||||
|
||||
## 状态与失败
|
||||
|
||||
- `ready`:可直接用。
|
||||
- `review`:建议看一眼(掩膜回退、覆盖率低、纸型存疑)。
|
||||
- `needs_corners`:检测为空或校验不过,已用“画面内缩 3%”兜底框出件,需人工补角。
|
||||
- `needs_orientation`:接近正方形,横竖判不出来,需人在页面旋转。
|
||||
- `render_failed` / `failed`:出页失败/原图打不开;后者会被跳过。
|
||||
- `deleted`:用户删除,不进出件。
|
||||
|
||||
单页检测/校验失败不再让整批失败;只有一页都出不了才 `ok:false`。检测到的无效四边形会
|
||||
退回兜底框,并把 `auto_corners` 一起换成兜底框,避免“恢复自动”把无效角点拖回来。
|
||||
|
||||
## 清理
|
||||
|
||||
审核页发布只保留最近 5 个批次的缩略图目录;新批次完成后删除更早的目录和 DB 记录。
|
||||
`batch_id` 重生成(apply)不会触发清理,避免把正在编辑的旧批次裁掉。
|
||||
@@ -1,159 +1,52 @@
|
||||
---
|
||||
name: document-photo-to-pdf
|
||||
description: 把手机拍摄的纸质文档照片拉正、去背景,按顺序合并为一个 PDF
|
||||
thinking: true
|
||||
description: 把手机拍摄的纸质文档照片裁剪、拉正,合并为 PDF,并给出可预览/微调的审核页
|
||||
visit_url: /app/{task_id}/doc-review/
|
||||
input_schema:
|
||||
type: object
|
||||
properties:
|
||||
images:
|
||||
type: array
|
||||
items: {type: string}
|
||||
description: 图片路径,按合并顺序;可只传一张
|
||||
output:
|
||||
type: string
|
||||
description: 输出 PDF 文件名,默认 文档照片_校正.pdf
|
||||
---
|
||||
|
||||
# 文档照片转 PDF
|
||||
|
||||
手机拍的纸质材料 → 一个版正字清的 PDF。适用于合同、票据、证照、手写稿等多页纸质材料,纸张尺寸不限。
|
||||
|
||||
## 怎么跑
|
||||
|
||||
本技能没有 `run` 入口,用 `terminal` 直接执行命令。脚本装在技能目录,而 terminal 的
|
||||
cwd 是会话任务目录——两者不是同一个目录,所以**脚本一律用绝对路径调用**,图片和
|
||||
`out/` 用任务目录里的相对路径:
|
||||
|
||||
```sh
|
||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py <子命令> ...
|
||||
```json
|
||||
{"images": ["图1.jpg", "图2.jpg"], "output": "合同_校正.pdf"}
|
||||
```
|
||||
|
||||
## 铁律
|
||||
`images` 必填,按顺序合并;`output` 可选,默认 `文档照片_校正.pdf`。纸张、方向、调色、
|
||||
清晰度不要传,交给审核页。
|
||||
|
||||
> 以下 8 条为硬约束,违反任一条即视为任务失败:立即停手、向用户说明,不得"先改完再说"。
|
||||
## 返回值
|
||||
|
||||
1. **先说再做。** 每次调脚本前用一句话说明这次干什么(要标注步骤编号)。
|
||||
2. **坐标只能靠眼睛读,禁止一切自建脚本。** 不另造任何纸张处理的辅助的代码,一次性脚本也不行。确实无法达成目的需要新能力=先停下问用户,不许自己造。
|
||||
3. **不裁切、不放大、不逐角看。** 四个角在同一张网格图上一次读全,直接报 4 组数字。不为了"读准一点"造局部图、加刻度、逐个角反复看。
|
||||
4. **每张图读 2 次,且每次都要有目的。** 网格 1 次 + 角标 1 次,看完就交付。
|
||||
- **读之前先说清这次要确认什么。** 说不出目的就不读。"再看一眼确认一下"不算目的。
|
||||
- **不为读而读。** 允许重读,但必须是被动的、有明确对象的:图没送达/像素没收到、要核对某个具体数字或某条边。没有新对象就直接用手上已有的数字。
|
||||
- **只读这两类图。** 网格、角标。原图、局部放大图、成品图、自己另造的辅助图都不读。
|
||||
- 没把握就按"宁可外扩"给保守值,交给角标图验证,不在读图阶段磨。
|
||||
5. **一次成件,禁止试错。** 一条 `build` 出件 + 检验。一轮里每页最多跑 1 次 `build`;不调参、不重取角点、不换方法、不"先出一版看看"。不通过就报诊断,停下等确认。
|
||||
6. **只有用户明确说"再改"才允许修复。** 完整一轮 = `prepare` → `set`(每页一次)→ `build`(一次)→ 看角标 → 交付。这一轮内发现的任何偏差(框偏了、裁到内容、比例不对),一律只报诊断和坐标建议,**不自行改、不重跑、不补做**。用户在本轮交付后明确说"再改/重做"时才能动手,且还是只跑一轮,改完重新过角标关。
|
||||
7. **指标 ≠ 成品。** 校验只查"框内"像素,框漏掉的内容查不出来。角标图必须亲眼看。
|
||||
8. **违规即停。** 一旦意识到自己正在写脚本、造辅助图、二次 build 或擅自修复,立即停手,向用户说明,不带病交付。
|
||||
- `ok:true`:`summary` 一句话摘要,`pdf` 文件名;末尾附完整「访问地址」。把 PDF 和地址
|
||||
一起给用户。
|
||||
- `ok:false`:转述 `error.message` / `error.hint`,忽略访问地址。
|
||||
|
||||
### 交付前自检(有一项为"是"就停手报告)
|
||||
|
||||
- 本轮是否新建/修改过任何脚本或辅助图?
|
||||
- 本轮 `build` 是否跑超过 1 次?
|
||||
- 是否在用户没说过"再改"时自行调整过角点或重出件?
|
||||
- 每张图是否只读了网格、角标各 1 次?
|
||||
- 校验未过的项是否只报告、没自行修?
|
||||
|
||||
## 流程
|
||||
|
||||
### 1 · 出网格图
|
||||
|
||||
```sh
|
||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py prepare 图1.jpg 图2.jpg
|
||||
```json
|
||||
{"ok":true,"summary":"3 页已生成;1 页建议在审核页确认","pdf":"合同_校正.pdf"}
|
||||
访问地址:https://agent.ai.foresh.com/app/task-20261023-001/doc-review/
|
||||
```
|
||||
|
||||
不传图片名 = 取当前目录所有图片。输出 `out/N-网格-<名称>.png`(100px 红色主线带坐标刻度、50px 蓝色辅助线,两者同宽)。
|
||||
审核页可拖角、旋转、删页/排序、改纸张与清晰度;改动后点「重新生成 PDF」覆盖同名文件。
|
||||
|
||||
### 2 · 从网格图中读出角点,一张图一条命令
|
||||
## 失败与拍摄
|
||||
|
||||
顺序固定:左上 → 右上 → 右下 → 左下(屏幕坐标,原点左上)。
|
||||
- 图片打不开:确认路径与格式(jpg/png/webp/bmp/tif)。
|
||||
- 检测不到纸张:整张纸入画,四边留 3%~5% 背景,换反差大的桌面。
|
||||
- 只有全部页都出不了才 `ok:false`;单页失败会用兜底框出件,审核页会标出来。
|
||||
|
||||
**要找的是纸张本身真实的四个角**——纸面这个四边形的 4 个顶点,**坐标的质量直接决定最终成品的质量!**。
|
||||
## 边界
|
||||
|
||||
读数方法:
|
||||
1. 先找离目标点最近的红线(整百、带数字刻度)读基准值;
|
||||
2. 再识别目标点相对相邻蓝线(50px)的偏移;
|
||||
3. 把目的坐标复述成一句话,例如“右下:x 在 1000 红线右 52px,y 在 1700 红线下 22px → 1052,1722”。
|
||||
|
||||
读数要求:
|
||||
- 读之前先定好这一张要报的 4 组数字,看完立刻 `set` 落盘到 `out/corners.json`。
|
||||
- 角被遮挡:沿两条可见纸边延长交会出顶点。
|
||||
- 角出画面:顺两条边的趋势延长,坐标可为负或超界(脚本接受)。
|
||||
|
||||
写入坐标:
|
||||
```sh
|
||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py set 图1.jpg \
|
||||
--quad "左上x,左上y;右上x,右上y;右下x,右下y;左下x,左下y"
|
||||
```
|
||||
- 一次只写一张图,脚本已强制拦截多图(防串页)。
|
||||
- 落盘 `out/corners.json`,同名键覆盖并打印旧值对比;`show` 可随时查看。
|
||||
- 报给用户的只能是 `--quad` 串或 4 组坐标数字,禁止"约 (140,390)、纸略倾斜"这类描述。
|
||||
|
||||
### 3 · 一条命令出件
|
||||
|
||||
```sh
|
||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py build \
|
||||
--out 文档照片_校正.pdf --paper a4 --force
|
||||
```
|
||||
|
||||
内部依次:回画角标 → 角点几何校验 → 透视校正+按纸比例拉正+提亮 → 合并 PDF → PDF 渲染检验 → 打印汇总。
|
||||
|
||||
参数:
|
||||
- `--paper`:标准纸必须传 —— `a4`(0.707)、`letter`(0.773)、`16k`(0.712)、`b5`(0.704),或数字比例。票据、异形纸不确定就省略,保留原始比例。
|
||||
- `--force`:角点校验不过也出件(默认)。
|
||||
- 位置参数传图名:只出指定页,如 `build 图2.jpg --out 结果.pdf`(输出 PDF 只含这一页)。
|
||||
- `--dpi`:PDF 检验渲染 dpi,默认 100。
|
||||
|
||||
### 4 · 看角标图(强制,一次)
|
||||
|
||||
**角标图是决定图像处理质量的唯一标准!**
|
||||
|
||||
读 `out/N-角标-*.png`(原图 + 同一套网格 + 四角标注),四个角在同一张图上核对:
|
||||
1. 绿线是否压在纸边交界?
|
||||
2. 纸张上内容是否全在绿线框内?
|
||||
3. 4个角标是否都在纸张实际的角上!
|
||||
|
||||
**只看不改:** 发现问题只记进报告,记录修正前后角标坐标的表格、不重跑 `build`、不补造辅助图。
|
||||
|
||||
| 页码 | 实际使用的角点坐标 | 修正后的角点坐标 |
|
||||
|---|---|---|
|
||||
| 1 | x1,y1;x2,y2;x3,y3;x4,y4 | x1',y1';x2',y2';x3',y3';x4',y4' |
|
||||
|
||||
## 判定与报告
|
||||
|
||||
| 检查项 | 判据 | 不达标含义 |
|
||||
|---|---|---|
|
||||
| 页数 | 与源图数一致 | 合并出错 |
|
||||
| 纸面短边 | ≥ 533px @100dpi | 角点错或分辨率过低 |
|
||||
| 纸张比例 | 仅提示 | 传了 `--paper` 应≈1.00,偏差大说明角点有误 |
|
||||
| 边缘背景残留 | 边带亮度 ≥ 纸面 80% | 角点落在背景上,内收 3~5px |
|
||||
| 内容贴边 | 页边无成片文字级暗像素 | 内容被切,外扩 5px |
|
||||
|
||||
- **全部通过**:给产物路径、大小、页数、指标,加一句角标图上确认的结果。
|
||||
- **"外侧纸面占比 100%"一律当角点错误**,误差是几十到上百 px,回第 2 步重读,不是微调。
|
||||
- **不达标**:只报诊断+ 建议,**不自行改、不重跑、不换方法**。用户明确说"再改"后,才改动后重跑一轮,然后交用户决策。
|
||||
- **用户没发话就交付**:即使只差 1px、即使一眼看出能更好,也不许动。报告里可写建议,手不能动。
|
||||
|
||||
## 内容安全
|
||||
|
||||
**禁止用生成式模型处理这类图片。** 生成模型是重绘而非变换,会改写文档内容,且结果看起来完全正常。本流程只做纯几何变换 + 全局等比提亮,文字、金额、印章保持原始像素。
|
||||
|
||||
## 命令速查
|
||||
|
||||
下表省略公共前缀 `python3 /workspace/skills/document-photo-to-pdf/doc_deskew/`。
|
||||
|
||||
| 场景 | 命令 |
|
||||
|---|---|
|
||||
| 出网格图 | `pipeline.py prepare 图1.jpg 图2.jpg` |
|
||||
| 写角点(一张一次) | `pipeline.py set 图1.jpg --quad "x1,y1;x2,y2;x3,y3;x4,y4"` |
|
||||
| 查看已落盘 | `pipeline.py show` |
|
||||
| 出件 | `pipeline.py build --out 结果.pdf --paper a4` |
|
||||
| 只出指定页(PDF 只含该页) | `pipeline.py build 图2.jpg --out 结果.pdf` |
|
||||
| 复检已有 PDF(不重新出件) | `pipeline.py check 结果.pdf --expect-pages 2` |
|
||||
|
||||
底层模块(与 `pipeline.py` 同目录,由它调用,日常不用直接执行):
|
||||
|
||||
| 模块 | 职责 |
|
||||
|---|---|
|
||||
| `grid.py` | 画带坐标的网格图 |
|
||||
| `preview_quad.py` | 画角标图(叠网格) |
|
||||
| `verify_quad.py` | 角点几何校验 |
|
||||
| `homography.py` | 透视变换 + 提亮 |
|
||||
| `run.py` | 拉正 + 拼页(纯模块,无 CLI) |
|
||||
| `make_pdf.py` | 多页合成 PDF |
|
||||
| `check.py` | 成品 PDF 检验 |
|
||||
|
||||
## 已知边界
|
||||
|
||||
- 纸张出画面的部分无法还原,角点可外扩到画面外。
|
||||
- 不为凑标准比例拉坐标;比例偏离通常是取点不准或纸张非该规格。
|
||||
- 拍摄时四边各留 3%~5% 背景余量。
|
||||
- 纸出画面无法还原;角点可外扩。
|
||||
- 自动方向只判 90°,180° 在审核页旋转。
|
||||
- 默认按同批中位比例;严格 A4/16K/B5 在审核页选。
|
||||
- 支持 jpg/png/webp/bmp/tif;HEIC 不支持。
|
||||
- 一图一纸;同框多纸只取最大。
|
||||
- 建议 ≤20 页;更多或 300dpi 可能超 120s,可分批。
|
||||
|
||||
@@ -0,0 +1,608 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""纸张边线检测:从一张已转正的 PIL 图里估四个纸角。
|
||||
|
||||
对外只有 detect(img, P) -> (corners, info):
|
||||
- img 是已做 exif_transpose 的 RGB 图;
|
||||
- corners 是原图像素坐标 [(x, y)],顺序 左上、右上、右下、左下;
|
||||
- info 里带覆盖率、纸面占比、回退标记,供上层做置信度告警。
|
||||
|
||||
设计要点:
|
||||
- 不打印、不落盘、不画图。检测失败返回 ([], info),由调用方决定是否终止。
|
||||
- 纸面掩膜保留两份:腐蚀过的只用于限定边缘搜索环带;回退几何用未腐蚀的
|
||||
原始掩膜,避免角点系统性内收(旧实现两次 erode 会让角点切进纸内)。
|
||||
- 回退时不再只用 4 个极值点连线,而是用整条掩膜边界按边拟合,再求交点。
|
||||
"""
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 基础工具
|
||||
|
||||
def _odd(size):
|
||||
size = int(size)
|
||||
if size < 3:
|
||||
size = 3
|
||||
if size % 2 == 0:
|
||||
size += 1
|
||||
return size
|
||||
|
||||
|
||||
def _slide_reduce(a, k, op, axis):
|
||||
"""沿 axis 做窗口 k 的滑动 max/min,越界按中性值填充(与 PIL 的边缘复制等价)。
|
||||
|
||||
van Herk / Gil-Werman:块内前缀极值 + 块内后缀极值,O(1)/像素。
|
||||
二维 max/min 可分离,故行列两遍与二维滤波逐像素等价,
|
||||
但避开了 PIL rankfilter 的 O(k^2)/像素(k=35 时慢 30 倍以上)。
|
||||
"""
|
||||
a = np.moveaxis(np.asarray(a, dtype=bool).astype(np.uint8) * 255,
|
||||
axis, -1)
|
||||
M, N = a.shape
|
||||
r = k // 2
|
||||
n = N + 2 * r
|
||||
nb = (n + k - 1) // k
|
||||
m = nb * k
|
||||
p = np.full((M, m), 0 if op == "max" else 255, dtype=np.uint8)
|
||||
p[:, r:r + N] = a
|
||||
blk = p.reshape(M, nb, k)
|
||||
if op == "max":
|
||||
fwd = np.maximum.accumulate(blk, axis=-1).reshape(M, m)
|
||||
bwd = np.maximum.accumulate(blk[:, :, ::-1], axis=-1)[:, :, ::-1]
|
||||
bwd = bwd.reshape(M, m)
|
||||
out = np.maximum(bwd[:, :N], fwd[:, k - 1:k - 1 + N])
|
||||
else:
|
||||
fwd = np.minimum.accumulate(blk, axis=-1).reshape(M, m)
|
||||
bwd = np.minimum.accumulate(blk[:, :, ::-1], axis=-1)[:, :, ::-1]
|
||||
bwd = bwd.reshape(M, m)
|
||||
out = np.minimum(bwd[:, :N], fwd[:, k - 1:k - 1 + N])
|
||||
return np.moveaxis(out, -1, axis) > 127
|
||||
|
||||
|
||||
def dilate(mask, size):
|
||||
k = _odd(size)
|
||||
return _slide_reduce(_slide_reduce(mask, k, "max", 1), k, "max", 0)
|
||||
|
||||
|
||||
def erode(mask, size):
|
||||
k = _odd(size)
|
||||
return _slide_reduce(_slide_reduce(mask, k, "min", 1), k, "min", 0)
|
||||
|
||||
|
||||
def closing(mask, size):
|
||||
return erode(dilate(mask, size), size)
|
||||
|
||||
|
||||
def opening(mask, size):
|
||||
return dilate(erode(mask, size), size)
|
||||
|
||||
|
||||
def rgb_to_sv(arr):
|
||||
maxc = arr.max(axis=-1)
|
||||
minc = arr.min(axis=-1)
|
||||
delta = maxc - minc
|
||||
S = np.zeros_like(maxc, dtype=np.float32)
|
||||
np.divide(delta, maxc, out=S, where=maxc > 1e-6)
|
||||
return S.astype(np.float32), maxc.astype(np.float32)
|
||||
|
||||
|
||||
def luminance(arr):
|
||||
return (0.299 * arr[..., 0] + 0.587 * arr[..., 1]
|
||||
+ 0.114 * arr[..., 2]).astype(np.float32)
|
||||
|
||||
|
||||
def box_mean(img, r):
|
||||
"""O(1)/像素的方框均值(积分图),r 为半径。"""
|
||||
img = np.asarray(img, dtype=np.float32)
|
||||
H, W = img.shape
|
||||
pad = np.zeros((H + 1, W + 1), dtype=np.float32)
|
||||
pad[1:, 1:] = img
|
||||
ii = pad.cumsum(0).cumsum(1)
|
||||
ys = np.arange(H)
|
||||
xs = np.arange(W)
|
||||
y0 = np.clip(ys - r, 0, H)
|
||||
y1 = np.clip(ys + r + 1, 0, H)
|
||||
x0 = np.clip(xs - r, 0, W)
|
||||
x1 = np.clip(xs + r + 1, 0, W)
|
||||
# 边界裁剪等价于把积分图向外按边复制 r 行/列,这样四个角全部可用切片取,
|
||||
# 避开 np.ix_ 花式索引的 4 次全图 gather(快 3 倍以上)。
|
||||
k = 2 * r + 1
|
||||
pe = np.pad(ii, ((r, r + 1), (r, r + 1)), mode="edge")
|
||||
S = (pe[k:k + H, k:k + W] - pe[0:H, k:k + W]
|
||||
- pe[k:k + H, 0:W] + pe[0:H, 0:W])
|
||||
cnt = (y1 - y0)[:, None] * (x1 - x0)[None, :]
|
||||
return S / np.maximum(cnt, 1).astype(np.float32)
|
||||
|
||||
|
||||
def gaussian_blur_np(img, sigma):
|
||||
radius = int(np.ceil(sigma * 3))
|
||||
if radius < 1:
|
||||
return img.astype(np.float32)
|
||||
x = np.arange(-radius, radius + 1, dtype=np.float32)
|
||||
k = np.exp(-x * x / (2 * sigma * sigma))
|
||||
k /= k.sum()
|
||||
H, W = img.shape
|
||||
p = np.pad(img, radius, mode="edge")
|
||||
tmp = np.zeros((H, W), dtype=np.float32)
|
||||
for i, kv in enumerate(k):
|
||||
tmp += kv * p[radius:radius + H, i:i + W]
|
||||
p = np.pad(tmp, ((radius, radius), (0, 0)), mode="edge")
|
||||
out = np.zeros((H, W), dtype=np.float32)
|
||||
for i, kv in enumerate(k):
|
||||
out += kv * p[i:i + H, :]
|
||||
return out
|
||||
|
||||
|
||||
def convolve3x3(img, kernel):
|
||||
H, W = img.shape
|
||||
p = np.pad(img, 1, mode="edge")
|
||||
out = np.zeros((H, W), dtype=np.float32)
|
||||
for i in range(3):
|
||||
for j in range(3):
|
||||
kv = kernel[i, j]
|
||||
if kv != 0:
|
||||
out += kv * p[i:i + H, j:j + W]
|
||||
return out
|
||||
|
||||
|
||||
def sobel_magnitude(gray):
|
||||
kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=np.float32)
|
||||
ky = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype=np.float32)
|
||||
gx = convolve3x3(gray, kx)
|
||||
gy = convolve3x3(gray, ky)
|
||||
return np.sqrt(gx * gx + gy * gy)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 连通域 & 填孔
|
||||
|
||||
def downsample_any(mask, factor):
|
||||
if factor <= 1:
|
||||
return mask
|
||||
H, W = mask.shape
|
||||
H2, W2 = H // factor, W // factor
|
||||
return mask[:H2 * factor, :W2 * factor].reshape(
|
||||
H2, factor, W2, factor).any(axis=(1, 3))
|
||||
|
||||
|
||||
def upsample_nearest(mask_small, target_h, target_w):
|
||||
h, w = mask_small.shape
|
||||
yi = np.linspace(0, h - 1, target_h).astype(np.int32)
|
||||
xi = np.linspace(0, w - 1, target_w).astype(np.int32)
|
||||
return mask_small[np.ix_(yi, xi)]
|
||||
|
||||
|
||||
def _row_runs(mask):
|
||||
"""把二值掩膜拆成每行的连续段,返回 (row, start, end),end 不含。"""
|
||||
H, W = mask.shape
|
||||
pm = np.zeros((H, W + 2), dtype=np.int8)
|
||||
pm[:, 1:-1] = mask
|
||||
d = np.diff(pm, axis=1)
|
||||
ry, rs = np.nonzero(d == 1)
|
||||
_, re_ = np.nonzero(d == -1)
|
||||
return ry, rs, re_
|
||||
|
||||
|
||||
def _run_labels(ry, rs, re_, H, eight):
|
||||
"""行程级并查集:4/8 邻接,返回每个行程的连通分量标号。"""
|
||||
n = len(ry)
|
||||
parent = np.arange(n)
|
||||
|
||||
def find(a):
|
||||
while parent[a] != a:
|
||||
parent[a] = parent[parent[a]]
|
||||
a = parent[a]
|
||||
return a
|
||||
|
||||
row_start = np.searchsorted(ry, np.arange(H + 1))
|
||||
for y in range(H - 1):
|
||||
a0, a1 = row_start[y], row_start[y + 1]
|
||||
b0, b1 = row_start[y + 1], row_start[y + 2]
|
||||
i, j = a0, b0
|
||||
while i < a1 and j < b1:
|
||||
s1, e1 = rs[i], re_[i]
|
||||
s2, e2 = rs[j], re_[j]
|
||||
hit = (s2 <= e1 and s1 <= e2) if eight else (s2 < e1 and s1 < e2)
|
||||
if hit:
|
||||
ra, rb = find(i), find(j)
|
||||
if ra != rb:
|
||||
parent[rb] = ra
|
||||
if e1 <= e2:
|
||||
i += 1
|
||||
else:
|
||||
j += 1
|
||||
return np.array([find(i) for i in range(n)], dtype=np.int64)
|
||||
|
||||
|
||||
def fill_holes(mask_small):
|
||||
"""填掉不与画面边界连通的背景(4 邻接),行程 + 并查集实现。"""
|
||||
m = np.asarray(mask_small, dtype=bool)
|
||||
H, W = m.shape
|
||||
bg = ~m
|
||||
if not bg.any():
|
||||
return m.copy()
|
||||
ry, rs, re_ = _row_runs(bg)
|
||||
if len(ry) == 0:
|
||||
return m.copy()
|
||||
lab = _run_labels(ry, rs, re_, H, eight=False)
|
||||
touch = (ry == 0) | (ry == H - 1) | (rs == 0) | (re_ >= W)
|
||||
nlab = int(lab.max()) + 1
|
||||
edge = np.zeros(nlab, dtype=bool)
|
||||
np.logical_or.at(edge, lab, touch)
|
||||
sel = ~edge[lab]
|
||||
out = m.copy()
|
||||
for y, s, e in zip(ry[sel], rs[sel], re_[sel]):
|
||||
out[y, s:e] = True
|
||||
return out
|
||||
|
||||
|
||||
def largest_connected(mask_small):
|
||||
"""取最大 8 邻接连通域(行程 + 并查集,逐像素计数)。"""
|
||||
m = np.asarray(mask_small, dtype=bool)
|
||||
H, W = m.shape
|
||||
if not m.any():
|
||||
return np.zeros_like(m)
|
||||
ry, rs, re_ = _row_runs(m)
|
||||
lab = _run_labels(ry, rs, re_, H, eight=True)
|
||||
w = (re_ - rs).astype(np.int64)
|
||||
nlab = int(lab.max()) + 1
|
||||
size = np.bincount(lab, weights=w, minlength=nlab)
|
||||
best = int(np.argmax(size))
|
||||
sel = lab == best
|
||||
out = np.zeros((H, W), dtype=bool)
|
||||
for y, s, e in zip(ry[sel], rs[sel], re_[sel]):
|
||||
out[y, s:e] = True
|
||||
return out
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 纸面掩膜
|
||||
|
||||
def otsu_threshold(score):
|
||||
hist, bin_edges = np.histogram(score, bins=256, range=(0.0, 1.0))
|
||||
hist = hist.astype(np.float64)
|
||||
total = hist.sum()
|
||||
if total <= 0:
|
||||
return 0.5
|
||||
p = hist / total
|
||||
centers = (bin_edges[:-1] + bin_edges[1:]) * 0.5
|
||||
w0 = np.cumsum(p)
|
||||
w1 = 1.0 - w0
|
||||
m0 = np.cumsum(p * centers)
|
||||
mt = m0[-1]
|
||||
with np.errstate(divide="ignore", invalid="ignore"):
|
||||
mu0 = m0 / np.maximum(w0, 1e-12)
|
||||
mu1 = (mt - m0) / np.maximum(w1, 1e-12)
|
||||
between = w0 * w1 * (mu0 - mu1) ** 2
|
||||
between = np.nan_to_num(between, nan=-1.0)
|
||||
return float(centers[int(np.argmax(between))])
|
||||
|
||||
|
||||
def paper_masks(arr, P):
|
||||
"""返回 (raw, eroded)。
|
||||
|
||||
raw:未腐蚀的纸面掩膜,用于回退几何,避免角点内收。
|
||||
eroded:腐蚀后的掩膜,只用于限定边缘搜索环带。
|
||||
"""
|
||||
d = P["detect"]
|
||||
H, W = arr.shape[:2]
|
||||
S, V = rgb_to_sv(arr)
|
||||
score = (V * (1.0 - S)).astype(np.float32)
|
||||
th = max(otsu_threshold(score), d["paper_otsu_floor"])
|
||||
bright = (score > th) & (V > d["paper_v_min"]) & (S < d["paper_s_max"])
|
||||
|
||||
factor = max(1, min(H, W) // d["paper_mask_factor_div"])
|
||||
bs = downsample_any(bright, factor)
|
||||
|
||||
m = closing(bs, _odd(max(d["paper_close_min"],
|
||||
min(bs.shape) // d["paper_close_div"])))
|
||||
m = fill_holes(m)
|
||||
m = largest_connected(m)
|
||||
m = closing(m, _odd(max(d["paper_close_min"],
|
||||
min(bs.shape) // d["paper_close2_div"])))
|
||||
m = fill_holes(m)
|
||||
|
||||
raw_small = m
|
||||
eroded_small = erode(m, _odd(d["mask_erode_kernel"]))
|
||||
|
||||
if factor > 1:
|
||||
raw = upsample_nearest(raw_small, H, W)
|
||||
eroded = upsample_nearest(eroded_small, H, W)
|
||||
else:
|
||||
raw, eroded = raw_small, eroded_small
|
||||
return raw, eroded
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 边缘图
|
||||
|
||||
def boundary_band(paper, k):
|
||||
k = _odd(k)
|
||||
return dilate(paper, k) & ~erode(paper, k)
|
||||
|
||||
|
||||
def edge_map(arr, P):
|
||||
d = P["detect"]
|
||||
H = arr.shape[0]
|
||||
gray = luminance(arr)
|
||||
sigma = max(1.0, H / float(d["edge_sigma_div"]))
|
||||
blurred = gaussian_blur_np(gray, sigma)
|
||||
mag = sobel_magnitude(blurred)
|
||||
local = box_mean(mag, max(3, H // d["edge_box_div"]))
|
||||
th = np.maximum(d["edge_low"], local * d["edge_local_gain"])
|
||||
return mag > th
|
||||
|
||||
|
||||
def boundary_step_filter(arr, paper, P):
|
||||
"""跨纸边亮度阶跃:比较掩膜内侧/外侧小窗的 V 均值。
|
||||
|
||||
纸边是真实的明暗台阶,木纹/阴影纹理阶跃小。两级窗口:掩膜准确处用
|
||||
r_small,掩膜略有偏移处依靠 r_big 补上。
|
||||
"""
|
||||
d = P["detect"]
|
||||
_, V = rgb_to_sv(arr)
|
||||
p = paper.astype(np.float32)
|
||||
q = 1.0 - p
|
||||
|
||||
def side_means(r):
|
||||
din = box_mean(p, r)
|
||||
dout = box_mean(q, r)
|
||||
vin = box_mean(V * p, r) / np.maximum(din, 1e-6)
|
||||
vout = box_mean(V * q, r) / np.maximum(dout, 1e-6)
|
||||
ok = (din > 0.08) & (dout > 0.08)
|
||||
return np.where(ok, np.abs(vin - vout), 0.0)
|
||||
|
||||
step = np.maximum(side_means(d["step_r_small"]),
|
||||
side_means(d["step_r_big"]))
|
||||
return step > d["step_th"]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 四角提取
|
||||
|
||||
def mask_boundary_points(mask, P):
|
||||
"""掩膜边界像素坐标 (N, 2),顺序 (x, y)。"""
|
||||
inner = erode(mask, _odd(P["detect"]["mask_erode_kernel"]))
|
||||
ys, xs = np.where(mask & ~inner)
|
||||
if len(xs) == 0:
|
||||
return np.zeros((0, 2), dtype=np.float32)
|
||||
return np.stack([xs, ys], axis=1).astype(np.float32)
|
||||
|
||||
|
||||
def _fit_line(pts):
|
||||
"""总体最小二乘拟直线,返回法向量 n 与 n·p = c。"""
|
||||
c0 = pts.mean(axis=0)
|
||||
q = pts - c0
|
||||
cov = (q.T @ q) / max(len(pts) - 1, 1)
|
||||
w, v = np.linalg.eigh(cov)
|
||||
direction = v[:, int(np.argmax(w))]
|
||||
n = np.array([-direction[1], direction[0]], dtype=np.float64)
|
||||
nn = np.linalg.norm(n)
|
||||
if nn < 1e-9:
|
||||
return np.array([0.0, 1.0]), float(c0[1])
|
||||
n = n / nn
|
||||
return n, float(n @ c0)
|
||||
|
||||
|
||||
def _robust_fit(pts, robust_px):
|
||||
n, c = _fit_line(pts)
|
||||
sel = pts
|
||||
for _ in range(2):
|
||||
res = np.abs(sel @ n - c)
|
||||
keep = sel[res <= max(robust_px, 1.0) * 2.5]
|
||||
if len(keep) < 40:
|
||||
break
|
||||
n, c = _fit_line(keep)
|
||||
sel = keep
|
||||
return n, c
|
||||
|
||||
|
||||
def _intersect(n1, c1, n2, c2):
|
||||
A = np.vstack([n1, n2])
|
||||
b = np.array([c1, c2], dtype=np.float64)
|
||||
try:
|
||||
return np.linalg.solve(A, b)
|
||||
except np.linalg.LinAlgError:
|
||||
return None
|
||||
|
||||
|
||||
def _side_line(p0, p1):
|
||||
dv = np.asarray(p1, dtype=np.float64) - np.asarray(p0, dtype=np.float64)
|
||||
L = float(np.linalg.norm(dv))
|
||||
if L < 1e-6:
|
||||
return None
|
||||
u = dv / L
|
||||
n = np.array([-u[1], u[0]], dtype=np.float64)
|
||||
return n, float(n @ np.asarray(p0, dtype=np.float64)), u, L
|
||||
|
||||
|
||||
def detect_quad_corners(paper_raw, points, P):
|
||||
"""从掩膜边界 + 边缘点估四角,返回 (corners, fallback_sides)。"""
|
||||
d = P["detect"]
|
||||
base = mask_boundary_points(paper_raw, P)
|
||||
if len(base) < 40:
|
||||
return [], [0, 1, 2, 3]
|
||||
|
||||
bx, by = base[:, 0], base[:, 1]
|
||||
anchors = np.array([
|
||||
base[int(np.argmin(bx + by))], # 左上
|
||||
base[int(np.argmax(bx - by))], # 右上
|
||||
base[int(np.argmax(bx + by))], # 右下
|
||||
base[int(np.argmin(bx - by))], # 左下
|
||||
], dtype=np.float64)
|
||||
|
||||
init = [_side_line(anchors[i], anchors[(i + 1) % 4]) for i in range(4)]
|
||||
|
||||
# 用整条掩膜边界重拟合每一边(排除两端,避开角点圆角)
|
||||
base_lines = []
|
||||
for i in range(4):
|
||||
sl = init[i]
|
||||
if sl is None:
|
||||
base_lines.append(None)
|
||||
continue
|
||||
n, c, u, L = sl
|
||||
p0 = anchors[i]
|
||||
dist = np.abs(base @ n - c)
|
||||
proj = (base - p0) @ u
|
||||
m = d["boundary_side_margin"]
|
||||
sel = base[(dist <= d["group_tol_px"]) &
|
||||
(proj >= L * m) & (proj <= L * (1.0 - m))]
|
||||
if len(sel) >= 40:
|
||||
n2, c2 = _robust_fit(sel, d["robust_px"])
|
||||
base_lines.append((n2, c2))
|
||||
else:
|
||||
base_lines.append((n, c))
|
||||
|
||||
final_lines = list(base_lines)
|
||||
fallback_sides = [0, 1, 2, 3]
|
||||
|
||||
if points is not None and len(points) >= d["min_edge_points"]:
|
||||
pts = np.asarray(points, dtype=np.float64)
|
||||
dists = []
|
||||
for line in base_lines:
|
||||
if line is None:
|
||||
dists.append(np.full(len(pts), 1e9))
|
||||
else:
|
||||
dists.append(np.abs(pts @ line[0] - line[1]))
|
||||
D = np.stack(dists, axis=1)
|
||||
idx = np.argmin(D, axis=1)
|
||||
dmin = D[np.arange(len(pts)), idx]
|
||||
|
||||
fallback_sides = []
|
||||
for i in range(4):
|
||||
line = base_lines[i]
|
||||
if line is None:
|
||||
fallback_sides.append(i)
|
||||
continue
|
||||
sel = pts[(idx == i) & (dmin <= d["group_tol_px"])]
|
||||
sl = init[i]
|
||||
if sl is not None:
|
||||
u, L = sl[2], sl[3]
|
||||
p0 = anchors[i]
|
||||
m = d["boundary_side_margin"]
|
||||
proj = (sel - p0) @ u
|
||||
sel = sel[(proj >= L * m) & (proj <= L * (1.0 - m))]
|
||||
if len(sel) >= d["min_edge_points"]:
|
||||
n2, c2 = _robust_fit(sel, d["robust_px"])
|
||||
final_lines[i] = (n2, c2)
|
||||
else:
|
||||
fallback_sides.append(i)
|
||||
|
||||
H, W = paper_raw.shape
|
||||
oob = d["corner_oob_frac"]
|
||||
out = []
|
||||
for i in range(4):
|
||||
chosen = None
|
||||
for lines in (final_lines, base_lines):
|
||||
l1, l2 = lines[i - 1], lines[i]
|
||||
if l1 is None or l2 is None:
|
||||
continue
|
||||
det = l1[0][0] * l2[0][1] - l2[0][0] * l1[0][1]
|
||||
if abs(det) < 1e-3:
|
||||
continue
|
||||
p = _intersect(l1[0], l1[1], l2[0], l2[1])
|
||||
if p is None:
|
||||
continue
|
||||
if (-oob * W <= p[0] <= (1.0 + oob) * W and
|
||||
-oob * H <= p[1] <= (1.0 + oob) * H):
|
||||
chosen = (float(p[0]), float(p[1]))
|
||||
break
|
||||
if chosen is None:
|
||||
chosen = (float(anchors[i][0]), float(anchors[i][1]))
|
||||
if i not in fallback_sides:
|
||||
fallback_sides.append(i)
|
||||
out.append(chosen)
|
||||
|
||||
return out, sorted(set(fallback_sides))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 覆盖率
|
||||
|
||||
def border_coverage_exact(edges, paper, tol):
|
||||
"""沿掩膜逐行/逐列的真实边界测边缘覆盖率。"""
|
||||
H, W = paper.shape
|
||||
col_has = paper.any(axis=0)
|
||||
row_has = paper.any(axis=1)
|
||||
top_hit = top_n = 0
|
||||
bot_hit = bot_n = 0
|
||||
for x in np.where(col_has)[0]:
|
||||
ys = np.where(paper[:, x])[0]
|
||||
y0, y1 = ys.min(), ys.max()
|
||||
if edges[max(0, y0 - tol):min(H, y0 + tol + 1), x].any():
|
||||
top_hit += 1
|
||||
top_n += 1
|
||||
if edges[max(0, y1 - tol):min(H, y1 + tol + 1), x].any():
|
||||
bot_hit += 1
|
||||
bot_n += 1
|
||||
left_hit = left_n = 0
|
||||
right_hit = right_n = 0
|
||||
for y in np.where(row_has)[0]:
|
||||
xs = np.where(paper[y, :])[0]
|
||||
x0, x1 = xs.min(), xs.max()
|
||||
if edges[y, max(0, x0 - tol):min(W, x0 + tol + 1)].any():
|
||||
left_hit += 1
|
||||
left_n += 1
|
||||
if edges[y, max(0, x1 - tol):min(W, x1 + tol + 1)].any():
|
||||
right_hit += 1
|
||||
right_n += 1
|
||||
return {
|
||||
"top": top_hit / max(top_n, 1),
|
||||
"bottom": bot_hit / max(bot_n, 1),
|
||||
"left": left_hit / max(left_n, 1),
|
||||
"right": right_hit / max(right_n, 1),
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 对外接口
|
||||
|
||||
def detect(img, P):
|
||||
"""检测纸张四角,返回 (corners_原图坐标, info)。失败返回 ([], info)。"""
|
||||
d = P["detect"]
|
||||
src_w, src_h = img.size
|
||||
scale = min(1.0, d["max_side"] / float(max(src_w, src_h)))
|
||||
if scale < 1.0:
|
||||
pil = img.resize((int(round(src_w * scale)), int(round(src_h * scale))),
|
||||
Image.LANCZOS)
|
||||
else:
|
||||
pil = img
|
||||
arr = np.asarray(pil).astype(np.float32) / 255.0
|
||||
H, W = arr.shape[:2]
|
||||
|
||||
paper_raw, paper_eroded = paper_masks(arr, P)
|
||||
frac = float(paper_raw.sum()) / float(H * W)
|
||||
|
||||
info = {
|
||||
"source_size": [int(src_w), int(src_h)],
|
||||
"processed_size": [int(W), int(H)],
|
||||
"scale": round(scale, 6),
|
||||
"paper_frac": round(frac, 4),
|
||||
"edge_pixels": 0,
|
||||
"coverage": {},
|
||||
"fallback": True,
|
||||
"fallback_sides": [0, 1, 2, 3],
|
||||
}
|
||||
|
||||
if frac < 0.005 or not paper_raw.any():
|
||||
return [], info
|
||||
|
||||
band = boundary_band(paper_eroded,
|
||||
_odd(max(d["band_min"], H // d["band_div"])))
|
||||
edges = edge_map(arr, P)
|
||||
step = boundary_step_filter(arr, paper_eroded, P)
|
||||
cand = closing(edges & band & step,
|
||||
_odd(max(d["cand_close_min"], H // d["cand_close_div"])))
|
||||
cand = opening(cand,
|
||||
_odd(max(d["cand_open_min"], H // d["cand_open_div"])))
|
||||
|
||||
ey, ex = np.nonzero(cand)
|
||||
pts = (np.stack([ex, ey], axis=1).astype(np.float32)
|
||||
if len(ex) >= d["min_edge_points"] else None)
|
||||
|
||||
corners, fallback_sides = detect_quad_corners(paper_raw, pts, P)
|
||||
info["edge_pixels"] = int(cand.sum())
|
||||
info["fallback_sides"] = fallback_sides
|
||||
info["fallback"] = bool(fallback_sides)
|
||||
if not corners:
|
||||
return [], info
|
||||
|
||||
info["coverage"] = {
|
||||
k: float(v) for k, v in border_coverage_exact(
|
||||
cand, paper_eroded,
|
||||
tol=max(d["coverage_tol_min"], H // d["coverage_tol_div"])).items()
|
||||
}
|
||||
src_pts = [(float(x / scale), float(y / scale)) for x, y in corners]
|
||||
return src_pts, info
|
||||
@@ -1,20 +1,11 @@
|
||||
"""PDF 产出的通用质量检验(渲染真实页面后评估)。
|
||||
# -*- coding: utf-8 -*-
|
||||
"""PDF 产出的数值检验:渲染真实页面后评估,不画图、不修改文件。
|
||||
|
||||
检验对象是最终交付的 PDF,不是中间图:把每页按固定 DPI 渲染成位图,
|
||||
在渲染结果上做统计判定,所见即交付物。
|
||||
|
||||
检验项:
|
||||
1. 页数与源图数一致
|
||||
2. 有效内容分辨率(页面渲染后纸面区域的像素尺寸)
|
||||
3. 纸张比例失真(纸面外接框的宽高比,对照页面书写比例)
|
||||
4. 纸面外背景残留(边带亮度显著低于纸面)
|
||||
5. 内容贴边被切(页边存在成片文字级暗像素)
|
||||
|
||||
判据全部基于该页自身像素分布自适应(Otsu / 分位数),不含针对特定文档的常量。
|
||||
|
||||
用法:
|
||||
python3 doc_deskew/check.py 结果.pdf [--expect-pages 2] [--dpi 100]
|
||||
python3 doc_deskew/check.py 结果.pdf --render # 导出渲染图供目视
|
||||
关键修正(相对旧版):
|
||||
- “边缘背景残留 / 内容贴边”改为在 paper_box 裁出的纸面区域上量。
|
||||
旧版量的是 PDF 页面最外圈,而正常出件四周是白边,这两项恒为通过。
|
||||
- 渲染页按文件名数字自然排序,≥10 页不会出现 1,10,2 的顺序。
|
||||
- 渲染后端增加 PyMuPDF(executor 镜像已装 pymupdf),pdftoppm 不在时也能跑。
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
@@ -25,23 +16,26 @@ import sys
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, HERE)
|
||||
from params import P # noqa: E402
|
||||
|
||||
def render(pdf, dpi=100, outdir='.tmp/pdfcheck'):
|
||||
os.makedirs(outdir, exist_ok=True)
|
||||
for f in os.listdir(outdir):
|
||||
if f.startswith('page'):
|
||||
os.remove(os.path.join(outdir, f))
|
||||
base = os.path.join(outdir, 'page')
|
||||
if shutil.which('pdftoppm'):
|
||||
subprocess.run(['pdftoppm', '-r', str(dpi), '-png', pdf, base], check=True)
|
||||
else: # 退路:pypdfium2
|
||||
import pypdfium2 as pdfium
|
||||
doc = pdfium.PdfDocument(pdf)
|
||||
scale = dpi / 72
|
||||
for i in range(len(doc)):
|
||||
doc[i].render(scale=scale).to_pil().save('%s-%d.png' % (base, i + 1))
|
||||
files = sorted(f for f in os.listdir(outdir) if f.startswith('page') and f.endswith('.png'))
|
||||
return [os.path.join(outdir, f) for f in files]
|
||||
|
||||
def _natural_key(path):
|
||||
name = os.path.basename(path)
|
||||
out = []
|
||||
cur = ""
|
||||
for ch in name:
|
||||
if ch.isdigit():
|
||||
cur += ch
|
||||
else:
|
||||
if cur:
|
||||
out.append((1, int(cur)))
|
||||
cur = ""
|
||||
out.append((0, ch.lower()))
|
||||
if cur:
|
||||
out.append((1, int(cur)))
|
||||
return out
|
||||
|
||||
|
||||
def otsu(vals):
|
||||
@@ -55,148 +49,238 @@ def otsu(vals):
|
||||
w1 = total - w0
|
||||
m0 = np.cumsum(hist * centers)
|
||||
mt = m0[-1]
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
with np.errstate(divide="ignore", invalid="ignore"):
|
||||
score = (mt * w0 / total - m0) ** 2 / (w0 * w1 / total ** 2)
|
||||
score = np.nan_to_num(score)
|
||||
return float(centers[int(np.argmax(score))])
|
||||
|
||||
|
||||
def analyse(img):
|
||||
g = np.asarray(img.convert('L')).astype(np.float32)
|
||||
def render(pdf, dpi=100, outdir=None):
|
||||
"""把 PDF 逐页渲染成图片,返回按页序排好的文件列表。
|
||||
|
||||
pdftoppm 默认输出 PPM(无损,且不压缩)。之前用 -png 时 zlib 压缩占了
|
||||
整个自检 2 秒里的 2 秒;改 PPM 后像素完全一致,耗时降到 0.1 秒量级。
|
||||
"""
|
||||
outdir = outdir or P["check"]["tmp_dir"]
|
||||
os.makedirs(outdir, exist_ok=True)
|
||||
for f in os.listdir(outdir):
|
||||
if f.startswith("page") and (f.endswith(".png") or f.endswith(".ppm")):
|
||||
try:
|
||||
os.remove(os.path.join(outdir, f))
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
base = os.path.join(outdir, "page")
|
||||
done = False
|
||||
if shutil.which("pdftoppm"):
|
||||
try:
|
||||
subprocess.run(["pdftoppm", "-r", str(dpi), pdf, base],
|
||||
check=True, stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL)
|
||||
done = True
|
||||
except Exception:
|
||||
done = False
|
||||
|
||||
if not done:
|
||||
try:
|
||||
import fitz # PyMuPDF
|
||||
doc = fitz.open(pdf)
|
||||
scale = float(dpi) / 72.0
|
||||
for i, page in enumerate(doc):
|
||||
pix = page.get_pixmap(matrix=fitz.Matrix(scale, scale))
|
||||
pix.save("%s-%d.png" % (base, i + 1))
|
||||
doc.close()
|
||||
except Exception:
|
||||
import pypdfium2 as pdfium
|
||||
doc = pdfium.PdfDocument(pdf)
|
||||
scale = float(dpi) / 72.0
|
||||
for i in range(len(doc)):
|
||||
doc[i].render(scale=scale).to_pil().save(
|
||||
"%s-%d.png" % (base, i + 1))
|
||||
|
||||
files = [os.path.join(outdir, f) for f in os.listdir(outdir)
|
||||
if f.startswith("page") and (f.endswith(".png")
|
||||
or f.endswith(".ppm"))]
|
||||
files.sort(key=_natural_key)
|
||||
return files
|
||||
|
||||
|
||||
def analyse(img, params=None):
|
||||
"""分析一张渲染页,返回纸面框、亮度、边缘与贴边指标。"""
|
||||
c = (params or P)["check"]
|
||||
g = np.asarray(img.convert("L")).astype(np.float32)
|
||||
h, w = g.shape
|
||||
res = {'w': w, 'h': h, 'ratio': w / h}
|
||||
res = {"w": int(w), "h": int(h), "ratio": float(w) / max(1, h)}
|
||||
|
||||
# 纸面亮度:取全图高分位(纸面是主体亮区)
|
||||
res['paper'] = float(np.percentile(g, 80))
|
||||
# 背景亮度:低分位
|
||||
res['back'] = float(np.percentile(g, 2))
|
||||
# 内容区阈值
|
||||
res['ink'] = min(otsu(g), res['paper'] * 0.8)
|
||||
|
||||
# 纸面内容框:A4 页面上纸面之外是纯白留白,故"非纯白"区域即实际纸面
|
||||
# 用相对亮度判定,避免把扫描白纸的轻微灰底误判成内容
|
||||
flat = float(np.percentile(g, 99.5))
|
||||
nonwhite = g < (flat - 8)
|
||||
# 先用“非纯白”找纸面框:PDF 页面四周是白边,纸面被提亮到 ~245
|
||||
flat = float(np.percentile(g, c["flat_pct"]))
|
||||
nonwhite = g < (flat - c["nonwhite_delta"])
|
||||
row_frac = nonwhite.mean(axis=1)
|
||||
col_frac = nonwhite.mean(axis=0)
|
||||
# 取占比超过 5% 的连续范围作为纸面主体
|
||||
rows = np.where(row_frac > 0.05)[0]
|
||||
cols = np.where(col_frac > 0.05)[0]
|
||||
rows = np.where(row_frac > c["paper_row_frac"])[0]
|
||||
cols = np.where(col_frac > c["paper_row_frac"])[0]
|
||||
box = None
|
||||
if rows.size > 4 and cols.size > 4:
|
||||
res['paper_box'] = (int(cols[0]), int(rows[0]), int(cols[-1]), int(rows[-1]))
|
||||
pw = cols[-1] - cols[0] + 1
|
||||
ph = rows[-1] - rows[0] + 1
|
||||
res['paper_ratio'] = pw / ph
|
||||
res['paper_px'] = (int(pw), int(ph))
|
||||
box = (int(cols[0]), int(rows[0]), int(cols[-1]), int(rows[-1]))
|
||||
res["paper_box"] = box
|
||||
res["box_fallback"] = box is None
|
||||
|
||||
if box:
|
||||
x0, y0, x1, y1 = box
|
||||
gp = g[y0:y1 + 1, x0:x1 + 1]
|
||||
else:
|
||||
res['paper_box'] = None
|
||||
res['paper_ratio'] = res['ratio']
|
||||
res['paper_px'] = (w, h)
|
||||
gp = g
|
||||
ph, pw = gp.shape
|
||||
|
||||
# 边缘背景残留:四周窄带的平均亮度明显低于纸面亮度
|
||||
# 边带宽度按比例取,过窄会漏判
|
||||
b = max(4, int(min(w, h) * 0.012))
|
||||
# 透视采样在纸边常带 1~2px 背景毛边,先内缩再量边缘;
|
||||
# 否则会被误判成“边缘背景残留”或“内容贴边”。
|
||||
inset = max(2, int(round(min(pw, ph) * c["edge_inset_frac"])))
|
||||
if pw > 2 * inset + 4 and ph > 2 * inset + 4:
|
||||
gc = gp[inset:-inset, inset:-inset]
|
||||
else:
|
||||
gc = gp
|
||||
gh, gw = gc.shape
|
||||
|
||||
res["paper"] = float(np.percentile(gc, c["paper_pct"]))
|
||||
res["back"] = float(np.percentile(gc, c["back_pct"]))
|
||||
res["ink"] = min(otsu(gc), res["paper"] * c["ink_ratio"])
|
||||
res["paper_ratio"] = float(pw) / max(1, ph)
|
||||
res["paper_px"] = (int(pw), int(ph))
|
||||
|
||||
# 边缘背景残留:在纸面区域的最外圈量,而不是 PDF 白边
|
||||
b = max(4, int(min(gw, gh) * c["border_band_frac"]))
|
||||
border = {
|
||||
'top': float(g[:b, :].mean()), 'bottom': float(g[-b:, :].mean()),
|
||||
'left': float(g[:, :b].mean()), 'right': float(g[:, -b:].mean()),
|
||||
"top": float(gc[:b, :].mean()),
|
||||
"bottom": float(gc[-b:, :].mean()),
|
||||
"left": float(gc[:, :b].mean()),
|
||||
"right": float(gc[:, -b:].mean()),
|
||||
}
|
||||
res['border'] = border
|
||||
res['border_bad'] = [k for k, v in border.items() if v < res['paper'] * 0.80]
|
||||
# 另一路判据:边带内是否存在"整行整列偏暗"的条带(纸外背景)
|
||||
edge_dark = {}
|
||||
for k, band in (('top', g[:b, :]), ('bottom', g[-b:, :]),
|
||||
('left', g[:, :b]), ('right', g[:, -b:])):
|
||||
edge_dark[k] = float((band < res['paper'] * 0.6).mean())
|
||||
res['edge_dark'] = edge_dark
|
||||
res["border"] = border
|
||||
res["border_bad"] = [k for k, v in border.items()
|
||||
if v < res["paper"] * c["border_ratio"]]
|
||||
|
||||
# 内容贴边:最外窄带内成片文字级暗像素 <=> 内容被切
|
||||
m = max(2, int(min(w, h) * 0.003))
|
||||
edge_dark = {}
|
||||
for k, band in (("top", gc[:b, :]), ("bottom", gc[-b:, :]),
|
||||
("left", gc[:, :b]), ("right", gc[:, -b:])):
|
||||
edge_dark[k] = float(
|
||||
(band < res["paper"] * c["edge_dark_ratio"]).mean())
|
||||
res["edge_dark"] = edge_dark
|
||||
|
||||
# 内容贴边:纸面最外窄带内成片文字级暗像素
|
||||
m = max(2, int(min(gw, gh) * c["cut_band_frac"]))
|
||||
cut = []
|
||||
for k, band in (('top', g[:m, :]), ('bottom', g[-m:, :]),
|
||||
('left', g[:, :m]), ('right', g[:, -m:])):
|
||||
frac = float((band < res['ink']).mean())
|
||||
if frac > 0.05:
|
||||
for k, band in (("top", gc[:m, :]), ("bottom", gc[-m:, :]),
|
||||
("left", gc[:, :m]), ("right", gc[:, -m:])):
|
||||
frac = float((band < res["ink"]).mean())
|
||||
if (frac > c["cut_frac"] and
|
||||
float(band.mean()) < res["paper"] * c["cut_mean_ratio"]):
|
||||
cut.append((k, frac))
|
||||
res['cut'] = cut
|
||||
res["cut"] = cut
|
||||
return res
|
||||
|
||||
|
||||
def inspect_pdf(pdf, expect_pages=None, dpi=None, params=None):
|
||||
"""渲染 + 逐页检验,返回结构化结果;渲染失败时 ok=None。"""
|
||||
p = params or P
|
||||
c = p["check"]
|
||||
dpi = int(dpi or c["dpi"])
|
||||
outdir = os.path.join(os.path.dirname(os.path.abspath(pdf)), c["tmp_dir"])
|
||||
try:
|
||||
files = render(pdf, dpi, outdir)
|
||||
except Exception as e:
|
||||
return {"ok": None, "error": str(e), "pages": [], "page_count": None}
|
||||
|
||||
actual = len(files)
|
||||
count_ok = (expect_pages is None) or (actual == expect_pages)
|
||||
out = {
|
||||
"ok": bool(count_ok),
|
||||
"page_count": {"expected": expect_pages, "actual": actual,
|
||||
"ok": bool(count_ok)},
|
||||
"pages": [],
|
||||
}
|
||||
|
||||
# 阈值以 150dpi 出件为基准;check 用的是较低的渲染 dpi,要同时按出件 dpi
|
||||
# 折算,否则选 300dpi 会白白放宽、选 72dpi 会误报“分辨率不足”。
|
||||
render_dpi = float(p["render"]["dpi"])
|
||||
min_short = c["min_short"] * dpi / render_dpi
|
||||
try:
|
||||
for i, path in enumerate(files, 1):
|
||||
with Image.open(path) as im:
|
||||
r = analyse(im, p)
|
||||
pw, ph = r["paper_px"]
|
||||
short = min(pw, ph)
|
||||
short_ok = short >= min_short
|
||||
border_ok = ((not r["border_bad"]) and
|
||||
all(v <= c["edge_dark_frac"]
|
||||
for v in r["edge_dark"].values()))
|
||||
cut_ok = not r["cut"]
|
||||
dev = (abs(r["paper_ratio"] - r["ratio"]) / r["ratio"]
|
||||
if r["ratio"] else 0.0)
|
||||
|
||||
item = {
|
||||
"index": i,
|
||||
"paper_px": [int(pw), int(ph)],
|
||||
"short_side_px": int(short),
|
||||
"short_side_ok": bool(short_ok),
|
||||
"paper_ratio": round(float(r["paper_ratio"]), 4),
|
||||
"page_ratio": round(float(r["ratio"]), 4),
|
||||
"ratio_dev_pct": round(float(dev * 100), 1),
|
||||
"border_ok": bool(border_ok),
|
||||
"cut_ok": bool(cut_ok),
|
||||
"border": {k: round(float(v), 1) for k, v in r["border"].items()},
|
||||
"edge_dark": {k: round(float(v), 3)
|
||||
for k, v in r["edge_dark"].items()},
|
||||
"cut": [[k, round(float(v), 3)] for k, v in r["cut"]],
|
||||
"box_fallback": bool(r["box_fallback"]),
|
||||
"paper_luma": round(float(r["paper"]), 1),
|
||||
"back_luma": round(float(r["back"]), 1),
|
||||
}
|
||||
out["pages"].append(item)
|
||||
if not (short_ok and border_ok and cut_ok):
|
||||
out["ok"] = False
|
||||
finally:
|
||||
# 渲染图只是自检的中间产物:PPM 未压缩,100dpi 的 A4 一页约 3MB,
|
||||
# 检验完立刻删(量已经记录在返回值里),不要留在会话目录占空间。
|
||||
shutil.rmtree(outdir, ignore_errors=True)
|
||||
return out
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument('pdf')
|
||||
ap.add_argument('--expect-pages', type=int, default=None)
|
||||
ap.add_argument('--dpi', type=int, default=100)
|
||||
ap.add_argument('--min-short', type=int, default=800)
|
||||
ap.add_argument('--ratio-tol', type=float, default=0.08)
|
||||
ap.add_argument('--render', action='store_true')
|
||||
ap = argparse.ArgumentParser(description="复检已有 PDF(只渲染检验)")
|
||||
ap.add_argument("pdf")
|
||||
ap.add_argument("--expect-pages", type=int, default=None)
|
||||
ap.add_argument("--dpi", type=int, default=P["check"]["dpi"])
|
||||
args = ap.parse_args()
|
||||
|
||||
if not os.path.exists(args.pdf):
|
||||
raise SystemExit('找不到 PDF: %s' % args.pdf)
|
||||
raise SystemExit("找不到 PDF:%s" % args.pdf)
|
||||
|
||||
files = render(args.pdf, args.dpi)
|
||||
print('PDF: %s' % args.pdf)
|
||||
print('文件大小: %.0f KB' % (os.path.getsize(args.pdf) / 1024))
|
||||
print('页数: %d' % len(files))
|
||||
|
||||
ok = True
|
||||
if args.expect_pages is not None:
|
||||
good = len(files) == args.expect_pages
|
||||
ok &= good
|
||||
print(' [%s] 页数与源图一致(期望 %d,实际 %d)'
|
||||
% ('OK' if good else 'XX', args.expect_pages, len(files)))
|
||||
|
||||
for i, f in enumerate(files, 1):
|
||||
img = Image.open(f)
|
||||
r = analyse(img)
|
||||
print('\n第 %d 页 渲染 %dx%d px @%ddpi' % (i, r['w'], r['h'], args.dpi))
|
||||
|
||||
# 纸张实际像素尺寸(按渲染图里纸面外接框估算,换算回原分辨率)
|
||||
if r['paper_box']:
|
||||
x0, y0, x1, y1 = r['paper_box']
|
||||
scale = 1.0 # 渲染已是实际像素
|
||||
short = min(x1 - x0, y1 - y0)
|
||||
good = short >= args.min_short * args.dpi / 150.0
|
||||
ok &= good
|
||||
print(' [%s] 纸面短边 %d px(下限 %d)'
|
||||
% ('OK' if good else 'XX', short,
|
||||
int(args.min_short * args.dpi / 150.0)))
|
||||
|
||||
# 纸张比例本身不是硬性标准:非 A4 材料(Letter / 16K / 票据)
|
||||
# 塞进 A4 页面时必然有留白,纸张比例 != 页面比例是正常的。
|
||||
# 这里只做"提示",判错交给内容贴边与背景残留两项。
|
||||
dev = abs(r['paper_ratio'] - r['ratio']) / r['ratio'] * 100
|
||||
note = '' if dev <= args.ratio_tol else '(留白 %.1f%%,非 A4 纸张属正常)' % dev
|
||||
print(' [--] 纸张比例 %.3f(页面 %.3f)%s'
|
||||
% (r['paper_ratio'], r['ratio'], note))
|
||||
else:
|
||||
print(' [XX] 未能识别纸面区域')
|
||||
ok = False
|
||||
|
||||
bad_dark = [k for k, v in r.get('edge_dark', {}).items() if v > 0.35]
|
||||
good = (not r['border_bad']) and (not bad_dark)
|
||||
ok &= good
|
||||
detail = ', '.join('%s=%.0f' % (k, v) for k, v in r['border'].items())
|
||||
print(' [%s] 边缘背景残留 %s(纸面 %.0f / 背景 %.0f)'
|
||||
% ('OK' if good else 'XX',
|
||||
'无' if good else '、'.join(r['border_bad']), r['paper'], r['back']))
|
||||
if not good:
|
||||
print(' 边带亮度: %s' % detail)
|
||||
if bad_dark:
|
||||
print(' 偏高暗占比: %s'
|
||||
% '、'.join('%s %.0f%%' % (k, r['edge_dark'][k] * 100) for k in bad_dark))
|
||||
|
||||
good = not r['cut']
|
||||
ok &= good
|
||||
print(' [%s] 内容贴边 %s'
|
||||
% ('OK' if good else 'XX',
|
||||
'、'.join('%s %.1f%%' % (k, v * 100) for k, v in r['cut']) if r['cut'] else '无'))
|
||||
|
||||
print('\n总判定:%s' % ('全部通过' if ok else '存在不达标项'))
|
||||
if args.render:
|
||||
print('渲染图: .tmp/pdfcheck/')
|
||||
return 0 if ok else 1
|
||||
r = inspect_pdf(args.pdf, args.expect_pages, args.dpi)
|
||||
print("PDF: %s" % args.pdf)
|
||||
print("文件大小: %.0f KB" % (os.path.getsize(args.pdf) / 1024.0))
|
||||
if r.get("ok") is None:
|
||||
print("渲染失败:%s" % r.get("error"))
|
||||
return 1
|
||||
print("页数: %d" % r["page_count"]["actual"])
|
||||
for item in r["pages"]:
|
||||
print("\n第 %d 页 纸面 %dx%d" % (
|
||||
item["index"], item["paper_px"][0], item["paper_px"][1]))
|
||||
print(" [%s] 纸面短边 %d px"
|
||||
% ("OK" if item["short_side_ok"] else "XX",
|
||||
item["short_side_px"]))
|
||||
print(" [%s] 边缘背景残留 %s"
|
||||
% ("OK" if item["border_ok"] else "XX",
|
||||
"无" if item["border_ok"] else "有"))
|
||||
print(" [%s] 内容贴边 %s"
|
||||
% ("OK" if item["cut_ok"] else "XX",
|
||||
"无" if item["cut_ok"] else
|
||||
"、".join("%s %.1f%%" % (k, v * 100)
|
||||
for k, v in item["cut"])))
|
||||
print(" [--] 纸面比例 %.3f(页面 %.3f)"
|
||||
% (item["paper_ratio"], item["page_ratio"]))
|
||||
print("\n总判定:%s" % ("全部通过" if r["ok"] else "存在不达标项"))
|
||||
return 0 if r["ok"] else 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""出页与合成:批次方向、目标尺寸、页面拼版、PDF 保存。
|
||||
|
||||
坐标与尺寸约定:
|
||||
- 所有 quad 都是“转正后原图”坐标;
|
||||
- 目标尺寸把 PDF 画布内容区(页面尺寸 - 四周留白)的一条边填满,可放大低分辨率照片,
|
||||
避免同一批页面因原图像素多少而大小不一;
|
||||
- 不做中间 PNG,页面图直接进内存后一次性写 PDF。
|
||||
"""
|
||||
import os
|
||||
import statistics
|
||||
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
from homography import brighten, warp_perspective
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 纸张
|
||||
|
||||
def resolve_paper(paper, P):
|
||||
"""把 paper 参数解析成 {name, canvas_mm, ratio}。
|
||||
|
||||
auto 不指定标准纸面比例;画布用 A4,比例由调用方按批次中位数决定
|
||||
a4/letter… 画布和纸面比例都用该标准纸
|
||||
数字 画布用 A4,纸面比例用该数字(宽/高)
|
||||
"""
|
||||
r = P["render"]
|
||||
name = str(paper or r["default_paper"]).strip().lower()
|
||||
a4 = r["papers_mm"]["a4"]
|
||||
if name in ("", "auto"):
|
||||
return {"name": "auto", "canvas_mm": a4, "ratio": None}
|
||||
if name in r["papers_mm"]:
|
||||
w_mm, h_mm = r["papers_mm"][name]
|
||||
return {"name": name, "canvas_mm": (w_mm, h_mm),
|
||||
"ratio": float(w_mm) / float(h_mm)}
|
||||
try:
|
||||
ratio = float(name)
|
||||
except ValueError:
|
||||
raise ValueError("未知纸张:%s(可选 %s / 数字宽高比 / auto)"
|
||||
% (paper, "/".join(r["papers_mm"])))
|
||||
if not (0.02 < ratio < 50.0):
|
||||
raise ValueError("纸张比例超出合理范围:%s" % paper)
|
||||
return {"name": "custom", "canvas_mm": a4, "ratio": ratio}
|
||||
|
||||
|
||||
def canvas_px(paper, orientation, P):
|
||||
"""按 dpi 换算画布像素;orientation=landscape 时交换宽高。"""
|
||||
w_mm, h_mm = paper["canvas_mm"]
|
||||
dpi = float(P["render"]["dpi"])
|
||||
cw = int(round(w_mm / 25.4 * dpi))
|
||||
ch = int(round(h_mm / 25.4 * dpi))
|
||||
if orientation == "landscape":
|
||||
cw, ch = ch, cw
|
||||
return max(1, cw), max(1, ch)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 方向
|
||||
|
||||
def aspect_of(quad):
|
||||
"""四边形在屏幕坐标下的自然宽高(像素)。"""
|
||||
q = np.asarray(quad, dtype=np.float64)
|
||||
w = (np.linalg.norm(q[1] - q[0]) + np.linalg.norm(q[2] - q[3])) / 2.0
|
||||
h = (np.linalg.norm(q[3] - q[0]) + np.linalg.norm(q[2] - q[1])) / 2.0
|
||||
return max(float(w), 1e-6), max(float(h), 1e-6)
|
||||
|
||||
|
||||
def classify(aspect, deadband):
|
||||
if aspect > 1.0 + deadband:
|
||||
return "landscape"
|
||||
if aspect < 1.0 - deadband:
|
||||
return "portrait"
|
||||
return "square"
|
||||
|
||||
|
||||
def decide_orientation(aspects, mode, P):
|
||||
"""返回 (batch_orientation, source, median_aspect)。"""
|
||||
dead = P["gate"]["aspect_deadband"]
|
||||
median = float(statistics.median(aspects)) if aspects else 1.0
|
||||
if mode == "none":
|
||||
return "mixed", "none", median
|
||||
if mode in ("portrait", "landscape"):
|
||||
return mode, "forced", median
|
||||
classes = [classify(a, dead) for a in aspects]
|
||||
n_land = sum(1 for c in classes if c == "landscape")
|
||||
n_port = sum(1 for c in classes if c == "portrait")
|
||||
if n_land > n_port:
|
||||
return "landscape", "majority", median
|
||||
if n_port > n_land:
|
||||
return "portrait", "majority", median
|
||||
if n_land == 0 and n_port == 0:
|
||||
return "portrait", "default", median
|
||||
return "portrait", "tie", median
|
||||
|
||||
|
||||
def rotate_quad_cw(quad):
|
||||
"""把角点序列旋转一位,等价于把页面顺时针转 90°。"""
|
||||
q = list(quad)
|
||||
return [q[3], q[0], q[1], q[2]]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 目标尺寸
|
||||
|
||||
def target_size(quad, paper, page_orientation, P, out_ratio=None):
|
||||
"""返回 (target_w, target_h, (canvas_w, canvas_h))。
|
||||
|
||||
out_ratio 是 auto 模式下按批次中位比例算出的输出宽高比;None 表示按本页
|
||||
自然比例。两种分支都把内容区的一条边填满:低分辨率照片会被放大,从而
|
||||
避免同一批页面因原图尺寸/分辨率不同而大小不一。
|
||||
"""
|
||||
w_src, h_src = aspect_of(quad)
|
||||
cw, ch = canvas_px(paper, page_orientation, P)
|
||||
margin = int(P["render"]["margin_px"])
|
||||
cont_w = max(1, cw - 2 * margin)
|
||||
cont_h = max(1, ch - 2 * margin)
|
||||
|
||||
if paper["ratio"] is not None:
|
||||
# 强制纸面比例:宽高比纠正透视压缩。
|
||||
r = (paper["ratio"] if page_orientation == "portrait"
|
||||
else 1.0 / paper["ratio"])
|
||||
else:
|
||||
r = out_ratio
|
||||
|
||||
if r is not None:
|
||||
tw = float(cont_w)
|
||||
th = tw / r
|
||||
if th > cont_h:
|
||||
th = float(cont_h)
|
||||
tw = th * r
|
||||
else:
|
||||
# auto 且未形成批次比例:按四边形自然比例填满内容区(可放大)。
|
||||
s = min(float(cont_w) / w_src, float(cont_h) / h_src)
|
||||
tw, th = w_src * s, h_src * s
|
||||
|
||||
return max(1, int(round(tw))), max(1, int(round(th))), (cw, ch)
|
||||
|
||||
|
||||
def warp_page(img, quad, size, P):
|
||||
"""透视校正 + 按 P["render"]["enhance"] 调色。"""
|
||||
r = P["render"]
|
||||
page = warp_perspective(img, quad, size,
|
||||
chunk_rows=r["warp_chunk_rows"])
|
||||
spec = P["enhance"].get(r.get("enhance") or r["default_enhance"])
|
||||
return tone(page, spec, r["brighten_target"], r["brighten_pct"])
|
||||
|
||||
|
||||
def tone(img, spec, target=245, pct=97):
|
||||
"""按调色档做整体映射;spec=None 表示原样返回。
|
||||
|
||||
只做“分位数 -> 目标亮度”的等比缩放(加可选灰度),不碰像素间的
|
||||
局部关系,所以同一张图重跑结果一致,也不会重绘文字边缘。
|
||||
"""
|
||||
if not spec:
|
||||
return img
|
||||
if spec.get("gray"):
|
||||
img = img.convert("L").convert("RGB")
|
||||
return brighten(img, spec.get("target", target), spec.get("pct", pct))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- PDF
|
||||
|
||||
def build_pdf(pages, out_pdf, canvases, P):
|
||||
"""把已校正的页面图居中放进各自画布,按顺序写 PDF。
|
||||
|
||||
pages / canvases 一一对应;画布可能因 orientation=none 而不同。
|
||||
"""
|
||||
r = P["render"]
|
||||
margin = int(r["margin_px"])
|
||||
dpi = int(r["dpi"])
|
||||
composed = []
|
||||
meta = []
|
||||
|
||||
for img, (cw, ch) in zip(pages, canvases):
|
||||
cw, ch = max(1, int(cw)), max(1, int(ch))
|
||||
cont_w = max(1, cw - 2 * margin)
|
||||
cont_h = max(1, ch - 2 * margin)
|
||||
s = min(1.0, float(cont_w) / img.width, float(cont_h) / img.height)
|
||||
if s < 1.0:
|
||||
nw = max(1, int(round(img.width * s)))
|
||||
nh = max(1, int(round(img.height * s)))
|
||||
img = img.resize((nw, nh), Image.LANCZOS)
|
||||
else:
|
||||
nw, nh = img.width, img.height
|
||||
canvas = Image.new("RGB", (cw, ch), "white")
|
||||
canvas.paste(img, ((cw - nw) // 2, (ch - nh) // 2))
|
||||
composed.append(canvas)
|
||||
meta.append({"canvas": [cw, ch], "pasted": [nw, nh]})
|
||||
|
||||
if not composed:
|
||||
raise ValueError("没有可合成的页面")
|
||||
|
||||
out_dir = os.path.dirname(os.path.abspath(out_pdf))
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
tmp = out_pdf + ".part.pdf"
|
||||
try:
|
||||
composed[0].save(tmp, "PDF", resolution=dpi, save_all=True,
|
||||
append_images=composed[1:])
|
||||
os.replace(tmp, out_pdf)
|
||||
finally:
|
||||
if os.path.exists(tmp):
|
||||
try:
|
||||
os.remove(tmp)
|
||||
except OSError:
|
||||
pass
|
||||
return meta
|
||||
@@ -1,45 +0,0 @@
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import sys
|
||||
|
||||
FONT = '/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc'
|
||||
|
||||
# 网格配色:100px 红线(主线,带坐标刻度)+ 50px 蓝线(辅助),两者同宽,
|
||||
# 只靠颜色分主次;线太细时缩略图上看不见,所以不靠粗细区分。
|
||||
COLOR_MAJOR = (255, 0, 0, 110)
|
||||
COLOR_MINOR = (0, 96, 255, 70)
|
||||
LINE_WIDTH = 2
|
||||
|
||||
|
||||
def draw_grid_on(im, step=50, major=100):
|
||||
"""在原图上叠加坐标网格,返回同一个 Image 对象(不落盘)。"""
|
||||
W, H = im.size
|
||||
d = ImageDraw.Draw(im, 'RGBA')
|
||||
try:
|
||||
f = ImageFont.truetype(FONT, 18)
|
||||
except Exception:
|
||||
f = ImageFont.load_default()
|
||||
for x in range(0, W, step):
|
||||
d.line([(x, 0), (x, H)],
|
||||
fill=COLOR_MAJOR if x % major == 0 else COLOR_MINOR,
|
||||
width=LINE_WIDTH)
|
||||
for y in range(0, H, step):
|
||||
d.line([(0, y), (W, y)],
|
||||
fill=COLOR_MAJOR if y % major == 0 else COLOR_MINOR,
|
||||
width=LINE_WIDTH)
|
||||
# 坐标数字:每 100px 在顶部和左侧标注
|
||||
for x in range(0, W, major):
|
||||
d.rectangle([x + 1, 1, x + 38, 21], fill=(255, 255, 255, 220))
|
||||
d.text((x + 3, 1), str(x), fill=(200, 0, 0), font=f)
|
||||
for y in range(0, H, major):
|
||||
d.rectangle([1, y + 1, 46, y + 21], fill=(255, 255, 255, 220))
|
||||
d.text((2, y + 1), str(y), fill=(0, 0, 200), font=f)
|
||||
return im
|
||||
|
||||
|
||||
def draw_grid(src, dst, step=50, major=100):
|
||||
im = draw_grid_on(Image.open(src).convert('RGB'), step, major)
|
||||
im.save(dst)
|
||||
print('written', dst, im.size)
|
||||
|
||||
if __name__ == '__main__':
|
||||
draw_grid(sys.argv[1], sys.argv[2])
|
||||
@@ -1,10 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""纯 numpy 单应变换:等价于 cv2.warpPerspective,只依赖 Pillow + numpy。
|
||||
|
||||
用法:
|
||||
python homography.py in.jpg out.png --quad x1,y1 x2,y2 x3,y3 x4,y4
|
||||
四角顺序:左上、右上、右下、左下。不给 --quad 时按整幅图重采样。
|
||||
与旧实现的区别:
|
||||
- 按行分块采样,峰值内存从“整页 × 多份 float64”降到几十 MB;
|
||||
- 只接受 PIL 图,坐标/方向由调用方统一(exif_transpose 已在入口做过);
|
||||
- 不再提供 CLI,出件统一走技能入口。
|
||||
"""
|
||||
import argparse
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
@@ -18,64 +19,71 @@ def _solve_h(src, dst):
|
||||
return vt[-1].reshape(3, 3)
|
||||
|
||||
|
||||
def warp_perspective(img, quad, out_size=None, fill=(255, 255, 255)):
|
||||
arr = np.asarray(img.convert('RGB') if isinstance(img, Image.Image) else img)
|
||||
def warp_perspective(img, quad, out_size=None, fill=(255, 255, 255),
|
||||
chunk_rows=256):
|
||||
"""把 quad 指定的四边形透视校正到 out_size。
|
||||
|
||||
quad 顺序:左上、右上、右下、左下(屏幕坐标)。
|
||||
out_size=None 时按四边形自然边长输出(保留照片原始比例)。
|
||||
"""
|
||||
arr = np.asarray(img if img.mode == "RGB" else img.convert("RGB"))
|
||||
src_h, src_w = arr.shape[:2]
|
||||
quad = np.asarray(quad, dtype=np.float64)
|
||||
tl, tr, br, bl = quad
|
||||
|
||||
if out_size is None:
|
||||
w = int(round(max(np.linalg.norm(tr - tl), np.linalg.norm(br - bl))))
|
||||
h = int(round(max(np.linalg.norm(bl - tl), np.linalg.norm(br - tr))))
|
||||
out_size = (w, h)
|
||||
out_w, out_h = out_size
|
||||
dst = np.array([[0, 0], [out_w - 1, 0], [out_w - 1, out_h - 1], [0, out_h - 1]],
|
||||
out_w = max(1, int(out_size[0]))
|
||||
out_h = max(1, int(out_size[1]))
|
||||
|
||||
dst = np.array([[0, 0], [out_w - 1, 0],
|
||||
[out_w - 1, out_h - 1], [0, out_h - 1]],
|
||||
dtype=np.float64)
|
||||
Hinv = np.linalg.inv(_solve_h(quad, dst))
|
||||
xs, ys = np.meshgrid(np.arange(out_w), np.arange(out_h))
|
||||
dst_h = np.stack([xs.ravel(), ys.ravel(), np.ones(xs.size)]).astype(np.float64)
|
||||
src_h = Hinv @ dst_h
|
||||
sx = src_h[0] / src_h[2]
|
||||
sy = src_h[1] / src_h[2]
|
||||
h, w = arr.shape[:2]
|
||||
valid = (sx >= 0) & (sx <= w - 1) & (sy >= 0) & (sy <= h - 1)
|
||||
sx = np.clip(sx, 0, w - 1); sy = np.clip(sy, 0, h - 1)
|
||||
x0 = np.floor(sx).astype(np.int32); x1 = np.minimum(x0 + 1, w - 1)
|
||||
y0 = np.floor(sy).astype(np.int32); y1 = np.minimum(y0 + 1, h - 1)
|
||||
fx = (sx - x0)[:, None]; fy = (sy - y0)[:, None]
|
||||
a = arr[y0, x0].astype(np.float64); b = arr[y0, x1].astype(np.float64)
|
||||
c = arr[y1, x0].astype(np.float64); d = arr[y1, x1].astype(np.float64)
|
||||
out = (a * (1 - fx) * (1 - fy) + b * fx * (1 - fy) +
|
||||
c * (1 - fx) * fy + d * fx * fy)
|
||||
out[~valid] = fill
|
||||
out = np.clip(out, 0, 255).astype(np.uint8).reshape(out_h, out_w, -1)
|
||||
|
||||
out = np.empty((out_h, out_w, 3), dtype=np.uint8)
|
||||
step = max(1, int(chunk_rows))
|
||||
for y0 in range(0, out_h, step):
|
||||
y1 = min(y0 + step, out_h)
|
||||
ys, xs = np.mgrid[y0:y1, 0:out_w]
|
||||
dst_h = np.stack([xs.ravel(), ys.ravel(),
|
||||
np.ones(xs.size)], axis=0).astype(np.float64)
|
||||
sp = Hinv @ dst_h
|
||||
den = sp[2]
|
||||
den = np.where(np.abs(den) < 1e-9, 1e-9, den)
|
||||
sx = sp[0] / den
|
||||
sy = sp[1] / den
|
||||
|
||||
valid = (sx >= 0) & (sx <= src_w - 1) & (sy >= 0) & (sy <= src_h - 1)
|
||||
sx = np.clip(sx, 0, src_w - 1)
|
||||
sy = np.clip(sy, 0, src_h - 1)
|
||||
|
||||
x0 = np.floor(sx).astype(np.int32)
|
||||
x1 = np.minimum(x0 + 1, src_w - 1)
|
||||
y0i = np.floor(sy).astype(np.int32)
|
||||
y1i = np.minimum(y0i + 1, src_h - 1)
|
||||
fx = (sx - x0)[:, None]
|
||||
fy = (sy - y0i)[:, None]
|
||||
|
||||
a = arr[y0i, x0].astype(np.float32)
|
||||
b = arr[y0i, x1].astype(np.float32)
|
||||
c = arr[y1i, x0].astype(np.float32)
|
||||
d = arr[y1i, x1].astype(np.float32)
|
||||
block = (a * (1 - fx) * (1 - fy) + b * fx * (1 - fy)
|
||||
+ c * (1 - fx) * fy + d * fx * fy)
|
||||
block[~valid] = fill
|
||||
out[y0:y1] = np.clip(block, 0, 255).astype(np.uint8).reshape(
|
||||
y1 - y0, out_w, 3)
|
||||
|
||||
return Image.fromarray(out)
|
||||
|
||||
|
||||
def trim(img, px=3):
|
||||
w, h = img.size
|
||||
px = max(0, min(px, (min(w, h) - 1) // 2))
|
||||
return img.crop((px, px, w - px, h - px))
|
||||
|
||||
|
||||
def brighten(img, target=245, pct=97):
|
||||
arr = np.asarray(img.convert('RGB')).astype(np.float32)
|
||||
hi = np.percentile(arr, pct)
|
||||
if hi > 1:
|
||||
arr = np.clip(arr * (target / hi), 0, 255)
|
||||
"""全局等比提亮:把 pct 分位映射到 target,保持相对对比度。"""
|
||||
arr = np.asarray(img if img.mode == "RGB" else img.convert("RGB")).astype(np.float32)
|
||||
hi = float(np.percentile(arr, pct))
|
||||
if hi > 1.0:
|
||||
arr = np.clip(arr * (float(target) / hi), 0, 255)
|
||||
return Image.fromarray(arr.astype(np.uint8))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument('src'); ap.add_argument('dst')
|
||||
ap.add_argument('--quad', nargs=4, metavar='x,y')
|
||||
ap.add_argument('--trim', type=int, default=3)
|
||||
a = ap.parse_args()
|
||||
im = Image.open(a.src).convert('RGB')
|
||||
if a.quad:
|
||||
quad = [tuple(float(v) for v in p.split(',')) for p in a.quad]
|
||||
else:
|
||||
w, h = im.size
|
||||
quad = [(0, 0), (w - 1, 0), (w - 1, h - 1), (0, h - 1)]
|
||||
out = trim(brighten(warp_perspective(im, quad)), a.trim)
|
||||
out.save(a.dst)
|
||||
print('written %s %dx%d' % (a.dst, out.size[0], out.size[1]))
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
"""多张图等比缩放居中,合并为一个 PDF。默认 A4 纵向 @150dpi。"""
|
||||
import sys
|
||||
from PIL import Image
|
||||
|
||||
A4_W, A4_H = 1240, 1754
|
||||
MARGIN = 40
|
||||
DPI = 150
|
||||
|
||||
|
||||
def fit_page(path):
|
||||
im = Image.open(path).convert('RGB')
|
||||
bw, bh = A4_W - MARGIN * 2, A4_H - MARGIN * 2
|
||||
s = min(bw / im.width, bh / im.height)
|
||||
nw, nh = max(1, int(round(im.width * s))), max(1, int(round(im.height * s)))
|
||||
im = im.resize((nw, nh), Image.LANCZOS)
|
||||
page = Image.new('RGB', (A4_W, A4_H), 'white')
|
||||
page.paste(im, ((A4_W - nw) // 2, (A4_H - nh) // 2))
|
||||
return page
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
*srcs, out = sys.argv[1:]
|
||||
pages = [fit_page(p) for p in srcs]
|
||||
pages[0].save(out, 'PDF', resolution=DPI, save_all=True, append_images=pages[1:])
|
||||
print('written %s (%d pages)' % (out, len(pages)))
|
||||
@@ -0,0 +1,120 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""document-photo-to-pdf 的全部可调参数。
|
||||
|
||||
改这里之前先读这段:
|
||||
- 这些值大多沿用旧流程(人工读网格图)的经验值,**没有用真实样张重新标定**。
|
||||
没有样张时只改行为开关,不要凭感觉调阈值。
|
||||
- 像素类的值默认指“检测处理图”坐标(检测会先缩到 detect.max_side),不是原图像素;
|
||||
带 _frac / _pct / _div 的是比例。少数原图/画布坐标的值会在字段名里写明 _px。
|
||||
- 纸张尺寸用毫米,画布像素由 render.dpi 换算,避免 1240/1754 这类数字散落各处。
|
||||
"""
|
||||
P = {
|
||||
"schema": "document-photo-to-pdf/2",
|
||||
|
||||
# ---------------------------------------------------------------- 检测
|
||||
"detect": {
|
||||
"max_side": 1600, # 检测降采样长边;越大越准越慢
|
||||
"min_edge_points": 40, # 单边拟合所需最少点数,低于则回退掩膜线
|
||||
"group_tol_px": 22.0, # 边线点归属容差(处理图像素)
|
||||
"robust_px": 2.0, # 稳健拟合的残差基准(处理图像素)
|
||||
"mask_erode_kernel": 3, # 掩膜内收核;3 = 半径 1 像素
|
||||
"paper_otsu_floor": 0.50, # 纸面阈值下限,防止亮桌面被并入
|
||||
"paper_v_min": 0.50, # 纸面亮度硬下限
|
||||
"paper_s_max": 0.45, # 纸面饱和度上限
|
||||
"paper_mask_factor_div": 400, # 掩膜降采样倍数 = min(H,W)//该值
|
||||
"paper_close_div": 60, # 第一次闭运算核 = min(H,W)//该值
|
||||
"paper_close_min": 5, # 形态学核最小值
|
||||
"paper_close2_div": 80, # 第二次闭运算核 = min(H,W)//该值
|
||||
"band_div": 45, # 搜索环带宽度 = H//该值
|
||||
"band_min": 15, # 环带宽度下限
|
||||
"edge_local_gain": 1.55, # 边缘图局部阈值增益
|
||||
"edge_low": 0.035, # 边缘图绝对下限
|
||||
"edge_sigma_div": 800, # 高斯模糊 sigma = H//该值
|
||||
"edge_box_div": 50, # 局部均值窗口 = H//该值
|
||||
"step_r_small": 10, # 阶跃检测小窗半径
|
||||
"step_r_big": 24, # 阶跃检测大窗半径
|
||||
"step_th": 0.055, # 跨纸边亮度阶跃阈值
|
||||
"cand_close_div": 160, # 候选边线闭运算 = H//该值
|
||||
"cand_close_min": 5,
|
||||
"cand_open_div": 500, # 候选边线开运算 = H//该值
|
||||
"cand_open_min": 3,
|
||||
"coverage_tol_div": 110, # 覆盖率检测容差 = H//该值
|
||||
"coverage_tol_min": 10,
|
||||
"corner_oob_frac": 0.15, # 角点越界允许幅度(相对画面长边)
|
||||
"boundary_side_margin": 0.10, # 拟合单边时排除两端各 10%,避开角点
|
||||
},
|
||||
|
||||
# ---------------------------------------------------------------- 几何门槛
|
||||
"gate": {
|
||||
"oob_max_pct": 15.0, # 越界超过此值判失败
|
||||
"inner_paper_min": 0.60, # 框内纸面占比下限
|
||||
"inner_edge_min": 0.75, # 每边内侧纸面占比下限
|
||||
"outer_dark_min": 0.35, # 外侧暗像素占比低于此值只告警
|
||||
"coverage_warn": 0.40, # 四边覆盖率低于此值告警(未标定)
|
||||
"aspect_deadband": 0.10, # 0.9~1.1 视为接近正方形,不参与方向投票
|
||||
"paper_mismatch_pct": 20.0, # 与批次中位宽高比偏差超过此值告警
|
||||
"sample_n": 60, # 校验采样网格边长
|
||||
"sample_shrink": 0.88, # 框内采样内收比例
|
||||
"fallback_inset": 0.03, # 检测为空时的兜底矩形:画面短边向内缩的比例
|
||||
},
|
||||
|
||||
# ---------------------------------------------------------------- 调色档
|
||||
# 全局四档,对应审核页的“图像处理”。都是“整体等比映射 + 可选灰度”:
|
||||
# 不做二值化——二值化会吃拮抗锯齿,笔画变胖变瘦,等于重绘文字。
|
||||
"enhance": {
|
||||
"original": None, # 原样:不提亮
|
||||
"normal": {"target": 245, "pct": 97}, # 标准
|
||||
"strong": {"target": 252, "pct": 90}, # 加强:背景白得更彻底
|
||||
"bw": {"target": 252, "pct": 60, "gray": True}, # 黑白(保留灰阶)
|
||||
},
|
||||
|
||||
# ---------------------------------------------------------------- 出页
|
||||
"render": {
|
||||
"dpi": 150, # PDF 输出 dpi
|
||||
"margin_px": 40, # PDF 页面四周留白(150dpi 下的像素)
|
||||
"brighten_target": 245, # 提亮目标:97 分位映射到该亮度
|
||||
"brighten_pct": 97,
|
||||
"warp_chunk_rows": 256, # 透视变换分块行数,控制峰值内存
|
||||
"default_paper": "auto", # auto=按批次中位比例统一版式,A4 画布内留白
|
||||
"default_enhance": "normal", # 调色档默认值,可选 key 见 P["enhance"]
|
||||
"enhance": "normal", # 本次生效的调色档(run.py 按参数覆写副本)
|
||||
"dpi_options": {"standard": 150, "high": 300}, # 清晰度档名 -> dpi
|
||||
"papers_mm": {
|
||||
"a4": (210.0, 297.0),
|
||||
"letter": (215.9, 279.4),
|
||||
"16k": (185.0, 260.0),
|
||||
"b5": (176.0, 250.0),
|
||||
},
|
||||
},
|
||||
|
||||
# ---------------------------------------------------------------- PDF 检验
|
||||
"check": {
|
||||
"dpi": 100, # 检验渲染 dpi
|
||||
"min_short": 800, # 纸面短边下限(按 150dpi 基准,随 dpi 换算)
|
||||
"ratio_tol": 0.08, # 纸面比例偏差提示阈值(仅提示)
|
||||
"border_ratio": 0.80, # 边带亮度 / 纸面亮度 下限
|
||||
"edge_warnings": False, # 边缘背景残留/内容贴边是否升为 warning
|
||||
# (启发式、未标定,误报多;默认只留在 checks)
|
||||
"edge_dark_ratio": 0.60, # 边带“暗像素”亮度 / 纸面亮度
|
||||
"edge_dark_frac": 0.35, # 边带暗像素占比上限
|
||||
"cut_frac": 0.05, # 最外带内文字级暗像素占比上限
|
||||
"cut_mean_ratio": 0.70, # 贴边带均值低于纸面该比例才算成片暗
|
||||
"edge_inset_frac": 0.002, # 纸面框内缩比例,避开透视采样毛边
|
||||
"border_band_frac": 0.012, # 边带宽度 = 纸面短边 * 该值
|
||||
"cut_band_frac": 0.003, # 贴边检测带宽度 = 纸面短边 * 该值
|
||||
"paper_pct": 80, # 纸面亮度分位
|
||||
"back_pct": 2, # 背景亮度分位
|
||||
"ink_ratio": 0.80, # 墨色阈值上限 = 纸面亮度 * 该值
|
||||
"flat_pct": 99.5, # 页面纯白分位
|
||||
"nonwhite_delta": 8.0, # 低于纯白该值视为非白(用于找纸面框)
|
||||
"paper_row_frac": 0.05, # 行列非白占比阈值
|
||||
"tmp_dir": ".tmp/pdfcheck", # 检验渲染临时目录(相对输出目录,检验完即删)
|
||||
},
|
||||
|
||||
# ---------------------------------------------------------------- 输出
|
||||
"output": {
|
||||
"default_name": "文档照片_校正.pdf",
|
||||
"state_suffix": ".state.json",
|
||||
"max_logs": 20,
|
||||
},
|
||||
}
|
||||
@@ -1,416 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""文档照片 -> 校正 PDF:全流程串起来的一站式脚本。
|
||||
|
||||
把原来手工分步的活(网格图 -> 读角点 -> 落盘 -> 回画确认 -> 出 PDF -> 检验)
|
||||
压成几条命令,且**产物都落在 out/ 里按序号命名**:
|
||||
|
||||
out/1-网格-<原图名>.png 每页的坐标网格图(读角点用)
|
||||
out/1-角标-<原图名>.png 每页角点回画图(原图 + 网格 + 四角标注,核查是否贴纸边)
|
||||
out/1-成品-<原图名>.png 每页矫正后的页面图
|
||||
out/corners.json 角点坐标(落盘,跨会话可用)
|
||||
out/<名字>.pdf 最终 PDF
|
||||
|
||||
角点约定(顺序固定,别写反):
|
||||
左上 ; 右上 ; 右下 ; 左下 —— 屏幕坐标,原点在图片左上角,单位像素
|
||||
|
||||
子命令:
|
||||
prepare 出网格图 + 生成 corners.json 模板(坐标留空待填)
|
||||
set 把某一页的四角坐标写进 corners.json(可反复调用)
|
||||
build 回画角标图 + 校验 + 出 PDF + 出成品图 + PDF 检验,一次跑完
|
||||
all prepare + build 合体(角点已存在则直接 build)
|
||||
check 复检已有 PDF(只渲染检验,不重新出件)
|
||||
|
||||
典型用法(脚本用绝对路径调用:cwd 是会话任务目录,不是本目录):
|
||||
# 1) 出网格图
|
||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py prepare 图1.jpg 图2.jpg
|
||||
# 2) 看 out/1-网格-图1.png,把坐标写进文件
|
||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py set "图1.jpg" --quad "150,360;1020,355;1050,1690;90,1660"
|
||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py set "图2.jpg" --quad "120,345;1010,345;1010,1710;120,1710"
|
||||
# 3) 一路出 PDF
|
||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py build --out 合同_校正.pdf
|
||||
|
||||
# 想一步到位(角点已落盘):
|
||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py all 图1.jpg 图2.jpg --out 合同_校正.pdf
|
||||
|
||||
# 列出 / 检查已落盘角点
|
||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py show
|
||||
|
||||
退出码:0 全部通过;2 角点校验未通过(已出件但需人工判断);3 PDF 检验未通过。
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, HERE)
|
||||
|
||||
# 提示里回显的命令:cwd 是会话任务目录,必须用脚本绝对路径,写相对的跑不起来
|
||||
SELF = 'python3 ' + os.path.join(HERE, 'pipeline.py')
|
||||
|
||||
OUT_DIR = 'out'
|
||||
CORNERS = os.path.join(OUT_DIR, 'corners.json')
|
||||
LABELS = ('左上', '右上', '右下', '左下')
|
||||
|
||||
IMG_EXT = ('.jpg', '.jpeg', '.png', '.webp', '.bmp', '.tif', '.tiff')
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 基础工具
|
||||
|
||||
def natural_key(name):
|
||||
return [int(t) if t.isdigit() else t.lower()
|
||||
for t in re.split(r'(\d+)', os.path.basename(name))]
|
||||
|
||||
|
||||
def quad_to_str(quad):
|
||||
return ';'.join('%g,%g' % (p[0], p[1]) for p in quad)
|
||||
|
||||
|
||||
def load_corners(path=CORNERS):
|
||||
if not os.path.exists(path):
|
||||
return {}
|
||||
with open(path, encoding='utf-8') as f:
|
||||
raw = json.load(f)
|
||||
out = {}
|
||||
for name, v in raw.items():
|
||||
if isinstance(v, dict):
|
||||
v = v.get('quad') or v.get('corners')
|
||||
if not v:
|
||||
continue
|
||||
out[name] = [(float(p[0]), float(p[1])) for p in v]
|
||||
return out
|
||||
|
||||
|
||||
def save_corners(data, path=CORNERS):
|
||||
os.makedirs(os.path.dirname(os.path.abspath(path)) or '.', exist_ok=True)
|
||||
tmp = path + '.tmp'
|
||||
with open(tmp, 'w', encoding='utf-8') as f:
|
||||
json.dump({k: [[int(c) if float(c).is_integer() else c for c in p]
|
||||
for p in v] for k, v in data.items()},
|
||||
f, ensure_ascii=False, indent=2)
|
||||
f.write('\n')
|
||||
os.replace(tmp, path)
|
||||
|
||||
|
||||
def parse_quad(text):
|
||||
quad = []
|
||||
for pt in text.replace(';', ';').split(';'):
|
||||
pt = pt.strip()
|
||||
if not pt:
|
||||
continue
|
||||
xy = pt.replace(' ', '').split(',')
|
||||
if len(xy) != 2:
|
||||
raise SystemExit('角点格式不对:%r(应为 x,y)' % pt)
|
||||
try:
|
||||
quad.append((float(xy[0]), float(xy[1])))
|
||||
except ValueError:
|
||||
raise SystemExit('角点不是数字:%r' % pt)
|
||||
if len(quad) != 4:
|
||||
raise SystemExit('角点必须 4 个(左上;右上;右下;左下),实际 %d 个' % len(quad))
|
||||
return quad
|
||||
|
||||
|
||||
def collect_images(args):
|
||||
"""参数里的图片 + 当前目录全部图片(去重、按自然序)。"""
|
||||
names = list(args)
|
||||
if not names:
|
||||
names = [f for f in os.listdir('.')
|
||||
if f.lower().endswith(IMG_EXT) and os.path.isfile(f)]
|
||||
seen, out = set(), []
|
||||
for n in names:
|
||||
if n not in seen:
|
||||
seen.add(n)
|
||||
out.append(n)
|
||||
if not out:
|
||||
raise SystemExit('没有找到图片:可在命令后面直接列文件名')
|
||||
missing = [n for n in out if not os.path.exists(n)]
|
||||
if missing:
|
||||
raise SystemExit('图片不存在:%s' % ', '.join(missing))
|
||||
return sorted(out, key=natural_key)
|
||||
|
||||
|
||||
def page_tag(i):
|
||||
return '%d' % i
|
||||
|
||||
|
||||
def out_path(prefix, i, name, ext='.png'):
|
||||
base = os.path.splitext(os.path.basename(name))[0]
|
||||
return os.path.join(OUT_DIR, '%s-%s-%s%s' % (page_tag(i), prefix, base, ext))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- prepare
|
||||
|
||||
def cmd_prepare(images):
|
||||
os.makedirs(OUT_DIR, exist_ok=True)
|
||||
from grid import draw_grid
|
||||
data = load_corners()
|
||||
for i, name in enumerate(images, 1):
|
||||
dst = out_path('网格', i, name)
|
||||
draw_grid(name, dst)
|
||||
data.setdefault(name, None)
|
||||
save_corners({k: v for k, v in data.items() if v})
|
||||
print('\n网格图已写入 %s/' % OUT_DIR)
|
||||
print('下一步:照着网格图读数,把角点写进 %s:' % CORNERS)
|
||||
for i, name in enumerate(images, 1):
|
||||
print(' %s set "%s" --quad "左上x,左上y;右上x,右上y;'
|
||||
'右下x,右下y;左下x,左下y"' % (SELF, name))
|
||||
return 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- set / show
|
||||
|
||||
def cmd_set(args):
|
||||
"""一次只写一张图:参数是 图片 + 一个 --quad,不批配、不错位。"""
|
||||
if '|' in args.image or ':' in args.image:
|
||||
raise SystemExit('图片名里不要带 : 或 |:%s' % args.image)
|
||||
if not os.path.exists(args.image):
|
||||
raise SystemExit('图片不存在:%s' % args.image)
|
||||
if len(args.quad) != 1:
|
||||
raise SystemExit('set 只能给一个 --quad(实际 %d 个)。一张图一次。'
|
||||
% len(args.quad))
|
||||
|
||||
name = args.image
|
||||
quad_s = args.quad[0]
|
||||
|
||||
data = load_corners()
|
||||
old = data.get(name)
|
||||
data[name] = parse_quad(quad_s)
|
||||
save_corners(data)
|
||||
if old is not None:
|
||||
print('已更新 %-36s %s(原 %s)'
|
||||
% (name, quad_to_str(data[name]), quad_to_str(old)))
|
||||
else:
|
||||
print('已写入 %-36s %s' % (name, quad_to_str(data[name])))
|
||||
print('→ %s(已存:%s)'
|
||||
% (CORNERS, '、'.join(sorted(data, key=natural_key))))
|
||||
left = [n for n in data if not os.path.exists(n)]
|
||||
if left:
|
||||
print('[注意] 这些图在 corners.json 里但文件不存在:%s' % '、'.join(left))
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_show():
|
||||
data = load_corners()
|
||||
if not data:
|
||||
print('%s 里还没有角点' % CORNERS)
|
||||
return 1
|
||||
for i, name in enumerate(sorted(data, key=natural_key), 1):
|
||||
print('%d. %-40s %s' % (i, name, quad_to_str(data[name])))
|
||||
return 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 角标图
|
||||
|
||||
def draw_marks(name, pts, dst):
|
||||
"""把四角画回原图(底图叠加与网格图相同的网格),供人工核查。"""
|
||||
from preview_quad import draw_quad
|
||||
im = draw_quad(name, pts, with_grid=True)
|
||||
im.save(dst)
|
||||
return dst
|
||||
|
||||
|
||||
def make_zooms(name, marked_png, idx, zoom):
|
||||
"""从角标图上裁出四侧放大块,命名同其它核查图的序号规则。"""
|
||||
from PIL import Image
|
||||
from preview_quad import zoom_boxes
|
||||
im = Image.open(marked_png)
|
||||
boxes = zoom_boxes(im, [(0, 0), (im.width, 0), (im.width, im.height), (0, im.height)])
|
||||
keys = list(boxes) if zoom == 'all' else [zoom]
|
||||
base = os.path.join(OUT_DIR, '%d-角标放大-%s' % (idx, os.path.basename(name)))
|
||||
base = os.path.splitext(base)[0]
|
||||
for k in keys:
|
||||
box = tuple(int(v) for v in boxes[k])
|
||||
if box[2] <= box[0] or box[3] <= box[1]:
|
||||
continue
|
||||
crop = im.crop(box)
|
||||
# 细长条按方向切成两段,否则 585x5760 没法看
|
||||
segs = []
|
||||
if k in ('left', 'right') and crop.height > crop.width * 2.2:
|
||||
h = crop.height // 2
|
||||
segs = [('上', crop.crop((0, 0, crop.width, h))),
|
||||
('下', crop.crop((0, h, crop.width, crop.height)))]
|
||||
elif k in ('top', 'bottom') and crop.width > crop.height * 2.2:
|
||||
w = crop.width // 2
|
||||
segs = [('左', crop.crop((0, 0, w, crop.height))),
|
||||
('右', crop.crop((w, 0, crop.width, crop.height)))]
|
||||
else:
|
||||
segs = [('', crop)]
|
||||
for tag, c in segs:
|
||||
f = min(3.0, max(1.0, 700.0 / max(1, c.width)))
|
||||
if f > 1.0:
|
||||
c = c.resize((int(c.width * f), int(c.height * f)), Image.LANCZOS)
|
||||
z = '%s-%s%s.png' % (base, k, tag)
|
||||
c.save(z)
|
||||
print(' 放大块 %s (%dx%d)' % (z, c.width, c.height))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- build
|
||||
|
||||
def cmd_build(images, out_pdf, paper, dpi, force, zoom):
|
||||
data = load_corners()
|
||||
if images:
|
||||
names = images
|
||||
else:
|
||||
names = sorted(data, key=natural_key)
|
||||
if not names:
|
||||
raise SystemExit('还没有角点。先跑 prepare 出网格图,再用 set 落盘角点。')
|
||||
|
||||
lacking = [n for n in names if n not in data]
|
||||
if lacking:
|
||||
raise SystemExit('这些图还没有角点:%s\n先跑 %s set ...'
|
||||
% (', '.join(lacking), SELF))
|
||||
|
||||
os.makedirs(OUT_DIR, exist_ok=True)
|
||||
from verify_quad import check as verify_quad
|
||||
from run import build, PAPERS
|
||||
|
||||
ratio = None
|
||||
if paper:
|
||||
key = paper.strip().lower()
|
||||
ratio = PAPERS.get(key)
|
||||
if ratio is None:
|
||||
try:
|
||||
ratio = float(paper)
|
||||
except ValueError:
|
||||
raise SystemExit('未知纸张:%s(可选 %s 或数字比例)'
|
||||
% (paper, '/'.join(PAPERS)))
|
||||
|
||||
# 1. 角标图 + 校验
|
||||
print('=== 步骤 1/4:回画角点(核查用) ===')
|
||||
verdicts = []
|
||||
for i, name in enumerate(names, 1):
|
||||
dst = out_path('角标', i, name)
|
||||
draw_marks(name, data[name], dst)
|
||||
print('\n第 %d 页 %s → %s' % (i, name, dst))
|
||||
ok = verify_quad(name, data[name])
|
||||
verdicts.append(ok)
|
||||
if zoom:
|
||||
make_zooms(name, dst, i, zoom)
|
||||
print('\n>>> 请打开上面这些 角标 图,逐个角确认是否压在纸角上。\n')
|
||||
|
||||
bad_pages = [i for i, ok in enumerate(verdicts, 1) if not ok]
|
||||
if bad_pages:
|
||||
print('!! 角点校验未通过:第 %s 页(上面标 [XX] 的项)。'
|
||||
% '、'.join(map(str, bad_pages)))
|
||||
if not force:
|
||||
print('已停止,未出件。按上面的提示调整角点,再重跑 set + build。')
|
||||
return 2
|
||||
print('已按 --force 继续出件:结果需人工确认,不能当作校验通过交付。\n')
|
||||
|
||||
# 2. 出 PDF
|
||||
print('=== 步骤 2/4:透视矫正 + 合成 PDF ===')
|
||||
workdir = os.path.join(OUT_DIR, '_pages')
|
||||
specs = ['%s:%s' % (n, quad_to_str(data[n])) for n in names]
|
||||
pages = build(specs, out_pdf, workdir, ratio)
|
||||
|
||||
# 成品图按页序号重命名进 out/
|
||||
for i, (name, page) in enumerate(zip(names, pages), 1):
|
||||
dst = out_path('成品', i, name)
|
||||
os.replace(page, dst)
|
||||
print(' 成品图 %s' % dst)
|
||||
|
||||
# 3. PDF 检验
|
||||
print('\n=== 步骤 3/4:最终 PDF 检验 ===')
|
||||
rc = subprocess.run([sys.executable, os.path.join(HERE, 'check.py'), out_pdf,
|
||||
'--dpi', str(dpi), '--expect-pages', str(len(names))]).returncode
|
||||
|
||||
# 4. 汇总
|
||||
bad = bad_pages
|
||||
print('\n=== 步骤 4/4:汇总 ===')
|
||||
print('PDF :%s(%.0f KB,%d 页)' % (out_pdf, os.path.getsize(out_pdf) / 1024, len(names)))
|
||||
print('角点校验 :%s' % ('全部通过' if not bad else '第 %s 页未通过' % '、'.join(map(str, bad))))
|
||||
print('PDF 检验 :%s' % ('通过' if rc == 0 else '未通过'))
|
||||
print('核查材料 :')
|
||||
for i, name in enumerate(names, 1):
|
||||
print(' %s %s %s' % (out_path('网格', i, name), out_path('角标', i, name),
|
||||
out_path('成品', i, name)))
|
||||
if bad or rc != 0:
|
||||
print('注意:以上为原样结果,未做任何自动修正。')
|
||||
return 2 if bad else 3
|
||||
return 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- check
|
||||
|
||||
def cmd_check(pdf, expect_pages, dpi):
|
||||
"""复检已有 PDF:只渲染检验,不重新校正、不改动文件。"""
|
||||
if not os.path.exists(pdf):
|
||||
raise SystemExit('找不到 PDF:%s' % pdf)
|
||||
cmd = [sys.executable, os.path.join(HERE, 'check.py'), pdf, '--dpi', str(dpi)]
|
||||
if expect_pages is not None:
|
||||
cmd += ['--expect-pages', str(expect_pages)]
|
||||
return subprocess.run(cmd).returncode
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- main
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(
|
||||
description='文档照片 -> 校正 PDF 全流程脚本',
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
epilog=__doc__)
|
||||
sub = ap.add_subparsers(dest='cmd', required=True)
|
||||
|
||||
p = sub.add_parser('prepare', help='出网格图(out/1-网格-*.png)')
|
||||
p.add_argument('images', nargs='*', help='图片名,省略则取当前目录所有图片')
|
||||
|
||||
p = sub.add_parser('set', help='把一张图的角点写进 out/corners.json')
|
||||
p.add_argument('image', metavar='IMG', help='图片名(一次一张)')
|
||||
p.add_argument('--quad', action='append', default=[], required=True,
|
||||
metavar='左上x,左上y;右上x,右上y;右下x,右下y;左下x,左下y',
|
||||
help='这一张图的四个角点(顺序固定:左上 右上 右下 左下)')
|
||||
|
||||
p = sub.add_parser('show', help='查看已落盘角点')
|
||||
|
||||
p = sub.add_parser('check', help='复检已有 PDF(只渲染检验,不重新出件)')
|
||||
p.add_argument('pdf', metavar='PDF')
|
||||
p.add_argument('--expect-pages', type=int, default=None)
|
||||
p.add_argument('--dpi', type=int, default=100)
|
||||
|
||||
p = sub.add_parser('build', help='回画角标 + 校验 + 出 PDF + 检验')
|
||||
p.add_argument('images', nargs='*', help='图片名,省略则用 corners.json 里的全部')
|
||||
p.add_argument('--out', default='文档_校正.pdf')
|
||||
p.add_argument('--paper', default=None, help='a4/letter/16k/b5 或比例数字')
|
||||
p.add_argument('--dpi', type=int, default=100, help='PDF 检验渲染 dpi')
|
||||
p.add_argument('--force', action='store_true', help='角点校验不通过也继续')
|
||||
p.add_argument('--zoom', default=None,
|
||||
choices=['left', 'right', 'top', 'bottom', 'all'],
|
||||
help='另存边角放大块,便于细看')
|
||||
|
||||
p = sub.add_parser('all', help='prepare + build 一步到位')
|
||||
p.add_argument('images', nargs='*')
|
||||
p.add_argument('--out', default='文档_校正.pdf')
|
||||
p.add_argument('--paper', default=None)
|
||||
p.add_argument('--dpi', type=int, default=100)
|
||||
p.add_argument('--force', action='store_true')
|
||||
p.add_argument('--zoom', default=None)
|
||||
|
||||
args = ap.parse_args()
|
||||
|
||||
if args.cmd == 'prepare':
|
||||
return cmd_prepare(collect_images(args.images))
|
||||
if args.cmd == 'set':
|
||||
return cmd_set(args)
|
||||
if args.cmd == 'show':
|
||||
return cmd_show()
|
||||
if args.cmd == 'check':
|
||||
return cmd_check(args.pdf, args.expect_pages, args.dpi)
|
||||
if args.cmd == 'build':
|
||||
imgs = collect_images(args.images) if args.images else []
|
||||
return cmd_build(imgs, args.out, args.paper, args.dpi, args.force, args.zoom)
|
||||
if args.cmd == 'all':
|
||||
imgs = collect_images(args.images)
|
||||
cmd_prepare(imgs)
|
||||
data = load_corners()
|
||||
lacking = [n for n in imgs if n not in data]
|
||||
if lacking:
|
||||
print('\n这些图还没有角点,填好后重跑 build:%s' % ', '.join(lacking))
|
||||
return 2
|
||||
return cmd_build(imgs, args.out, args.paper, args.dpi, args.force, args.zoom)
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -1,185 +0,0 @@
|
||||
"""角点回画预览:把已落盘的四角画回原图,供二次确认。
|
||||
|
||||
用途:把落盘的数字角点重新变成可看的画面。
|
||||
本脚本把四边形 + 四角标记 + 边标签画回原图,让"数字"重新变成可看的画面,
|
||||
在出件前确认四条边是否真的压在纸张亮面与桌面暗面的交界线上。
|
||||
|
||||
图上元素:
|
||||
绿色四边形 —— 当前角点连线,应紧贴纸边
|
||||
红色十字 + 序号 —— 四个角点(1左上 2右上 3右下 4左下)
|
||||
黄色短刻度线 —— 每条边中点的外法线,用于看边是否贴边
|
||||
右下角文字 —— 两行:文件名 / 角点串(避开顶部/左侧刻度)
|
||||
|
||||
用法:
|
||||
python3 doc_deskew/preview_quad.py 图1.jpg 图2.jpg
|
||||
python3 doc_deskew/preview_quad.py --corners .tmp/corners.json
|
||||
python3 doc_deskew/preview_quad.py --corners .tmp/corners.json --zoom 右
|
||||
|
||||
输出:.tmp/quad_<名称>.png
|
||||
--zoom 另存右侧/左侧/上边/下边放大块,.tmp/quad_<名称>_<区域>.png
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, HERE)
|
||||
|
||||
DEFAULT_CORNERS = '.tmp/corners.json'
|
||||
OUT_DIR = '.tmp'
|
||||
|
||||
LABELS = ['1 左上', '2 右上', '3 右下', '4 左下']
|
||||
COLOR_QUAD = (0, 255, 0)
|
||||
COLOR_CORNER = (255, 40, 40)
|
||||
COLOR_EDGE = (255, 220, 0)
|
||||
|
||||
|
||||
def load_corners(path):
|
||||
with open(path, encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
out = {}
|
||||
for name, quad in data.items():
|
||||
if isinstance(quad, dict):
|
||||
quad = quad.get('quad') or quad.get('corners')
|
||||
out[name] = [(float(p[0]), float(p[1])) for p in quad]
|
||||
return out
|
||||
|
||||
|
||||
def get_font(size=26):
|
||||
# 先找中文字体(图上要画 "1 左上" 这类标签,西文字体会变成豆腐块)
|
||||
for p in ('/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc',
|
||||
'/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc',
|
||||
'/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc',
|
||||
'/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',
|
||||
'/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf'):
|
||||
if os.path.exists(p):
|
||||
try:
|
||||
return ImageFont.truetype(p, size)
|
||||
except Exception:
|
||||
pass
|
||||
return ImageFont.load_default()
|
||||
|
||||
|
||||
def draw_quad(img_path, pts, scale=1.0, with_grid=False):
|
||||
im = Image.open(img_path).convert('RGB')
|
||||
# 角标图默认叠上与网格图同一套网格:坐标读数与框边可以对着看
|
||||
if with_grid:
|
||||
from grid import draw_grid_on
|
||||
im = draw_grid_on(im)
|
||||
if scale != 1.0:
|
||||
im = im.resize((int(im.width * scale), int(im.height * scale)),
|
||||
Image.LANCZOS)
|
||||
d = ImageDraw.Draw(im)
|
||||
font = get_font(max(14, int(26 * min(1.0, scale + 0.15))))
|
||||
sml = get_font(max(11, int(18 * min(1.0, scale + 0.15))))
|
||||
w = max(2, int(3 * scale + 0.5))
|
||||
P = [(x * scale, y * scale) for x, y in pts]
|
||||
|
||||
# 四边形
|
||||
d.line(P + [P[0]], fill=COLOR_QUAD, width=w)
|
||||
|
||||
# 四角十字 + 序号
|
||||
r = max(10, int(22 * scale + 0.5))
|
||||
for i, (x, y) in enumerate(P):
|
||||
d.line([(x - r, y), (x + r, y)], fill=COLOR_CORNER, width=w)
|
||||
d.line([(x, y - r), (x, y + r)], fill=COLOR_CORNER, width=w)
|
||||
tx, ty = x + r + 4, y - r - 4
|
||||
d.text((tx, ty), LABELS[i], fill=COLOR_CORNER, font=sml,
|
||||
stroke_width=2, stroke_fill=(0, 0, 0))
|
||||
|
||||
# 每条边中点外法线刻度
|
||||
cx = sum(p[0] for p in P) / 4.0
|
||||
cy = sum(p[1] for p in P) / 4.0
|
||||
L = max(16, int(40 * scale + 0.5))
|
||||
for i in range(4):
|
||||
a, b = P[i], P[(i + 1) % 4]
|
||||
mx, my = (a[0] + b[0]) / 2.0, (a[1] + b[1]) / 2.0
|
||||
dx, dy = mx - cx, my - cy
|
||||
n = (dx * dx + dy * dy) ** 0.5 or 1.0
|
||||
dx, dy = dx / n, dy / n
|
||||
d.line([(mx, my), (mx + dx * L, my + dy * L)],
|
||||
fill=COLOR_EDGE, width=max(2, w - 1))
|
||||
|
||||
# 右下角信息条(避开顶部/左侧的网格刻度):第一行文件名,第二行角点串
|
||||
lines = [os.path.basename(img_path),
|
||||
';'.join('%g,%g' % (p[0], p[1]) for p in pts)]
|
||||
pad = 8
|
||||
gap = 6
|
||||
tw = int(max(d.textlength(s, font=font) for s in lines))
|
||||
ascent, descent = font.getmetrics()
|
||||
line_h = ascent + descent
|
||||
th = line_h * len(lines) + gap * (len(lines) - 1)
|
||||
tx = max(pad, im.width - tw - pad * 2)
|
||||
ty = max(pad, im.height - th - pad * 2)
|
||||
d.rectangle([tx - 4, ty - 4, tx + tw + 4, ty + th + 4], fill=(0, 0, 0))
|
||||
for i, s in enumerate(lines):
|
||||
d.text((tx, ty + i * (line_h + gap)), s, fill=(255, 255, 255), font=font)
|
||||
return im
|
||||
|
||||
|
||||
def zoom_boxes(im, pts):
|
||||
"""按角点外接框给出四侧放大区域。"""
|
||||
xs = [p[0] for p in pts]
|
||||
ys = [p[1] for p in pts]
|
||||
x0, x1, y0, y1 = min(xs), max(xs), min(ys), max(ys)
|
||||
W, H = im.size
|
||||
mx = (x1 - x0) * 0.18
|
||||
my = (y1 - y0) * 0.18
|
||||
boxes = {
|
||||
'left': (max(0, x0 - mx), max(0, y0 - my), min(W, x0 + mx), min(H, y1 + my)),
|
||||
'right': (max(0, x1 - mx), max(0, y0 - my), min(W, x1 + mx), min(H, y1 + my)),
|
||||
'top': (max(0, x0 - mx), max(0, y0 - my), min(W, x1 + mx), min(H, y0 + my)),
|
||||
'bottom': (max(0, x0 - mx), max(0, y1 - my), min(W, x1 + mx), min(H, y1 + my)),
|
||||
}
|
||||
return boxes
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description='把角点画回原图做二次确认')
|
||||
ap.add_argument('images', nargs='*', help='图片文件名(角点已在 corners.json 中)')
|
||||
ap.add_argument('--corners', default=DEFAULT_CORNERS)
|
||||
ap.add_argument('--zoom', choices=['left', 'right', 'top', 'bottom', 'all'],
|
||||
default=None, help='另存某侧放大块')
|
||||
ap.add_argument('--scale', type=float, default=1.0, help='整体缩放,>1 放大')
|
||||
ap.add_argument('--out', default=None, help='输出目录,默认 .tmp')
|
||||
args = ap.parse_args()
|
||||
|
||||
data = load_corners(args.corners)
|
||||
names = args.images or list(data.keys())
|
||||
missing = [n for n in names if n not in data]
|
||||
if missing:
|
||||
raise SystemExit('corners.json 中没有这些图的角点:%s' % ', '.join(missing))
|
||||
|
||||
outdir = args.out or OUT_DIR
|
||||
os.makedirs(outdir, exist_ok=True)
|
||||
|
||||
for name in names:
|
||||
pts = data[name]
|
||||
im = draw_quad(name, pts, args.scale)
|
||||
base = os.path.splitext(os.path.basename(name))[0]
|
||||
dst = os.path.join(outdir, 'quad_%s.png' % base)
|
||||
im.save(dst)
|
||||
print('written %s (%dx%d) %s' % (dst, im.width, im.height,
|
||||
';'.join('%g,%g' % p for p in pts)))
|
||||
if args.zoom:
|
||||
boxes = zoom_boxes(im, [(x * args.scale, y * args.scale) for x, y in pts])
|
||||
keys = list(boxes) if args.zoom == 'all' else [args.zoom]
|
||||
for k in keys:
|
||||
box = tuple(int(v) for v in boxes[k])
|
||||
if box[2] <= box[0] or box[3] <= box[1]:
|
||||
continue
|
||||
crop = im.crop(box)
|
||||
f = min(3.0, max(1.0, 700.0 / max(1, crop.width)))
|
||||
if f > 1.0:
|
||||
crop = crop.resize((int(crop.width * f), int(crop.height * f)),
|
||||
Image.LANCZOS)
|
||||
z = os.path.join(outdir, 'quad_%s_%s.png' % (base, k))
|
||||
crop.save(z)
|
||||
print('written %s (%dx%d)' % (z, crop.width, crop.height))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,66 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""审核页发布:模板副本 + 每批缩略图,落会话 .app/doc-review/。
|
||||
|
||||
模板的单一事实源在 skill 目录的 web/;会话里的 index.html 只是发布副本,
|
||||
不含任何数据注入。页面运行时通过 /api/db 读 doc_review_* 表,因此同一份
|
||||
HTML 可服务所有批次。core 只托管会话 .app/,不托管 skill 目录,这是必须
|
||||
复制一次的原因(不是第二份源码)。
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from PIL import Image, ImageOps
|
||||
|
||||
WEB_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "web")
|
||||
APP_SUBDIR = os.path.join(".app", "doc-review")
|
||||
THUMB_LONG = 1200
|
||||
THUMB_Q = 80
|
||||
|
||||
|
||||
def publish(session_root, batch_id, pages):
|
||||
"""发布模板与缩略图。
|
||||
|
||||
pages: [{idx, source_abs}];返回 (url, thumbs),thumbs[idx] = (相对路径, w, h)。
|
||||
"""
|
||||
app_dir = os.path.join(session_root, APP_SUBDIR)
|
||||
os.makedirs(app_dir, exist_ok=True)
|
||||
for name in sorted(os.listdir(WEB_DIR)):
|
||||
src = os.path.join(WEB_DIR, name)
|
||||
if os.path.isfile(src):
|
||||
shutil.copy2(src, os.path.join(app_dir, name))
|
||||
|
||||
thumb_dir = os.path.join(app_dir, batch_id)
|
||||
os.makedirs(thumb_dir, exist_ok=True)
|
||||
thumbs = {}
|
||||
for p in pages:
|
||||
src = p.get("source_abs")
|
||||
idx = p.get("idx")
|
||||
if not src or idx is None or not os.path.isfile(src):
|
||||
continue
|
||||
try:
|
||||
with Image.open(src) as raw:
|
||||
im = ImageOps.exif_transpose(raw).convert("RGB")
|
||||
w, h = im.size
|
||||
s = min(1.0, float(THUMB_LONG) / max(w, h))
|
||||
if s < 1.0:
|
||||
im = im.resize((max(1, int(round(w * s))), max(1, int(round(h * s)))),
|
||||
Image.LANCZOS)
|
||||
name = "p%d.jpg" % int(idx)
|
||||
im.save(os.path.join(thumb_dir, name), "JPEG", quality=THUMB_Q, optimize=True)
|
||||
thumbs[int(idx)] = (batch_id + "/" + name, im.width, im.height)
|
||||
except Exception:
|
||||
continue
|
||||
task_id = os.path.basename(os.path.abspath(session_root))
|
||||
return ("/app/%s/doc-review/?b=%s" % (task_id, batch_id), thumbs)
|
||||
|
||||
|
||||
def prune_dirs(session_root, keep_ids):
|
||||
"""删除不在 keep_ids 里的旧批次缩略图目录(模板文件不动)。"""
|
||||
root = os.path.join(session_root, APP_SUBDIR)
|
||||
if not os.path.isdir(root):
|
||||
return
|
||||
keep = set(keep_ids)
|
||||
for name in os.listdir(root):
|
||||
path = os.path.join(root, name)
|
||||
if os.path.isdir(path) and name not in keep:
|
||||
shutil.rmtree(path, ignore_errors=True)
|
||||
@@ -0,0 +1,223 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""审核页运行记录:会话 data.db 里的 doc_review_* 表。
|
||||
|
||||
为什么由技能自己写:
|
||||
- core 对 data.db 只有通用 SQL API(/api/db)和 db 工具,不认识任何业务表;
|
||||
- 技能自己拥有 schema、写入与清理,core 保持零感知(松耦合)。
|
||||
约定:
|
||||
- 时间一律 ISO8601 UTC 字符串;
|
||||
- 坐标/结构一律 JSON 文本,页面用 JSON.parse;
|
||||
- 表名统一 doc_review_ 前缀,避免和用户业务表混淆。
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import secrets
|
||||
import sqlite3
|
||||
from datetime import datetime, timezone
|
||||
|
||||
SCHEMA_VERSION = 1
|
||||
DB_NAME = "data.db"
|
||||
|
||||
|
||||
def now_iso():
|
||||
return datetime.now(timezone.utc).isoformat(timespec="seconds")
|
||||
|
||||
|
||||
def new_batch_id():
|
||||
# 不含点/斜杠/空格:避免 skills.absolutizeArgs 把 batch_id 误判成路径。
|
||||
return "dr" + datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S") + "-" + secrets.token_hex(2)
|
||||
|
||||
|
||||
def _json(v):
|
||||
return None if v is None else json.dumps(v, ensure_ascii=False, separators=(",", ":"))
|
||||
|
||||
|
||||
def _loads(s, default=None):
|
||||
if not s:
|
||||
return default
|
||||
try:
|
||||
return json.loads(s)
|
||||
except Exception:
|
||||
return default
|
||||
|
||||
|
||||
def connect(session_root):
|
||||
path = os.path.join(session_root, DB_NAME)
|
||||
conn = sqlite3.connect(path, timeout=5.0)
|
||||
conn.row_factory = sqlite3.Row
|
||||
conn.execute("PRAGMA busy_timeout=5000")
|
||||
# core 建库时已是 WAL;这里 best-effort,不强行切换(并发打开时切换会拿写锁)。
|
||||
try:
|
||||
conn.execute("PRAGMA journal_mode=WAL")
|
||||
except sqlite3.DatabaseError:
|
||||
pass
|
||||
ensure_schema(conn)
|
||||
return conn
|
||||
|
||||
|
||||
def ensure_schema(conn):
|
||||
conn.executescript("""
|
||||
CREATE TABLE IF NOT EXISTS doc_review_batch (
|
||||
id TEXT PRIMARY KEY,
|
||||
output TEXT NOT NULL,
|
||||
paper TEXT,
|
||||
enhance TEXT,
|
||||
dpi TEXT,
|
||||
orientation TEXT,
|
||||
status TEXT,
|
||||
summary TEXT,
|
||||
warnings TEXT,
|
||||
schema_version INTEGER,
|
||||
created_at TEXT,
|
||||
updated_at TEXT
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS doc_review_page (
|
||||
batch_id TEXT NOT NULL,
|
||||
idx INTEGER NOT NULL,
|
||||
source TEXT,
|
||||
source_w INTEGER,
|
||||
source_h INTEGER,
|
||||
w INTEGER,
|
||||
h INTEGER,
|
||||
auto_corners TEXT,
|
||||
auto_corners_norm TEXT,
|
||||
corners TEXT,
|
||||
auto_rotate INTEGER,
|
||||
rotate INTEGER,
|
||||
deleted INTEGER DEFAULT 0,
|
||||
sort INTEGER,
|
||||
status TEXT,
|
||||
reason TEXT,
|
||||
thumb TEXT,
|
||||
error TEXT,
|
||||
PRIMARY KEY (batch_id, idx)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS doc_review_job (
|
||||
batch_id TEXT PRIMARY KEY,
|
||||
edits TEXT,
|
||||
status TEXT,
|
||||
message TEXT,
|
||||
requested_at TEXT,
|
||||
started_at TEXT,
|
||||
finished_at TEXT
|
||||
);
|
||||
""")
|
||||
conn.commit()
|
||||
|
||||
|
||||
def upsert_batch(conn, batch_id, output, opts):
|
||||
ts = now_iso()
|
||||
conn.execute("""
|
||||
INSERT INTO doc_review_batch
|
||||
(id, output, paper, enhance, dpi, orientation, status, summary, warnings,
|
||||
schema_version, created_at, updated_at)
|
||||
VALUES (?,?,?,?,?,?,'running',NULL,NULL,?,?,?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
output=excluded.output, paper=excluded.paper, enhance=excluded.enhance,
|
||||
dpi=excluded.dpi, orientation=excluded.orientation,
|
||||
schema_version=excluded.schema_version, updated_at=excluded.updated_at
|
||||
""", (batch_id, output, opts.get("paper"), opts.get("enhance"),
|
||||
str(opts.get("dpi")), opts.get("orientation"),
|
||||
SCHEMA_VERSION, ts, ts))
|
||||
conn.commit()
|
||||
|
||||
|
||||
def replace_pages(conn, batch_id, pages):
|
||||
conn.execute("DELETE FROM doc_review_page WHERE batch_id = ?", (batch_id,))
|
||||
for p in pages:
|
||||
conn.execute("""
|
||||
INSERT INTO doc_review_page
|
||||
(batch_id, idx, source, source_w, source_h, w, h,
|
||||
auto_corners, auto_corners_norm, corners, auto_rotate, rotate,
|
||||
deleted, sort, status, reason, thumb, error)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
""", (batch_id, int(p.get("idx")), p.get("source"),
|
||||
p.get("source_w"), p.get("source_h"), p.get("w"), p.get("h"),
|
||||
_json(p.get("auto_corners")), _json(p.get("auto_corners_norm")),
|
||||
_json(p.get("corners")), int(p.get("auto_rotate") or 0),
|
||||
int(p.get("rotate") or 0), 1 if p.get("deleted") else 0,
|
||||
int(p.get("sort") or 0), p.get("status"), p.get("reason"),
|
||||
p.get("thumb"), p.get("error")))
|
||||
conn.commit()
|
||||
|
||||
|
||||
def _row(r):
|
||||
return dict(r) if r is not None else None
|
||||
|
||||
|
||||
def get_batch(conn, batch_id):
|
||||
return _row(conn.execute("SELECT * FROM doc_review_batch WHERE id = ?", (batch_id,)).fetchone())
|
||||
|
||||
|
||||
def get_latest_batch(conn):
|
||||
return _row(conn.execute(
|
||||
"SELECT * FROM doc_review_batch ORDER BY created_at DESC, updated_at DESC LIMIT 1").fetchone())
|
||||
|
||||
|
||||
def get_pages(conn, batch_id):
|
||||
rows = [_row(r) for r in conn.execute(
|
||||
"SELECT * FROM doc_review_page WHERE batch_id = ? ORDER BY sort, idx", (batch_id,))]
|
||||
for r in rows:
|
||||
r["auto_corners"] = _loads(r.get("auto_corners"))
|
||||
r["auto_corners_norm"] = _loads(r.get("auto_corners_norm"))
|
||||
r["corners"] = _loads(r.get("corners"))
|
||||
return rows
|
||||
|
||||
|
||||
def get_job(conn, batch_id):
|
||||
r = _row(conn.execute("SELECT * FROM doc_review_job WHERE batch_id = ?", (batch_id,)).fetchone())
|
||||
if r:
|
||||
r["edits_obj"] = _loads(r.get("edits"))
|
||||
return r
|
||||
|
||||
|
||||
def upsert_job(conn, batch_id, edits, status="queued", message=None):
|
||||
ts = now_iso()
|
||||
conn.execute("""
|
||||
INSERT INTO doc_review_job(batch_id, edits, status, message, requested_at, started_at, finished_at)
|
||||
VALUES (?,?,?,?,?,NULL,NULL)
|
||||
ON CONFLICT(batch_id) DO UPDATE SET
|
||||
edits=excluded.edits, status=excluded.status, message=excluded.message,
|
||||
requested_at=excluded.requested_at, started_at=NULL, finished_at=NULL
|
||||
""", (batch_id, _json(edits), status, message, ts))
|
||||
conn.commit()
|
||||
|
||||
|
||||
def set_job(conn, batch_id, status, message=None):
|
||||
ts = now_iso()
|
||||
if status == "running":
|
||||
conn.execute("UPDATE doc_review_job SET status=?, message=?, started_at=?, finished_at=NULL WHERE batch_id=?",
|
||||
(status, message, ts, batch_id))
|
||||
elif status in ("done", "error"):
|
||||
conn.execute("UPDATE doc_review_job SET status=?, message=?, finished_at=? WHERE batch_id=?",
|
||||
(status, message, ts, batch_id))
|
||||
else:
|
||||
conn.execute("UPDATE doc_review_job SET status=?, message=? WHERE batch_id=?",
|
||||
(status, message, batch_id))
|
||||
conn.commit()
|
||||
|
||||
|
||||
def finish_batch(conn, batch_id, ok, summary, warnings, status=None):
|
||||
ts = now_iso()
|
||||
if status is None:
|
||||
status = "ready" if ok else "error"
|
||||
conn.execute("UPDATE doc_review_batch SET status=?, summary=?, warnings=?, updated_at=? WHERE id=?",
|
||||
(status, summary, _json(warnings or []), ts, batch_id))
|
||||
conn.commit()
|
||||
|
||||
|
||||
def prune(conn, keep=5, protect=None):
|
||||
"""只保留最近 keep 个批次;protect 指定的批次永不删(防止在旧批次上重生成时被裁掉)。"""
|
||||
rows = conn.execute(
|
||||
"SELECT id FROM doc_review_batch ORDER BY created_at DESC, updated_at DESC").fetchall()
|
||||
kept = [r["id"] for r in rows[:keep]]
|
||||
if protect and protect not in kept:
|
||||
kept.append(protect)
|
||||
old = [r["id"] for r in rows if r["id"] not in kept]
|
||||
for bid in old:
|
||||
conn.execute("DELETE FROM doc_review_page WHERE batch_id=?", (bid,))
|
||||
conn.execute("DELETE FROM doc_review_job WHERE batch_id=?", (bid,))
|
||||
conn.execute("DELETE FROM doc_review_batch WHERE id=?", (bid,))
|
||||
if old:
|
||||
conn.commit()
|
||||
return old, kept
|
||||
@@ -1,69 +0,0 @@
|
||||
"""拉正合成的核心模块:四边形透视变换 + 按页拼版。
|
||||
|
||||
由 pipeline.py 的 build 子命令调用,不单独提供命令行入口(原来那套
|
||||
--grid/--page/--check 已分别由 prepare / build / check 接管,免得两个入口各说各话)。
|
||||
|
||||
设计原则:
|
||||
1. 不做中间校正。角点只用来定义纸张四边,几何变换一次到位,
|
||||
校正结果直接进 PDF;所有质量判定都在最终 PDF 上做。
|
||||
2. 不在流程里"自动收紧/自动修补"。这类启发式规则容易误裁内容,
|
||||
只保留能明确判定对错的检验项。
|
||||
3. 判定阈值来自每页图像自身统计,不是针对某份文档调出来的常量。
|
||||
"""
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, HERE)
|
||||
|
||||
import numpy as np
|
||||
from homography import brighten, warp_perspective
|
||||
from PIL import Image
|
||||
|
||||
# 纸张宽高比(宽/高):pipeline 与 run 共用这一份,避免两套系数漂移
|
||||
PAPERS = {'a4': 210 / 297, 'letter': 8.5 / 11, '16k': 185 / 260, 'b5': 176 / 250}
|
||||
|
||||
|
||||
def parse_page(spec):
|
||||
src, _, quad_s = spec.partition(':')
|
||||
if not src or not quad_s:
|
||||
raise SystemExit('格式应为 "图片:左上;右上;右下;左下":%s' % spec)
|
||||
quad = []
|
||||
for pt in quad_s.split(';'):
|
||||
x, y = pt.split(',')
|
||||
quad.append((float(x), float(y)))
|
||||
if len(quad) != 4:
|
||||
raise SystemExit('角点必须 4 个:%s' % spec)
|
||||
return src, quad
|
||||
|
||||
|
||||
def build(specs, out_pdf, workdir='out', ratio=None):
|
||||
"""一次几何变换得到页面图,直接合并。不做二次裁切。
|
||||
|
||||
ratio=None 时按四边形自然尺寸输出(保留照片原始比例);
|
||||
给定 ratio(如 A4=0.707)时强制映射到该纸张比例,纠正透视压缩。
|
||||
"""
|
||||
os.makedirs(workdir, exist_ok=True)
|
||||
pages = []
|
||||
for i, spec in enumerate(specs, 1):
|
||||
src, quad = parse_page(spec)
|
||||
im = Image.open(src).convert('RGB')
|
||||
if ratio is None:
|
||||
page = brighten(warp_perspective(im, quad))
|
||||
else:
|
||||
q = np.array(quad, float)
|
||||
wpx = (np.linalg.norm(q[1] - q[0]) + np.linalg.norm(q[2] - q[3])) / 2
|
||||
hpx = wpx / ratio
|
||||
page = brighten(warp_perspective(
|
||||
im, quad, out_size=(int(round(wpx)), int(round(hpx)))))
|
||||
path = os.path.join(workdir, 'p%d.png' % i)
|
||||
page.save(path)
|
||||
pages.append(path)
|
||||
nw, nh = page.size
|
||||
print(' 第 %d 页 %-28s -> %s %dx%d 比例 %.3f'
|
||||
% (i, os.path.basename(src), path, nw, nh, nw / nh))
|
||||
|
||||
subprocess.run([sys.executable, os.path.join(HERE, 'make_pdf.py')]
|
||||
+ pages + [out_pdf], check=True)
|
||||
return pages
|
||||
@@ -0,0 +1,225 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""合成样张回归测试(不依赖真实照片)。
|
||||
|
||||
覆盖:
|
||||
1. 单页透视校正 + JSON 输出 + PDF 生成 + state 文件
|
||||
2. EXIF Orientation=6 的转正
|
||||
3. 批次方向不一致时的旋转告警
|
||||
4. paper=a4 的画布尺寸
|
||||
5. orientation=none 的混排
|
||||
6. orientation=landscape 的强制方向
|
||||
|
||||
运行:
|
||||
python3 skills/document-photo-to-pdf/doc_deskew/smoke_test.py
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
SKILL = os.path.dirname(HERE)
|
||||
sys.path.insert(0, HERE)
|
||||
from homography import _solve_h # noqa: E402
|
||||
import check # noqa: E402
|
||||
from params import P # noqa: E402
|
||||
|
||||
W_PAGE, H_PAGE = 900, 1272
|
||||
QUAD = [(200, 240), (1080, 190), (1120, 1420), (150, 1380)]
|
||||
PHOTO_SIZE = (1200, 1600)
|
||||
|
||||
|
||||
def make_page():
|
||||
im = Image.new("RGB", (W_PAGE, H_PAGE), "white")
|
||||
d = ImageDraw.Draw(im)
|
||||
for i in range(18):
|
||||
y = 90 + i * 58
|
||||
d.line([(90, y), (W_PAGE - 90, y)], fill=(30, 30, 30), width=8)
|
||||
if i % 4 == 0:
|
||||
d.line([(90, y + 18), (W_PAGE - 300, y + 18)],
|
||||
fill=(60, 60, 60), width=5)
|
||||
return im
|
||||
|
||||
|
||||
def make_photo(page, quad=QUAD, size=PHOTO_SIZE):
|
||||
bg = Image.new("RGB", size, (38, 42, 48))
|
||||
dst = [(0, 0), (W_PAGE - 1, 0), (W_PAGE - 1, H_PAGE - 1),
|
||||
(0, H_PAGE - 1)]
|
||||
H = _solve_h(quad, dst)
|
||||
H = H / H[2, 2]
|
||||
coeffs = (H[0, 0], H[0, 1], H[0, 2],
|
||||
H[1, 0], H[1, 1], H[1, 2],
|
||||
H[2, 0], H[2, 1])
|
||||
warped = page.transform(size, Image.PERSPECTIVE, coeffs, Image.BICUBIC)
|
||||
mask = Image.new("L", (W_PAGE, H_PAGE), 255)
|
||||
mask = mask.transform(size, Image.PERSPECTIVE, coeffs, Image.BICUBIC)
|
||||
mask = mask.point(lambda v: 255 if v > 127 else 0)
|
||||
photo = bg.copy()
|
||||
photo.paste(warped, mask=mask)
|
||||
return photo
|
||||
|
||||
|
||||
def run_skill(args, cwd):
|
||||
proc = subprocess.run(
|
||||
[sys.executable, os.path.join(SKILL, "run.py"),
|
||||
json.dumps(args, ensure_ascii=False)],
|
||||
cwd=cwd, capture_output=True, text=True)
|
||||
assert proc.returncode == 0, proc.stderr
|
||||
# stdout 必须是纯 JSON(不含进度日志)
|
||||
data = json.loads(proc.stdout)
|
||||
assert isinstance(data, dict)
|
||||
return data
|
||||
|
||||
|
||||
def assert_corners(got, want, tol=35):
|
||||
assert len(got) == 4, got
|
||||
for (gx, gy), (ex, ey) in zip(got, want):
|
||||
assert abs(gx - ex) <= tol and abs(gy - ey) <= tol, (
|
||||
"角点偏差过大:got=%s want=%s" % (got, want))
|
||||
|
||||
|
||||
def assert_checks(res):
|
||||
checks = res.get("checks") or {}
|
||||
if checks.get("ok") is None:
|
||||
print("[skip] PDF 渲染不可用:%s" % checks.get("error"))
|
||||
return
|
||||
assert checks.get("ok") is True, checks
|
||||
assert checks["page_count"]["ok"], checks
|
||||
|
||||
|
||||
def verbose(label, res):
|
||||
if not os.environ.get("SMOKE_VERBOSE"):
|
||||
return
|
||||
print("[%s] %s" % (label, json.dumps({
|
||||
"summary": res.get("summary"),
|
||||
"batch": res.get("batch"),
|
||||
"warnings": res.get("warnings"),
|
||||
"checks": res.get("checks"),
|
||||
}, ensure_ascii=False, indent=2)))
|
||||
|
||||
|
||||
def make_composed_page(strip=None, edge_text=False):
|
||||
"""造一张“PDF 渲染页”:白画布 + 居中纸面(~245 亮度)。"""
|
||||
canvas = Image.new("RGB", (1240, 1754), "white")
|
||||
paper = Image.new("RGB", (1160, 1640), (245, 245, 245))
|
||||
d = ImageDraw.Draw(paper)
|
||||
for i in range(12):
|
||||
y = 120 + i * 90
|
||||
d.line([(100, y), (1060, y)], fill=(30, 30, 30), width=8)
|
||||
if strip is not None:
|
||||
d.rectangle([0, 0, 1159, strip - 1], fill=(60, 60, 60))
|
||||
if edge_text:
|
||||
d.rectangle([60, 0, 1100, 4], fill=(0, 0, 0))
|
||||
canvas.paste(paper, (40, 57))
|
||||
return canvas
|
||||
|
||||
|
||||
def test_analyse():
|
||||
"""直接验证 check.analyse 量的是纸边,不是 PDF 白边。"""
|
||||
normal = check.analyse(make_composed_page(), P)
|
||||
assert not normal["border_bad"], normal["border_bad"]
|
||||
assert not normal["cut"], normal["cut"]
|
||||
|
||||
residue = check.analyse(make_composed_page(strip=12), P)
|
||||
assert "top" in residue["border_bad"], residue["border_bad"]
|
||||
assert residue["edge_dark"]["top"] > P["check"]["edge_dark_frac"], residue
|
||||
|
||||
cut = check.analyse(make_composed_page(edge_text=True), P)
|
||||
assert any(k == "top" for k, _ in cut["cut"]), cut["cut"]
|
||||
|
||||
|
||||
def main():
|
||||
test_analyse()
|
||||
page = make_page()
|
||||
photo = make_photo(page)
|
||||
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
# 1. 单页 + auto
|
||||
src = os.path.join(td, "图1.jpg")
|
||||
photo.save(src, quality=92)
|
||||
res = run_skill({"images": ["图1.jpg"], "output": "结果.pdf",
|
||||
"paper": "auto"}, td)
|
||||
assert res["ok"], res
|
||||
assert res["output"]["pages"] == 1, res
|
||||
assert os.path.exists(res["output"]["path"])
|
||||
assert_corners(res["pages"][0]["corners"], QUAD)
|
||||
assert_checks(res)
|
||||
# 低分辨率纸面也要放大到内容区,避免各页大小不一
|
||||
assert res["pages"][0]["out_size"][0] == 1160, res["pages"][0]
|
||||
assert os.path.exists(res["output"]["state"])
|
||||
verbose("auto", res)
|
||||
|
||||
# check.py CLI 也跑一遍(executor 有渲染后端时)
|
||||
if (res.get("checks") or {}).get("ok") is not None:
|
||||
cli = subprocess.run(
|
||||
[sys.executable, os.path.join(HERE, "check.py"),
|
||||
res["output"]["path"], "--expect-pages", "1"],
|
||||
capture_output=True, text=True)
|
||||
assert cli.returncode == 0, cli.stdout + cli.stderr
|
||||
|
||||
# 2. EXIF Orientation=6:存 90° CCW + 标记 6,读入应转回原方向
|
||||
stored = photo.transpose(Image.ROTATE_90)
|
||||
exif = Image.Exif()
|
||||
exif[274] = 6
|
||||
exif_src = os.path.join(td, "exif.jpg")
|
||||
stored.save(exif_src, quality=92, exif=exif)
|
||||
res2 = run_skill({"images": ["exif.jpg"], "output": "exif.pdf"}, td)
|
||||
assert res2["ok"], res2
|
||||
assert_corners(res2["pages"][0]["corners"], QUAD)
|
||||
assert_checks(res2)
|
||||
verbose("exif", res2)
|
||||
|
||||
# 3. 批次方向:第二页横拍,应被旋转并告警
|
||||
sideways = photo.transpose(Image.ROTATE_270)
|
||||
side_src = os.path.join(td, "图2.jpg")
|
||||
sideways.save(side_src, quality=92)
|
||||
res3 = run_skill({"images": ["图1.jpg", "图2.jpg"],
|
||||
"output": "批次.pdf"}, td)
|
||||
assert res3["ok"], res3
|
||||
assert res3["output"]["pages"] == 2, res3
|
||||
assert res3["batch"]["rotated_pages"] == [2], res3["batch"]
|
||||
assert res3["pages"][1]["warnings"], res3["pages"][1]
|
||||
# 同批页面即使一竖一横,也应放大/缩放到同一版式宽度
|
||||
s1, s2 = res3["pages"][0]["out_size"], res3["pages"][1]["out_size"]
|
||||
assert s1[0] == s2[0] == 1160, (s1, s2)
|
||||
assert abs(s1[1] - s2[1]) <= 2, (s1, s2)
|
||||
assert_checks(res3)
|
||||
verbose("batch", res3)
|
||||
|
||||
# 4. paper=a4 画布
|
||||
res4 = run_skill({"images": ["图1.jpg"], "output": "a4.pdf",
|
||||
"paper": "a4"}, td)
|
||||
assert res4["ok"], res4
|
||||
assert res4["batch"]["canvas_px"] == [1240, 1754], res4["batch"]
|
||||
assert res4["batch"]["content_px"] == [1160, 1674], res4["batch"]
|
||||
assert_checks(res4)
|
||||
verbose("a4", res4)
|
||||
|
||||
# 5. orientation=none:不旋转,允许横竖混排
|
||||
res5 = run_skill({"images": ["图1.jpg", "图2.jpg"],
|
||||
"output": "none.pdf", "orientation": "none"}, td)
|
||||
assert res5["ok"], res5
|
||||
assert res5["batch"]["orientation"] == "mixed", res5["batch"]
|
||||
assert res5["batch"]["rotated_pages"] == [], res5["batch"]
|
||||
assert res5["batch"]["canvas_px"] is None, res5["batch"]
|
||||
assert_checks(res5)
|
||||
verbose("none", res5)
|
||||
|
||||
# 6. orientation=landscape:强制横版,第一页(竖版)被旋转
|
||||
res6 = run_skill({"images": ["图1.jpg", "图2.jpg"],
|
||||
"output": "land.pdf",
|
||||
"orientation": "landscape"}, td)
|
||||
assert res6["ok"], res6
|
||||
assert res6["batch"]["rotated_pages"] == [1], res6["batch"]
|
||||
assert_checks(res6)
|
||||
verbose("landscape", res6)
|
||||
|
||||
print("SMOKE OK")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,236 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""state.json v2 与 edits.json:归一化坐标、页面状态分级、编辑覆盖。
|
||||
|
||||
坐标约定
|
||||
--------
|
||||
corners_norm / edits 里的 "c" 一律是归一化 0~1,相对 exif_transpose 之后的
|
||||
**原图**:[[x_tl,y_tl],[x_tr,y_tr],[x_br,y_br],[x_bl,y_bl]]。
|
||||
原图像素坐标只在检测、渲染时用;进出 state 一律换成归一化,
|
||||
换预览分辨率、换 UI 画布都不会错位。
|
||||
|
||||
页面状态(取代"整批失败")
|
||||
--------------------------
|
||||
ready 已就绪,无需人看
|
||||
review 建议看一眼(掩膜回退角点、覆盖率低、纸型存疑)
|
||||
needs_orientation 宽高比接近正方形,横竖判不出来,需要人点旋转
|
||||
needs_corners 必须补角(检测为空、框内纸面占比过低)
|
||||
render_failed 出页失败,已用白页占位(极少见)
|
||||
failed 原图打不开,本页已跳过(传错文件)
|
||||
deleted 人主动删掉的页,不进出件
|
||||
|
||||
严重度只用来决定"一页命中多条规则时记哪一条",不参与任何几何计算。
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
|
||||
STATE_SCHEMA = "document-photo-to-pdf/2"
|
||||
EDITS_SCHEMA = "review/1"
|
||||
|
||||
# 顺序即严重度,越靠后越需要人介入
|
||||
STATUS_ORDER = ["ready", "review", "needs_orientation",
|
||||
"needs_corners", "render_failed", "failed", "deleted"]
|
||||
_SEV = {s: i for i, s in enumerate(STATUS_ORDER)}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 坐标
|
||||
|
||||
def norm_quad(quad, size):
|
||||
"""原图像素坐标 -> 归一化 0~1(4 位小数,300dpi 往返误差远小于 1px)。"""
|
||||
w = float(size[0]) or 1.0
|
||||
h = float(size[1]) or 1.0
|
||||
return [[round(float(p[0]) / w, 4), round(float(p[1]) / h, 4)]
|
||||
for p in quad]
|
||||
|
||||
|
||||
def denorm_quad(norm, size):
|
||||
"""归一化 0~1 -> 原图像素坐标。"""
|
||||
w, h = float(size[0]), float(size[1])
|
||||
return [[float(p[0]) * w, float(p[1]) * h] for p in norm]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 状态
|
||||
|
||||
def escalate(status, reason, cand, cand_reason):
|
||||
"""取更严重的一对 (status, reason);同级保留先到的。"""
|
||||
if _SEV.get(cand, 0) > _SEV.get(status, 0):
|
||||
return cand, cand_reason
|
||||
return status, reason
|
||||
|
||||
|
||||
def grade_page(meta, P, ref_ratio=None):
|
||||
"""按方案第 6 节的表,把一页的检测数字转成"要不要人看"。
|
||||
|
||||
纯路由:不重新检测、不改数字,只决定这一页在审核页上标不标 `!`。
|
||||
ref_ratio 是这张纸应有的宽高比(竖版为正);None 表示未知,跳过纸型判断。
|
||||
"""
|
||||
g = P["gate"]
|
||||
dead = g["aspect_deadband"]
|
||||
status, reason = "ready", None
|
||||
|
||||
if meta.get("fallback"):
|
||||
status, reason = escalate(status, reason, "review", "mask_fallback")
|
||||
|
||||
cov = meta.get("coverage") or {}
|
||||
if cov and min(float(v) for v in cov.values()) < g["coverage_warn"]:
|
||||
status, reason = escalate(status, reason, "review", "low_coverage")
|
||||
|
||||
metrics = (meta.get("validation") or {}).get("metrics") or {}
|
||||
frac = metrics.get("inner_paper_frac")
|
||||
if frac is not None and float(frac) <= g["inner_paper_min"]:
|
||||
status, reason = escalate(status, reason,
|
||||
"needs_corners", "inner_paper_low")
|
||||
|
||||
aspect = meta.get("aspect")
|
||||
if aspect:
|
||||
if abs(float(aspect) - 1.0) <= dead:
|
||||
# 横竖看不出来:不猜,交给人点旋转
|
||||
status, reason = escalate(status, reason,
|
||||
"needs_orientation", "aspect_ambiguous")
|
||||
elif ref_ratio:
|
||||
# 同向折算后再比:横放的 A4 不该被判成异形纸
|
||||
r = float(ref_ratio)
|
||||
if float(aspect) > 1.0:
|
||||
r = 1.0 / r if r > 0 else r
|
||||
dev = abs(float(aspect) - r) / r if r > 0 else 0.0
|
||||
if dev > g["paper_mismatch_pct"] / 100.0:
|
||||
status, reason = escalate(status, reason,
|
||||
"review", "paper_mismatch")
|
||||
return status, reason
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- edits
|
||||
|
||||
def _quad_ok(c):
|
||||
"""校验归一化四边形:4 点、每点 2 数、范围放宽到 -1~2(角点允许拖出画面)。"""
|
||||
if not isinstance(c, (list, tuple)) or len(c) != 4:
|
||||
return None
|
||||
out = []
|
||||
for p in c:
|
||||
if not isinstance(p, (list, tuple)) or len(p) != 2:
|
||||
return None
|
||||
try:
|
||||
x, y = float(p[0]), float(p[1])
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
# 方案第 11 节:纸被切掉一块时角点必须能拖到画面外,所以下限放到 -1
|
||||
if not (-1.0 <= x <= 2.0 and -1.0 <= y <= 2.0):
|
||||
return None
|
||||
out.append([x, y])
|
||||
return out
|
||||
|
||||
|
||||
def _rot_ok(v):
|
||||
"""旋转角归一到 0/90/180/270,认不出来按 0(不转)处理。"""
|
||||
try:
|
||||
r = int(round(float(v))) % 360
|
||||
except (TypeError, ValueError):
|
||||
return 0
|
||||
return r if r in (0, 90, 180, 270) else 0
|
||||
|
||||
|
||||
def load_edits(value):
|
||||
"""把 edits.json 读成内部结构;value 是路径、dict 或 None。
|
||||
|
||||
只校验结构与范围,不认识的多余字段一律忽略——前端的版本总比后端快,
|
||||
不该因为多了一个字段就拒绝出件。
|
||||
"""
|
||||
if value is None or value == "" or value == {}:
|
||||
return None
|
||||
if isinstance(value, str):
|
||||
path = os.path.abspath(os.path.expanduser(value.strip()))
|
||||
if not os.path.isfile(path):
|
||||
raise ValueError("edits 文件不存在:%s" % path)
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
value = json.load(f)
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError("edits 必须是 JSON 对象或 edits.json 路径")
|
||||
|
||||
batch = value.get("batch")
|
||||
batch = dict(batch) if isinstance(batch, dict) else {}
|
||||
|
||||
pages = {}
|
||||
for item in (value.get("pages") or []):
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
try:
|
||||
idx = int(item.get("i"))
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if idx < 1:
|
||||
continue
|
||||
rec = {}
|
||||
q = _quad_ok(item.get("c"))
|
||||
if q:
|
||||
rec["corners_norm"] = q
|
||||
if item.get("r") is not None:
|
||||
rec["rotate"] = _rot_ok(item.get("r"))
|
||||
if rec:
|
||||
pages[idx] = rec
|
||||
|
||||
order = []
|
||||
for v in (value.get("order") or []):
|
||||
try:
|
||||
order.append(int(v))
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
# order 缺省/为空 = 按 images 传入顺序出件,调用方自己兜底
|
||||
|
||||
deleted = set()
|
||||
for v in (value.get("deleted") or []):
|
||||
try:
|
||||
deleted.add(int(v))
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
|
||||
return {"schema": EDITS_SCHEMA, "batch": batch, "pages": pages,
|
||||
"order": order, "deleted": deleted}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- state v2
|
||||
|
||||
_STATE_PAGE_KEYS = ("index", "source", "source_size", "auto_corners",
|
||||
"corners_norm", "rotate", "rotate_source",
|
||||
"status", "reason")
|
||||
|
||||
|
||||
def build_state(batch, pages):
|
||||
"""state.json v2:只放重建这次结果需要的东西,不放 PDF 检验数据。
|
||||
|
||||
pages 项由调用方组装好,这里只裁字段、保顺序,不再做判断。
|
||||
"""
|
||||
return {
|
||||
"schema": STATE_SCHEMA,
|
||||
"batch": {
|
||||
"doc_type": batch.get("doc_type"),
|
||||
"paper": batch.get("paper"),
|
||||
"enhance": batch.get("enhance"),
|
||||
"dpi": batch.get("dpi"),
|
||||
"orientation": batch.get("orientation"),
|
||||
},
|
||||
"pages": [{k: p.get(k) for k in _STATE_PAGE_KEYS} for p in pages],
|
||||
}
|
||||
|
||||
|
||||
def load_state(value):
|
||||
"""读上一份 state v2(可选输入):按 index 取 auto_corners,供"恢复自动"。
|
||||
|
||||
缺文件、旧 schema、字段不全都当成没传——state 是参考,不是契约。
|
||||
"""
|
||||
if value in (None, ""):
|
||||
return {}
|
||||
try:
|
||||
if isinstance(value, str):
|
||||
path = os.path.abspath(os.path.expanduser(value.strip()))
|
||||
if not os.path.isfile(path):
|
||||
return {}
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
value = json.load(f)
|
||||
if not isinstance(value, dict) or value.get("schema") != STATE_SCHEMA:
|
||||
return {}
|
||||
out = {}
|
||||
for item in (value.get("pages") or []):
|
||||
if isinstance(item, dict) and item.get("index") is not None:
|
||||
out[int(item["index"])] = item
|
||||
return out
|
||||
except Exception:
|
||||
return {}
|
||||
@@ -0,0 +1,149 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""角点几何校验:纯函数,返回结论和指标,不打印、不落盘。
|
||||
|
||||
判定只看几何与亮度关系,不依赖固定像素常量:
|
||||
1. 四边形凸,且顺序为 左上→右上→右下→左下
|
||||
2. 越界幅度在允许范围内(角点出画面属正常)
|
||||
3. 框内以纸面(亮像素)为主
|
||||
4. 每边内侧都是纸面(混入桌面说明取点落在纸外)
|
||||
外侧偏亮只作提示:可能是取点偏内,也可能是纸边倾斜,亮度无法区分。
|
||||
"""
|
||||
import numpy as np
|
||||
|
||||
|
||||
def _sample_map(a, pts, n, shrink=1.0, outward=0.0):
|
||||
"""在四边形内部取网格点(shrink<1 向内收),或在外部环带取点(outward>0)。"""
|
||||
h, w = a.shape
|
||||
tl, tr, br, bl = [np.asarray(p, dtype=np.float64) for p in pts]
|
||||
c = (tl + tr + br + bl) / 4.0
|
||||
if shrink != 1.0:
|
||||
tl, tr, br, bl = [c + (p - c) * shrink for p in (tl, tr, br, bl)]
|
||||
if outward > 0:
|
||||
tl, tr, br, bl = [c + (p - c) * (1.0 + outward)
|
||||
for p in (tl, tr, br, bl)]
|
||||
vals = []
|
||||
for u in np.linspace(0.03, 0.97, n):
|
||||
p1 = tl + (tr - tl) * u
|
||||
p2 = bl + (br - bl) * u
|
||||
for v in np.linspace(0.03, 0.97, n):
|
||||
x, y = p1 + (p2 - p1) * v
|
||||
xi, yi = int(round(x)), int(round(y))
|
||||
if 0 <= xi < w and 0 <= yi < h:
|
||||
vals.append(a[yi, xi])
|
||||
return np.asarray(vals)
|
||||
|
||||
|
||||
def _cross(o, a, b):
|
||||
return ((a[0] - o[0]) * (b[1] - o[1])
|
||||
- (a[1] - o[1]) * (b[0] - o[0]))
|
||||
|
||||
|
||||
def validate(img, pts, P):
|
||||
"""返回 {ok, metrics, failures, warnings}。"""
|
||||
g = P["gate"]
|
||||
a = np.asarray(img.convert("L")).astype(np.float32)
|
||||
h, w = a.shape
|
||||
paper = float(np.percentile(a, 80))
|
||||
back = float(np.percentile(a, 2))
|
||||
thr = back + (paper - back) * 0.5
|
||||
|
||||
failures = []
|
||||
warnings = []
|
||||
metrics = {}
|
||||
|
||||
# 1. 凸性与顺序
|
||||
signs = [_cross(pts[i], pts[(i + 1) % 4], pts[(i + 2) % 4])
|
||||
for i in range(4)]
|
||||
convex = all(s > 0 for s in signs)
|
||||
metrics["convex"] = bool(convex)
|
||||
if not convex:
|
||||
failures.append("角点顺序/凸性不对,应为 左上→右上→右下→左下")
|
||||
|
||||
# 2. 越界幅度
|
||||
xs = [p[0] for p in pts]
|
||||
ys = [p[1] for p in pts]
|
||||
out = [max(0.0, -min(xs)), max(0.0, max(xs) - w),
|
||||
max(0.0, -min(ys)), max(0.0, max(ys) - h)]
|
||||
oob = max(out) / float(max(w, h)) * 100.0
|
||||
metrics["oob_pct"] = round(float(oob), 2)
|
||||
if oob >= g["oob_max_pct"]:
|
||||
failures.append("角点越界 %.1f%%,超过 %.0f%%"
|
||||
% (oob, g["oob_max_pct"]))
|
||||
|
||||
# 3. 框内纸面占比
|
||||
inner = _sample_map(a, pts, g["sample_n"], shrink=g["sample_shrink"])
|
||||
if inner.size == 0:
|
||||
frac_paper = 0.0
|
||||
else:
|
||||
frac_paper = float((inner > thr).mean())
|
||||
metrics["inner_paper_frac"] = round(frac_paper, 4)
|
||||
if frac_paper <= g["inner_paper_min"]:
|
||||
failures.append("框内纸面占比 %.0f%%,偏低(可能取到桌面)"
|
||||
% (frac_paper * 100))
|
||||
|
||||
# 4. 四边外侧:只提示
|
||||
names = ["上边", "右边", "下边", "左边"]
|
||||
outer_dark = {}
|
||||
span = float(max(w, h))
|
||||
for i in range(4):
|
||||
p1, p2 = np.asarray(pts[i], float), np.asarray(pts[(i + 1) % 4], float)
|
||||
mid = (p1 + p2) / 2.0
|
||||
c = sum(np.asarray(p, float) for p in pts) / 4.0
|
||||
d = mid - c
|
||||
norm = float(np.linalg.norm(d))
|
||||
if norm < 1e-6:
|
||||
continue
|
||||
d = d / norm
|
||||
vals = []
|
||||
for t in np.linspace(0.15, 0.85, 12):
|
||||
base = p1 + (p2 - p1) * t
|
||||
for k in (span * 0.02, span * 0.035, span * 0.05):
|
||||
x, y = base + d * k
|
||||
xi, yi = int(round(x)), int(round(y))
|
||||
if 0 <= xi < w and 0 <= yi < h:
|
||||
vals.append(a[yi, xi])
|
||||
if len(vals) < 8:
|
||||
continue
|
||||
vals = np.asarray(vals)
|
||||
dark_frac = float((vals < thr).mean())
|
||||
outer_dark[names[i]] = round(dark_frac, 3)
|
||||
if dark_frac < g["outer_dark_min"]:
|
||||
warnings.append("%s外侧仍偏亮(可能取点偏内,也可能纸边倾斜)"
|
||||
% names[i])
|
||||
metrics["outer_dark"] = outer_dark
|
||||
|
||||
# 5. 四边内侧:必须是纸面
|
||||
inner_paper = {}
|
||||
for i in range(4):
|
||||
p1, p2 = np.asarray(pts[i], float), np.asarray(pts[(i + 1) % 4], float)
|
||||
mid = (p1 + p2) / 2.0
|
||||
c = sum(np.asarray(p, float) for p in pts) / 4.0
|
||||
d = mid - c
|
||||
norm = float(np.linalg.norm(d))
|
||||
if norm < 1e-6:
|
||||
continue
|
||||
d = d / norm
|
||||
vals = []
|
||||
for t in np.linspace(0.15, 0.85, 12):
|
||||
base = p1 + (p2 - p1) * t
|
||||
for k in (span * 0.015, span * 0.03, span * 0.045):
|
||||
x, y = base - d * k
|
||||
xi, yi = int(round(x)), int(round(y))
|
||||
if 0 <= xi < w and 0 <= yi < h:
|
||||
vals.append(a[yi, xi])
|
||||
if len(vals) < 8:
|
||||
continue
|
||||
vals = np.asarray(vals)
|
||||
paper_frac = float((vals > thr).mean())
|
||||
inner_paper[names[i]] = round(paper_frac, 3)
|
||||
if paper_frac < g["inner_edge_min"]:
|
||||
failures.append("%s内侧混入桌面(纸面仅 %.0f%%),取点落在纸外"
|
||||
% (names[i], paper_frac * 100))
|
||||
metrics["inner_paper"] = inner_paper
|
||||
|
||||
return {
|
||||
"ok": not failures,
|
||||
"metrics": metrics,
|
||||
"failures": failures,
|
||||
"warnings": warnings,
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
"""几何校验:四角坐标是否合理覆盖了纸张。
|
||||
|
||||
不依赖图像亮度,只看几何关系,因此不受白底干扰:
|
||||
1. 四边形必须凸,且顺序为 左上→右上→右下→左下
|
||||
2. 四边形必须落在源图范围内(允许少量越界,用于角点出画面的情况)
|
||||
3. 四边形内应基本是纸面(亮像素占多数),说明没有取到桌面
|
||||
4. 沿四条边外侧一圈应是桌面(暗像素占多数),说明没有少取纸张
|
||||
|
||||
任一项失败即说明该角点需调整,并指出是哪条边。
|
||||
|
||||
用法:
|
||||
python3 doc_deskew/verify_quad.py 图.jpg "左上;右上;右下;左下"
|
||||
"""
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def parse(qs):
|
||||
pts = []
|
||||
for pt in qs.split(';'):
|
||||
x, y = pt.split(',')
|
||||
pts.append((float(x), float(y)))
|
||||
if len(pts) != 4:
|
||||
raise SystemExit('需要 4 个角点')
|
||||
return pts
|
||||
|
||||
|
||||
def sample_map(a, pts, n=60, shrink=1.0, outward=0.0):
|
||||
"""在四边形内部取网格点(shrink<1 向内收),或在外部环带取点(outward>0)。"""
|
||||
|
||||
h, w = a.shape
|
||||
tl, tr, br, bl = [np.asarray(p, float) for p in pts]
|
||||
c = (tl + tr + br + bl) / 4.0
|
||||
if shrink != 1.0:
|
||||
tl, tr, br, bl = [c + (p - c) * shrink for p in (tl, tr, br, bl)]
|
||||
if outward > 0:
|
||||
tl, tr, br, bl = [c + (p - c) * (1 + outward) for p in (tl, tr, br, bl)]
|
||||
vals = []
|
||||
for u in np.linspace(0.03, 0.97, n):
|
||||
p1 = tl + (tr - tl) * u
|
||||
p2 = bl + (br - bl) * u
|
||||
for v in np.linspace(0.03, 0.97, n):
|
||||
x, y = p1 + (p2 - p1) * v
|
||||
xi, yi = int(round(x)), int(round(y))
|
||||
if 0 <= xi < w and 0 <= yi < h:
|
||||
vals.append(a[yi, xi])
|
||||
return np.asarray(vals)
|
||||
|
||||
|
||||
def cross(o, a, b):
|
||||
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
|
||||
|
||||
|
||||
def check(img, pts, verbose=True):
|
||||
a = np.asarray(Image.open(img).convert('L')).astype(np.float32)
|
||||
h, w = a.shape
|
||||
paper = float(np.percentile(a, 80))
|
||||
back = float(np.percentile(a, 2))
|
||||
thr = back + (paper - back) * 0.5
|
||||
ok = True
|
||||
|
||||
# 1. 凸性与顺序
|
||||
signs = [cross(pts[i], pts[(i + 1) % 4], pts[(i + 2) % 4]) for i in range(4)]
|
||||
convex = all(s > 0 for s in signs)
|
||||
if verbose:
|
||||
print(' [%s] 角点顺序/凸性' % ('OK' if convex else 'XX'))
|
||||
if not convex:
|
||||
print(' 方向不一致,顺序应为 左上→右上→右下→左下')
|
||||
ok = False
|
||||
|
||||
# 2. 越界幅度
|
||||
xs = [p[0] for p in pts]; ys = [p[1] for p in pts]
|
||||
out = [max(0, -min(xs)), max(0, max(xs) - w), max(0, -min(ys)), max(0, max(ys) - h)]
|
||||
oob = max(out) / max(w, h) * 100
|
||||
good = oob < 15
|
||||
if verbose:
|
||||
print(' [%s] 越界幅度 %.1f%%(角点出画面属正常,上限 15%%)'
|
||||
% ('OK' if good else 'XX', oob))
|
||||
ok &= good
|
||||
|
||||
# 3. 内部应是纸面(亮)
|
||||
inner = sample_map(a, pts, shrink=0.88)
|
||||
frac_paper = float((inner > thr).mean())
|
||||
good = frac_paper > 0.6
|
||||
if verbose:
|
||||
print(' [%s] 框内纸面占比 %.0f%%(应 >60%%,偏低说明取到桌面)'
|
||||
% ('OK' if good else 'XX', frac_paper * 100))
|
||||
ok &= good
|
||||
|
||||
# 4. 四条边外侧应是桌面(暗)——逐边判定,指出问题边
|
||||
names = ['上边', '右边', '下边', '左边']
|
||||
bad_edges = []
|
||||
for i in range(4):
|
||||
p1, p2 = pts[i], pts[(i + 1) % 4]
|
||||
mid = (np.asarray(p1) + np.asarray(p2)) / 2
|
||||
c = sum(np.asarray(p, float) for p in pts) / 4
|
||||
d = mid - c
|
||||
norm = np.linalg.norm(d)
|
||||
if norm < 1e-6:
|
||||
continue
|
||||
d = d / norm
|
||||
# 沿该边外侧取样
|
||||
tc = (np.asarray(p1) + np.asarray(p2)) / 2
|
||||
fracs = []
|
||||
for t in np.linspace(0.15, 0.85, 12):
|
||||
base = np.asarray(p1) + (np.asarray(p2) - np.asarray(p1)) * t
|
||||
span = max(w, h)
|
||||
for k in (span * 0.02, span * 0.035, span * 0.05): # 外移 2%%~5%% 图幅
|
||||
x, y = base + d * k
|
||||
xi, yi = int(round(x)), int(round(y))
|
||||
if 0 <= xi < w and 0 <= yi < h:
|
||||
fracs.append(a[yi, xi])
|
||||
if len(fracs) < 8:
|
||||
continue
|
||||
fracs = np.asarray(fracs)
|
||||
dark_frac = float((fracs < thr).mean())
|
||||
# 外侧大部分应是暗背景;若几乎全亮,说明该边取进了纸内,纸边被切
|
||||
if dark_frac < 0.35:
|
||||
bad_edges.append((names[i], dark_frac))
|
||||
# 5. 沿四条边内侧取样: 应全是纸面; 若混入桌面说明该边取到纸外
|
||||
outside_edges = []
|
||||
for i in range(4):
|
||||
p1, p2 = pts[i], pts[(i + 1) % 4]
|
||||
mid = (np.asarray(p1) + np.asarray(p2)) / 2
|
||||
c = sum(np.asarray(p, float) for p in pts) / 4
|
||||
d = mid - c
|
||||
norm = np.linalg.norm(d)
|
||||
if norm < 1e-6:
|
||||
continue
|
||||
d = d / norm
|
||||
vals = []
|
||||
span = max(w, h)
|
||||
for t in np.linspace(0.15, 0.85, 12):
|
||||
base = np.asarray(p1) + (np.asarray(p2) - np.asarray(p1)) * t
|
||||
for k in (span * 0.015, span * 0.03, span * 0.045):
|
||||
x, y = base - d * k # 向内
|
||||
xi, yi = int(round(x)), int(round(y))
|
||||
if 0 <= xi < w and 0 <= yi < h:
|
||||
vals.append(a[yi, xi])
|
||||
if len(vals) < 8:
|
||||
continue
|
||||
vals = np.asarray(vals)
|
||||
paper_frac = float((vals > thr).mean())
|
||||
if paper_frac < 0.75:
|
||||
outside_edges.append((names[i], paper_frac))
|
||||
good2 = not outside_edges
|
||||
|
||||
if verbose:
|
||||
if not bad_edges:
|
||||
print(' [OK] 四边外侧检测到桌面,纸边已被框住')
|
||||
else:
|
||||
# 外侧仍偏亮有两种可能:取点偏内(会切内容),或纸边本身倾斜导致
|
||||
# 固定外移采样仍落在纸内。二者无法用亮度区分,故只作提示不判错。
|
||||
print(' [--] 提示:下列边外侧仍偏亮(可能取点偏内,也可能是纸边倾斜):')
|
||||
for n, f in bad_edges:
|
||||
print(' %s 外侧纸面占比 %.0f%% → 可尝试再向外扩 3~5px 后目视确认'
|
||||
% (n, (1 - f) * 100))
|
||||
|
||||
if verbose:
|
||||
if good2:
|
||||
print(' [OK] 四边内侧均为纸面')
|
||||
else:
|
||||
print(' [XX] 下列边内侧混入桌面,说明取点落在纸外:')
|
||||
for n, f in outside_edges:
|
||||
print(' %s(内侧纸面仅 %.0f%%)→ 该边角点向内收 5px' % (n, f * 100))
|
||||
ok &= good2
|
||||
return ok
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument('img'); ap.add_argument('quad')
|
||||
a = ap.parse_args()
|
||||
pts = parse(a.quad)
|
||||
print('校验 %s 角点 %s' % (a.img, a.quad))
|
||||
ok = check(a.img, pts)
|
||||
print('\n结论:%s' % ('角点可用' if ok else '需调整上面标 XX 的项'))
|
||||
return 0 if ok else 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,888 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""document-photo-to-pdf 技能入口。
|
||||
|
||||
两种用法共用一条出件内核:
|
||||
全自动:{"images": [...], "output": "..."}
|
||||
只出 PDF,不碰 .app/、不写 data.db。
|
||||
半自动:{"images": [...], "review": "always|auto"}
|
||||
出 PDF,并把审核页发布到 .app/doc-review/、运行记录写进 data.db。
|
||||
重生成:{"batch_id": "dr..."}
|
||||
从 data.db 读上次的源图/输出/编辑,覆盖同名 PDF;args 带 edits 时以 edits
|
||||
为准,不带时读 doc_review_job.edits,再退化为当前页记录(按现在的角点再渲染)。
|
||||
|
||||
stdout 只输出一个 JSON 对象;进度、警告、异常都在字段里,不写 stderr。
|
||||
失败也返回 exit 0,用 ok=false 表达(平台非 0 会给输出追加 [exit code],破坏 JSON)。
|
||||
"""
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import statistics
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
|
||||
from PIL import Image, ImageOps
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, os.path.join(HERE, "doc_deskew"))
|
||||
|
||||
from params import P # noqa: E402
|
||||
from auto_quad import detect # noqa: E402
|
||||
from validate import validate # noqa: E402
|
||||
import compose # noqa: E402
|
||||
import check # noqa: E402
|
||||
import state # noqa: E402
|
||||
import review_store # noqa: E402
|
||||
import review_page # noqa: E402
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 基础
|
||||
|
||||
def _err(code, message, page=None, hint=None):
|
||||
e = {"code": code, "message": message}
|
||||
if page is not None:
|
||||
e["page"] = int(page)
|
||||
if hint:
|
||||
e["hint"] = hint
|
||||
return e
|
||||
|
||||
|
||||
def _json_default(o):
|
||||
try:
|
||||
import numpy as np
|
||||
if isinstance(o, np.integer):
|
||||
return int(o)
|
||||
if isinstance(o, np.floating):
|
||||
return float(o)
|
||||
if isinstance(o, np.ndarray):
|
||||
return o.tolist()
|
||||
except Exception:
|
||||
pass
|
||||
return str(o)
|
||||
|
||||
|
||||
def load_image(path):
|
||||
"""唯一的读图入口:转正 EXIF、压白透明底、转 RGB。"""
|
||||
with Image.open(path) as im:
|
||||
im = ImageOps.exif_transpose(im)
|
||||
if (im.mode in ("RGBA", "LA")
|
||||
or (im.mode == "P" and "transparency" in im.info)):
|
||||
im = im.convert("RGBA")
|
||||
bg = Image.new("RGB", im.size, "white")
|
||||
bg.paste(im, mask=im.split()[-1])
|
||||
im = bg
|
||||
else:
|
||||
im = im.convert("RGB")
|
||||
im.load()
|
||||
return im
|
||||
|
||||
|
||||
def parse_images(args):
|
||||
raw = args.get("images")
|
||||
if raw is None:
|
||||
return []
|
||||
if isinstance(raw, str):
|
||||
return [raw.strip()] if raw.strip() else []
|
||||
if isinstance(raw, (list, tuple)):
|
||||
out, seen = [], set()
|
||||
for v in raw:
|
||||
if not isinstance(v, str) or not v.strip():
|
||||
raise ValueError("images 里只能放路径字符串")
|
||||
p = v.strip()
|
||||
if p not in seen:
|
||||
seen.add(p)
|
||||
out.append(p)
|
||||
return out
|
||||
raise ValueError("images 必须是字符串或字符串数组")
|
||||
|
||||
|
||||
def write_state(path, data):
|
||||
if not path:
|
||||
return False
|
||||
try:
|
||||
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
|
||||
tmp = path + ".tmp"
|
||||
with open(tmp, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
os.replace(tmp, path)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 参数/路径
|
||||
|
||||
def _review_mode(args):
|
||||
if args.get("batch_id") and "review" not in args:
|
||||
return "always"
|
||||
v = args.get("review", "always")
|
||||
if isinstance(v, bool):
|
||||
return "always" if v else "off"
|
||||
s = str(v).strip().lower()
|
||||
if s in ("1", "true", "yes", "on"):
|
||||
s = "always"
|
||||
if s in ("0", "false", "no", ""):
|
||||
s = "off"
|
||||
if s not in ("off", "always", "auto"):
|
||||
raise ValueError("review 只能是 off/always/auto")
|
||||
return s
|
||||
|
||||
|
||||
def _resolve_dpi(v, P):
|
||||
if v is None or str(v).strip() == "":
|
||||
return int(P["render"]["dpi"])
|
||||
s = str(v).strip().lower()
|
||||
opts = P["render"]["dpi_options"]
|
||||
if s in opts:
|
||||
return int(opts[s])
|
||||
try:
|
||||
n = int(float(s))
|
||||
except ValueError:
|
||||
raise ValueError("dpi 只能是 standard/high 或 72~600 的数字")
|
||||
if not (72 <= n <= 600):
|
||||
raise ValueError("dpi 超出范围(72~600):%s" % v)
|
||||
return n
|
||||
|
||||
|
||||
def _dpi_label(v, dpi):
|
||||
# 150/300 归一到审核页的两个档名,页面分段控件才能正确高亮;其它数值原样返回。
|
||||
if int(dpi) == 150:
|
||||
return "standard"
|
||||
if int(dpi) == 300:
|
||||
return "high"
|
||||
return str(dpi)
|
||||
|
||||
|
||||
def _batch_opts(args, ed, P):
|
||||
b = (ed or {}).get("batch") or {}
|
||||
paper_input = b.get("paper", args.get("paper"))
|
||||
if paper_input is None or str(paper_input).strip() == "":
|
||||
paper_input = P["render"]["default_paper"]
|
||||
paper_input = str(paper_input).strip()
|
||||
paper = compose.resolve_paper(paper_input, P)
|
||||
|
||||
orientation = str(b.get("orientation", args.get("orientation") or "auto")).strip().lower()
|
||||
if orientation not in ("auto", "none", "portrait", "landscape"):
|
||||
raise ValueError("orientation 只能是 auto/none/portrait/landscape")
|
||||
|
||||
enhance = str(b.get("enhance", args.get("enhance") or P["render"]["default_enhance"])).strip().lower()
|
||||
if enhance not in P["enhance"]:
|
||||
raise ValueError("enhance 只能是 " + "/".join(P["enhance"].keys()))
|
||||
P["render"]["enhance"] = enhance
|
||||
|
||||
dpi_arg = b.get("dpi", args.get("dpi"))
|
||||
dpi = _resolve_dpi(dpi_arg, P)
|
||||
P["render"]["dpi"] = dpi
|
||||
return paper_input, paper, orientation, enhance, _dpi_label(dpi_arg, dpi), dpi
|
||||
|
||||
|
||||
def _inside(root, path):
|
||||
try:
|
||||
rel = os.path.relpath(os.path.abspath(path), os.path.abspath(root))
|
||||
return rel != ".." and not rel.startswith(".." + os.sep)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def _rel(root, path):
|
||||
try:
|
||||
rel = os.path.relpath(os.path.abspath(path), os.path.abspath(root))
|
||||
if rel == ".." or rel.startswith(".." + os.sep):
|
||||
return os.path.abspath(path)
|
||||
return rel
|
||||
except Exception:
|
||||
return path
|
||||
|
||||
|
||||
def _abs(root, path):
|
||||
p = str(path or "")
|
||||
return p if os.path.isabs(p) else os.path.join(root, p)
|
||||
|
||||
|
||||
def _norm(quad, size):
|
||||
w = float(size[0]) or 1.0
|
||||
h = float(size[1]) or 1.0
|
||||
return [[round(float(p[0]) / w, 4), round(float(p[1]) / h, 4)]
|
||||
for p in quad]
|
||||
|
||||
|
||||
def _denorm(norm, size):
|
||||
w, h = float(size[0]), float(size[1])
|
||||
return [[float(p[0]) * w, float(p[1]) * h] for p in norm]
|
||||
|
||||
|
||||
def _aspect(quad):
|
||||
w, h = compose.aspect_of(quad)
|
||||
return round(float(w / h), 4)
|
||||
|
||||
|
||||
def _fallback_quad(size, P):
|
||||
inset = float(P["gate"]["fallback_inset"])
|
||||
w, h = float(size[0]), float(size[1])
|
||||
return [[inset * w, inset * h], [(1 - inset) * w, inset * h],
|
||||
[(1 - inset) * w, (1 - inset) * h], [inset * w, (1 - inset) * h]]
|
||||
|
||||
|
||||
def _prev_page(rows, idx):
|
||||
for r in rows or []:
|
||||
try:
|
||||
if int(r.get("idx")) == int(idx):
|
||||
return r
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def _edits_from_pages(rows, batch):
|
||||
pages, deleted, order = [], [], []
|
||||
for r in rows or []:
|
||||
try:
|
||||
idx = int(r.get("idx"))
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if r.get("deleted"):
|
||||
deleted.append(idx)
|
||||
continue
|
||||
c = r.get("corners")
|
||||
if c:
|
||||
pages.append({"i": idx, "c": c, "r": int(r.get("rotate") or 0)})
|
||||
order.append(idx)
|
||||
b = {}
|
||||
if batch:
|
||||
b = {"paper": batch.get("paper"), "enhance": batch.get("enhance"),
|
||||
"dpi": batch.get("dpi"), "orientation": batch.get("orientation")}
|
||||
return state.load_edits({"schema": "review/1", "batch": b, "pages": pages,
|
||||
"order": order, "deleted": deleted})
|
||||
|
||||
|
||||
def _targets(args, batch_id):
|
||||
output_arg = args.get("output")
|
||||
if output_arg is not None:
|
||||
output_arg = str(output_arg).strip()
|
||||
cwd = os.getcwd()
|
||||
if batch_id:
|
||||
if args.get("db"):
|
||||
root = os.path.dirname(os.path.abspath(os.path.expanduser(str(args["db"]))))
|
||||
elif output_arg:
|
||||
root = os.path.dirname(os.path.abspath(os.path.expanduser(output_arg)))
|
||||
else:
|
||||
root = cwd
|
||||
conn = review_store.connect(root)
|
||||
batch = review_store.get_batch(conn, batch_id)
|
||||
if not batch:
|
||||
conn.close()
|
||||
raise ValueError("batch_id 不存在:%s" % batch_id)
|
||||
output = _abs(root, batch["output"])
|
||||
if not output.lower().endswith(".pdf"):
|
||||
output += ".pdf"
|
||||
return output, root, conn, batch
|
||||
|
||||
output = os.path.abspath(os.path.expanduser(
|
||||
str(output_arg or P["output"]["default_name"])))
|
||||
if not output.lower().endswith(".pdf"):
|
||||
output += ".pdf"
|
||||
# 生产环境 cwd 就是会话目录;输出在会话内时以 cwd 为根,子目录输出也不会把
|
||||
# .app/data.db 建到子目录里。本地从别的目录跑时退化为输出目录。
|
||||
root = cwd if _inside(cwd, output) else os.path.dirname(output)
|
||||
return output, root, None, None
|
||||
|
||||
|
||||
def _mark_error(conn, batch_id, message):
|
||||
if not conn or not batch_id:
|
||||
return
|
||||
try:
|
||||
review_store.set_job(conn, batch_id, "error", str(message))
|
||||
review_store.finish_batch(conn, batch_id, False, str(message), [], status="error")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 主流程
|
||||
|
||||
def process(args, t0):
|
||||
review_mode = _review_mode(args)
|
||||
batch_id_arg = args.get("batch_id")
|
||||
batch_id_arg = str(batch_id_arg).strip() if batch_id_arg else None
|
||||
|
||||
output, session_root, conn, batch_row = _targets(args, batch_id_arg)
|
||||
# 审核页由 core 从会话 .app/ 托管;输出不在会话目录内时页面/下载都不可达。
|
||||
if review_mode != "off" and not _inside(os.getcwd(), output):
|
||||
raise ValueError("审核页要求 PDF 位于会话目录内(当前工作目录):%s" % output)
|
||||
|
||||
use_db = bool(batch_id_arg) or review_mode != "off"
|
||||
if use_db and conn is None:
|
||||
conn = review_store.connect(session_root)
|
||||
|
||||
batch_id = batch_id_arg
|
||||
is_new_batch = batch_id is None
|
||||
if use_db and not batch_id:
|
||||
batch_id = review_store.new_batch_id()
|
||||
|
||||
state_path = os.path.splitext(output)[0] + P["output"]["state_suffix"]
|
||||
|
||||
try:
|
||||
old_pages = []
|
||||
if batch_id_arg:
|
||||
if not batch_row:
|
||||
batch_row = review_store.get_batch(conn, batch_id)
|
||||
old_pages = review_store.get_pages(conn, batch_id)
|
||||
if not old_pages:
|
||||
raise ValueError("批次没有页面记录:%s" % batch_id)
|
||||
|
||||
ed = state.load_edits(args.get("edits"))
|
||||
job = None
|
||||
if ed is None and batch_id:
|
||||
job = review_store.get_job(conn, batch_id)
|
||||
if job and job.get("edits_obj"):
|
||||
ed = state.load_edits(job["edits_obj"])
|
||||
else:
|
||||
ed = _edits_from_pages(old_pages, batch_row)
|
||||
if batch_id:
|
||||
if not job:
|
||||
job = review_store.get_job(conn, batch_id)
|
||||
if job:
|
||||
review_store.set_job(conn, batch_id, "running")
|
||||
|
||||
paper_input, paper, orientation, enhance, dpi_label, dpi = \
|
||||
_batch_opts(args, ed, P)
|
||||
|
||||
# ---------------- 源图清单
|
||||
if batch_id_arg:
|
||||
source_map = {}
|
||||
for r in old_pages:
|
||||
try:
|
||||
source_map[int(r["idx"])] = _abs(session_root, r["source"])
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
all_indices = sorted(source_map)
|
||||
else:
|
||||
images = parse_images(args)
|
||||
if not images:
|
||||
raise ValueError("必须提供 images")
|
||||
all_indices = list(range(1, len(images) + 1))
|
||||
source_map = {i: os.path.abspath(os.path.expanduser(str(images[i - 1])))
|
||||
for i in all_indices}
|
||||
|
||||
deleted = set(ed.get("deleted") or []) if ed else set()
|
||||
run_indices = [i for i in all_indices if i not in deleted]
|
||||
if not run_indices:
|
||||
raise ValueError("所有页面都被删除,没有可处理的页")
|
||||
|
||||
pages = []
|
||||
plan = []
|
||||
unreadable = []
|
||||
|
||||
for idx in all_indices:
|
||||
src = source_map.get(idx)
|
||||
meta = {"index": idx, "source_abs": src, "warnings": [],
|
||||
"deleted": idx in deleted}
|
||||
if meta["deleted"]:
|
||||
meta.update(status="deleted", reason="user_deleted",
|
||||
corners=None, corners_norm=None, auto_corners=None,
|
||||
auto_corners_norm=None, auto_rotate=0, rotated_deg=0)
|
||||
pages.append(meta)
|
||||
continue
|
||||
|
||||
try:
|
||||
img = load_image(src)
|
||||
except Exception as e:
|
||||
err = _err(
|
||||
"IMAGE_UNREADABLE",
|
||||
"第 %d 页 %s 打不开:%s" % (idx, os.path.basename(src or ""), e),
|
||||
idx,
|
||||
"确认文件存在且是 jpg/png/webp/bmp/tif 等常见格式")
|
||||
meta.update(status="failed", reason="unreadable", error=err,
|
||||
corners=None, corners_norm=None,
|
||||
auto_corners=None, auto_corners_norm=None,
|
||||
auto_rotate=0, rotated_deg=0)
|
||||
meta["warnings"].append(err["message"])
|
||||
unreadable.append(err)
|
||||
pages.append(meta)
|
||||
continue
|
||||
|
||||
meta["source_size"] = [int(img.width), int(img.height)]
|
||||
edit = (ed or {}).get("pages", {}).get(idx) if ed else None
|
||||
manual = bool(edit and edit.get("corners_norm"))
|
||||
|
||||
if manual:
|
||||
meta["manual"] = True
|
||||
meta["corners"] = _denorm(edit["corners_norm"], meta["source_size"])
|
||||
meta["corners_norm"] = [[float(x), float(y)]
|
||||
for x, y in edit["corners_norm"]]
|
||||
meta["aspect"] = _aspect(meta["corners"])
|
||||
meta["coverage"] = {}
|
||||
meta["paper_frac"] = None
|
||||
meta["fallback"] = False
|
||||
meta["fallback_sides"] = []
|
||||
meta["validation"] = {"ok": True, "metrics": {},
|
||||
"warnings": [], "failures": []}
|
||||
prev = _prev_page(old_pages, idx)
|
||||
if prev and prev.get("auto_corners"):
|
||||
meta["auto_corners"] = prev["auto_corners"]
|
||||
meta["auto_corners_norm"] = prev.get("auto_corners_norm")
|
||||
meta["auto_rotate"] = int(prev.get("auto_rotate") or 0)
|
||||
else:
|
||||
meta["auto_corners"] = [list(map(float, q))
|
||||
for q in meta["corners"]]
|
||||
meta["auto_corners_norm"] = meta["corners_norm"]
|
||||
meta["auto_rotate"] = 0
|
||||
meta["status"] = "ready"
|
||||
meta["reason"] = None
|
||||
else:
|
||||
try:
|
||||
corners, info = detect(img, P)
|
||||
except Exception as e:
|
||||
corners, info = [], {"error": str(e)}
|
||||
|
||||
if not corners:
|
||||
corners = _fallback_quad(meta["source_size"], P)
|
||||
info = {"fallback": True, "fallback_sides": [0, 1, 2, 3],
|
||||
"coverage": {}, "paper_frac": None}
|
||||
meta["status"] = "needs_corners"
|
||||
meta["reason"] = "detect_failed"
|
||||
else:
|
||||
meta["status"] = "ready"
|
||||
meta["reason"] = None
|
||||
|
||||
meta["corners"] = [[float(x), float(y)] for x, y in corners]
|
||||
meta["corners_norm"] = _norm(meta["corners"], meta["source_size"])
|
||||
meta["auto_corners"] = [list(map(float, q))
|
||||
for q in meta["corners"]]
|
||||
meta["auto_corners_norm"] = meta["corners_norm"]
|
||||
meta["auto_rotate"] = 0
|
||||
meta["coverage"] = {k: round(float(v), 3)
|
||||
for k, v in (info.get("coverage") or {}).items()}
|
||||
pf = info.get("paper_frac")
|
||||
meta["paper_frac"] = None if pf is None else round(float(pf), 4)
|
||||
meta["fallback"] = bool(info.get("fallback", False))
|
||||
meta["fallback_sides"] = [int(s)
|
||||
for s in (info.get("fallback_sides") or [])]
|
||||
try:
|
||||
v = validate(img, meta["corners"], P)
|
||||
except Exception as e:
|
||||
v = {"ok": False, "metrics": {},
|
||||
"failures": ["校验异常:%s" % e], "warnings": []}
|
||||
meta["validation"] = v
|
||||
if not v["ok"]:
|
||||
# 检测到了但几何不可信:退回兜底框,交给人补角,而不是让整批失败。
|
||||
# auto_corners 也要跟着换成兜底框,否则“恢复自动”会把无效角点拖回来。
|
||||
meta["detected_corners"] = meta["corners"]
|
||||
meta["corners"] = _fallback_quad(meta["source_size"], P)
|
||||
meta["corners_norm"] = _norm(meta["corners"],
|
||||
meta["source_size"])
|
||||
meta["auto_corners"] = [list(map(float, q))
|
||||
for q in meta["corners"]]
|
||||
meta["auto_corners_norm"] = meta["corners_norm"]
|
||||
meta["status"] = "needs_corners"
|
||||
meta["reason"] = "invalid_quad"
|
||||
meta["aspect"] = _aspect(meta["corners"])
|
||||
|
||||
pages.append(meta)
|
||||
plan.append((idx, src, meta))
|
||||
img.close()
|
||||
|
||||
if not plan:
|
||||
failures = unreadable or [
|
||||
_err("NO_PAGES", "没有可处理的页面", None,
|
||||
"检查 images 是否都删除了/是否都打不开")]
|
||||
res = finish_failure(pages, failures, output, state_path, t0)
|
||||
_mark_error(conn, batch_id, res["summary"])
|
||||
return res
|
||||
|
||||
# ---------------- 批次方向
|
||||
aspects = [m["aspect"] for (_, _, m) in plan]
|
||||
batch_orientation, _, median_aspect = \
|
||||
compose.decide_orientation(aspects, orientation, P)
|
||||
dead = P["gate"]["aspect_deadband"]
|
||||
if batch_orientation in ("portrait", "landscape"):
|
||||
same = [m["aspect"] for (_, _, m) in plan
|
||||
if compose.classify(m["aspect"], dead) == batch_orientation]
|
||||
batch_median = float(statistics.median(same)) if same else float(median_aspect)
|
||||
else:
|
||||
batch_median = float(median_aspect)
|
||||
|
||||
batch_ratio = None
|
||||
if paper["ratio"] is None and orientation != "none":
|
||||
m_class = compose.classify(batch_median, dead)
|
||||
if m_class == batch_orientation:
|
||||
batch_ratio = batch_median
|
||||
elif m_class != "square":
|
||||
batch_ratio = 1.0 / batch_median
|
||||
|
||||
# 人工改过的页不再重新评级;自动页按方案表分级后与已有状态取较严重者
|
||||
# grade_page 的 ref_ratio 约定是「竖版为正」,横版批次要先折回竖版,否则
|
||||
# 所有横页都会被判成 paper_mismatch。
|
||||
ref_ratio = paper["ratio"]
|
||||
if ref_ratio is None:
|
||||
ref_ratio = batch_median
|
||||
if ref_ratio > 1.0:
|
||||
ref_ratio = 1.0 / ref_ratio
|
||||
for (_, _, m) in plan:
|
||||
if m.get("manual"):
|
||||
continue
|
||||
st2, r2 = state.grade_page(m, P, ref_ratio)
|
||||
m["status"], m["reason"] = state.escalate(
|
||||
m.get("status") or "ready", m.get("reason"), st2, r2)
|
||||
|
||||
# ---------------- 出页
|
||||
out_meta = {}
|
||||
for (idx, src, meta) in plan:
|
||||
page_class = compose.classify(meta["aspect"], dead)
|
||||
auto_rotate = 0
|
||||
if orientation == "auto":
|
||||
if page_class not in ("square", batch_orientation):
|
||||
auto_rotate = 90
|
||||
elif orientation in ("portrait", "landscape"):
|
||||
if page_class not in ("square", orientation):
|
||||
auto_rotate = 90
|
||||
|
||||
edit = (ed or {}).get("pages", {}).get(idx) if ed else None
|
||||
user_r = edit.get("rotate") if edit else None
|
||||
applied = int(user_r) if user_r is not None else auto_rotate
|
||||
applied = ((applied % 360) + 360) % 360
|
||||
if applied not in (0, 90, 180, 270):
|
||||
applied = 0
|
||||
|
||||
out_corners = [list(map(float, q)) for q in meta["corners"]]
|
||||
for _ in range(applied // 90):
|
||||
out_corners = compose.rotate_quad_cw(out_corners)
|
||||
|
||||
meta["rotated_deg"] = applied
|
||||
if not meta.get("manual"):
|
||||
meta["auto_rotate"] = auto_rotate
|
||||
|
||||
if orientation == "none":
|
||||
out_aspect = _aspect(out_corners)
|
||||
page_orient = compose.classify(out_aspect, dead)
|
||||
if page_orient == "square":
|
||||
page_orient = "portrait"
|
||||
elif orientation in ("portrait", "landscape"):
|
||||
page_orient = orientation
|
||||
else:
|
||||
# auto 模式下,用户显式旋转过就按旋转后的有效方向出画布;
|
||||
# 否则跟批次统一方向。否则 90° 旋转会把内容塞进反向画布压扁。
|
||||
if user_r is not None:
|
||||
page_orient = compose.classify(_aspect(out_corners), dead)
|
||||
if page_orient == "square":
|
||||
page_orient = batch_orientation \
|
||||
if batch_orientation in ("portrait", "landscape") \
|
||||
else "portrait"
|
||||
else:
|
||||
page_orient = batch_orientation
|
||||
|
||||
out_ratio = None
|
||||
if batch_ratio is not None:
|
||||
eff_w, eff_h = compose.aspect_of(out_corners)
|
||||
eff = eff_w / eff_h
|
||||
if abs(eff - batch_ratio) / batch_ratio \
|
||||
<= P["gate"]["paper_mismatch_pct"] / 100.0:
|
||||
out_ratio = batch_ratio
|
||||
|
||||
tw, th, canvas = compose.target_size(
|
||||
out_corners, paper, page_orient, P, out_ratio)
|
||||
|
||||
try:
|
||||
img = load_image(src)
|
||||
try:
|
||||
page = compose.warp_page(img, out_corners, (tw, th), P)
|
||||
finally:
|
||||
img.close()
|
||||
except Exception as e:
|
||||
err = _err("INTERNAL",
|
||||
"第 %d 页重新读取/透视变换失败:%s" % (idx, e), idx,
|
||||
"重试;若持续出现请反馈")
|
||||
meta["status"] = "render_failed"
|
||||
meta["reason"] = "render_failed"
|
||||
meta["error"] = err
|
||||
meta["warnings"].append(err["message"])
|
||||
out_meta[idx] = {"page": None, "canvas": None, "error": err}
|
||||
continue
|
||||
|
||||
meta["out_size"] = [int(tw), int(th)]
|
||||
meta["canvas_size"] = [int(canvas[0]), int(canvas[1])]
|
||||
meta["page_orientation"] = page_orient
|
||||
out_meta[idx] = {"page": page, "canvas": canvas, "error": None}
|
||||
|
||||
cov = meta.get("coverage") or {}
|
||||
if cov:
|
||||
cmin = min(float(v) for v in cov.values())
|
||||
if cmin < P["gate"]["coverage_warn"]:
|
||||
meta["warnings"].append(
|
||||
"四边覆盖率最低 %.2f(低于 %.2f),角点可能不准"
|
||||
% (cmin, P["gate"]["coverage_warn"]))
|
||||
if meta.get("reason") == "detect_failed":
|
||||
meta["warnings"].append(
|
||||
"第 %d 页未检测到纸张四角,已用画面内缩兜底框出件;需人工补角"
|
||||
% idx)
|
||||
elif meta.get("reason") == "invalid_quad":
|
||||
meta["warnings"].append(
|
||||
"第 %d 页角点校验未通过,已用兜底框出件;需人工补角" % idx)
|
||||
elif meta.get("fallback"):
|
||||
names = ["上", "右", "下", "左"]
|
||||
sides = "、".join(names[s] for s in meta.get("fallback_sides", [])
|
||||
if 0 <= s < 4)
|
||||
if sides:
|
||||
meta["warnings"].append(
|
||||
"第 %s 边使用了掩膜回退角点,精度可能略低" % sides)
|
||||
for w in (meta.get("validation") or {}).get("warnings", []):
|
||||
meta["warnings"].append(w)
|
||||
if (not meta.get("manual") and batch_orientation != "mixed"
|
||||
and batch_median > 0 and page_class == batch_orientation):
|
||||
dev = abs(meta["aspect"] - batch_median) / batch_median * 100.0
|
||||
if dev > P["gate"]["paper_mismatch_pct"]:
|
||||
meta["warnings"].append(
|
||||
"宽高比与批次中位相差 %.0f%%,可能不是同一种纸;"
|
||||
"可改 paper=auto 或单独处理" % dev)
|
||||
if applied != 0 and user_r is None:
|
||||
meta["warnings"].append(
|
||||
"方向与批次不一致,已旋转 %d°;文字方向需人工确认" % applied)
|
||||
|
||||
# ---------------- 页序 / PDF
|
||||
order = []
|
||||
if ed and ed.get("order"):
|
||||
for v in ed["order"]:
|
||||
if v in out_meta and out_meta[v].get("page") is not None \
|
||||
and v not in order:
|
||||
order.append(v)
|
||||
for idx in sorted(out_meta.keys()):
|
||||
if out_meta[idx].get("page") is not None and idx not in order:
|
||||
order.append(idx)
|
||||
|
||||
ordered_pages = [out_meta[i]["page"] for i in order]
|
||||
ordered_canvases = [out_meta[i]["canvas"] for i in order]
|
||||
if not ordered_pages:
|
||||
failures = unreadable or [
|
||||
_err("RENDER_FAILED", "所有页面都未能出页", None, "重试或检查原图")]
|
||||
res = finish_failure(pages, failures, output, state_path, t0)
|
||||
_mark_error(conn, batch_id, res["summary"])
|
||||
return res
|
||||
|
||||
try:
|
||||
compose.build_pdf(ordered_pages, output, ordered_canvases, P)
|
||||
except Exception as e:
|
||||
err = _err("PDF_WRITE_FAILED", "写 PDF 失败:%s" % e, None,
|
||||
"确认输出目录可写、磁盘空间足够")
|
||||
res = finish_failure(pages, [err], output, state_path, t0)
|
||||
_mark_error(conn, batch_id, res["summary"])
|
||||
return res
|
||||
|
||||
# ---------------- 检验
|
||||
try:
|
||||
checks = check.inspect_pdf(output, len(ordered_pages),
|
||||
P["check"]["dpi"], P)
|
||||
except Exception as e:
|
||||
checks = {"ok": None, "error": str(e), "pages": [],
|
||||
"page_count": None}
|
||||
|
||||
warnings = []
|
||||
for m in pages:
|
||||
warnings.extend(m.get("warnings", []))
|
||||
if checks.get("ok") is None:
|
||||
warnings.append("PDF 自检无法执行:%s" % checks.get("error"))
|
||||
else:
|
||||
for item in checks.get("pages", []):
|
||||
orig = order[item["index"] - 1] if 0 < item["index"] <= len(order) \
|
||||
else item["index"]
|
||||
if item.get("box_fallback"):
|
||||
warnings.append(
|
||||
"第 %d 页未能稳定识别纸面区域,边缘检验仅供参考" % orig)
|
||||
if not item["short_side_ok"]:
|
||||
warnings.append(
|
||||
"第 %d 页纸面短边 %dpx 偏低,可能分辨率不足"
|
||||
% (orig, item["short_side_px"]))
|
||||
if not item["border_ok"]:
|
||||
warnings.append(
|
||||
"第 %d 页边缘有背景残留,角点可能落在背景上" % orig)
|
||||
if not item["cut_ok"]:
|
||||
warnings.append(
|
||||
"第 %d 页内容贴边,可能裁到了内容" % orig)
|
||||
if (paper["ratio"] is not None
|
||||
and item["ratio_dev_pct"] > P["check"]["ratio_tol"] * 100):
|
||||
warnings.append(
|
||||
"第 %d 页纸面比例与页面相差 %.1f%%"
|
||||
% (orig, item["ratio_dev_pct"]))
|
||||
pc = checks.get("page_count") or {}
|
||||
if pc and not pc.get("ok"):
|
||||
warnings.append(
|
||||
"PDF 页数与出页数不一致(期望 %s,实际 %s)"
|
||||
% (pc.get("expected"), pc.get("actual")))
|
||||
|
||||
# ---------------- 记录顺序 / 审核状态
|
||||
sort_map = {idx: pos for pos, idx in enumerate(order)}
|
||||
next_sort = len(order)
|
||||
for idx in all_indices:
|
||||
if idx not in sort_map:
|
||||
sort_map[idx] = next_sort
|
||||
next_sort += 1
|
||||
|
||||
needs = []
|
||||
for m in pages:
|
||||
if m.get("status") in ("review", "needs_corners", "needs_orientation",
|
||||
"render_failed", "failed"):
|
||||
needs.append(m)
|
||||
|
||||
# 发布审核页:always 总是发布;auto 只在有页需要人看时发布
|
||||
publish = review_mode == "always" or (review_mode == "auto" and bool(needs))
|
||||
thumbs = {}
|
||||
if publish and batch_id:
|
||||
_, thumbs = review_page.publish(
|
||||
session_root, batch_id,
|
||||
[{"idx": m["index"], "source_abs": m.get("source_abs")}
|
||||
for m in pages
|
||||
if not m.get("deleted") and m.get("source_abs")])
|
||||
|
||||
# ---------------- state v2
|
||||
order_all = sorted(pages, key=lambda m: (sort_map.get(m["index"], m["index"]),
|
||||
m["index"]))
|
||||
state_pages = []
|
||||
for m in order_all:
|
||||
src_rel = _rel(session_root, m.get("source_abs") or "")
|
||||
auto_r = int(m.get("auto_rotate") or 0)
|
||||
state_pages.append({
|
||||
"index": m["index"],
|
||||
"source": src_rel,
|
||||
"source_size": m.get("source_size"),
|
||||
"auto_corners": m.get("auto_corners"),
|
||||
"corners_norm": m.get("corners_norm"),
|
||||
"rotate": auto_r,
|
||||
"rotate_source": "manual" if m.get("manual") else "auto",
|
||||
"status": m.get("status"),
|
||||
"reason": m.get("reason"),
|
||||
})
|
||||
|
||||
state_obj = state.build_state({
|
||||
"paper": paper_input,
|
||||
"enhance": enhance,
|
||||
"dpi": int(P["render"]["dpi"]),
|
||||
"orientation": orientation,
|
||||
}, state_pages)
|
||||
state_ok = write_state(state_path, state_obj)
|
||||
if not state_ok:
|
||||
warnings.append("state 文件写入失败:%s" % state_path)
|
||||
|
||||
if checks.get("ok") is False:
|
||||
summary = "%d 页已生成;自检有未通过项" % len(ordered_pages)
|
||||
elif needs:
|
||||
summary = "%d 页已生成;%d 页建议在审核页确认" % (
|
||||
len(ordered_pages), len(needs))
|
||||
elif warnings:
|
||||
summary = "%d 页已生成;%d 项提示,见审核页" % (
|
||||
len(ordered_pages), len(warnings))
|
||||
else:
|
||||
summary = "%d 页已生成" % len(ordered_pages)
|
||||
|
||||
# ---------------- 写 data.db
|
||||
if use_db:
|
||||
output_rel = _rel(session_root, output)
|
||||
review_store.upsert_batch(conn, batch_id, output_rel, {
|
||||
"paper": paper_input, "enhance": enhance,
|
||||
"dpi": dpi_label, "orientation": orientation,
|
||||
})
|
||||
recs = []
|
||||
for m in pages:
|
||||
ti = thumbs.get(m["index"])
|
||||
size = m.get("source_size") or [None, None]
|
||||
rec = {
|
||||
"idx": m["index"],
|
||||
"source": _rel(session_root, m.get("source_abs") or ""),
|
||||
"source_w": size[0], "source_h": size[1],
|
||||
"auto_corners": m.get("auto_corners"),
|
||||
"auto_corners_norm": m.get("auto_corners_norm"),
|
||||
"corners": m.get("corners_norm"),
|
||||
"auto_rotate": int(m.get("auto_rotate") or 0),
|
||||
"rotate": int(m.get("rotated_deg") or 0),
|
||||
"deleted": 1 if m.get("deleted") else 0,
|
||||
"sort": sort_map.get(m["index"], m["index"]),
|
||||
"status": m.get("status"),
|
||||
"reason": m.get("reason"),
|
||||
"thumb": ti[0] if ti else None,
|
||||
"w": ti[1] if ti else (size[0] or None),
|
||||
"h": ti[2] if ti else (size[1] or None),
|
||||
"error": ((m.get("error") or {}).get("message")
|
||||
if isinstance(m.get("error"), dict)
|
||||
else m.get("error")),
|
||||
}
|
||||
recs.append(rec)
|
||||
review_store.replace_pages(conn, batch_id, recs)
|
||||
batch_status = "ready" if not needs else "review"
|
||||
review_store.finish_batch(conn, batch_id, True, summary,
|
||||
warnings, status=batch_status)
|
||||
if review_store.get_job(conn, batch_id):
|
||||
review_store.set_job(conn, batch_id, "done", summary)
|
||||
if is_new_batch:
|
||||
old, kept = review_store.prune(conn, keep=5, protect=batch_id)
|
||||
if old:
|
||||
review_page.prune_dirs(session_root, kept)
|
||||
|
||||
# AI 侧只回三件事:是否成功、一句摘要、PDF 文件名。
|
||||
# 完整访问地址由 core 的 visit_url 追加;pages/checks/批次 ID 都在 data.db/state 里。
|
||||
return {
|
||||
"ok": True,
|
||||
"summary": summary,
|
||||
"pdf": _rel(session_root, output),
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
_mark_error(conn, batch_id, e)
|
||||
raise
|
||||
finally:
|
||||
if conn:
|
||||
try:
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def finish_failure(pages, failures, output, state_path, t0):
|
||||
err = failures[0] if failures else _err("INTERNAL", "未知错误")
|
||||
return {
|
||||
"ok": False,
|
||||
"summary": "失败:%s" % err.get("message", "未知错误"),
|
||||
"error": err,
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 入口
|
||||
|
||||
def main():
|
||||
t0 = time.time()
|
||||
buf = io.StringIO()
|
||||
old_stderr = sys.stderr
|
||||
sys.stderr = buf
|
||||
try:
|
||||
if len(sys.argv) != 2:
|
||||
raise ValueError(
|
||||
'用法:python3 run.py \'{"images": ["图1.jpg"]}\'')
|
||||
try:
|
||||
args = json.loads(sys.argv[1])
|
||||
except json.JSONDecodeError as e:
|
||||
raise ValueError("参数不是合法 JSON:%s" % e)
|
||||
if not isinstance(args, dict):
|
||||
raise ValueError("参数必须是 JSON 对象")
|
||||
res = process(args, t0)
|
||||
except Exception as e:
|
||||
code = "BAD_ARGS" if isinstance(e, ValueError) else "INTERNAL"
|
||||
hint = ("检查参数后重试" if code == "BAD_ARGS"
|
||||
else "可用 DOC_DESKEW_DEBUG=1 重跑查看堆栈")
|
||||
res = {
|
||||
"ok": False,
|
||||
"summary": "失败:%s" % e,
|
||||
"error": _err(code, str(e), None, hint),
|
||||
}
|
||||
if os.environ.get("DOC_DESKEW_DEBUG"):
|
||||
res["logs"] = traceback.format_exc().splitlines()
|
||||
finally:
|
||||
sys.stderr = old_stderr
|
||||
|
||||
logs = [line for line in buf.getvalue().splitlines() if line.strip()]
|
||||
if logs:
|
||||
res["logs"] = (res.get("logs", []) + logs)[:P["output"]["max_logs"]]
|
||||
|
||||
print(json.dumps(res, ensure_ascii=False, separators=(",", ":"),
|
||||
default=_json_default))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user