重构 document-photo-to-pdf:审核页改为 data.db 驱动并支持一键重生成
run.py 接上 edits/enhance/dpi/status/review,并把运行记录写进会话 data.db;审核页模板移到 skill/web,运行时发布到 .app/doc-review/,页面通过 /api/db 水合并调用通用技能执行接口重生成;移除 make_review.py 与模板注入。
This commit is contained in:
@@ -1,167 +1,188 @@
|
|||||||
---
|
---
|
||||||
name: document-photo-to-pdf
|
name: document-photo-to-pdf
|
||||||
description: 把手机拍摄的纸质文档照片拉正、去背景,按顺序合并为一个 PDF
|
description: 把手机拍摄的纸质文档照片逐页裁剪、拉正、判定方向,合并为一个 PDF;可选发布可拖拽角点的审核页
|
||||||
thinking: true
|
input_schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
images:
|
||||||
|
type: array
|
||||||
|
items: {type: string}
|
||||||
|
description: 图片路径,按合并顺序;新批次必填,传 batch_id 时可省略
|
||||||
|
output:
|
||||||
|
type: string
|
||||||
|
description: 输出 PDF 路径,默认 文档照片_校正.pdf;batch_id 模式忽略此参数,沿用批次原输出
|
||||||
|
paper:
|
||||||
|
type: string
|
||||||
|
description: a4/letter/16k/b5/数字宽高比/auto;默认 auto
|
||||||
|
orientation:
|
||||||
|
type: string
|
||||||
|
enum: [auto, none, portrait, landscape]
|
||||||
|
description: 方向策略;默认 auto(逐页按纸张宽高比判横竖)
|
||||||
|
enhance:
|
||||||
|
type: string
|
||||||
|
enum: [original, normal, strong, bw]
|
||||||
|
description: 调色档;默认 normal
|
||||||
|
dpi:
|
||||||
|
type: [string, integer]
|
||||||
|
description: 清晰度;standard(150)/high(300) 或 72~600 的数字
|
||||||
|
review:
|
||||||
|
type: string
|
||||||
|
enum: [off, always, auto]
|
||||||
|
description: 审核页策略;off(默认)不发布,always 总是发布,auto 只在有页需要人看时发布
|
||||||
|
batch_id:
|
||||||
|
type: string
|
||||||
|
description: 重生成用;从 data.db 读源图/输出/编辑,覆盖同名 PDF。忽略 images/output
|
||||||
|
edits:
|
||||||
|
type: [string, object]
|
||||||
|
description: review/1 修改对象或 JSON 路径;batch_id 模式不传时读 doc_review_job.edits
|
||||||
|
state:
|
||||||
|
type: string
|
||||||
|
description: 上一份 state.json 路径;可选,仅用于恢复自动角点/调试
|
||||||
---
|
---
|
||||||
|
|
||||||
# 文档照片转 PDF
|
# 文档照片转 PDF
|
||||||
|
|
||||||
手机拍的纸质材料 → 一个版正字清的 PDF。适用于合同、票据、证照、手写稿等多页纸质材料,纸张尺寸不限。
|
手机拍的纸质材料 → 一个版正字清的 PDF。自动检测纸张四角、自动拉正,不读图、不传坐标。
|
||||||
|
需要人工确认时,可以发布一个可拖拽角点的审核页,改完点「生成 PDF」直接覆盖重出。
|
||||||
|
|
||||||
## 怎么跑
|
## 两种用法
|
||||||
|
|
||||||
本技能没有 `run` 入口,用 `terminal` 直接执行命令。脚本装在技能目录,而 terminal 的
|
### 全自动(默认)
|
||||||
cwd 是会话任务目录——两者不是同一个目录,所以**脚本一律用绝对路径调用**,图片和
|
|
||||||
`out/` 用任务目录里的相对路径:
|
|
||||||
|
|
||||||
```sh
|
```json
|
||||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py <子命令> ...
|
{"images": ["图1.jpg", "图2.jpg"], "output": "合同_校正.pdf"}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 铁律
|
只出 PDF,不写 `.app/`、不碰 `data.db`。适合“直接出件、不用看”的场景。
|
||||||
|
|
||||||
> 以下 8 条为硬约束,违反任一条即视为任务失败:立即停手、向用户说明,不得"先改完再说"。
|
### 半自动(审核页)
|
||||||
|
|
||||||
1. **先说再做。** 每次调脚本前用一句话说明这次干什么(要标注步骤编号)。
|
```json
|
||||||
2. **坐标只能靠眼睛读,禁止一切自建脚本。** 不另造任何纸张处理的辅助的代码,一次性脚本也不行。确实无法达成目的需要新能力=先停下问用户,不许自己造。
|
{"images": ["图1.jpg", "图2.jpg"], "review": "always"}
|
||||||
3. **不裁切、不放大、不逐角看。** 四个角在同一张网格图上一次读全,直接报 4 组数字。不为了"读准一点"造局部图、加刻度、逐个角反复看。
|
|
||||||
4. **每张源图对应1网格图、1角标图。** 每轮读网格图 1 次 + 角标 1 次,看完就交付。
|
|
||||||
- **读之前先说清这次要确认什么。** 说不出目的就不读。"再看一眼确认一下"不算目的。
|
|
||||||
- **不为读而读。** 允许重读,但必须是被动的、有明确对象的:图没送达/像素没收到、要核对某个具体数字或某条边。没有新对象就直接用手上已有的数字。
|
|
||||||
- **只读这两类图。** 网格、角标。原图、局部放大图、成品图、自己另造的辅助图都不读。
|
|
||||||
5. **一次成件,禁止试错。** 一条 `build` 出件 + 检验。一轮里每页最多跑 1 次 `build`;不调参、不重取角点、不换方法、不"先出一版看看"。不通过就报诊断,停下等确认。
|
|
||||||
6. **只有用户明确说"再改"才允许修复。** 完整一轮 = `prepare` → `set`(每页一次)→ `build`(一次)→ 看角标 → 交付。这一轮内发现的任何偏差(框偏了、裁到内容、比例不对),一律只报诊断和坐标建议,**不自行改、不重跑、不补做**。用户在本轮交付后明确说"再改/重做"时才能动手,且还是只跑一轮,改完重新过角标关。
|
|
||||||
7. **指标 ≠ 成品。** 校验只查"框内"像素,框漏掉的内容查不出来。角标图必须亲眼看。
|
|
||||||
8. **违规即停。** 一旦意识到自己正在写脚本、造辅助图、二次 build 或擅自修复,立即停手,向用户说明,不带病交付。
|
|
||||||
|
|
||||||
### 交付前自检(有一项为"是"就停手报告)
|
|
||||||
|
|
||||||
- 本轮是否新建/修改过任何脚本或辅助图?
|
|
||||||
- 本轮 `build` 是否跑超过 1 次?
|
|
||||||
- 是否在用户没说过"再改"时自行调整过角点或重出件?
|
|
||||||
- 每张图是否只读了网格、角标各 1 次?
|
|
||||||
- 校验未过的项是否只报告、没自行修?
|
|
||||||
|
|
||||||
## 流程
|
|
||||||
|
|
||||||
### 1 · 出网格图,并输出候选的角点坐标
|
|
||||||
|
|
||||||
```sh
|
|
||||||
python3 /workspace/skills/document-photo-to-pdf/doc_deskew/pipeline.py prepare 图1.jpg 图2.jpg
|
|
||||||
```
|
```
|
||||||
|
|
||||||
输出 `out/N-网格-<名称>.png`(100px 红色主线带坐标刻度、50px 蓝色辅助线,两者同宽)。
|
在出 PDF 的同时:
|
||||||
必须显式传入图片名,合并成PDF时即按此传入顺序。
|
|
||||||
|
|
||||||
网格图上会叠加一组**候选角点**:品红实心圆 = 候选角点。同时按文件名打印 markdown 表格(`图片|左上|右上|右下|左下`,单元格为 `x,y`),可直接对照读数。品红虚线是检出的边线,用来辅助判断线是否贴边。
|
1. 把源图缩略图发布到会话 `.app/doc-review/<batch_id>/`;
|
||||||
|
2. 把批次/页面/任务记录写进会话 `data.db` 的 `doc_review_*` 表;
|
||||||
|
3. 返回 `review.url`(站内路径 `/app/<task_id>/doc-review/?b=<batch_id>`)。
|
||||||
|
|
||||||
### 2 · 从网格图中读出角点,一张图一条命令
|
用户拖角点、旋转、删页、调页序、改四项参数后,点页面右下角「生成 PDF」:
|
||||||
|
页面把修改写进 `doc_review_job`,再调用通用执行接口
|
||||||
|
`POST /api/sessions/<task_id>/skills/document-photo-to-pdf/run`(只传 `batch_id`),
|
||||||
|
服务端按同一份 `run.py` 覆盖同名 PDF,页面自动刷新。**不需要复制 JSON,也不经过模型。**
|
||||||
|
|
||||||
顺序固定:左上 → 右上 → 右下 → 左下(屏幕坐标,原点左上)。
|
`review=auto` 是两者之间的桥:只有存在 `needs_attention` 的页(检测不到角点、方向存疑、
|
||||||
|
覆盖率低等)才发布审核页;全部就绪时不发布。
|
||||||
|
|
||||||
**要找的是纸张本身真实的四个角**——纸面这个四边形的 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 | <prepare估算出坐标> | x1,y1;x2,y2;x3,y3;x4,y4 | <事后评估后的坐标> |
|
|
||||||
|
|
||||||
## 判定与报告
|
|
||||||
|
|
||||||
| 检查项 | 判据 | 不达标含义 |
|
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
| 页数 | 与源图数一致 | 合并出错 |
|
| `images` | 新批次必填 | 图片路径,按传入顺序合并;相对路径按会话目录解析 |
|
||||||
| 纸面短边 | ≥ 533px @100dpi | 角点错或分辨率过低 |
|
| `output` | 否 | 默认 `文档照片_校正.pdf`;`batch_id` 模式沿用批次原输出并覆盖 |
|
||||||
| 纸张比例 | 仅提示 | 传了 `--paper` 应≈1.00,偏差大说明角点有误 |
|
| `paper` | 否 | 默认 `auto`:按同批中位比例统一版式,填满 A4 内容区;偏差超过 20% 的页保持原比例。整批都是 A4 时可传 `a4`;票据/异形纸保持 `auto` 或传数字比例(宽/高) |
|
||||||
| 边缘背景残留 | 边带亮度 ≥ 纸面 80% | 角点落在背景上,内收 3~5px |
|
| `orientation` | 否 | 默认 `auto`:**逐页**按本页纸张宽高比定横竖,不看批次多数票。`none` 保持每页原方向;`portrait`/`landscape` 强制整批对齐 |
|
||||||
| 内容贴边 | 页边无成片文字级暗像素 | 内容被切,外扩 5px |
|
| `enhance` | 否 | 调色档,默认 `normal`。`original` 原样不提亮;`strong` 加强;`bw` 黑白(保留灰阶,不二值化) |
|
||||||
|
| `dpi` | 否 | `standard`(150) / `high`(300) / 72~600 的数字,默认 150 |
|
||||||
|
| `review` | 否 | `off`(默认)/`always`/`auto`;传 `batch_id` 且不传 review 时按 `always` |
|
||||||
|
| `batch_id` | 否 | 重生成:读 `data.db` 里的源图、输出、编辑,覆盖同名 PDF;忽略 `images`/`output` |
|
||||||
|
| `edits` | 否 | `review/1` 对象或路径;`batch_id` 模式不传时读 `doc_review_job.edits` |
|
||||||
|
| `state` | 否 | 上一份 `state.json` 路径;可选,只用于恢复自动角点/调试 |
|
||||||
|
|
||||||
- **全部通过**:给产物路径、大小、页数、指标,加一句角标图上确认的结果。
|
优先级:`edits.batch` > 显式参数 > 默认值。`edits` 里的角点会跳过检测,直接用给定坐标。
|
||||||
- **不达标**:只报诊断+ 建议,**不自行改、不重跑、不换方法**。用户明确说"再改"后,才改动后重跑一轮,然后交用户决策。
|
|
||||||
- **用户没发话就交付**:即使只差 1px、即使一眼看出能更好,也不许动。报告里可写建议,手不能动。
|
## 看返回值
|
||||||
|
|
||||||
|
stdout 是一个 JSON 对象,**先看 `ok`**:
|
||||||
|
|
||||||
|
- `ok:true`:`output.path` 是 PDF;`summary` 可直接转述;`warnings` 非空时要一起告诉用户。
|
||||||
|
- `ok:false`:不要重试,把 `error.message` 和 `error.hint` 转述给用户。
|
||||||
|
|
||||||
|
**单页拍得不好不再等于失败**:检测不到角点、角点校验不过的页会用“画面内缩 3%”的兜底框
|
||||||
|
照常出件,该页 `status` 为 `needs_corners`;原图打不开的页标记 `failed` 并跳过。只有一页都
|
||||||
|
出不了时才返回 `ok:false`。
|
||||||
|
|
||||||
|
成功示例(半自动):
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"ok":true,"summary":"3 页已生成;1 页需人工确认",
|
||||||
|
"output":{"path":"/.../合同_校正.pdf","pages":3},
|
||||||
|
"review":{"batch_id":"dr20261023-120000-a1b2","published":true,
|
||||||
|
"url":"/app/task-20261023-001/doc-review/?b=dr20261023-120000-a1b2",
|
||||||
|
"ready":2,"needs_attention":[{"index":2,"status":"needs_corners","reason":"detect_failed"}]},
|
||||||
|
"warnings":[]}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `review.published=false` 时没有 `url`,不要给用户审核入口。
|
||||||
|
- `review.url` 是站内路径:Web 里直接输出成 Markdown 链接即可;微信/外部端需要拼上站点
|
||||||
|
地址才能打开。审核页是拖拽页面,建议让用户在浏览器里用。
|
||||||
|
- `review.ready` 是 `status=ready` 的页数;`needs_attention` 含 `review`、`needs_corners`、
|
||||||
|
`needs_orientation`、`render_failed`、`failed`。
|
||||||
|
|
||||||
|
`warnings` 里常见的是:某页已旋转 90°、某页用兜底框出件、某页透视校正失败已重试、
|
||||||
|
纸面短边偏低。这些表示“已出件但需确认”,按原话转述,不要自己判断“应该没事”。
|
||||||
|
`checks.pages` 是逐页数值指标,用户追问时再展开,不必主动倒给用户。
|
||||||
|
|
||||||
|
## 审核页与数据
|
||||||
|
|
||||||
|
- 页面模板的单一事实源:`skills/document-photo-to-pdf/web/index.html`。
|
||||||
|
- 运行时副本:会话 `.app/doc-review/index.html`(每次发布覆盖;不含任何数据注入)。
|
||||||
|
- 缩略图:会话 `.app/doc-review/<batch_id>/p<idx>.jpg`(长边 1200,JPEG)。
|
||||||
|
- 运行记录:会话 `data.db` 的 `doc_review_batch` / `doc_review_page` / `doc_review_job`。
|
||||||
|
页面通过通用 `/api/db/<task_id>/query` 读,技能自己写;core 不认识这些表。
|
||||||
|
|
||||||
|
页面点「生成 PDF」的流程:
|
||||||
|
|
||||||
|
1. 把当前修改写成 `doc_review_job.edits`(完整快照,状态 `queued`);
|
||||||
|
2. 调 `POST /api/sessions/<task_id>/skills/document-photo-to-pdf/run`,args 只传 `{"batch_id":"..."}`;
|
||||||
|
3. 技能读 job → 应用 edits → 覆盖同名 PDF → 更新 batch/page/job;
|
||||||
|
4. 页面重新读 `data.db`,刷新摘要、状态和 PDF 链接。
|
||||||
|
|
||||||
|
`edits` 的结构(`review/1`):
|
||||||
|
|
||||||
|
```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 的 1-based 下标**,不随页序变化;
|
||||||
|
- `c` 是归一化 0~1,相对 exif_transpose 后的原图,顺序左上、右上、右下、左下;
|
||||||
|
- `r` 是该页的**最终顺时针旋转**(0/90/180/270),覆盖自动方向判断;不传则用自动结果;
|
||||||
|
- `pages` 只需要放与自动基线不同的页(人工角点/旋转);未改的页由服务端重新检测/评级;
|
||||||
|
- `order` 是出件页序,`deleted` 是删除的页;缺省按 images 原序。
|
||||||
|
|
||||||
|
技能每次出件会在 PDF 同目录写 `<名字>.state.json`(schema `document-photo-to-pdf/2`):
|
||||||
|
`batch` 是本次四项参数,`pages[]` 记 `auto_corners`(原图像素)、`corners_norm`、
|
||||||
|
`rotate`(自动基线)、`rotate_source`、`status`、`reason`。坐标一律归一化,换预览分辨率不会错位。
|
||||||
|
|
||||||
|
审核页发布的旧批次只保留最近 5 个;更早的缩略图目录会随下一次新批次自动清理。
|
||||||
|
|
||||||
## 内容安全
|
## 内容安全
|
||||||
|
|
||||||
**禁止用生成式模型处理这类图片。** 生成模型是重绘而非变换,即使只做几何变换,也可能引入伪影或改写文字。本流程只做纯几何变换 + 全局等比提亮,文字信息保持原始像素。
|
只做纯几何透视变换 + 全局等比提亮,不调用生成式模型,文字像素不会被重绘。
|
||||||
|
|
||||||
## 命令速查
|
## 拍摄要求(失败时这样建议用户)
|
||||||
|
|
||||||
下表省略公共前缀 `python3 /workspace/skills/document-photo-to-pdf/doc_deskew/`。
|
- 一次只拍一张纸,整张纸完整入画,四边各留 3%~5% 背景;
|
||||||
|
- 桌面与纸张反差明显,避开强阴影和反光;
|
||||||
|
- 同批材料尽量同方向拍摄;横竖混拍不会自动翻转,需要人确认方向。
|
||||||
|
|
||||||
| 场景 | 命令 |
|
## 边界
|
||||||
|---|---|
|
|
||||||
| 出网格图 | `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` 同目录,由它调用,日常不用直接执行):
|
- 纸张出画面的部分无法还原;角点可以外扩到画面外。
|
||||||
|
- 只保证页面拉正,不保证文字朝上;自动方向只判 90°,180° 需要人在审核页旋转。
|
||||||
| 模块 | 职责 |
|
- 检测不到角点的页用“画面内缩 3%”的兜底框出件,标 `needs_corners`,需人补角才是准确裁切。
|
||||||
|---|---|
|
- 默认 `paper=auto` 会把同批页面统一到中位比例(偏差 20% 以内),因此各页尺寸与留白一致;
|
||||||
| `grid.py` | 画带坐标的网格图 |
|
偏差更大的页按原比例出件。要严格 A4 用 `paper=a4`。
|
||||||
| `auto_quad.py` | 算候选角点 |
|
- 票据等窄纸建议单独成批,避免与 A4 混批时留白显得很宽。
|
||||||
| `prepare.py` | 出网格图:叠候选角点与检出边线 |
|
- 低分辨率照片会被放大到统一版式,不会因原图像素少而变小;清晰度仍受原图限制。
|
||||||
| `preview_quad.py` | 画角标图(叠网格) |
|
- 数字比例只改纸面比例,PDF 画布仍是 A4;换画布请用 a4/letter/16k/b5。
|
||||||
| `verify_quad.py` | 角点几何校验 |
|
- 支持 jpg/png/webp/bmp/tif;HEIC 需要额外依赖,当前不支持。
|
||||||
| `homography.py` | 透视变换 + 提亮 |
|
- 低分辨率、边缘背景残留、内容贴边只在 `warnings`/`checks` 里给指标,不会自动修正。
|
||||||
| `run.py` | 拉正 + 拼页(纯模块,无 CLI) |
|
- 深色纸、彩色纸、纸张与桌面颜色接近时,自动检测可能不稳,建议换对比明显的背景重拍。
|
||||||
| `make_pdf.py` | 多页合成 PDF |
|
- 单次建议不超过 20 页;页数更多或原图很大时可能触发技能 120s 超时,可分批处理或调高
|
||||||
| `check.py` | 成品 PDF 检验 |
|
`skills.timeout_ms`。
|
||||||
|
- 一图只处理一张纸;同框多张纸只取最大的一张。
|
||||||
## 已知边界
|
|
||||||
|
|
||||||
- 纸张出画面的部分无法还原,角点可外扩到画面外。
|
|
||||||
- 不为凑标准比例拉坐标;比例偏离通常是取点不准或纸张非该规格。
|
|
||||||
- 拍摄时四边各留 3%~5% 背景余量。
|
|
||||||
|
|||||||
@@ -1,27 +1,78 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""边线检测 + 纸角候选提取(从 edge_clean_c.py v3 抽取的精简模块)。
|
"""纸张边线检测:从一张已转正的 PIL 图里估四个纸角。
|
||||||
|
|
||||||
对外只暴露一个函数:
|
对外只有 detect(img, P) -> (corners, info):
|
||||||
detect(image_path, max_side=1600) -> (corners, info)
|
- img 是已做 exif_transpose 的 RGB 图;
|
||||||
|
- corners 是原图像素坐标 [(x, y)],顺序 左上、右上、右下、左下;
|
||||||
|
- info 里带覆盖率、纸面占比、回退标记,供上层做置信度告警。
|
||||||
|
|
||||||
corners:原图像素坐标系下的四角 [(x, y)],顺序 左上、右上、右下、左下;
|
设计要点:
|
||||||
info :附加信息(处理尺寸、缩放比、各边覆盖率、边线掩膜),供出图与诊断用。
|
- 不打印、不落盘、不画图。检测失败返回 ([], info),由调用方决定是否终止。
|
||||||
|
- 纸面掩膜保留两份:腐蚀过的只用于限定边缘搜索环带;回退几何用未腐蚀的
|
||||||
注意:本模块只产出**候选**坐标。它不写 corners.json,也不参与出件判定——
|
原始掩膜,避免角点系统性内收(旧实现两次 erode 会让角点切进纸内)。
|
||||||
最终角点必须以人工读角标图的结果为准。
|
- 回退时不再只用 4 个极值点连线,而是用整条掩膜边界按边拟合,再求交点。
|
||||||
"""
|
"""
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from PIL import Image, ImageOps, ImageFilter
|
from PIL import Image
|
||||||
from collections import deque
|
|
||||||
|
|
||||||
# ---------------- 基础工具 ----------------
|
|
||||||
def load_and_scale(path, max_side=1600):
|
# ---------------------------------------------------------------- 基础工具
|
||||||
pil = ImageOps.exif_transpose(Image.open(path)).convert("RGB")
|
|
||||||
w, h = pil.size
|
def _odd(size):
|
||||||
s = max_side / max(w, h)
|
size = int(size)
|
||||||
if s < 1:
|
if size < 3:
|
||||||
pil = pil.resize((int(round(w * s)), int(round(h * s))), Image.LANCZOS)
|
size = 3
|
||||||
return pil, np.asarray(pil).astype(np.float32) / 255.0
|
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):
|
def rgb_to_sv(arr):
|
||||||
@@ -34,7 +85,8 @@ def rgb_to_sv(arr):
|
|||||||
|
|
||||||
|
|
||||||
def luminance(arr):
|
def luminance(arr):
|
||||||
return (0.299 * arr[..., 0] + 0.587 * arr[..., 1] + 0.114 * arr[..., 2]).astype(np.float32)
|
return (0.299 * arr[..., 0] + 0.587 * arr[..., 1]
|
||||||
|
+ 0.114 * arr[..., 2]).astype(np.float32)
|
||||||
|
|
||||||
|
|
||||||
def box_mean(img, r):
|
def box_mean(img, r):
|
||||||
@@ -50,8 +102,12 @@ def box_mean(img, r):
|
|||||||
y1 = np.clip(ys + r + 1, 0, H)
|
y1 = np.clip(ys + r + 1, 0, H)
|
||||||
x0 = np.clip(xs - r, 0, W)
|
x0 = np.clip(xs - r, 0, W)
|
||||||
x1 = np.clip(xs + r + 1, 0, W)
|
x1 = np.clip(xs + r + 1, 0, W)
|
||||||
S = (ii[np.ix_(y1, x1)] - ii[np.ix_(y0, x1)]
|
# 边界裁剪等价于把积分图向外按边复制 r 行/列,这样四个角全部可用切片取,
|
||||||
- ii[np.ix_(y1, x0)] + ii[np.ix_(y0, x0)])
|
# 避开 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, :]
|
cnt = (y1 - y0)[:, None] * (x1 - x0)[None, :]
|
||||||
return S / np.maximum(cnt, 1).astype(np.float32)
|
return S / np.maximum(cnt, 1).astype(np.float32)
|
||||||
|
|
||||||
@@ -95,35 +151,8 @@ def sobel_magnitude(gray):
|
|||||||
return np.sqrt(gx * gx + gy * gy)
|
return np.sqrt(gx * gx + gy * gy)
|
||||||
|
|
||||||
|
|
||||||
# ---------------- 形态学(Pillow Max/MinFilter)----------------
|
# ---------------------------------------------------------------- 连通域 & 填孔
|
||||||
def _odd(size):
|
|
||||||
size = int(size)
|
|
||||||
if size < 3:
|
|
||||||
size = 3
|
|
||||||
if size % 2 == 0:
|
|
||||||
size += 1
|
|
||||||
return size
|
|
||||||
|
|
||||||
|
|
||||||
def dilate(mask, size):
|
|
||||||
pil = Image.fromarray((mask.astype(np.uint8) * 255))
|
|
||||||
return np.asarray(pil.filter(ImageFilter.MaxFilter(_odd(size)))) > 127
|
|
||||||
|
|
||||||
|
|
||||||
def erode(mask, size):
|
|
||||||
pil = Image.fromarray((mask.astype(np.uint8) * 255))
|
|
||||||
return np.asarray(pil.filter(ImageFilter.MinFilter(_odd(size)))) > 127
|
|
||||||
|
|
||||||
|
|
||||||
def closing(mask, size):
|
|
||||||
return erode(dilate(mask, size), size)
|
|
||||||
|
|
||||||
|
|
||||||
def opening(mask, size):
|
|
||||||
return dilate(erode(mask, size), size)
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------- 连通域 & 填孔(降采样图上做)----------------
|
|
||||||
def downsample_any(mask, factor):
|
def downsample_any(mask, factor):
|
||||||
if factor <= 1:
|
if factor <= 1:
|
||||||
return mask
|
return mask
|
||||||
@@ -140,74 +169,92 @@ def upsample_nearest(mask_small, target_h, target_w):
|
|||||||
return mask_small[np.ix_(yi, xi)]
|
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):
|
def fill_holes(mask_small):
|
||||||
H, W = mask_small.shape
|
"""填掉不与画面边界连通的背景(4 邻接),行程 + 并查集实现。"""
|
||||||
bg = ~mask_small
|
m = np.asarray(mask_small, dtype=bool)
|
||||||
visited = np.zeros((H, W), dtype=bool)
|
H, W = m.shape
|
||||||
q = deque()
|
bg = ~m
|
||||||
for x in range(W):
|
if not bg.any():
|
||||||
if bg[0, x] and not visited[0, x]:
|
return m.copy()
|
||||||
visited[0, x] = True
|
ry, rs, re_ = _row_runs(bg)
|
||||||
q.append((0, x))
|
if len(ry) == 0:
|
||||||
if bg[H - 1, x] and not visited[H - 1, x]:
|
return m.copy()
|
||||||
visited[H - 1, x] = True
|
lab = _run_labels(ry, rs, re_, H, eight=False)
|
||||||
q.append((H - 1, x))
|
touch = (ry == 0) | (ry == H - 1) | (rs == 0) | (re_ >= W)
|
||||||
for y in range(H):
|
nlab = int(lab.max()) + 1
|
||||||
if bg[y, 0] and not visited[y, 0]:
|
edge = np.zeros(nlab, dtype=bool)
|
||||||
visited[y, 0] = True
|
np.logical_or.at(edge, lab, touch)
|
||||||
q.append((y, 0))
|
sel = ~edge[lab]
|
||||||
if bg[y, W - 1] and not visited[y, W - 1]:
|
out = m.copy()
|
||||||
visited[y, W - 1] = True
|
for y, s, e in zip(ry[sel], rs[sel], re_[sel]):
|
||||||
q.append((y, W - 1))
|
out[y, s:e] = True
|
||||||
while q:
|
return out
|
||||||
y, x = q.popleft()
|
|
||||||
if y > 0 and bg[y - 1, x] and not visited[y - 1, x]:
|
|
||||||
visited[y - 1, x] = True
|
|
||||||
q.append((y - 1, x))
|
|
||||||
if y < H - 1 and bg[y + 1, x] and not visited[y + 1, x]:
|
|
||||||
visited[y + 1, x] = True
|
|
||||||
q.append((y + 1, x))
|
|
||||||
if x > 0 and bg[y, x - 1] and not visited[y, x - 1]:
|
|
||||||
visited[y, x - 1] = True
|
|
||||||
q.append((y, x - 1))
|
|
||||||
if x < W - 1 and bg[y, x + 1] and not visited[y, x + 1]:
|
|
||||||
visited[y, x + 1] = True
|
|
||||||
q.append((y, x + 1))
|
|
||||||
return mask_small | (bg & ~visited)
|
|
||||||
|
|
||||||
|
|
||||||
def largest_connected(mask_small):
|
def largest_connected(mask_small):
|
||||||
H, W = mask_small.shape
|
"""取最大 8 邻接连通域(行程 + 并查集,逐像素计数)。"""
|
||||||
visited = np.zeros((H, W), dtype=bool)
|
m = np.asarray(mask_small, dtype=bool)
|
||||||
best = np.zeros_like(mask_small)
|
H, W = m.shape
|
||||||
best_n = 0
|
if not m.any():
|
||||||
ys, xs = np.where(mask_small)
|
return np.zeros_like(m)
|
||||||
for y0, x0 in zip(ys, xs):
|
ry, rs, re_ = _row_runs(m)
|
||||||
if visited[y0, x0]:
|
lab = _run_labels(ry, rs, re_, H, eight=True)
|
||||||
continue
|
w = (re_ - rs).astype(np.int64)
|
||||||
stack = [(y0, x0)]
|
nlab = int(lab.max()) + 1
|
||||||
visited[y0, x0] = True
|
size = np.bincount(lab, weights=w, minlength=nlab)
|
||||||
comp = []
|
best = int(np.argmax(size))
|
||||||
while stack:
|
sel = lab == best
|
||||||
y, x = stack.pop()
|
out = np.zeros((H, W), dtype=bool)
|
||||||
comp.append((y, x))
|
for y, s, e in zip(ry[sel], rs[sel], re_[sel]):
|
||||||
for dy, dx in ((-1, -1), (-1, 0), (-1, 1), (0, -1),
|
out[y, s:e] = True
|
||||||
(0, 1), (1, -1), (1, 0), (1, 1)):
|
return out
|
||||||
ny, nx = y + dy, x + dx
|
|
||||||
if 0 <= ny < H and 0 <= nx < W and mask_small[ny, nx] and not visited[ny, nx]:
|
|
||||||
visited[ny, nx] = True
|
|
||||||
stack.append((ny, nx))
|
|
||||||
if len(comp) > best_n:
|
|
||||||
best_n = len(comp)
|
|
||||||
best = np.zeros_like(mask_small)
|
|
||||||
for y, x in comp:
|
|
||||||
best[y, x] = True
|
|
||||||
return best
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------- 纸面掩膜(整形后只用于限定搜索区域)----------------
|
# ---------------------------------------------------------------- 纸面掩膜
|
||||||
|
|
||||||
def otsu_threshold(score):
|
def otsu_threshold(score):
|
||||||
"""一维 Otsu:返回使类间方差最大的阈值"""
|
|
||||||
hist, bin_edges = np.histogram(score, bins=256, range=(0.0, 1.0))
|
hist, bin_edges = np.histogram(score, bins=256, range=(0.0, 1.0))
|
||||||
hist = hist.astype(np.float64)
|
hist = hist.astype(np.float64)
|
||||||
total = hist.sum()
|
total = hist.sum()
|
||||||
@@ -227,61 +274,67 @@ def otsu_threshold(score):
|
|||||||
return float(centers[int(np.argmax(between))])
|
return float(centers[int(np.argmax(between))])
|
||||||
|
|
||||||
|
|
||||||
def paper_mask(arr, debug=False):
|
def paper_masks(arr, P):
|
||||||
|
"""返回 (raw, eroded)。
|
||||||
|
|
||||||
|
raw:未腐蚀的纸面掩膜,用于回退几何,避免角点内收。
|
||||||
|
eroded:腐蚀后的掩膜,只用于限定边缘搜索环带。
|
||||||
|
"""
|
||||||
|
d = P["detect"]
|
||||||
H, W = arr.shape[:2]
|
H, W = arr.shape[:2]
|
||||||
S, V = rgb_to_sv(arr)
|
S, V = rgb_to_sv(arr)
|
||||||
# 纸面近白:亮度高、饱和低;木桌面偏色暗。用 Otsu 自动定阈值,
|
|
||||||
# 再叠加硬约束 V>0.50 防止桌面亮反射被并入。
|
|
||||||
score = (V * (1.0 - S)).astype(np.float32)
|
score = (V * (1.0 - S)).astype(np.float32)
|
||||||
th = otsu_threshold(score)
|
th = max(otsu_threshold(score), d["paper_otsu_floor"])
|
||||||
th = max(th, 0.50)
|
bright = (score > th) & (V > d["paper_v_min"]) & (S < d["paper_s_max"])
|
||||||
bright = (score > th) & (V > 0.50) & (S < 0.45)
|
|
||||||
if debug:
|
|
||||||
print(f"[paper_mask] otsu={th:.3f} bright frac={bright.mean():.3f}")
|
|
||||||
|
|
||||||
factor = max(1, min(H, W) // 400)
|
factor = max(1, min(H, W) // d["paper_mask_factor_div"])
|
||||||
bs = downsample_any(bright, factor)
|
bs = downsample_any(bright, factor)
|
||||||
|
|
||||||
size = _odd(max(5, min(bs.shape) // 60))
|
m = closing(bs, _odd(max(d["paper_close_min"],
|
||||||
m = closing(bs, size)
|
min(bs.shape) // d["paper_close_div"])))
|
||||||
m = fill_holes(m)
|
m = fill_holes(m)
|
||||||
m = largest_connected(m)
|
m = largest_connected(m)
|
||||||
m = closing(m, _odd(max(5, min(bs.shape) // 80))) # 抹掉内凹缺口/阶梯
|
m = closing(m, _odd(max(d["paper_close_min"],
|
||||||
|
min(bs.shape) // d["paper_close2_div"])))
|
||||||
m = fill_holes(m)
|
m = fill_holes(m)
|
||||||
# 掩膜轻微内收:纸边最外侧 1~2px 常被阴影/暗边吃掉,内收后环带更贴合真实纸边
|
|
||||||
m = erode(m, 3)
|
|
||||||
|
|
||||||
paper = upsample_nearest(m, H, W) if factor > 1 else m
|
raw_small = m
|
||||||
if debug:
|
eroded_small = erode(m, _odd(d["mask_erode_kernel"]))
|
||||||
Image.fromarray((paper * 255).astype(np.uint8)).save("dbg_v3_paper.png")
|
|
||||||
return paper
|
|
||||||
|
|
||||||
|
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):
|
def boundary_band(paper, k):
|
||||||
"""纸面掩膜边界两侧各 k 像素的环带。"""
|
|
||||||
k = _odd(k)
|
k = _odd(k)
|
||||||
return dilate(paper, k) & ~erode(paper, k)
|
return dilate(paper, k) & ~erode(paper, k)
|
||||||
|
|
||||||
|
|
||||||
# ---------------- 边缘图(局部自适应阈值)----------------
|
def edge_map(arr, P):
|
||||||
def edge_map(arr, local_gain=1.55, low=0.035):
|
d = P["detect"]
|
||||||
H = arr.shape[0]
|
H = arr.shape[0]
|
||||||
gray = luminance(arr)
|
gray = luminance(arr)
|
||||||
sigma = max(1.0, H / 800.0)
|
sigma = max(1.0, H / float(d["edge_sigma_div"]))
|
||||||
blurred = gaussian_blur_np(gray, sigma)
|
blurred = gaussian_blur_np(gray, sigma)
|
||||||
mag = sobel_magnitude(blurred)
|
mag = sobel_magnitude(blurred)
|
||||||
local = box_mean(mag, max(3, H // 50))
|
local = box_mean(mag, max(3, H // d["edge_box_div"]))
|
||||||
th = np.maximum(low, local * local_gain)
|
th = np.maximum(d["edge_low"], local * d["edge_local_gain"])
|
||||||
return mag > th
|
return mag > th
|
||||||
|
|
||||||
|
|
||||||
def boundary_step_filter(arr, paper, r_small=10, r_big=24, step_th=0.055):
|
def boundary_step_filter(arr, paper, P):
|
||||||
"""跨纸边亮度阶跃检测:比较掩膜内侧/外侧小窗的 V 均值。
|
"""跨纸边亮度阶跃:比较掩膜内侧/外侧小窗的 V 均值。
|
||||||
|
|
||||||
纸边是真实的明暗台阶(阶跃大),木纹/阴影纹理阶跃小。相比固定亮度
|
纸边是真实的明暗台阶,木纹/阴影纹理阶跃小。两级窗口:掩膜准确处用
|
||||||
阈值(V>0.60 / V<0.45),不受整体曝影响。两级窗口:掩膜准确的
|
r_small,掩膜略有偏移处依靠 r_big 补上。
|
||||||
位置用 r_small,掩膜略有内收/偏移处依靠 r_big 补上。
|
|
||||||
"""
|
"""
|
||||||
|
d = P["detect"]
|
||||||
_, V = rgb_to_sv(arr)
|
_, V = rgb_to_sv(arr)
|
||||||
p = paper.astype(np.float32)
|
p = paper.astype(np.float32)
|
||||||
q = 1.0 - p
|
q = 1.0 - p
|
||||||
@@ -294,27 +347,30 @@ def boundary_step_filter(arr, paper, r_small=10, r_big=24, step_th=0.055):
|
|||||||
ok = (din > 0.08) & (dout > 0.08)
|
ok = (din > 0.08) & (dout > 0.08)
|
||||||
return np.where(ok, np.abs(vin - vout), 0.0)
|
return np.where(ok, np.abs(vin - vout), 0.0)
|
||||||
|
|
||||||
step = np.maximum(side_means(r_small), side_means(r_big))
|
step = np.maximum(side_means(d["step_r_small"]),
|
||||||
return step > step_th
|
side_means(d["step_r_big"]))
|
||||||
|
return step > d["step_th"]
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- 四角提取
|
||||||
|
|
||||||
# ---------------- 四角提取(掩膜 -> 四边拟合 -> 交点) ----------------
|
def mask_boundary_points(mask, P):
|
||||||
def mask_boundary_points(mask):
|
"""掩膜边界像素坐标 (N, 2),顺序 (x, y)。"""
|
||||||
"""掩膜边界像素坐标 (N,2) float32,顺序为 (x, y)"""
|
inner = erode(mask, _odd(P["detect"]["mask_erode_kernel"]))
|
||||||
inner = erode(mask, 3)
|
|
||||||
ys, xs = np.where(mask & ~inner)
|
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)
|
return np.stack([xs, ys], axis=1).astype(np.float32)
|
||||||
|
|
||||||
|
|
||||||
def _fit_line(pts):
|
def _fit_line(pts):
|
||||||
"""总体最小二乘拟直线,返回法向量 n 与 n·p = c"""
|
"""总体最小二乘拟直线,返回法向量 n 与 n·p = c。"""
|
||||||
c0 = pts.mean(axis=0)
|
c0 = pts.mean(axis=0)
|
||||||
q = pts - c0
|
q = pts - c0
|
||||||
cov = (q.T @ q) / max(len(pts) - 1, 1)
|
cov = (q.T @ q) / max(len(pts) - 1, 1)
|
||||||
w, v = np.linalg.eigh(cov)
|
w, v = np.linalg.eigh(cov)
|
||||||
d = v[:, int(np.argmax(w))] # 主方向
|
direction = v[:, int(np.argmax(w))]
|
||||||
n = np.array([-d[1], d[0]], dtype=np.float64)
|
n = np.array([-direction[1], direction[0]], dtype=np.float64)
|
||||||
nn = np.linalg.norm(n)
|
nn = np.linalg.norm(n)
|
||||||
if nn < 1e-9:
|
if nn < 1e-9:
|
||||||
return np.array([0.0, 1.0]), float(c0[1])
|
return np.array([0.0, 1.0]), float(c0[1])
|
||||||
@@ -322,6 +378,19 @@ def _fit_line(pts):
|
|||||||
return n, float(n @ c0)
|
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):
|
def _intersect(n1, c1, n2, c2):
|
||||||
A = np.vstack([n1, n2])
|
A = np.vstack([n1, n2])
|
||||||
b = np.array([c1, c2], dtype=np.float64)
|
b = np.array([c1, c2], dtype=np.float64)
|
||||||
@@ -331,106 +400,120 @@ def _intersect(n1, c1, n2, c2):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def detect_quad_corners(paper, points=None, robust_px=2.0, group_tol=22.0):
|
def _side_line(p0, p1):
|
||||||
"""提取四个纸角,返回 [(x,y)],顺序 左上、右上、右下、左下。
|
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
|
||||||
|
|
||||||
锚点一律取自纸面掩膜边界(稳定,不受噪声小刺影响);points 传入纸边黑线
|
|
||||||
像素时,按到四条基准边的距离分组后重新拟合,得到更贴真实纸边的角点。
|
def detect_quad_corners(paper_raw, points, P):
|
||||||
每个角都有退化与越界检查,不通过就回退到掩膜基准边交点。
|
"""从掩膜边界 + 边缘点估四角,返回 (corners, fallback_sides)。"""
|
||||||
"""
|
d = P["detect"]
|
||||||
base = mask_boundary_points(paper)
|
base = mask_boundary_points(paper_raw, P)
|
||||||
if len(base) < 40:
|
if len(base) < 40:
|
||||||
return []
|
return [], [0, 1, 2, 3]
|
||||||
|
|
||||||
bx, by = base[:, 0], base[:, 1]
|
bx, by = base[:, 0], base[:, 1]
|
||||||
a_s = bx + by
|
|
||||||
a_d = bx - by
|
|
||||||
anchors = np.array([
|
anchors = np.array([
|
||||||
base[int(np.argmin(a_s))], # 左上
|
base[int(np.argmin(bx + by))], # 左上
|
||||||
base[int(np.argmax(a_d))], # 右上
|
base[int(np.argmax(bx - by))], # 右上
|
||||||
base[int(np.argmax(a_s))], # 右下
|
base[int(np.argmax(bx + by))], # 右下
|
||||||
base[int(np.argmin(a_d))], # 左下
|
base[int(np.argmin(bx - by))], # 左下
|
||||||
], dtype=np.float64)
|
], dtype=np.float64)
|
||||||
|
|
||||||
|
init = [_side_line(anchors[i], anchors[(i + 1) % 4]) for i in range(4)]
|
||||||
|
|
||||||
|
# 用整条掩膜边界重拟合每一边(排除两端,避开角点圆角)
|
||||||
base_lines = []
|
base_lines = []
|
||||||
for a, b in ((0, 1), (1, 2), (2, 3), (3, 0)):
|
for i in range(4):
|
||||||
p0, p1 = anchors[a], anchors[b]
|
sl = init[i]
|
||||||
dv = p1 - p0
|
if sl is None:
|
||||||
L = float(np.linalg.norm(dv))
|
|
||||||
if L < 1e-6:
|
|
||||||
base_lines.append(None)
|
base_lines.append(None)
|
||||||
continue
|
continue
|
||||||
dv = dv / L
|
n, c, u, L = sl
|
||||||
n0 = np.array([-dv[1], dv[0]])
|
p0 = anchors[i]
|
||||||
base_lines.append((n0, float(n0 @ p0)))
|
dist = np.abs(base @ n - c)
|
||||||
|
proj = (base - p0) @ u
|
||||||
corners = []
|
m = d["boundary_side_margin"]
|
||||||
for i in range(4):
|
sel = base[(dist <= d["group_tol_px"]) &
|
||||||
l1, l2 = base_lines[i - 1], base_lines[i]
|
(proj >= L * m) & (proj <= L * (1.0 - m))]
|
||||||
if l1 is None or l2 is None:
|
if len(sel) >= 40:
|
||||||
corners.append(anchors[i].copy())
|
n2, c2 = _robust_fit(sel, d["robust_px"])
|
||||||
continue
|
base_lines.append((n2, c2))
|
||||||
p = _intersect(l1[0], l1[1], l2[0], l2[1])
|
|
||||||
corners.append(anchors[i].copy() if p is None else p)
|
|
||||||
|
|
||||||
if points is None:
|
|
||||||
return [(float(p[0]), float(p[1])) for p in corners]
|
|
||||||
|
|
||||||
pts = np.asarray(points, dtype=np.float64)
|
|
||||||
if len(pts) < 40:
|
|
||||||
return [(float(p[0]), float(p[1])) for p in corners]
|
|
||||||
|
|
||||||
dd = []
|
|
||||||
for l in base_lines:
|
|
||||||
if l is None:
|
|
||||||
dd.append(np.full(len(pts), 1e9))
|
|
||||||
else:
|
else:
|
||||||
dd.append(np.abs(pts @ l[0] - l[1]))
|
base_lines.append((n, c))
|
||||||
D = np.stack(dd, axis=1)
|
|
||||||
idx = np.argmin(D, axis=1)
|
|
||||||
dmin = D[np.arange(len(pts)), idx]
|
|
||||||
|
|
||||||
lines = []
|
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):
|
for i in range(4):
|
||||||
sel = pts[(idx == i) & (dmin <= group_tol)]
|
chosen = None
|
||||||
if len(sel) < 40:
|
for lines in (final_lines, base_lines):
|
||||||
lines.append(base_lines[i])
|
l1, l2 = lines[i - 1], lines[i]
|
||||||
continue
|
if l1 is None or l2 is None:
|
||||||
n, c = _fit_line(sel)
|
continue
|
||||||
for _ in range(2):
|
det = l1[0][0] * l2[0][1] - l2[0][0] * l1[0][1]
|
||||||
res = np.abs(sel @ n - c)
|
if abs(det) < 1e-3:
|
||||||
keep = sel[res <= max(robust_px, 1.0) * 2.5]
|
continue
|
||||||
if len(keep) < 40:
|
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
|
break
|
||||||
n, c = _fit_line(keep)
|
if chosen is None:
|
||||||
sel = keep
|
chosen = (float(anchors[i][0]), float(anchors[i][1]))
|
||||||
lines.append((n, c))
|
if i not in fallback_sides:
|
||||||
|
fallback_sides.append(i)
|
||||||
|
out.append(chosen)
|
||||||
|
|
||||||
H, W = paper.shape
|
return out, sorted(set(fallback_sides))
|
||||||
for i in range(4):
|
|
||||||
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 not (-0.15 * W <= p[0] <= 1.15 * W and -0.15 * H <= p[1] <= 1.15 * H):
|
|
||||||
continue
|
|
||||||
corners[i] = p
|
|
||||||
|
|
||||||
return [(float(p[0]), float(p[1])) for p in corners]
|
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- 覆盖率
|
||||||
|
|
||||||
|
def border_coverage_exact(edges, paper, tol):
|
||||||
# ---------------- 覆盖率指标 ----------------
|
"""沿掩膜逐行/逐列的真实边界测边缘覆盖率。"""
|
||||||
def border_coverage_exact(edges, paper, tol=14):
|
|
||||||
"""沿掩膜逐行/逐列的真实边界测覆盖率(适配斜边,不受 bbox 影响)。
|
|
||||||
|
|
||||||
对每一列取掩膜的上/下边界,对每一行取左/右边界,检查 ±tol 内是否有边缘像素。
|
|
||||||
"""
|
|
||||||
H, W = paper.shape
|
H, W = paper.shape
|
||||||
col_has = paper.any(axis=0)
|
col_has = paper.any(axis=0)
|
||||||
row_has = paper.any(axis=1)
|
row_has = paper.any(axis=1)
|
||||||
@@ -463,38 +546,63 @@ def border_coverage_exact(edges, paper, tol=14):
|
|||||||
"right": right_hit / max(right_n, 1),
|
"right": right_hit / max(right_n, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
# ---------------- 对外接口 ----------------
|
|
||||||
|
|
||||||
def detect(image_path, max_side=1600):
|
# ---------------------------------------------------------------- 对外接口
|
||||||
|
|
||||||
|
def detect(img, P):
|
||||||
"""检测纸张四角,返回 (corners_原图坐标, info)。失败返回 ([], info)。"""
|
"""检测纸张四角,返回 (corners_原图坐标, info)。失败返回 ([], info)。"""
|
||||||
src = ImageOps.exif_transpose(Image.open(image_path)).convert('RGB')
|
d = P["detect"]
|
||||||
src_w, src_h = src.size
|
src_w, src_h = img.size
|
||||||
pil, arr = load_and_scale(image_path, max_side=max_side)
|
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]
|
H, W = arr.shape[:2]
|
||||||
|
|
||||||
paper = paper_mask(arr)
|
paper_raw, paper_eroded = paper_masks(arr, P)
|
||||||
frac = float(paper.sum()) / (H * W)
|
frac = float(paper_raw.sum()) / float(H * W)
|
||||||
|
|
||||||
band = boundary_band(paper, _odd(max(15, H // 45)))
|
info = {
|
||||||
edges = edge_map(arr)
|
"source_size": [int(src_w), int(src_h)],
|
||||||
step = boundary_step_filter(arr, paper)
|
"processed_size": [int(W), int(H)],
|
||||||
cand = closing(edges & band & step, _odd(max(5, H // 160)))
|
"scale": round(scale, 6),
|
||||||
cand = opening(cand, _odd(max(3, H // 500)))
|
"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)
|
ey, ex = np.nonzero(cand)
|
||||||
pts = np.stack([ex, ey], axis=1).astype(np.float32) if len(ex) > 40 else None
|
pts = (np.stack([ex, ey], axis=1).astype(np.float32)
|
||||||
corners = detect_quad_corners(paper, points=pts)
|
if len(ex) >= d["min_edge_points"] else None)
|
||||||
|
|
||||||
scale = min(1.0, float(max_side) / max(src_w, src_h))
|
corners, fallback_sides = detect_quad_corners(paper_raw, pts, P)
|
||||||
src_pts = [(x / scale, y / scale) for x, y in corners] if corners else []
|
info["edge_pixels"] = int(cand.sum())
|
||||||
info = {
|
info["fallback_sides"] = fallback_sides
|
||||||
'source_size': [src_w, src_h],
|
info["fallback"] = bool(fallback_sides)
|
||||||
'processed_size': [W, H],
|
if not corners:
|
||||||
'scale': round(scale, 6),
|
return [], info
|
||||||
'paper_frac': round(frac, 4),
|
|
||||||
'edge_pixels': int(cand.sum()),
|
info["coverage"] = {
|
||||||
'coverage': border_coverage_exact(cand, paper, tol=max(10, H // 110)) if corners else {},
|
k: float(v) for k, v in border_coverage_exact(
|
||||||
'processed_pts': corners,
|
cand, paper_eroded,
|
||||||
'edge_mask_small': cand, # 处理尺寸上的边线掩膜(供叠加出图)
|
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
|
return src_pts, info
|
||||||
|
|||||||
@@ -1,20 +1,11 @@
|
|||||||
"""PDF 产出的通用质量检验(渲染真实页面后评估)。
|
# -*- coding: utf-8 -*-
|
||||||
|
"""PDF 产出的数值检验:渲染真实页面后评估,不画图、不修改文件。
|
||||||
|
|
||||||
检验对象是最终交付的 PDF,不是中间图:把每页按固定 DPI 渲染成位图,
|
关键修正(相对旧版):
|
||||||
在渲染结果上做统计判定,所见即交付物。
|
- “边缘背景残留 / 内容贴边”改为在 paper_box 裁出的纸面区域上量。
|
||||||
|
旧版量的是 PDF 页面最外圈,而正常出件四周是白边,这两项恒为通过。
|
||||||
检验项:
|
- 渲染页按文件名数字自然排序,≥10 页不会出现 1,10,2 的顺序。
|
||||||
1. 页数与源图数一致
|
- 渲染后端增加 PyMuPDF(executor 镜像已装 pymupdf),pdftoppm 不在时也能跑。
|
||||||
2. 有效内容分辨率(页面渲染后纸面区域的像素尺寸)
|
|
||||||
3. 纸张比例失真(纸面外接框的宽高比,对照页面书写比例)
|
|
||||||
4. 纸面外背景残留(边带亮度显著低于纸面)
|
|
||||||
5. 内容贴边被切(页边存在成片文字级暗像素)
|
|
||||||
|
|
||||||
判据全部基于该页自身像素分布自适应(Otsu / 分位数),不含针对特定文档的常量。
|
|
||||||
|
|
||||||
用法:
|
|
||||||
python3 doc_deskew/check.py 结果.pdf [--expect-pages 2] [--dpi 100]
|
|
||||||
python3 doc_deskew/check.py 结果.pdf --render # 导出渲染图供目视
|
|
||||||
"""
|
"""
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
@@ -25,23 +16,26 @@ import sys
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from PIL import Image
|
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)
|
def _natural_key(path):
|
||||||
for f in os.listdir(outdir):
|
name = os.path.basename(path)
|
||||||
if f.startswith('page'):
|
out = []
|
||||||
os.remove(os.path.join(outdir, f))
|
cur = ""
|
||||||
base = os.path.join(outdir, 'page')
|
for ch in name:
|
||||||
if shutil.which('pdftoppm'):
|
if ch.isdigit():
|
||||||
subprocess.run(['pdftoppm', '-r', str(dpi), '-png', pdf, base], check=True)
|
cur += ch
|
||||||
else: # 退路:pypdfium2
|
else:
|
||||||
import pypdfium2 as pdfium
|
if cur:
|
||||||
doc = pdfium.PdfDocument(pdf)
|
out.append((1, int(cur)))
|
||||||
scale = dpi / 72
|
cur = ""
|
||||||
for i in range(len(doc)):
|
out.append((0, ch.lower()))
|
||||||
doc[i].render(scale=scale).to_pil().save('%s-%d.png' % (base, i + 1))
|
if cur:
|
||||||
files = sorted(f for f in os.listdir(outdir) if f.startswith('page') and f.endswith('.png'))
|
out.append((1, int(cur)))
|
||||||
return [os.path.join(outdir, f) for f in files]
|
return out
|
||||||
|
|
||||||
|
|
||||||
def otsu(vals):
|
def otsu(vals):
|
||||||
@@ -55,148 +49,233 @@ def otsu(vals):
|
|||||||
w1 = total - w0
|
w1 = total - w0
|
||||||
m0 = np.cumsum(hist * centers)
|
m0 = np.cumsum(hist * centers)
|
||||||
mt = m0[-1]
|
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 = (mt * w0 / total - m0) ** 2 / (w0 * w1 / total ** 2)
|
||||||
score = np.nan_to_num(score)
|
score = np.nan_to_num(score)
|
||||||
return float(centers[int(np.argmax(score))])
|
return float(centers[int(np.argmax(score))])
|
||||||
|
|
||||||
|
|
||||||
def analyse(img):
|
def render(pdf, dpi=100, outdir=None):
|
||||||
g = np.asarray(img.convert('L')).astype(np.float32)
|
"""把 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
|
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)}
|
||||||
|
|
||||||
# 纸面亮度:取全图高分位(纸面是主体亮区)
|
# 先用“非纯白”找纸面框:PDF 页面四周是白边,纸面被提亮到 ~245
|
||||||
res['paper'] = float(np.percentile(g, 80))
|
flat = float(np.percentile(g, c["flat_pct"]))
|
||||||
# 背景亮度:低分位
|
nonwhite = g < (flat - c["nonwhite_delta"])
|
||||||
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)
|
|
||||||
row_frac = nonwhite.mean(axis=1)
|
row_frac = nonwhite.mean(axis=1)
|
||||||
col_frac = nonwhite.mean(axis=0)
|
col_frac = nonwhite.mean(axis=0)
|
||||||
# 取占比超过 5% 的连续范围作为纸面主体
|
rows = np.where(row_frac > c["paper_row_frac"])[0]
|
||||||
rows = np.where(row_frac > 0.05)[0]
|
cols = np.where(col_frac > c["paper_row_frac"])[0]
|
||||||
cols = np.where(col_frac > 0.05)[0]
|
box = None
|
||||||
if rows.size > 4 and cols.size > 4:
|
if rows.size > 4 and cols.size > 4:
|
||||||
res['paper_box'] = (int(cols[0]), int(rows[0]), int(cols[-1]), int(rows[-1]))
|
box = (int(cols[0]), int(rows[0]), int(cols[-1]), int(rows[-1]))
|
||||||
pw = cols[-1] - cols[0] + 1
|
res["paper_box"] = box
|
||||||
ph = rows[-1] - rows[0] + 1
|
res["box_fallback"] = box is None
|
||||||
res['paper_ratio'] = pw / ph
|
|
||||||
res['paper_px'] = (int(pw), int(ph))
|
if box:
|
||||||
|
x0, y0, x1, y1 = box
|
||||||
|
gp = g[y0:y1 + 1, x0:x1 + 1]
|
||||||
else:
|
else:
|
||||||
res['paper_box'] = None
|
gp = g
|
||||||
res['paper_ratio'] = res['ratio']
|
ph, pw = gp.shape
|
||||||
res['paper_px'] = (w, h)
|
|
||||||
|
|
||||||
# 边缘背景残留:四周窄带的平均亮度明显低于纸面亮度
|
# 透视采样在纸边常带 1~2px 背景毛边,先内缩再量边缘;
|
||||||
# 边带宽度按比例取,过窄会漏判
|
# 否则会被误判成“边缘背景残留”或“内容贴边”。
|
||||||
b = max(4, int(min(w, h) * 0.012))
|
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 = {
|
border = {
|
||||||
'top': float(g[:b, :].mean()), 'bottom': float(g[-b:, :].mean()),
|
"top": float(gc[:b, :].mean()),
|
||||||
'left': float(g[:, :b].mean()), 'right': float(g[:, -b:].mean()),
|
"bottom": float(gc[-b:, :].mean()),
|
||||||
|
"left": float(gc[:, :b].mean()),
|
||||||
|
"right": float(gc[:, -b:].mean()),
|
||||||
}
|
}
|
||||||
res['border'] = border
|
res["border"] = border
|
||||||
res['border_bad'] = [k for k, v in border.items() if v < res['paper'] * 0.80]
|
res["border_bad"] = [k for k, v in border.items()
|
||||||
# 另一路判据:边带内是否存在"整行整列偏暗"的条带(纸外背景)
|
if v < res["paper"] * c["border_ratio"]]
|
||||||
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
|
|
||||||
|
|
||||||
# 内容贴边:最外窄带内成片文字级暗像素 <=> 内容被切
|
edge_dark = {}
|
||||||
m = max(2, int(min(w, h) * 0.003))
|
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 = []
|
cut = []
|
||||||
for k, band in (('top', g[:m, :]), ('bottom', g[-m:, :]),
|
for k, band in (("top", gc[:m, :]), ("bottom", gc[-m:, :]),
|
||||||
('left', g[:, :m]), ('right', g[:, -m:])):
|
("left", gc[:, :m]), ("right", gc[:, -m:])):
|
||||||
frac = float((band < res['ink']).mean())
|
frac = float((band < res["ink"]).mean())
|
||||||
if frac > 0.05:
|
if (frac > c["cut_frac"] and
|
||||||
|
float(band.mean()) < res["paper"] * c["cut_mean_ratio"]):
|
||||||
cut.append((k, frac))
|
cut.append((k, frac))
|
||||||
res['cut'] = cut
|
res["cut"] = cut
|
||||||
return res
|
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
|
||||||
|
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
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
ap = argparse.ArgumentParser()
|
ap = argparse.ArgumentParser(description="复检已有 PDF(只渲染检验)")
|
||||||
ap.add_argument('pdf')
|
ap.add_argument("pdf")
|
||||||
ap.add_argument('--expect-pages', type=int, default=None)
|
ap.add_argument("--expect-pages", type=int, default=None)
|
||||||
ap.add_argument('--dpi', type=int, default=100)
|
ap.add_argument("--dpi", type=int, default=P["check"]["dpi"])
|
||||||
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')
|
|
||||||
args = ap.parse_args()
|
args = ap.parse_args()
|
||||||
|
|
||||||
if not os.path.exists(args.pdf):
|
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)
|
r = inspect_pdf(args.pdf, args.expect_pages, args.dpi)
|
||||||
print('PDF: %s' % args.pdf)
|
print("PDF: %s" % args.pdf)
|
||||||
print('文件大小: %.0f KB' % (os.path.getsize(args.pdf) / 1024))
|
print("文件大小: %.0f KB" % (os.path.getsize(args.pdf) / 1024.0))
|
||||||
print('页数: %d' % len(files))
|
if r.get("ok") is None:
|
||||||
|
print("渲染失败:%s" % r.get("error"))
|
||||||
ok = True
|
return 1
|
||||||
if args.expect_pages is not None:
|
print("页数: %d" % r["page_count"]["actual"])
|
||||||
good = len(files) == args.expect_pages
|
for item in r["pages"]:
|
||||||
ok &= good
|
print("\n第 %d 页 纸面 %dx%d" % (
|
||||||
print(' [%s] 页数与源图一致(期望 %d,实际 %d)'
|
item["index"], item["paper_px"][0], item["paper_px"][1]))
|
||||||
% ('OK' if good else 'XX', args.expect_pages, len(files)))
|
print(" [%s] 纸面短边 %d px"
|
||||||
|
% ("OK" if item["short_side_ok"] else "XX",
|
||||||
for i, f in enumerate(files, 1):
|
item["short_side_px"]))
|
||||||
img = Image.open(f)
|
print(" [%s] 边缘背景残留 %s"
|
||||||
r = analyse(img)
|
% ("OK" if item["border_ok"] else "XX",
|
||||||
print('\n第 %d 页 渲染 %dx%d px @%ddpi' % (i, r['w'], r['h'], args.dpi))
|
"无" if item["border_ok"] else "有"))
|
||||||
|
print(" [%s] 内容贴边 %s"
|
||||||
# 纸张实际像素尺寸(按渲染图里纸面外接框估算,换算回原分辨率)
|
% ("OK" if item["cut_ok"] else "XX",
|
||||||
if r['paper_box']:
|
"无" if item["cut_ok"] else
|
||||||
x0, y0, x1, y1 = r['paper_box']
|
"、".join("%s %.1f%%" % (k, v * 100)
|
||||||
scale = 1.0 # 渲染已是实际像素
|
for k, v in item["cut"])))
|
||||||
short = min(x1 - x0, y1 - y0)
|
print(" [--] 纸面比例 %.3f(页面 %.3f)"
|
||||||
good = short >= args.min_short * args.dpi / 150.0
|
% (item["paper_ratio"], item["page_ratio"]))
|
||||||
ok &= good
|
print("\n总判定:%s" % ("全部通过" if r["ok"] else "存在不达标项"))
|
||||||
print(' [%s] 纸面短边 %d px(下限 %d)'
|
return 0 if r["ok"] else 1
|
||||||
% ('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
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
sys.exit(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,50 +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):
|
|
||||||
"""在原图上叠加坐标网格,返回叠加后的图像(不落盘)。
|
|
||||||
|
|
||||||
先画到独立的 RGBA 图层再合成:RGBA 底图直接画会被 ImageDraw 覆盖,
|
|
||||||
半透明线变成实心色,跟角标图上的网格对不上。
|
|
||||||
"""
|
|
||||||
W, H = im.size
|
|
||||||
layer = Image.new('RGBA', im.size, (0, 0, 0, 0))
|
|
||||||
d = ImageDraw.Draw(layer)
|
|
||||||
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 Image.alpha_composite(im.convert('RGBA'), layer).convert(im.mode)
|
|
||||||
|
|
||||||
|
|
||||||
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。
|
"""纯 numpy 单应变换:等价于 cv2.warpPerspective,只依赖 Pillow + numpy。
|
||||||
|
|
||||||
用法:
|
与旧实现的区别:
|
||||||
python homography.py in.jpg out.png --quad x1,y1 x2,y2 x3,y3 x4,y4
|
- 按行分块采样,峰值内存从“整页 × 多份 float64”降到几十 MB;
|
||||||
四角顺序:左上、右上、右下、左下。不给 --quad 时按整幅图重采样。
|
- 只接受 PIL 图,坐标/方向由调用方统一(exif_transpose 已在入口做过);
|
||||||
|
- 不再提供 CLI,出件统一走技能入口。
|
||||||
"""
|
"""
|
||||||
import argparse
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
@@ -18,64 +19,71 @@ def _solve_h(src, dst):
|
|||||||
return vt[-1].reshape(3, 3)
|
return vt[-1].reshape(3, 3)
|
||||||
|
|
||||||
|
|
||||||
def warp_perspective(img, quad, out_size=None, fill=(255, 255, 255)):
|
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)
|
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)
|
quad = np.asarray(quad, dtype=np.float64)
|
||||||
tl, tr, br, bl = quad
|
tl, tr, br, bl = quad
|
||||||
|
|
||||||
if out_size is None:
|
if out_size is None:
|
||||||
w = int(round(max(np.linalg.norm(tr - tl), np.linalg.norm(br - bl))))
|
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))))
|
h = int(round(max(np.linalg.norm(bl - tl), np.linalg.norm(br - tr))))
|
||||||
out_size = (w, h)
|
out_size = (w, h)
|
||||||
out_w, out_h = out_size
|
out_w = max(1, int(out_size[0]))
|
||||||
dst = np.array([[0, 0], [out_w - 1, 0], [out_w - 1, out_h - 1], [0, out_h - 1]],
|
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)
|
dtype=np.float64)
|
||||||
Hinv = np.linalg.inv(_solve_h(quad, dst))
|
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)
|
out = np.empty((out_h, out_w, 3), dtype=np.uint8)
|
||||||
src_h = Hinv @ dst_h
|
step = max(1, int(chunk_rows))
|
||||||
sx = src_h[0] / src_h[2]
|
for y0 in range(0, out_h, step):
|
||||||
sy = src_h[1] / src_h[2]
|
y1 = min(y0 + step, out_h)
|
||||||
h, w = arr.shape[:2]
|
ys, xs = np.mgrid[y0:y1, 0:out_w]
|
||||||
valid = (sx >= 0) & (sx <= w - 1) & (sy >= 0) & (sy <= h - 1)
|
dst_h = np.stack([xs.ravel(), ys.ravel(),
|
||||||
sx = np.clip(sx, 0, w - 1); sy = np.clip(sy, 0, h - 1)
|
np.ones(xs.size)], axis=0).astype(np.float64)
|
||||||
x0 = np.floor(sx).astype(np.int32); x1 = np.minimum(x0 + 1, w - 1)
|
sp = Hinv @ dst_h
|
||||||
y0 = np.floor(sy).astype(np.int32); y1 = np.minimum(y0 + 1, h - 1)
|
den = sp[2]
|
||||||
fx = (sx - x0)[:, None]; fy = (sy - y0)[:, None]
|
den = np.where(np.abs(den) < 1e-9, 1e-9, den)
|
||||||
a = arr[y0, x0].astype(np.float64); b = arr[y0, x1].astype(np.float64)
|
sx = sp[0] / den
|
||||||
c = arr[y1, x0].astype(np.float64); d = arr[y1, x1].astype(np.float64)
|
sy = sp[1] / den
|
||||||
out = (a * (1 - fx) * (1 - fy) + b * fx * (1 - fy) +
|
|
||||||
c * (1 - fx) * fy + d * fx * fy)
|
valid = (sx >= 0) & (sx <= src_w - 1) & (sy >= 0) & (sy <= src_h - 1)
|
||||||
out[~valid] = fill
|
sx = np.clip(sx, 0, src_w - 1)
|
||||||
out = np.clip(out, 0, 255).astype(np.uint8).reshape(out_h, out_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)
|
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):
|
def brighten(img, target=245, pct=97):
|
||||||
arr = np.asarray(img.convert('RGB')).astype(np.float32)
|
"""全局等比提亮:把 pct 分位映射到 target,保持相对对比度。"""
|
||||||
hi = np.percentile(arr, pct)
|
arr = np.asarray(img if img.mode == "RGB" else img.convert("RGB")).astype(np.float32)
|
||||||
if hi > 1:
|
hi = float(np.percentile(arr, pct))
|
||||||
arr = np.clip(arr * (target / hi), 0, 255)
|
if hi > 1.0:
|
||||||
|
arr = np.clip(arr * (float(target) / hi), 0, 255)
|
||||||
return Image.fromarray(arr.astype(np.uint8))
|
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,454 +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 = ('左上', '右上', '右下', '左下')
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- 基础工具
|
|
||||||
|
|
||||||
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 = [n for n in args if n]
|
|
||||||
if not names:
|
|
||||||
raise SystemExit('必须显式指定图片,例如:prepare 图1.jpg 图2.jpg')
|
|
||||||
seen, out = set(), []
|
|
||||||
for n in names:
|
|
||||||
if n not in seen:
|
|
||||||
seen.add(n)
|
|
||||||
out.append(n)
|
|
||||||
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, auto_quad=True):
|
|
||||||
os.makedirs(OUT_DIR, exist_ok=True)
|
|
||||||
import prepare as prep
|
|
||||||
data = load_corners()
|
|
||||||
cand = {}
|
|
||||||
for i, name in enumerate(images, 1):
|
|
||||||
dst = out_path('网格', i, name)
|
|
||||||
if auto_quad:
|
|
||||||
corners, info = prep.detect_corners(name)
|
|
||||||
if corners:
|
|
||||||
prep.render(name, dst, corners=corners,
|
|
||||||
edge_mask=info.get('edge_mask_small'),
|
|
||||||
scale=info.get('scale', 1.0))
|
|
||||||
cand[name] = corners
|
|
||||||
else:
|
|
||||||
prep.render(name, dst)
|
|
||||||
else:
|
|
||||||
prep.render(name, dst)
|
|
||||||
data.setdefault(name, None)
|
|
||||||
save_corners({k: v for k, v in data.items() if v})
|
|
||||||
|
|
||||||
if cand:
|
|
||||||
print('[候选角点] 已叠加到网格图(品红实心圆 = 候选角点,品红虚线 = 候选四边形,黑 = 检出边线)')
|
|
||||||
print('')
|
|
||||||
print('| 图片 | 左上 | 右上 | 右下 | 左下 |')
|
|
||||||
print('|---|---|---|---|---|')
|
|
||||||
for name, pts in cand.items():
|
|
||||||
cells = ['%d,%d' % (round(p[0]), round(p[1])) for p in pts]
|
|
||||||
print('| %s | %s |' % (name, ' | '.join(cells)))
|
|
||||||
print('')
|
|
||||||
print('[仅供参考] 候选值不得直接写入 corners.json,一律以角标图人工判读为准')
|
|
||||||
|
|
||||||
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 grid_path(name):
|
|
||||||
"""找 prepare 生成的网格图(out/N-网格-<图名>.png);没有返回 None。"""
|
|
||||||
base = os.path.splitext(os.path.basename(name))[0]
|
|
||||||
if not os.path.isdir(OUT_DIR):
|
|
||||||
return None
|
|
||||||
# 用完整的 "-网格-<basename>.png" 后缀精确匹配,避免 a.jpg / 合同a.jpg 这类包含关系串图
|
|
||||||
want = '-网格-%s.png' % base
|
|
||||||
hits = [fn for fn in os.listdir(OUT_DIR)
|
|
||||||
if fn[:1].isdigit() and fn.endswith(want)]
|
|
||||||
if not hits:
|
|
||||||
return None
|
|
||||||
# 同名网格图有多份时取最新的一份(防止旧轮次的残留文件)
|
|
||||||
fn = max(hits, key=lambda f: os.path.getmtime(os.path.join(OUT_DIR, f)))
|
|
||||||
return os.path.join(OUT_DIR, fn)
|
|
||||||
|
|
||||||
|
|
||||||
def draw_marks(name, pts, dst):
|
|
||||||
"""把四角叠加到已生成的网格图上(找不到网格图时退回原图并即时叠网格)。"""
|
|
||||||
from preview_quad import draw_quad
|
|
||||||
im = draw_quad(name, pts, base_path=grid_path(name), 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.add_argument('--no-auto-quad', action='store_true',
|
|
||||||
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='*', help='图片名(必填,可多个)')
|
|
||||||
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),
|
|
||||||
auto_quad=not args.no_auto_quad)
|
|
||||||
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,94 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""prepare 阶段的出图逻辑:坐标网格图 + 脚本候选角点/边线叠加。
|
|
||||||
|
|
||||||
产物
|
|
||||||
out/N-网格-<图名>.png 网格 + 候选角点(人工读数的主依据)
|
|
||||||
|
|
||||||
约定
|
|
||||||
- 候选角点只做**参考**:不落盘、不写 corners.json、不参与出件,
|
|
||||||
由 pipeline 直接在终端打成 markdown 表格供对照;
|
|
||||||
- 图上不写字,避免遮挡纸面内容。
|
|
||||||
"""
|
|
||||||
import math
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from PIL import Image, ImageDraw
|
|
||||||
|
|
||||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
||||||
from grid import draw_grid_on
|
|
||||||
|
|
||||||
COLOR_EDGE = (0, 0, 0) # 黑:脚本检出的边线像素
|
|
||||||
COLOR_CAND = (255, 0, 180) # 品红:候选四边形与角点
|
|
||||||
LABELS = ('左上', '右上', '右下', '左下')
|
|
||||||
|
|
||||||
|
|
||||||
def _dashed(d, p0, p1, fill, width=2, dash=14, gap=9):
|
|
||||||
dx, dy = p1[0] - p0[0], p1[1] - p0[1]
|
|
||||||
L = math.hypot(dx, dy)
|
|
||||||
if L < 1e-6:
|
|
||||||
return
|
|
||||||
ux, uy = dx / L, dy / L
|
|
||||||
t = 0.0
|
|
||||||
while t < L:
|
|
||||||
a, b = t, min(t + dash, L)
|
|
||||||
d.line([(p0[0] + ux * a, p0[1] + uy * a),
|
|
||||||
(p0[0] + ux * b, p0[1] + uy * b)], fill=fill, width=width)
|
|
||||||
t += dash + gap
|
|
||||||
|
|
||||||
|
|
||||||
def _extend(p0, p1, px):
|
|
||||||
dx, dy = p1[0] - p0[0], p1[1] - p0[1]
|
|
||||||
L = math.hypot(dx, dy)
|
|
||||||
if L < 1e-6:
|
|
||||||
return p0, p1
|
|
||||||
ux, uy = dx / L, dy / L
|
|
||||||
return (p0[0] - ux * px, p0[1] - uy * px), (p1[0] + ux * px, p1[1] + uy * px)
|
|
||||||
|
|
||||||
|
|
||||||
def render(src, dst, corners=None, edge_mask=None, scale=1.0, extend_px=None):
|
|
||||||
"""画网格图;corners 非空时叠加候选边线与角点。"""
|
|
||||||
im = Image.open(src).convert('RGBA')
|
|
||||||
|
|
||||||
if edge_mask is not None:
|
|
||||||
m = Image.fromarray((edge_mask * 255).astype('uint8'), 'L')
|
|
||||||
if m.size != im.size:
|
|
||||||
m = m.resize(im.size, Image.NEAREST)
|
|
||||||
lay = Image.new('RGBA', im.size, (0, 0, 0, 0))
|
|
||||||
lay.paste(COLOR_EDGE + (200,), (0, 0), m.point(lambda v: 255 if v > 127 else 0))
|
|
||||||
im = Image.alpha_composite(im, lay)
|
|
||||||
|
|
||||||
im = draw_grid_on(im)
|
|
||||||
if not corners:
|
|
||||||
im.convert('RGB').save(dst, quality=92)
|
|
||||||
return dst
|
|
||||||
|
|
||||||
d = ImageDraw.Draw(im, 'RGBA')
|
|
||||||
n = len(corners)
|
|
||||||
ext = extend_px if extend_px is not None else max(30, im.size[1] // 36)
|
|
||||||
for i in range(n):
|
|
||||||
a, b = _extend(corners[i], corners[(i + 1) % n], ext)
|
|
||||||
_dashed(d, a, b, COLOR_CAND + (240,), width=2)
|
|
||||||
|
|
||||||
for x, y in corners:
|
|
||||||
d.ellipse([x - 14, y - 14, x + 14, y + 14],
|
|
||||||
outline=(255, 255, 255, 255), width=4)
|
|
||||||
d.ellipse([x - 8, y - 8, x + 8, y + 8], fill=COLOR_CAND + (255,))
|
|
||||||
d.ellipse([x - 2, y - 2, x + 2, y + 2], fill=(255, 255, 255, 255))
|
|
||||||
|
|
||||||
im.convert('RGB').save(dst, quality=92)
|
|
||||||
return dst
|
|
||||||
|
|
||||||
|
|
||||||
def detect_corners(src, max_side=1600):
|
|
||||||
"""调用 auto_quad 取候选角点;失败返回 (None, None),不抛异常打断 prepare。"""
|
|
||||||
try:
|
|
||||||
import auto_quad
|
|
||||||
pts, info = auto_quad.detect(src, max_side=max_side)
|
|
||||||
return (pts or None), info
|
|
||||||
except Exception as e: # 检测失败不该阻塞流程
|
|
||||||
print('[提示] 候选角点检测跳过:%s' % e)
|
|
||||||
return None, None
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,189 +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, base_path=None, with_grid=False):
|
|
||||||
"""在底图上叠加四角标注。
|
|
||||||
|
|
||||||
base_path 非空时直接用该图当底图(通常是 prepare 生成的网格图,连同候选标记一起继承);
|
|
||||||
为空时退回原图,with_grid=True 则即时叠一层网格。
|
|
||||||
"""
|
|
||||||
im = Image.open(base_path or img_path).convert('RGB')
|
|
||||||
if with_grid and base_path is None:
|
|
||||||
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,967 @@
|
|||||||
|
#!/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", "off")
|
||||||
|
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, orientation_source, 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)
|
||||||
|
ready = sum(1 for m in pages if m.get("status") == "ready")
|
||||||
|
|
||||||
|
# 发布审核页:always 总是发布;auto 只在有页需要人看时发布
|
||||||
|
publish = review_mode == "always" or (review_mode == "auto" and bool(needs))
|
||||||
|
url = None
|
||||||
|
thumbs = {}
|
||||||
|
if publish and batch_id:
|
||||||
|
url, 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"]))
|
||||||
|
pages_out = []
|
||||||
|
state_pages = []
|
||||||
|
for m in order_all:
|
||||||
|
src_rel = _rel(session_root, m.get("source_abs") or "")
|
||||||
|
applied = int(m.get("rotated_deg") or 0)
|
||||||
|
auto_r = int(m.get("auto_rotate") or 0)
|
||||||
|
pages_out.append({
|
||||||
|
"index": m["index"],
|
||||||
|
"source": src_rel,
|
||||||
|
"source_size": m.get("source_size"),
|
||||||
|
"corners": m.get("corners"),
|
||||||
|
"corners_norm": m.get("corners_norm"),
|
||||||
|
"auto_corners": m.get("auto_corners"),
|
||||||
|
"auto_corners_norm": m.get("auto_corners_norm"),
|
||||||
|
"rotate": auto_r,
|
||||||
|
"applied_rotate": applied,
|
||||||
|
"status": m.get("status"),
|
||||||
|
"reason": m.get("reason"),
|
||||||
|
"warnings": m.get("warnings") or [],
|
||||||
|
"out_size": m.get("out_size"),
|
||||||
|
"page_orientation": m.get("page_orientation"),
|
||||||
|
"error": m.get("error"),
|
||||||
|
})
|
||||||
|
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)
|
||||||
|
|
||||||
|
canvas_set = {tuple(c) for c in ordered_canvases}
|
||||||
|
canvas_px = list(next(iter(canvas_set))) if len(canvas_set) == 1 else None
|
||||||
|
batch = {
|
||||||
|
"paper": str(paper_input),
|
||||||
|
"orientation": "mixed" if orientation == "none" else batch_orientation,
|
||||||
|
"orientation_source": orientation_source,
|
||||||
|
"median_aspect": round(float(batch_median), 4),
|
||||||
|
"rotated_pages": [m["index"] for m in pages
|
||||||
|
if int(m.get("rotated_deg") or 0) != 0],
|
||||||
|
"canvas_px": canvas_px,
|
||||||
|
}
|
||||||
|
if canvas_px:
|
||||||
|
margin = int(P["render"]["margin_px"])
|
||||||
|
batch["content_px"] = [int(canvas_px[0] - 2 * margin),
|
||||||
|
int(canvas_px[1] - 2 * margin)]
|
||||||
|
|
||||||
|
if checks.get("ok") is False:
|
||||||
|
summary = "%d 页已生成;自检有未通过项,见 warnings" % len(ordered_pages)
|
||||||
|
elif needs:
|
||||||
|
summary = "%d 页已生成;%d 页需人工确认" % (len(ordered_pages), len(needs))
|
||||||
|
elif warnings:
|
||||||
|
summary = "%d 页已生成;%d 项提示,请查看 warnings" % (
|
||||||
|
len(ordered_pages), len(warnings))
|
||||||
|
else:
|
||||||
|
summary = "%d 页已生成;自检全部通过" % len(ordered_pages)
|
||||||
|
|
||||||
|
res = {
|
||||||
|
"schema": P["schema"],
|
||||||
|
"ok": True,
|
||||||
|
"summary": summary,
|
||||||
|
"output": {
|
||||||
|
"path": output,
|
||||||
|
"state": state_path,
|
||||||
|
"bytes": int(os.path.getsize(output)),
|
||||||
|
"pages": len(ordered_pages),
|
||||||
|
"dpi": int(P["render"]["dpi"]),
|
||||||
|
},
|
||||||
|
"batch": batch,
|
||||||
|
"pages": pages_out,
|
||||||
|
"checks": checks,
|
||||||
|
"warnings": warnings,
|
||||||
|
"elapsed_ms": int((time.time() - t0) * 1000),
|
||||||
|
}
|
||||||
|
if use_db:
|
||||||
|
res["review"] = {
|
||||||
|
"batch_id": batch_id,
|
||||||
|
"published": bool(publish),
|
||||||
|
"url": url,
|
||||||
|
"ready": ready,
|
||||||
|
"needs_attention": [
|
||||||
|
{"index": m["index"], "status": m.get("status"),
|
||||||
|
"reason": m.get("reason")} for m in needs
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---------------- 写 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)
|
||||||
|
|
||||||
|
# 重生成路径给模型的回执要短:页面从 data.db 读详情,模型只需摘要/状态
|
||||||
|
if batch_id_arg:
|
||||||
|
res["pages"] = [{"index": m["index"], "status": m.get("status"),
|
||||||
|
"reason": m.get("reason")} for m in pages_out]
|
||||||
|
res.pop("checks", None)
|
||||||
|
res["compact"] = True
|
||||||
|
return res
|
||||||
|
|
||||||
|
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", "未知错误")
|
||||||
|
warnings = []
|
||||||
|
for m in pages:
|
||||||
|
warnings.extend(m.get("warnings", []))
|
||||||
|
res = {
|
||||||
|
"schema": P["schema"],
|
||||||
|
"ok": False,
|
||||||
|
"summary": "失败:%s" % err.get("message", "未知错误"),
|
||||||
|
"error": err,
|
||||||
|
"pages": pages,
|
||||||
|
"warnings": warnings,
|
||||||
|
"elapsed_ms": int((time.time() - t0) * 1000),
|
||||||
|
}
|
||||||
|
if output:
|
||||||
|
res["output"] = {"path": output, "state": state_path}
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- 入口
|
||||||
|
|
||||||
|
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 = {
|
||||||
|
"schema": P["schema"],
|
||||||
|
"ok": False,
|
||||||
|
"summary": "失败:%s" % e,
|
||||||
|
"error": _err(code, str(e), None, hint),
|
||||||
|
"pages": [],
|
||||||
|
"warnings": [],
|
||||||
|
"elapsed_ms": int((time.time() - t0) * 1000),
|
||||||
|
}
|
||||||
|
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())
|
||||||
@@ -0,0 +1,954 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover">
|
||||||
|
<meta name="theme-color" content="#0b0f17">
|
||||||
|
<title>文档照片审核</title>
|
||||||
|
<style>
|
||||||
|
*{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
|
||||||
|
html,body{margin:0;height:100%;background:#0b0f17;color:#e8edf5;
|
||||||
|
font:14px/1.45 -apple-system,BlinkMacSystemFont,"PingFang SC","Noto Sans CJK SC",sans-serif;
|
||||||
|
overscroll-behavior:none}
|
||||||
|
body{display:flex;flex-direction:column;height:100%;height:100dvh}
|
||||||
|
|
||||||
|
.top{display:flex;align-items:center;gap:6px;flex:none;z-index:5;
|
||||||
|
padding:6px 10px;padding-top:calc(6px + env(safe-area-inset-top));
|
||||||
|
border-bottom:1px solid #1b2434;background:#0b0f17}
|
||||||
|
.top .nav{width:36px;height:40px;font-size:20px;line-height:1;border:0;border-radius:9px;
|
||||||
|
background:#161f2e;color:#e8edf5;flex:none;padding:0}
|
||||||
|
.top .nav:disabled{opacity:.25}
|
||||||
|
.pager{min-width:48px;text-align:center;color:#9fb0c8;
|
||||||
|
font-variant-numeric:tabular-nums;flex:none}
|
||||||
|
.pager b{color:#e8edf5;font-size:15px}
|
||||||
|
.acts{margin-left:auto;display:flex;gap:4px;flex:none}
|
||||||
|
.ico{height:40px;width:40px;padding:0;border:0;border-radius:9px;background:#161f2e;
|
||||||
|
color:#c3d0e2;display:flex;align-items:center;justify-content:center;flex:none;gap:7px}
|
||||||
|
.ico:active{background:#22304a}
|
||||||
|
.ico.pri{background:#1f6feb;color:#fff}
|
||||||
|
.ico svg{width:20px;height:20px;display:block;flex:none}
|
||||||
|
.lbl{display:none;font-size:13px;white-space:nowrap}
|
||||||
|
|
||||||
|
/* 移动端翻页按钮:浮在图片左右两侧 */
|
||||||
|
/* 图片下方底栏:‹ 文件名 › */
|
||||||
|
.footbar{display:flex;align-items:center;gap:8px;flex:none;z-index:6;
|
||||||
|
padding:6px 10px 6px;padding-bottom:calc(6px + env(safe-area-inset-bottom));
|
||||||
|
border-top:1px solid #1b2434;background:#0b0f17}
|
||||||
|
.footbar .pnav{width:44px;height:44px;padding:0;border:0;border-radius:10px;flex:none;
|
||||||
|
background:#161f2e;color:#e8edf5;font-size:22px;line-height:1;
|
||||||
|
display:flex;align-items:center;justify-content:center}
|
||||||
|
.footbar .pnav:disabled{opacity:.25}
|
||||||
|
.footname{flex:1;min-width:0;text-align:center;font-size:12px;color:#9fb0c8;
|
||||||
|
overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
||||||
|
|
||||||
|
.stage{position:relative;flex:1 1 auto;min-height:0;overflow:hidden;touch-action:none;
|
||||||
|
background:repeating-conic-gradient(#101724 0 25%,#0d1420 0 50%) 0 0/22px 22px}
|
||||||
|
#photo{position:absolute;inset:0;width:100%;height:100%;touch-action:none}
|
||||||
|
.h{position:absolute;width:44px;height:44px;margin:0;touch-action:none;cursor:grab;
|
||||||
|
display:flex;align-items:center;justify-content:center;z-index:5}
|
||||||
|
.h::after{content:"";width:16px;height:16px;border-radius:50%;
|
||||||
|
background:#4ea1ff;border:2px solid #fff;
|
||||||
|
box-shadow:0 0 0 1px rgba(0,0,0,.5),0 2px 6px rgba(0,0,0,.5)}
|
||||||
|
.h.on::after{background:#ffb020;transform:scale(1.15)}
|
||||||
|
.h.pushed::before{content:"";position:absolute;width:22px;height:22px;border-radius:50%;
|
||||||
|
border:1px dashed rgba(78,161,255,.55)}
|
||||||
|
#loupe{position:absolute;width:130px;height:130px;border-radius:50%;
|
||||||
|
border:2px solid #4ea1ff;box-shadow:0 6px 20px rgba(0,0,0,.6);display:none;
|
||||||
|
background:#000;pointer-events:none;left:0;top:0;z-index:6}
|
||||||
|
/* 底栏在预览模式下也显示,所以底栏要跟 stage 一起隐藏/显示 */
|
||||||
|
|
||||||
|
/* 移动端只显示一个窗口:默认原图,点左上角切到预览 */
|
||||||
|
#prevwrap{display:none;flex:1 1 auto;min-height:0;flex-direction:column;
|
||||||
|
background:#0a0e16}
|
||||||
|
.pv-hd{display:flex;align-items:center;gap:8px;padding:8px 12px 0;flex:none;
|
||||||
|
font-size:12px;color:#7d8ea6}
|
||||||
|
#prevwrap .box{flex:1;min-height:0;display:flex;align-items:center;justify-content:center;
|
||||||
|
padding:8px 12px 10px;overflow:hidden}
|
||||||
|
#preview{display:block;max-width:100%;max-height:100%}
|
||||||
|
|
||||||
|
@media (max-width:899.98px){
|
||||||
|
.top .nav{display:none}
|
||||||
|
body.view-prev .stage{display:none}
|
||||||
|
body.view-prev #prevwrap{display:flex}
|
||||||
|
}
|
||||||
|
|
||||||
|
.sheet{position:fixed;inset:0;background:rgba(4,7,12,.74);display:none;
|
||||||
|
align-items:flex-end;justify-content:center;z-index:20}
|
||||||
|
.sheet.on{display:flex}
|
||||||
|
.sheet .card{background:#111826;width:100%;max-width:520px;border-radius:16px 16px 0 0;
|
||||||
|
padding:16px 16px calc(14px + env(safe-area-inset-bottom));max-height:86vh;overflow:auto}
|
||||||
|
.sheet .card h3{margin:0 0 6px;font-size:15px;font-weight:600}
|
||||||
|
.sheet .card p{margin:0 0 10px;color:#8ea1ba;font-size:12px;line-height:1.5}
|
||||||
|
.grp{margin:14px 0 0}
|
||||||
|
.gl{font-size:12px;color:#8ea1ba;margin-bottom:6px}
|
||||||
|
.seg{display:flex;gap:6px;flex-wrap:wrap}
|
||||||
|
.seg button{flex:1 1 0;min-width:62px;height:38px;border:1px solid #22304a;
|
||||||
|
border-radius:9px;background:#141d2b;color:#c3d0e2;font-size:13px;padding:0 6px}
|
||||||
|
.seg button.on{background:#1f6feb;border-color:#1f6feb;color:#fff}
|
||||||
|
.sheet .row{display:flex;gap:8px;margin-top:16px}
|
||||||
|
.sheet .row button{flex:1;height:44px;border:0;border-radius:10px;
|
||||||
|
background:#1b2536;color:#e8edf5;font-size:15px}
|
||||||
|
.sheet .row button.pri{background:#1f6feb;color:#fff}
|
||||||
|
.hint{color:#6f8098;font-size:11px;margin:12px 0 0}
|
||||||
|
|
||||||
|
#toast{position:fixed;left:50%;bottom:calc(20px + env(safe-area-inset-bottom));
|
||||||
|
transform:translateX(-50%);background:rgba(24,34,50,.96);color:#e8edf5;
|
||||||
|
padding:9px 16px;border-radius:99px;font-size:13px;opacity:0;pointer-events:none;
|
||||||
|
transition:opacity .18s;z-index:40;max-width:86vw;text-align:center}
|
||||||
|
#toast.on{opacity:1}
|
||||||
|
|
||||||
|
/* 桌面:左原图、右预览,顶栏按钮带文字 */
|
||||||
|
@media (min-width:900px){
|
||||||
|
body{display:grid;grid-template-columns:minmax(0,1fr) 432px;
|
||||||
|
grid-template-rows:auto minmax(0,1fr) auto;height:100dvh}
|
||||||
|
.top{grid-column:1/-1;grid-row:1}
|
||||||
|
.stage{grid-row:2;grid-column:1}
|
||||||
|
#prevwrap{grid-row:2/4;grid-column:2;border-left:1px solid #1b2434}
|
||||||
|
#view{display:none}
|
||||||
|
.footbar{grid-row:3;grid-column:1;border-top:1px solid #1b2434;padding-bottom:6px}
|
||||||
|
.footbar .pnav{display:none}
|
||||||
|
/* 桌面端顶栏已有翻页,底栏两侧占位保持文件名真正居中 */
|
||||||
|
.ico{width:auto;padding:0 14px}
|
||||||
|
.ico .lbl{display:inline}
|
||||||
|
/* 桌面端翻页在顶栏,底栏只留居中的文件名 */
|
||||||
|
.footname{font-size:13px;color:#b9c8dd}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 一键重生成:浮动主按钮。手机放在底栏上方,桌面放在预览区左侧,避免挤顶栏。 */
|
||||||
|
.fab{position:fixed;right:14px;bottom:calc(64px + env(safe-area-inset-bottom));z-index:15;
|
||||||
|
height:48px;padding:0 18px;border:0;border-radius:24px;background:#1f6feb;color:#fff;
|
||||||
|
font-size:15px;font-weight:600;box-shadow:0 6px 20px rgba(0,0,0,.45);
|
||||||
|
display:flex;align-items:center;justify-content:center}
|
||||||
|
.fab:active{background:#1a5fd0}
|
||||||
|
.fab:disabled{opacity:.55}
|
||||||
|
.fab .lbl{display:inline}
|
||||||
|
@media (min-width:900px){.fab{right:456px;bottom:76px}}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="top">
|
||||||
|
<button class="ico" id="view" 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.5 12S5.5 4.5 12 4.5 22.5 12 22.5 12 18.5 19.5 12 19.5 1.5 12 1.5 12z"/><circle cx="12" cy="12" r="3"/></svg></button>
|
||||||
|
<button class="nav" id="navPrev" aria-label="上一页">‹</button>
|
||||||
|
<div class="pager"><b id="cur">1</b> / <span id="total">1</span></div>
|
||||||
|
<button class="nav" id="navNext" aria-label="下一页">›</button>
|
||||||
|
<div class="acts">
|
||||||
|
<button class="ico" id="rot" title="旋转 90%" aria-label="旋转"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12a9 9 0 1 1-2.64-6.36"/><path d="M21 3v6h-6"/></svg><span class="lbl">旋转 90°</span></button>
|
||||||
|
<button class="ico" id="reset" title="重置本页" aria-label="重置"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 14L4 9l5-5"/><path d="M4 9h10a6 6 0 0 1 0 12h-4"/></svg><span class="lbl">重置本页</span></button>
|
||||||
|
<button class="ico" id="gear" title="全局设置" aria-label="全局设置"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M4 7h16M4 12h16M4 17h16"/><circle cx="9" cy="7" r="2.1" fill="currentColor" stroke="none"/><circle cx="15" cy="12" r="2.1" fill="currentColor" stroke="none"/><circle cx="9" cy="17" r="2.1" fill="currentColor" stroke="none"/></svg><span class="lbl">全局设置</span></button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="stage" id="stage">
|
||||||
|
<canvas id="photo"></canvas>
|
||||||
|
<div class="h" id="h0"></div>
|
||||||
|
<div class="h" id="h1"></div>
|
||||||
|
<div class="h" id="h2"></div>
|
||||||
|
<div class="h" id="h3"></div>
|
||||||
|
<canvas id="loupe"></canvas>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="prevwrap">
|
||||||
|
<div class="pv-hd"><span>校正预览</span></div>
|
||||||
|
<div class="box" id="pbox"><canvas id="preview"></canvas></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="footbar" id="footbar">
|
||||||
|
<button class="pnav" id="prev" aria-label="上一页">‹</button>
|
||||||
|
<div class="footname" id="name"></div>
|
||||||
|
<button class="pnav" id="next" aria-label="下一页">›</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="setSheet" class="sheet">
|
||||||
|
<div class="card">
|
||||||
|
<h3>全局设置</h3>
|
||||||
|
<p>这四项对整批生效,不影响单页角点。</p>
|
||||||
|
<div class="grp"><div class="gl">纸张</div>
|
||||||
|
<div class="seg" id="seg-paper">
|
||||||
|
<button data-v="a4">A4</button>
|
||||||
|
<button data-v="auto">原比例</button>
|
||||||
|
<button data-v="16k">16K</button>
|
||||||
|
<button data-v="b5">B5</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="grp"><div class="gl">图像处理</div>
|
||||||
|
<div class="seg" id="seg-enhance">
|
||||||
|
<button data-v="original">原样</button>
|
||||||
|
<button data-v="normal">标准</button>
|
||||||
|
<button data-v="strong">加强</button>
|
||||||
|
<button data-v="bw">黑白</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="grp"><div class="gl">清晰度</div>
|
||||||
|
<div class="seg" id="seg-dpi">
|
||||||
|
<button data-v="standard">标准 150dpi</button>
|
||||||
|
<button data-v="high">高清 300dpi</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="grp"><div class="gl">方向</div>
|
||||||
|
<div class="seg" id="seg-orientation">
|
||||||
|
<button data-v="auto">自动</button>
|
||||||
|
<button data-v="portrait">全部竖</button>
|
||||||
|
<button data-v="landscape">全部横</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="grp"><div class="gl">本页</div>
|
||||||
|
<div class="seg" id="seg-page">
|
||||||
|
<button id="moveUp">上移</button>
|
||||||
|
<button id="moveDown">下移</button>
|
||||||
|
<button id="delPage">删除/恢复</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p id="summary" class="hint"></p>
|
||||||
|
<div class="row"><button id="pdf" class="pri">下载当前 PDF</button></div>
|
||||||
|
<div class="row"><button id="setClose" class="pri">完成</button></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="toast"></div>
|
||||||
|
|
||||||
|
<button id="apply" class="fab" title="应用修改并重新生成"><span class="lbl">生成 PDF</span></button>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
"use strict";
|
||||||
|
var TASK = (location.pathname.split("/").filter(Boolean)[1] || "");
|
||||||
|
var BATCH = (new URLSearchParams(location.search).get("b") || "");
|
||||||
|
|
||||||
|
var DPR = Math.min(2, window.devicePixelRatio || 1);
|
||||||
|
var SAVE_KEY = "docreview/v2/" + (BATCH || "x");
|
||||||
|
var HANDLES = [0,1,2,3].map(function(i){ return document.getElementById("h"+i); });
|
||||||
|
var cv, ctx, loupe, drag = null;
|
||||||
|
var V = {dw:0, dh:0, ox:0, oy:0};
|
||||||
|
|
||||||
|
/* 全局四项:只影响出件参数,不影响角点。对应 params 里的 paper/enhance/dpi/orientation */
|
||||||
|
var G = {paper:"auto", enhance:"normal", dpi:"standard", orientation:"auto"};
|
||||||
|
var SEGS = [["seg-paper","paper"],["seg-enhance","enhance"],
|
||||||
|
["seg-dpi","dpi"],["seg-orientation","orientation"]];
|
||||||
|
var ICON_EYE = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M1.5 12S5.5 4.5 12 4.5 22.5 12 22.5 12 18.5 19.5 12 19.5 1.5 12 1.5 12z"/><circle cx="12" cy="12" r="3"/></svg>';
|
||||||
|
var ICON_CROP = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 2v14a2 2 0 0 0 2 2h14"/><path d="M18 22V8a2 2 0 0 0-2-2H2"/></svg>';
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------- 数据 */
|
||||||
|
var S = {cur: 0, pages: [], batch: null, busy: false};
|
||||||
|
|
||||||
|
function parseJSON(s, d) {
|
||||||
|
if (!s) return d;
|
||||||
|
try { return JSON.parse(s); } catch (e) { return d; }
|
||||||
|
}
|
||||||
|
|
||||||
|
function apiDb(sql, params) {
|
||||||
|
return fetch("/api/db/" + encodeURIComponent(TASK) + "/query", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {"Content-Type": "application/json"},
|
||||||
|
body: JSON.stringify({sql: sql, params: params || []})
|
||||||
|
}).then(function(r){
|
||||||
|
return r.json().then(function(j){
|
||||||
|
if (j && j.error) throw new Error(j.error);
|
||||||
|
return j;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function paintSummary() {
|
||||||
|
var b = S.batch || {};
|
||||||
|
var el = document.getElementById("summary");
|
||||||
|
if (el) el.textContent = b.summary || "";
|
||||||
|
var pdf = document.getElementById("pdf");
|
||||||
|
if (pdf) {
|
||||||
|
if (b.output) {
|
||||||
|
pdf.disabled = false;
|
||||||
|
pdf.dataset.url = "/api/sessions/" + encodeURIComponent(TASK) +
|
||||||
|
"/files/download?path=" + encodeURIComponent(b.output) +
|
||||||
|
"&v=" + encodeURIComponent(b.updated_at || Date.now());
|
||||||
|
} else {
|
||||||
|
pdf.disabled = true;
|
||||||
|
pdf.dataset.url = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadBatch() {
|
||||||
|
var sql = BATCH
|
||||||
|
? "SELECT * FROM doc_review_batch WHERE id = ? LIMIT 1"
|
||||||
|
: "SELECT * FROM doc_review_batch ORDER BY created_at DESC, updated_at DESC LIMIT 1";
|
||||||
|
return apiDb(sql, BATCH ? [BATCH] : []).then(function(rows){
|
||||||
|
var b = rows && rows[0];
|
||||||
|
if (!b) throw new Error("没有可审核的批次");
|
||||||
|
BATCH = b.id;
|
||||||
|
S.batch = b;
|
||||||
|
SAVE_KEY = "docreview/v2/" + BATCH;
|
||||||
|
G.paper = b.paper || "auto";
|
||||||
|
G.enhance = b.enhance || "normal";
|
||||||
|
G.dpi = (b.dpi === "150" ? "standard" : b.dpi === "300" ? "high" : (b.dpi || "standard"));
|
||||||
|
G.orientation = b.orientation || "auto";
|
||||||
|
return apiDb("SELECT * FROM doc_review_page WHERE batch_id = ? ORDER BY sort, idx", [BATCH]);
|
||||||
|
}).then(function(rows){
|
||||||
|
S.pages = (rows || []).map(function(r){
|
||||||
|
var c = parseJSON(r.corners, null) || parseJSON(r.auto_corners_norm, null);
|
||||||
|
var base = parseJSON(r.auto_corners_norm, null) || c;
|
||||||
|
return {
|
||||||
|
i: r.idx,
|
||||||
|
name: (r.source || "").split("/").pop() || ("第 " + r.idx + " 页"),
|
||||||
|
w: r.w || 0,
|
||||||
|
h: r.h || 0,
|
||||||
|
img: r.thumb ? ("./" + r.thumb) : "",
|
||||||
|
c: c ? c.map(function(q){ return [q[0], q[1]]; }) : [[0,0],[1,0],[1,1],[0,1]],
|
||||||
|
base: base ? base.map(function(q){ return [q[0], q[1]]; }) : [[0,0],[1,0],[1,1],[0,1]],
|
||||||
|
r: r.rotate || 0,
|
||||||
|
r0: r.auto_rotate || 0,
|
||||||
|
status: r.status || "ready",
|
||||||
|
reason: r.reason || null,
|
||||||
|
deleted: !!r.deleted,
|
||||||
|
error: r.error || null,
|
||||||
|
im: null
|
||||||
|
};
|
||||||
|
});
|
||||||
|
if (!S.pages.length) throw new Error("批次没有页面");
|
||||||
|
if (!S.pages.some(function(p){ return !p.deleted; })) throw new Error("所有页面都被删除了");
|
||||||
|
S.cur = 0;
|
||||||
|
while (S.cur < S.pages.length && S.pages[S.cur].deleted) S.cur++;
|
||||||
|
if (S.cur >= S.pages.length) S.cur = 0;
|
||||||
|
paintSummary();
|
||||||
|
return S.pages;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function showEmpty(msg) {
|
||||||
|
var st = document.getElementById("stage");
|
||||||
|
if (st) {
|
||||||
|
st.innerHTML = "";
|
||||||
|
var d = document.createElement("div");
|
||||||
|
d.style.cssText = "padding:40px;text-align:center;color:#8a93a5;font-size:14px;line-height:1.7";
|
||||||
|
d.textContent = msg;
|
||||||
|
st.appendChild(d);
|
||||||
|
}
|
||||||
|
var foot = document.getElementById("name");
|
||||||
|
if (foot) foot.textContent = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadSaved() {
|
||||||
|
try {
|
||||||
|
var raw = localStorage.getItem(SAVE_KEY);
|
||||||
|
if (!raw) return;
|
||||||
|
var d = JSON.parse(raw);
|
||||||
|
if (!d) return;
|
||||||
|
if (d.g) {
|
||||||
|
for (var k in G) { if (d.g[k] != null) G[k] = d.g[k]; }
|
||||||
|
}
|
||||||
|
if (!d.pages) return;
|
||||||
|
d.pages.forEach(function(item){
|
||||||
|
/* 按 index 找,不靠数组下标:删页/跳过页后 index 可能不连续 */
|
||||||
|
var p = null;
|
||||||
|
S.pages.forEach(function(x){ if (x.i === item.i) p = x; });
|
||||||
|
if (!p) return;
|
||||||
|
if (item.c && item.c.length === 4) p.c = item.c;
|
||||||
|
if (typeof item.r === "number") p.r = item.r;
|
||||||
|
if (typeof item.deleted === "boolean") p.deleted = item.deleted;
|
||||||
|
});
|
||||||
|
if (d.order && d.order.length) {
|
||||||
|
var by = {}, next = [];
|
||||||
|
S.pages.forEach(function(x){ by[x.i] = x; });
|
||||||
|
d.order.forEach(function(i){ if (by[i]) { next.push(by[i]); delete by[i]; } });
|
||||||
|
S.pages.forEach(function(x){ if (by[x.i]) next.push(x); });
|
||||||
|
if (next.length === S.pages.length) S.pages = next;
|
||||||
|
}
|
||||||
|
} catch (e) { /* 存坏了就当没存过 */ }
|
||||||
|
}
|
||||||
|
function save() {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(SAVE_KEY, JSON.stringify({
|
||||||
|
pages: S.pages.map(function(p){ return {i:p.i, c:p.c, r:p.r, deleted:!!p.deleted}; }),
|
||||||
|
order: S.pages.map(function(p){ return p.i; }),
|
||||||
|
g: G
|
||||||
|
}));
|
||||||
|
} catch (e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------- 几何 */
|
||||||
|
function clamp(v, a, b){ return v < a ? a : (v > b ? b : v); }
|
||||||
|
function toCanvas(q){ return [V.ox + q[0]*V.dw, V.oy + q[1]*V.dh]; }
|
||||||
|
function dist(a, b){ return Math.hypot(a[0]-b[0], a[1]-b[1]); }
|
||||||
|
function r4(v){ return Math.round(v*10000)/10000; }
|
||||||
|
|
||||||
|
function rotateCw(q){
|
||||||
|
/* 与服务端 compose.rotate_quad_cw 一致:序列移一位 = 页面顺时针 90° */
|
||||||
|
return [q[3], q[0], q[1], q[2]];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------- 绘原图 */
|
||||||
|
function stageSize() {
|
||||||
|
var st = document.getElementById("stage");
|
||||||
|
return [st.clientWidth, st.clientHeight];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 把手半径 22px。图片四周至少留出这个量,否则纸边一旦贴到画面边缘,
|
||||||
|
把手就有一半在屏幕外——竖版纸放在竖屏里就是这种常态。 */
|
||||||
|
var PAD_EDGE = 26;
|
||||||
|
|
||||||
|
function layout() {
|
||||||
|
var sz = stageSize(), w = sz[0], h = sz[1];
|
||||||
|
if (!w || !h) { drawPreview(); return; } /* stage 隐藏时(切到预览)尺寸为 0 */
|
||||||
|
cv = document.getElementById("photo");
|
||||||
|
cv.width = Math.max(1, Math.round(w*DPR));
|
||||||
|
cv.height = Math.max(1, Math.round(h*DPR));
|
||||||
|
cv.style.width = w+"px";
|
||||||
|
cv.style.height = h+"px";
|
||||||
|
|
||||||
|
ctx = cv.getContext("2d");
|
||||||
|
ctx.setTransform(1,0,0,1,0,0);
|
||||||
|
ctx.clearRect(0,0,cv.width,cv.height);
|
||||||
|
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p || p.deleted || !p.w || !p.h) { placeHandles(); drawPreview(); return; }
|
||||||
|
var s = Math.min((w - 2*PAD_EDGE)/p.w, (h - 2*PAD_EDGE)/p.h);
|
||||||
|
V.dw = p.w*s; V.dh = p.h*s;
|
||||||
|
V.ox = (w - V.dw)/2; V.oy = (h - V.dh)/2;
|
||||||
|
|
||||||
|
drawPhoto();
|
||||||
|
placeHandles();
|
||||||
|
drawPreview();
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawPhoto() {
|
||||||
|
if (!ctx) return;
|
||||||
|
ctx = cv.getContext("2d");
|
||||||
|
ctx.setTransform(1,0,0,1,0,0);
|
||||||
|
ctx.clearRect(0,0,cv.width,cv.height);
|
||||||
|
ctx.setTransform(DPR,0,0,DPR,0,0);
|
||||||
|
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p || p.deleted || !p.im || !p.w || !p.h) return;
|
||||||
|
if (p.im) ctx.drawImage(p.im, V.ox, V.oy, V.dw, V.dh);
|
||||||
|
else { /* 图还没解出来,先留空 */ }
|
||||||
|
|
||||||
|
var pts = p.c.map(toCanvas);
|
||||||
|
/* 四边形以外压暗:evenodd 让内圈变成洞 */
|
||||||
|
ctx.save();
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.rect(V.ox, V.oy, V.dw, V.dh);
|
||||||
|
ctx.moveTo(pts[0][0], pts[0][1]);
|
||||||
|
ctx.lineTo(pts[3][0], pts[3][1]);
|
||||||
|
ctx.lineTo(pts[2][0], pts[2][1]);
|
||||||
|
ctx.lineTo(pts[1][0], pts[1][1]);
|
||||||
|
ctx.closePath();
|
||||||
|
ctx.fillStyle = "rgba(6,10,17,.66)";
|
||||||
|
ctx.fill("evenodd");
|
||||||
|
ctx.restore();
|
||||||
|
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(pts[0][0], pts[0][1]);
|
||||||
|
ctx.lineTo(pts[1][0], pts[1][1]);
|
||||||
|
ctx.lineTo(pts[2][0], pts[2][1]);
|
||||||
|
ctx.lineTo(pts[3][0], pts[3][1]);
|
||||||
|
ctx.closePath();
|
||||||
|
ctx.strokeStyle = "#4ea1ff";
|
||||||
|
ctx.lineWidth = 1.5;
|
||||||
|
ctx.stroke();
|
||||||
|
}
|
||||||
|
|
||||||
|
function placeHandles() {
|
||||||
|
var sz = stageSize(), w = sz[0], h = sz[1];
|
||||||
|
var R = 22; /* 把手半径,与服务端 CSS 的 44px 对应 */
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p || p.deleted || !p.w || !p.h) {
|
||||||
|
for (var k=0;k<HANDLES.length;k++) HANDLES[k].style.display = "none";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (var k2=0;k2<HANDLES.length;k2++) HANDLES[k2].style.display = "";
|
||||||
|
for (var i=0;i<4;i++) {
|
||||||
|
var q = toCanvas(p.c[i]);
|
||||||
|
var cx = q[0], cy = q[1];
|
||||||
|
/* 角点贴边时把手整体往内对齐,让整个圆留在屏幕内,且保持视觉可见 */
|
||||||
|
if (cx < R + 2) cx = R + 2;
|
||||||
|
if (cx > w - R - 2) cx = w - R - 2;
|
||||||
|
if (cy < R + 2) cy = R + 2;
|
||||||
|
if (cy > h - R - 2) cy = h - R - 2;
|
||||||
|
HANDLES[i].style.left = (cx-R)+"px";
|
||||||
|
HANDLES[i].style.top = (cy-R)+"px";
|
||||||
|
/* 被推过的把手加个细边,提醒“圆圈位置不等于角点位置” */
|
||||||
|
var pushed = Math.abs(cx - q[0]) > 0.5 || Math.abs(cy - q[1]) > 0.5;
|
||||||
|
HANDLES[i].className = "h" + (pushed ? " pushed" : "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------- 放大镜 */
|
||||||
|
function showLoupe(i, ev) {
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p || p.deleted || !p.im || !p.w || !p.h) return;
|
||||||
|
loupe = document.getElementById("loupe");
|
||||||
|
var R = 130;
|
||||||
|
loupe.width = R*DPR; loupe.height = R*DPR;
|
||||||
|
loupe.style.display = "block";
|
||||||
|
var g = loupe.getContext("2d");
|
||||||
|
g.setTransform(1,0,0,1,0,0);
|
||||||
|
g.fillStyle = "#000";
|
||||||
|
g.fillRect(0,0,loupe.width,loupe.height);
|
||||||
|
g.setTransform(DPR,0,0,DPR,0,0);
|
||||||
|
|
||||||
|
var cx = p.c[i][0]*p.w, cy = p.c[i][1]*p.h;
|
||||||
|
var span = 60; /* 取源图 60px 见方,放大到 130px */
|
||||||
|
g.drawImage(p.im, cx-span/2, cy-span/2, span, span, 0, 0, R, R);
|
||||||
|
g.strokeStyle = "rgba(255,77,79,.9)";
|
||||||
|
g.lineWidth = 1;
|
||||||
|
g.beginPath();
|
||||||
|
g.moveTo(R/2-14,R/2); g.lineTo(R/2+14,R/2);
|
||||||
|
g.moveTo(R/2,R/2-14); g.lineTo(R/2,R/2+14);
|
||||||
|
g.stroke();
|
||||||
|
|
||||||
|
/* 摆在手指上方;上方放不下就翻到下方;下方也放不下则避开该边往侧边器 */
|
||||||
|
var sz = stageSize(), sw = sz[0], sh = sz[1];
|
||||||
|
var r = document.getElementById("stage").getBoundingClientRect();
|
||||||
|
var fx = ev.clientX - r.left, fy = ev.clientY - r.top;
|
||||||
|
var W = R, Hh = R;
|
||||||
|
var x = clamp(fx - W/2, 6, sw - W - 6);
|
||||||
|
var y = fy - Hh - 20;
|
||||||
|
if (y < 6) y = fy + 30; /* 上方不行 → 下方 */
|
||||||
|
if (y + Hh > sh - 6) { /* 下方也不行 → 左右两侧 */
|
||||||
|
y = clamp(fy - Hh/2, 6, sh - Hh - 6);
|
||||||
|
x = (fx + 40 + W <= sw - 6) ? fx + 40 : fx - 40 - W;
|
||||||
|
x = clamp(x, 6, sw - W - 6);
|
||||||
|
}
|
||||||
|
loupe.style.left = x+"px";
|
||||||
|
loupe.style.top = y+"px";
|
||||||
|
}
|
||||||
|
function hideLoupe(){ if (loupe) loupe.style.display = "none"; }
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------- 透视预览 */
|
||||||
|
function drawPreview() {
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p || p.deleted || !p.im || !p.w || !p.h) {
|
||||||
|
var empty = document.getElementById("preview");
|
||||||
|
if (empty) { empty.width = 1; empty.height = 1; }
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var out = document.getElementById("preview");
|
||||||
|
var box = out.parentElement;
|
||||||
|
var maxW = Math.max(120, box.clientWidth - 8);
|
||||||
|
var maxH = Math.max(80, box.clientHeight - 8);
|
||||||
|
|
||||||
|
/* 归一化 -> 缩略图像素,再按 r 旋转(与服务端同一套角点旋转规则) */
|
||||||
|
var q = p.c.map(function(v){ return [v[0]*p.w, v[1]*p.h]; });
|
||||||
|
var steps = ((p.r % 360) + 360) % 360 / 90;
|
||||||
|
for (var k=0;k<steps;k++) q = rotateCw(q);
|
||||||
|
|
||||||
|
var wsrc = (dist(q[0],q[1]) + dist(q[3],q[2])) / 2;
|
||||||
|
var hsrc = (dist(q[0],q[3]) + dist(q[1],q[2])) / 2;
|
||||||
|
var asp = Math.max(0.05, wsrc / Math.max(1e-6, hsrc));
|
||||||
|
|
||||||
|
var W = maxW, H = W/asp;
|
||||||
|
if (H > maxH) { H = maxH; W = H*asp; }
|
||||||
|
W = Math.max(20, Math.round(W)); H = Math.max(20, Math.round(H));
|
||||||
|
|
||||||
|
out.width = Math.round(W*DPR); out.height = Math.round(H*DPR);
|
||||||
|
out.style.width = W+"px"; out.style.height = H+"px";
|
||||||
|
warp(p.im, q, out, W, H, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
function warp(img, quad, out, W, H, N) {
|
||||||
|
var g = out.getContext("2d");
|
||||||
|
g.setTransform(DPR,0,0,DPR,0,0);
|
||||||
|
g.clearRect(0,0,W,H);
|
||||||
|
g.fillStyle = "#fff";
|
||||||
|
g.fillRect(0,0,W,H);
|
||||||
|
g.imageSmoothingEnabled = true;
|
||||||
|
if ("imageSmoothingQuality" in g) g.imageSmoothingQuality = "high";
|
||||||
|
|
||||||
|
/* 双线性取源点:把四边形划成 N×N 格,每格两三角形用仿射近似投影。
|
||||||
|
网格够细时与真正的单应变换肉眼无差,且不需要任何库。 */
|
||||||
|
function srcPt(u, v) {
|
||||||
|
var tl=quad[0], tr=quad[1], br=quad[2], bl=quad[3];
|
||||||
|
var tx = tl[0]+(tr[0]-tl[0])*u, ty = tl[1]+(tr[1]-tl[1])*u;
|
||||||
|
var bx = bl[0]+(br[0]-bl[0])*u, by = bl[1]+(br[1]-bl[1])*u;
|
||||||
|
return [tx+(bx-tx)*v, ty+(by-ty)*v];
|
||||||
|
}
|
||||||
|
function tri(s0,s1,s2, d0,d1,d2) {
|
||||||
|
var sx1=s1[0]-s0[0], sy1=s1[1]-s0[1], sx2=s2[0]-s0[0], sy2=s2[1]-s0[1];
|
||||||
|
var dx1=d1[0]-d0[0], dy1=d1[1]-d0[1], dx2=d2[0]-d0[0], dy2=d2[1]-d0[1];
|
||||||
|
var det = sx1*sy2 - sx2*sy1;
|
||||||
|
if (Math.abs(det) < 1e-9) return;
|
||||||
|
var a = (dx1*sy2 - dx2*sy1)/det;
|
||||||
|
var c = (-dx1*sx2 + dx2*sx1)/det;
|
||||||
|
var b = (dy1*sy2 - dy2*sy1)/det;
|
||||||
|
var d = (-dy1*sx2 + dy2*sx1)/det;
|
||||||
|
var e = d0[0] - (a*s0[0] + c*s0[1]);
|
||||||
|
var f = d0[1] - (b*s0[0] + d*s0[1]);
|
||||||
|
g.save();
|
||||||
|
g.beginPath();
|
||||||
|
g.moveTo(d0[0],d0[1]); g.lineTo(d1[0],d1[1]); g.lineTo(d2[0],d2[1]);
|
||||||
|
g.closePath();
|
||||||
|
g.clip();
|
||||||
|
g.setTransform(a*DPR, b*DPR, c*DPR, d*DPR, e*DPR, f*DPR);
|
||||||
|
g.drawImage(img, 0, 0);
|
||||||
|
g.restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i=0;i<N;i++) {
|
||||||
|
for (var j=0;j<N;j++) {
|
||||||
|
var u0=i/N, u1=(i+1)/N, v0=j/N, v1=(j+1)/N;
|
||||||
|
var s00=srcPt(u0,v0), s10=srcPt(u1,v0), s11=srcPt(u1,v1), s01=srcPt(u0,v1);
|
||||||
|
var d00=[u0*W, v0*H], d10=[u1*W, v0*H], d11=[u1*W, v1*H], d01=[u0*W, v1*H];
|
||||||
|
tri(s00,s10,s11, d00,d10,d11);
|
||||||
|
tri(s00,s11,s01, d00,d11,d01);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------- 交互 */
|
||||||
|
function bindDrag() {
|
||||||
|
var cvEl = document.getElementById("photo");
|
||||||
|
var HIT_R = 60; /* 命中半径:手指不用精确摸到 44px 的圆 */
|
||||||
|
|
||||||
|
function toNorm(ev) {
|
||||||
|
var r = cvEl.getBoundingClientRect();
|
||||||
|
return [(ev.clientX - r.left - V.ox) / V.dw,
|
||||||
|
(ev.clientY - r.top - V.oy) / V.dh];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 角点优先于翻页按钮:两者可能撞在同一块屏幕上,角点是主操作 */
|
||||||
|
function hitHandle(ev) {
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p || p.deleted || !p.w || !p.h) return -1;
|
||||||
|
var r = cvEl.getBoundingClientRect();
|
||||||
|
var px = ev.clientX - r.left, py = ev.clientY - r.top;
|
||||||
|
var best = -1, bestD = HIT_R;
|
||||||
|
for (var i=0;i<4;i++) {
|
||||||
|
var q = toCanvas(p.c[i]);
|
||||||
|
var d = Math.hypot(q[0]-px, q[1]-py);
|
||||||
|
if (d <= bestD) { bestD = d; best = i; }
|
||||||
|
}
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
|
function begin(i, ev) {
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p || p.deleted || !p.w || !p.h) return;
|
||||||
|
drag = {i: i, id: ev.pointerId};
|
||||||
|
HANDLES[i].classList.add("on");
|
||||||
|
showLoupe(i, ev);
|
||||||
|
}
|
||||||
|
|
||||||
|
function move(ev) {
|
||||||
|
if (!drag) return;
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p || p.deleted || !p.w || !p.h) return;
|
||||||
|
var n = toNorm(ev);
|
||||||
|
/* 允许拖出画面:纸被切掉一块时角点必须在画面外,服务端会外推填白 */
|
||||||
|
p.c[drag.i] = [clamp(n[0],-0.35,1.35), clamp(n[1],-0.35,1.35)];
|
||||||
|
drawPhoto();
|
||||||
|
placeHandles();
|
||||||
|
showLoupe(drag.i, ev);
|
||||||
|
}
|
||||||
|
|
||||||
|
function end() {
|
||||||
|
if (!drag) return;
|
||||||
|
var i = drag.i;
|
||||||
|
drag = null;
|
||||||
|
if (HANDLES[i]) HANDLES[i].classList.remove("on");
|
||||||
|
hideLoupe();
|
||||||
|
drawPreview();
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 1) 按下:优先判断是否落在某个角的命中半径内(不看把手元素位置) */
|
||||||
|
cvEl.addEventListener("pointerdown", function(ev){
|
||||||
|
var i = hitHandle(ev);
|
||||||
|
if (i < 0) return;
|
||||||
|
ev.preventDefault();
|
||||||
|
try { cvEl.setPointerCapture(ev.pointerId); } catch (e) {}
|
||||||
|
begin(i, ev);
|
||||||
|
}, {passive: false});
|
||||||
|
|
||||||
|
/* 2) 拖动:canvas 同路径 + document 兵底。setPointerCapture 在指针
|
||||||
|
跑出视口/被浏览器收走时可能丢事件,靠 document 这一层也不会卡住。 */
|
||||||
|
cvEl.addEventListener("pointermove", move);
|
||||||
|
document.addEventListener("pointermove", move);
|
||||||
|
|
||||||
|
/* 3) 收尾:pointerup/cancel 可能在任意元素上触发,统一挂 document */
|
||||||
|
document.addEventListener("pointerup", end);
|
||||||
|
document.addEventListener("pointercancel", end);
|
||||||
|
|
||||||
|
/* 四、保底:把手自身的按下(部分浏览器在 canvas 上层元素上不派 pointerdown) */
|
||||||
|
HANDLES.forEach(function(el, i){
|
||||||
|
el.addEventListener("pointerdown", function(ev){
|
||||||
|
if (drag) return;
|
||||||
|
ev.preventDefault();
|
||||||
|
ev.stopPropagation();
|
||||||
|
begin(i, ev);
|
||||||
|
}, {passive: false});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function paintChrome() {
|
||||||
|
if (!S.pages.length) return;
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p) return;
|
||||||
|
document.getElementById("cur").textContent = S.cur + 1;
|
||||||
|
document.getElementById("total").textContent = S.pages.length;
|
||||||
|
document.getElementById("name").textContent = p.name +
|
||||||
|
(p.deleted ? "(已标记删除)" : (!p.img ? "(图片不可读)" : ""));
|
||||||
|
document.getElementById("prev").disabled = (S.cur === 0);
|
||||||
|
document.getElementById("navPrev").disabled = (S.cur === 0);
|
||||||
|
document.getElementById("next").disabled = (S.cur === S.pages.length - 1);
|
||||||
|
document.getElementById("navNext").disabled = (S.cur === S.pages.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function goto(idx) {
|
||||||
|
S.cur = clamp(idx, 0, S.pages.length - 1);
|
||||||
|
paintChrome();
|
||||||
|
layout();
|
||||||
|
}
|
||||||
|
|
||||||
|
function step(delta) {
|
||||||
|
goto(S.cur + delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------- 导出 */
|
||||||
|
function paintSeg() {
|
||||||
|
SEGS.forEach(function(g){
|
||||||
|
var el = document.getElementById(g[0]);
|
||||||
|
if (!el) return;
|
||||||
|
var bs = el.querySelectorAll("button");
|
||||||
|
for (var i=0;i<bs.length;i++) {
|
||||||
|
if (bs[i].getAttribute("data-v") === G[g[1]]) bs[i].classList.add("on");
|
||||||
|
else bs[i].classList.remove("on");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function collect() {
|
||||||
|
var out = {schema: "review/1", batch: {
|
||||||
|
paper: G.paper, enhance: G.enhance, dpi: G.dpi, orientation: G.orientation
|
||||||
|
}, pages: [], order: [], deleted: []};
|
||||||
|
/* batch 总是全量输出;pages 只放“与自动基线不同”的页(人工角点/旋转),
|
||||||
|
未改的页由服务端重新检测/评级——这样 fallback 页的状态不会被误升成 ready。 */
|
||||||
|
S.pages.forEach(function(p){
|
||||||
|
if (p.deleted) { out.deleted.push(p.i); return; }
|
||||||
|
out.order.push(p.i);
|
||||||
|
var moved = p.c.some(function(v, i){
|
||||||
|
return Math.abs(v[0]-p.base[i][0]) > 1e-4 || Math.abs(v[1]-p.base[i][1]) > 1e-4;
|
||||||
|
});
|
||||||
|
if (!moved && p.r === p.r0) return;
|
||||||
|
out.pages.push({i: p.i, c: p.c.map(function(v){ return [r4(v[0]), r4(v[1])]; }), r: p.r || 0});
|
||||||
|
});
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setBusy(on) {
|
||||||
|
S.busy = on;
|
||||||
|
var b = document.getElementById("apply");
|
||||||
|
if (!b) return;
|
||||||
|
b.disabled = on;
|
||||||
|
var l = b.querySelector(".lbl");
|
||||||
|
if (l) l.textContent = on ? "生成中…" : "生成";
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadImages() {
|
||||||
|
var pending = 0;
|
||||||
|
S.pages.forEach(function(p){
|
||||||
|
p.im = null;
|
||||||
|
if (!p.img) return;
|
||||||
|
pending++;
|
||||||
|
var im = new Image();
|
||||||
|
im.onload = function(){
|
||||||
|
p.im = im;
|
||||||
|
pending--;
|
||||||
|
if (S.pages[S.cur] === p) { drawPhoto(); drawPreview(); }
|
||||||
|
if (pending === 0) { drawPhoto(); drawPreview(); }
|
||||||
|
};
|
||||||
|
im.onerror = function(){
|
||||||
|
pending--;
|
||||||
|
toast("第 " + p.i + " 页图片加载失败");
|
||||||
|
};
|
||||||
|
im.src = p.img;
|
||||||
|
});
|
||||||
|
if (pending === 0) { drawPhoto(); drawPreview(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
function reloadAfterApply() {
|
||||||
|
return loadBatch().then(function(){
|
||||||
|
/* 服务端已经把应用后的角点写回 DB;清掉本地草稿,避免旧值盖回来 */
|
||||||
|
try { localStorage.removeItem(SAVE_KEY); } catch (e) {}
|
||||||
|
S.cur = 0;
|
||||||
|
while (S.cur < S.pages.length && S.pages[S.cur].deleted) S.cur++;
|
||||||
|
if (S.cur >= S.pages.length) S.cur = 0;
|
||||||
|
loadImages();
|
||||||
|
paintChrome();
|
||||||
|
paintSeg();
|
||||||
|
layout();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyChanges() {
|
||||||
|
if (S.busy) return;
|
||||||
|
var edits = collect();
|
||||||
|
setBusy(true);
|
||||||
|
toast("已提交,正在重新生成…");
|
||||||
|
var ts = new Date().toISOString();
|
||||||
|
apiDb(
|
||||||
|
"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='queued',message=NULL," +
|
||||||
|
"requested_at=excluded.requested_at,started_at=NULL,finished_at=NULL",
|
||||||
|
[BATCH, JSON.stringify(edits), "queued", null, ts]
|
||||||
|
).then(function(){
|
||||||
|
return fetch("/api/sessions/" + encodeURIComponent(TASK) +
|
||||||
|
"/skills/document-photo-to-pdf/run", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {"Content-Type": "application/json"},
|
||||||
|
body: JSON.stringify({args: {batch_id: BATCH}})
|
||||||
|
});
|
||||||
|
}).then(function(r){
|
||||||
|
return r.json().then(function(j){ return {status: r.status, body: j}; });
|
||||||
|
}).then(function(x){
|
||||||
|
var j = x.body || {};
|
||||||
|
if (x.status >= 400 || j.ok === false) {
|
||||||
|
throw new Error(j.error || ("HTTP " + x.status));
|
||||||
|
}
|
||||||
|
var res = j.result || {};
|
||||||
|
if (res.ok === false) {
|
||||||
|
throw new Error((res.error && res.error.message) || res.summary || "生成失败");
|
||||||
|
}
|
||||||
|
return reloadAfterApply();
|
||||||
|
}).then(function(){
|
||||||
|
toast("已重新生成");
|
||||||
|
}).catch(function(e){
|
||||||
|
toast("生成失败:" + (e && e.message || e));
|
||||||
|
}).then(function(){
|
||||||
|
setBusy(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function movePage(delta) {
|
||||||
|
if (S.busy) return;
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p) return;
|
||||||
|
var j = S.cur + delta;
|
||||||
|
while (j >= 0 && j < S.pages.length && S.pages[j].deleted) j += delta;
|
||||||
|
if (j < 0 || j >= S.pages.length) return;
|
||||||
|
S.pages[S.cur] = S.pages[j];
|
||||||
|
S.pages[j] = p;
|
||||||
|
S.cur = j;
|
||||||
|
save();
|
||||||
|
paintChrome();
|
||||||
|
toast(delta < 0 ? "已上移" : "已下移");
|
||||||
|
}
|
||||||
|
|
||||||
|
function deletePage() {
|
||||||
|
if (S.busy) return;
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p) return;
|
||||||
|
p.deleted = !p.deleted;
|
||||||
|
save();
|
||||||
|
paintChrome();
|
||||||
|
layout();
|
||||||
|
toast(p.deleted ? "已标记删除(生成时跳过);再点一次可恢复" : "已恢复本页");
|
||||||
|
}
|
||||||
|
|
||||||
|
function toast(msg) {
|
||||||
|
var t = document.getElementById("toast");
|
||||||
|
t.textContent = msg;
|
||||||
|
t.classList.add("on");
|
||||||
|
setTimeout(function(){ t.classList.remove("on"); }, 1600);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------- 启动 */
|
||||||
|
function boot() {
|
||||||
|
document.getElementById("total").textContent = S.pages.length;
|
||||||
|
loadImages();
|
||||||
|
|
||||||
|
cv = document.getElementById("photo");
|
||||||
|
bindDrag();
|
||||||
|
paintChrome();
|
||||||
|
paintSeg();
|
||||||
|
layout();
|
||||||
|
|
||||||
|
/* --- 全局面板:四项作用于整批,写进 edits.json 的 batch --- */
|
||||||
|
document.getElementById("gear").onclick = function(){
|
||||||
|
paintSeg();
|
||||||
|
document.getElementById("setSheet").classList.add("on");
|
||||||
|
};
|
||||||
|
document.getElementById("setClose").onclick = function(){
|
||||||
|
document.getElementById("setSheet").classList.remove("on");
|
||||||
|
drawPreview();
|
||||||
|
};
|
||||||
|
document.getElementById("setSheet").onclick = function(e){
|
||||||
|
if (e.target === this) this.classList.remove("on");
|
||||||
|
};
|
||||||
|
SEGS.forEach(function(g){
|
||||||
|
document.getElementById(g[0]).onclick = function(e){
|
||||||
|
var t = e.target, b = null;
|
||||||
|
while (t && t !== this) {
|
||||||
|
if (t.getAttribute && t.getAttribute("data-v")) { b = t; break; }
|
||||||
|
t = t.parentNode;
|
||||||
|
}
|
||||||
|
if (!b) return;
|
||||||
|
G[g[1]] = b.getAttribute("data-v");
|
||||||
|
paintSeg(); save();
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
/* --- 移动端:原图 / 预览 二选一,按钮在左上角。桌面端两个都显示,按钮隐藏 --- */
|
||||||
|
var viewBtn = document.getElementById("view");
|
||||||
|
viewBtn.onclick = function(){
|
||||||
|
var on = document.body.classList.toggle("view-prev");
|
||||||
|
viewBtn.innerHTML = on ? ICON_CROP : ICON_EYE;
|
||||||
|
setTimeout(function(){ layout(); drawPreview(); }, 40);
|
||||||
|
};
|
||||||
|
|
||||||
|
document.getElementById("prev").onclick = function(){ step(-1); };
|
||||||
|
document.getElementById("next").onclick = function(){ step(1); };
|
||||||
|
document.getElementById("navPrev").onclick = function(){ step(-1); };
|
||||||
|
document.getElementById("navNext").onclick = function(){ step(1); };
|
||||||
|
|
||||||
|
document.getElementById("apply").onclick = applyChanges;
|
||||||
|
document.getElementById("moveUp").onclick = function(){ movePage(-1); };
|
||||||
|
document.getElementById("moveDown").onclick = function(){ movePage(1); };
|
||||||
|
document.getElementById("delPage").onclick = deletePage;
|
||||||
|
document.getElementById("pdf").onclick = function(){
|
||||||
|
var u = this.dataset.url;
|
||||||
|
if (u) window.location.href = u;
|
||||||
|
};
|
||||||
|
|
||||||
|
document.getElementById("rot").onclick = function(){
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p || p.deleted) return;
|
||||||
|
p.r = (p.r + 90) % 360;
|
||||||
|
drawPreview();
|
||||||
|
save();
|
||||||
|
toast("已转 " + p.r + "°");
|
||||||
|
};
|
||||||
|
|
||||||
|
document.getElementById("reset").onclick = function(){
|
||||||
|
var p = S.pages[S.cur];
|
||||||
|
if (!p || p.deleted) return;
|
||||||
|
p.c = p.base.map(function(v){ return [v[0], v[1]]; });
|
||||||
|
p.r = p.r0;
|
||||||
|
drawPhoto(); placeHandles(); drawPreview(); save();
|
||||||
|
toast("已恢复自动角点");
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("resize", function(){ layout(); });
|
||||||
|
window.addEventListener("orientationchange", function(){
|
||||||
|
setTimeout(layout, 220);
|
||||||
|
});
|
||||||
|
/* 兜底:首帧 stage 量到 0 时 layout 会直接返回,稍后再来一次 */
|
||||||
|
setTimeout(layout, 80);
|
||||||
|
if (document.fonts && document.fonts.ready) document.fonts.ready.then(layout);
|
||||||
|
}
|
||||||
|
|
||||||
|
function start() {
|
||||||
|
loadBatch().then(function(){
|
||||||
|
loadSaved();
|
||||||
|
boot();
|
||||||
|
}).catch(function(e){
|
||||||
|
showEmpty("加载审核批次失败:" + (e && e.message || e));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (document.readyState === "loading") {
|
||||||
|
document.addEventListener("DOMContentLoaded", start);
|
||||||
|
} else {
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user