feat: PDF support via pdftoppm extraction

This commit is contained in:
2026-06-23 02:44:25 +08:00
parent 05f7cd2be5
commit eec7cb6b34
5 changed files with 330 additions and 27 deletions
+20
View File
@@ -0,0 +1,20 @@
{
"2241.01.05": "上料押金",
"1001.01": "人民币",
"1221.01": "员工借款",
"1012.01": "POS刷卡未到账",
"6051.08": "转售水电费收入",
"2203.01": "物业管理费",
"1002.01.01": "淇县支行1710020809200182975",
"6603.02": "手续费",
"6051.02": "办证工本收入",
"1002.02.01": "漓江支行4105016228400000750",
"1411.02": "电子设备",
"6401.51.09": "日常维修费",
"6401.02.02": "办公费",
"6401.01.02": "职工福利费",
"6402.01": "付垃圾清运费",
"2241.01.01": "装修押金",
"6051.01": "垃圾清运费",
"6051.05": "家政服务费"
}
+1 -1
View File
@@ -1,6 +1,6 @@
ocr:
det_db_box_thresh: 0.5
det_db_score_mode: fast
det_db_score_mode: slow
det_db_thresh: 0.3
det_db_unclip_ratio: 1.5
lang: ch
+83 -8
View File
@@ -19,6 +19,75 @@ class K3VoucherParser:
def __init__(self, lines):
"""lines: [(text, conf, cx, cy, box), ...]"""
self.lines = sorted(lines, key=lambda l: (l[3], l[2]))
self._merge_continuation_lines()
def _merge_continuation_lines(self):
"""合并续行:会计科目名称被OCR分成多行时,合并回一行
策略:续行以横杠开头(如 -淇县支行1710020809200182975),
找到上方最近的一个以横杠结尾的代码行进行合并。
如果找不到以横杠结尾的代码行,则找上方最近的不以横杠开头的代码行。
"""
# 先识别所有代码行
code_lines = [] # [(idx, text, ends_with_dash, cy, cx)]
for i, (text, conf, cx, cy, box) in enumerate(self.lines):
t = text.strip()
if re.match(r'\d{3,4}\.\d', t):
code_lines.append((i, t, t.endswith('-'), cy, cx))
if not code_lines:
return
merged = list(self.lines)
removed = set()
merged_codes = set() # 已接收续行的代码行,不再接收
for i, (text, conf, cx, cy, box) in enumerate(self.lines):
t = text.strip()
if not (t.startswith('-') or t.startswith('-') or t.startswith('—')):
continue
if i in removed:
continue
# 跳过包含日期/表头关键词的续行
stripped = t.lstrip('--—')
if any(k in stripped for k in ['日期', '记账凭证', '记账', '业务日期', '凭证号', '附件数']):
continue
# 匹配代码行
best_code = None
best_score = 99999
for idx, ct, ends_dash, ccy, ccx in code_lines:
if idx in merged_codes:
continue # 已合并过的代码行不再接收续行
if abs(cy - ccy) > 300:
continue
if abs(cx - ccx) > 300:
continue
dist_y = abs(cy - ccy)
dist_x = abs(cx - ccx)
if ends_dash:
score = dist_y * 2 + dist_x * 2
else:
# 不以横杠结尾的代码行:x必须非常接近才接受
if dist_x > 100:
continue
score = 1500 + dist_y + dist_x * 2
if score < best_score:
best_score = score
best_code = idx
if best_code is not None and best_score < 2000:
orig_text, orig_conf, orig_cx, orig_cy, orig_box = merged[best_code]
merged[best_code] = (orig_text + text, min(orig_conf, conf), orig_cx, orig_cy, orig_box)
removed.add(i)
merged_codes.add(best_code) # 标记已合并
self.lines = [merged[i] for i in range(len(merged)) if i not in removed]
def parse(self):
result = {
@@ -185,7 +254,9 @@ class K3VoucherParser:
# 提取名称 — 按用户规则:取最后一个横杠之后的文字
name = t[m.end():].lstrip('--—= ')
name = re.sub(r'[\d.\-—=]+$', '', name).strip()
# 保留长数字尾(银行账号),仅清除短数字尾
if not re.search(r'\d{6,}', name):
name = re.sub(r'[\d.\-—=]+$', '', name).strip()
name = name.lstrip('-—-=')
if '\\' in name:
@@ -199,7 +270,7 @@ class K3VoucherParser:
if code_part in code_map:
existing = codes[code_map[code_part]]
# 已有名称为空,而新行有名称 → 更新
if (not existing['name'] or len(existing['name']) < 2) and len(name) >= 2:
if len(name) >= 2 and (not existing['name'] or len(existing['name']) < 2 or len(name) > len(existing['name'])): # 优先长名
existing['name'] = name
existing['y_pos'] = cy
existing['x_pos'] = cx
@@ -251,17 +322,19 @@ class K3VoucherParser:
for t, cy, cx in name_candidates:
if cy <= code['y_pos']:
continue # 只找下方的
if abs(cx - code['x_pos']) > 600:
if abs(cx - code['x_pos']) > 400:
continue
dist = cy - code['y_pos']
if dist < best_dist and dist < 600:
if dist < best_dist and dist < 400:
best = (t, cy, cx)
best_dist = dist
if best:
t, cy, cx = best
# 按用户规则提取名称(原始逻辑:先清洗后取最后横杠)
n = t.lstrip('--—= ')
n = re.sub(r'[\d.\-—=]+$', '', n).strip()
# 保留长数字尾(银行账号)
if not re.search(r'\d{6,}', n):
n = re.sub(r'[\d.\-—=]+$', '', n).strip()
n = n.lstrip('-—-=')
if '\\' in n:
n = n.split('\\', 1)[0].strip()
@@ -286,17 +359,19 @@ class K3VoucherParser:
for nt, ncy, ncx in name_candidates:
if ncy <= code['y_pos']:
continue
if abs(ncx - code['x_pos']) > 600:
if abs(ncx - code['x_pos']) > 400:
continue
ndist = ncy - code['y_pos']
if ndist < next_dist and ndist < 600:
if ndist < next_dist and ndist < 400:
next_best = (nt, ncy, ncx)
next_dist = ndist
if not next_best:
break
nt, ncy, ncx = next_best
nn = nt.lstrip('--—= ')
nn = re.sub(r'[\d.\-—=]+$', '', nn).strip()
# 保留长数字尾(银行账号)
if not re.search(r'\d{6,}', nn):
nn = re.sub(r'[\d.\-—=]+$', '', nn).strip()
nn = nn.lstrip('-—-=')
if '\\' in nn:
nn = nn.split('\\', 1)[0].strip()
+13 -2
View File
@@ -27,18 +27,29 @@ def get_ocr_instance(config: dict):
return _ocr
def run_ocr(image, ocr_config: dict) -> list:
def run_ocr(image, ocr_config: dict, photo_type: str = 'normal') -> list:
"""对图像执行 OCR,返回 [(text, confidence, cx, cy, box), ...]
输入:
image: numpy array (cv2.imread) 或 图像文件路径
ocr_config: dict(PaddleOCR 参数)
photo_type: str — 照片类型,用于动态调参
返回:
排序后的 lines: [(text, conf, cx, cy, box), ...]
按 (y, x) 排序
"""
ocr = get_ocr_instance(ocr_config)
# 根据照片类型动态调整 OCR 参数
config_override = dict(ocr_config)
if photo_type == 'flash':
config_override['det_db_thresh'] = config_override.get('det_db_thresh_flash', 0.18)
elif photo_type == 'dark':
config_override['det_db_thresh'] = config_override.get('det_db_thresh_dark', 0.25)
elif photo_type == 'blurry':
config_override['det_db_thresh'] = config_override.get('det_db_thresh_blurry', 0.2)
# normal 保持默认
ocr = get_ocr_instance(config_override)
# 支持 numpy array 和 文件路径
result = ocr(image)
+213 -16
View File
@@ -22,6 +22,7 @@ KOCR v4 — 金蝶 K3 凭证 OCR 填表工具
import os
import sys
import json
import argparse
import yaml
import cv2
@@ -31,10 +32,60 @@ PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, PROJECT_DIR)
from core.recognizer import run_ocr
from core.preprocessor import analyze_image, VoucherPreprocessor
from core.parser import K3VoucherParser
from core.postprocessor import validate_voucher, validate_merged_vouchers, generate_review_file
from core.excel_writer import fill_template, merge_output, merge_voucher_data
# ======== 科目代码标准映射表 ========
CODE_MAP_PATH = os.path.join(PROJECT_DIR, 'code_map.json')
_code_map_cache = None
def get_code_map():
global _code_map_cache
if _code_map_cache is not None:
return _code_map_cache
if os.path.exists(CODE_MAP_PATH):
with open(CODE_MAP_PATH, 'r', encoding='utf-8') as f:
_code_map_cache = json.load(f)
else:
_code_map_cache = {}
return _code_map_cache
def update_code_map(new_entries: dict):
"""更新映射表(用于后续完善)"""
current = get_code_map()
current.update(new_entries)
with open(CODE_MAP_PATH, 'w', encoding='utf-8') as f:
json.dump(current, f, ensure_ascii=False, indent=2)
global _code_map_cache
_code_map_cache = current
def get_reverse_code_map():
"""名称 → 代码 反向映射(精确匹配)"""
code_map = get_code_map()
return {v: k for k, v in code_map.items()}
def fuzzy_find_code(name_or_fragment: str):
"""根据名称部分字段模糊查找代码
匹配规则:名称包含该字段,或字段包含该名称
返回最长的匹配项(最具体)
"""
code_map = get_code_map()
name_or_fragment = name_or_fragment.strip()
if not name_or_fragment or len(name_or_fragment) < 2:
return None
best_code = None
best_len = 0
for code, name in code_map.items():
if name_or_fragment in name or name in name_or_fragment:
if len(name) > best_len:
best_code = code
best_len = len(name)
return best_code
# ======== 路径常量 ========
DEFAULT_CONFIG = os.path.join(PROJECT_DIR, 'config.yaml')
@@ -134,6 +185,57 @@ def is_processed(filename):
return filename in idx
# ======== PDF 页面提取 ========
def extract_pdf_pages(pdf_path, output_dir):
"""将 PDF 每页转换为 JPG 图片
直接输出到 output_dir,命名格式:_{pdf_name}_p{n}.jpg
返回 [(page_path, page_label), ...]
"""
import subprocess
import re
pdf_name = os.path.splitext(os.path.basename(pdf_path))[0]
# pdftoppm: 每页生成 {prefix}-{页码}.jpg
result = subprocess.run(
['pdftoppm', '-jpeg', '-r', '300', pdf_path, os.path.join(output_dir, pdf_name)],
capture_output=True, text=True, timeout=120
)
if result.returncode != 0:
raise RuntimeError(f"pdftoppm 失败: {result.stderr}")
# 收集生成的图片 → 重命名为 _{pdf_name}_p{n}.jpg
pages = sorted([
f for f in os.listdir(output_dir)
if f.startswith(pdf_name) and f.lower().endswith('.jpg')
])
result_list = []
for p in pages:
old_path = os.path.join(output_dir, p)
# 从文件名提取页码,如 test-01.jpg → _test_p1.jpg
m = re.search(r'-(\d+)\.jpg$', p)
page_num = int(m.group(1)) if m else 0
new_name = f"_{pdf_name}_p{page_num}.jpg"
new_path = os.path.join(output_dir, new_name)
os.rename(old_path, new_path)
page_label = f"第{page_num}页" if m else os.path.basename(pdf_path)
result_list.append((new_path, page_label))
return result_list
def cleanup_pdf_temp_files(input_path):
"""清理 PDF 提取的临时图片(以下划线开头的 JPG)"""
for f in os.listdir(input_path):
if f.startswith('_') and f.lower().endswith('.jpg'):
try:
os.remove(os.path.join(input_path, f))
except OSError:
pass
# ======== 单张凭证处理 ========
def process_single(image_path, config, output_path=None, verbose=False, mark_done=True):
"""处理单张凭证照片
@@ -153,20 +255,94 @@ def process_single(image_path, config, output_path=None, verbose=False, mark_don
if img is None:
raise ValueError(f"无法读取图片: {image_path}")
# 2. 原图直出 OCR(无预处理)
ocr_config = config.get('ocr', {})
# 2. 双管线 OCR:原图 + 预处理图,合并结果
if verbose:
print(f" 📋 OCR 识别中...")
lines = run_ocr(img, ocr_config)
print(f" 📊 分析图片质量...")
quality = analyze_image(img)
if verbose:
print(f" OCR: {len(lines)} 行文字")
print(f" 📊 照片类型: {quality.photo_type} (亮度={quality.brightness:.0f}, 阴影比={quality.shadow_ratio:.2f})")
# 3. 解析
parser = K3VoucherParser(lines)
# 管线1: 原图OCR(保留横杠标记/续行结构)
if verbose:
print(f" 📋 管线1: 原图OCR...")
lines1 = run_ocr(img, ocr_config, photo_type=quality.photo_type)
# 管线2: 预处理后OCR(去阴影/纠偏/锐化,提高文字识别率)
preprocess_config = config.get('preprocessing', {})
preprocessor = VoucherPreprocessor.from_config(preprocess_config, quality.photo_type)
img_processed = preprocessor.process(img)
if verbose:
print(f" 📋 管线2: 预处理图OCR...")
lines2 = run_ocr(img_processed, ocr_config, photo_type=quality.photo_type)
# 合并双管线结果:按 y 坐标邻近去重
all_lines = lines1 + lines2
# 按 y 排序后,合并相近位置的重复文本
all_lines.sort(key=lambda l: (l[3], l[2]))
merged_lines = []
for line in all_lines:
is_dup = False
for existing in merged_lines:
if abs(line[3] - existing[3]) < 20 and abs(line[2] - existing[2]) < 50:
# 同位置,保留文本更长的
if len(line[0].strip()) > len(existing[0].strip()):
existing = line
is_dup = True
break
if not is_dup:
merged_lines.append(line)
if verbose:
print(f" 📋 双管线合并: {len(lines1)}+{len(lines2)} → {len(merged_lines)} 行去重")
# 3. 解析(使用双管线合并后的数据)
parser = K3VoucherParser(merged_lines)
voucher = parser.parse()
# 3.5 双向对照表校正(代码→名称 + 名称→代码反查)
CODE_MAP = get_code_map()
REVERSE_MAP = get_reverse_code_map()
corrections = 0
for e in voucher['entries']:
code = e.get('account_code', '')
name = e.get('account_name', '')
# 正向:代码在映射表中 → 用标准名称
if code in CODE_MAP:
std_name = CODE_MAP[code]
if name != std_name:
if verbose:
print(f" 🔄 名称修正: {code} [{name}] → [{std_name}]")
e['account_name'] = std_name
corrections += 1
# 反向:名称精确匹配 → 用标准代码
if name in REVERSE_MAP:
std_code = REVERSE_MAP[name]
if code != std_code:
if verbose:
print(f" 🔄 代码修正: [{code}] → [{std_code}] (名称={name})")
e['account_code'] = std_code
corrections += 1
# 模糊反向:代码不在映射表但名称部分匹配 → 反查
if code not in CODE_MAP and name:
fuzzy_code = fuzzy_find_code(name)
if fuzzy_code:
std_name = CODE_MAP[fuzzy_code]
if verbose:
print(f" 🔄 模糊反查: [{code},{name}] → [{fuzzy_code},{std_name}]")
e['account_code'] = fuzzy_code
e['account_name'] = std_name
corrections += 1
if verbose and corrections > 0:
print(f" ✅ 对照表双向修正: {corrections} 处")
if verbose:
print(f" 公司: {voucher['company']}")
print(f" 日期: {voucher['date'].strftime('%Y-%m-%d') if voucher['date'] else '未识别'}")
@@ -218,6 +394,7 @@ def show_status(config):
exts = ('.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.tif', '.webp')
pending_files = sorted([f for f in os.listdir(input_dir) if f.lower().endswith(exts)]) if os.path.exists(input_dir) else []
pdf_files = sorted([f for f in os.listdir(input_dir) if f.lower().endswith('.pdf')]) if os.path.exists(input_dir) else []
done_files = sorted([f for f in os.listdir(done_dir) if f.lower().endswith(exts)]) if os.path.exists(done_dir) else []
processed = set(idx.keys())
@@ -225,7 +402,9 @@ def show_status(config):
print("📊 KOCR v4 处理状态")
print(f" {'='*40}")
print(f" 索引记录: {len(processed)} 张已处理")
print(f" input/ 待处理: {len(pending_files)} 张")
print(f" input/ 待处理: {len(pending_files)} 张图片")
if pdf_files:
print(f" input/ 待转换 PDF: {len(pdf_files)} 个")
print(f" done/ 已归档: {len(done_files)} 张")
print()
@@ -315,17 +494,32 @@ def main():
if os.path.isdir(input_path):
# 批量模式
exts = ('.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.tif', '.webp')
files = sorted([f for f in os.listdir(input_path) if f.lower().endswith(exts)])
img_exts = ('.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.tif', '.webp')
files = sorted([f for f in os.listdir(input_path) if f.lower().endswith(img_exts)])
# PDF 文件检测与提取
pdf_files = sorted([f for f in os.listdir(input_path) if f.lower().endswith('.pdf')])
if pdf_files:
print(f"📄 发现 {len(pdf_files)} 个 PDF 文件,正在提取页面...")
for pdf_file in pdf_files:
pdf_path = os.path.join(input_path, pdf_file)
try:
pages = extract_pdf_pages(pdf_path, input_path)
# 将页面图片加入待处理列表
page_fnames = [os.path.basename(p) for p, _ in pages]
files.extend(page_fnames)
print(f" 📄 {pdf_file} → {len(pages)} 页")
except Exception as e:
print(f" ❌ {pdf_file} 提取失败: {e}")
if not files:
print(f"📭 {input_path} 中没有图片文件")
print(f"📭 {input_path} 中没有图片或 PDF 文件")
sys.exit(0)
# --new 模式过滤
# --new 模式过滤(PDF 临时页面以下划线开头,始终视为新文件)
idx = load_index()
if args.new or not args.force:
new_files = [f for f in files if f not in idx]
new_files = [f for f in files if f not in idx or f.startswith('_')]
skipped = len(files) - len(new_files)
files = new_files
if skipped > 0:
@@ -434,12 +628,15 @@ def main():
for fname, voucher, out_path, quality in results:
src = os.path.join(input_path, fname)
dst = os.path.join(done_dir, fname)
if os.path.exists(src):
if os.path.exists(src) and not fname.startswith('_'):
os.rename(src, dst)
archived += 1
if archived > 0:
print(f" 📦 已归档 {archived} 张照片到 done/")
# ---- 清理 PDF 临时页面图片 ----
cleanup_pdf_temp_files(input_path)
print(f"🎉 完成! {len(results)}/{len(files)} 成功")
else: