feat(fields): 字段抽取支持按行锁定与多项匹配开关,修复进度日志被吞
字段抽取 src/ocr_server/fields.py - 新增 row:只从第 N 行取值,行号由 bbox 纵向重叠的坐标分组得出,-1 表示不限定; 用于排除正文/页眉里同名标签造成的误取 - 新增 separator_optional(标签后无冒号也抽,默认关闭) 与 label_linebreak(标签内部跨行,如 姓 + 名:,默认关闭) - 新增 occurrence(first/last,正副本、页眉页脚重复打印时取最后一个) 与全局 extract_include_empty(未命中的字段返回空串,便于固定 schema) - 修复:跨行命中的标签文本包含换行,归一化时未去换行导致匹配不到字段规则、 取到的值被丢弃 - build_specs 在服务启动时校验:字段名重复、同一标签被多个字段共用、 正则语法、max_length / occurrence / row 取值合法性 日志 src/ocr_server/logging_setup.py - handler 改挂到 ocr_server logger 并关闭向 root 传播:Paddle 初始化会把 root 级别改成 WARNING,依赖 root 会导致其后所有 INFO 日志(含 10/20/50/80/100 进度行)被静默过滤,看起来像任务卡死 引擎配置 - enable_mkldnn 由写死的 False 改为可配置;cpu_threads 支持 max(吃满逻辑核心) - pyproject 固定 paddlepaddle>=3.2,<3.3:3.3.x 的 PIR + oneDNN 组合会直接推理失败 打包 - 移除多余的 scripts 目录打包;新增 -DistPath 便于 dist 被占用时改址构建; 构建结束输出 exe SHA256(避免再拿到旧产物) - Ubuntu 脚本同步更新,并输出二进制 SHA256 文档 - README / config.yaml 注释按实测修订:行结构、模型档位对比、oneDNN 与版本差异、 汉字字符类的范围与缺口、防止取错的机制与建议 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,439 @@
|
||||
from ocr_server.config import FieldPreset, Settings
|
||||
from ocr_server.fields import (
|
||||
FieldSpec,
|
||||
build_specs,
|
||||
clean_value,
|
||||
extract_fields,
|
||||
)
|
||||
|
||||
MEDICAL_SPECS = [
|
||||
FieldSpec("姓名", ("患者姓名",)),
|
||||
FieldSpec("检查号", ("检查编号",)),
|
||||
FieldSpec("性别"),
|
||||
FieldSpec("年龄", compact=True),
|
||||
FieldSpec("门诊号"),
|
||||
FieldSpec("床号"),
|
||||
FieldSpec("科室"),
|
||||
FieldSpec("镜型"),
|
||||
FieldSpec("临床诊断"),
|
||||
]
|
||||
|
||||
|
||||
def test_same_line_contains_multiple_fields() -> None:
|
||||
"""OCR 常把多个字段合成一段,必须逐个切分。"""
|
||||
text = "检查号:26100830064姓名:万镭"
|
||||
fields = extract_fields(text, MEDICAL_SPECS)
|
||||
|
||||
assert fields["检查号"] == "26100830064"
|
||||
assert fields["姓名"] == "万镭"
|
||||
|
||||
|
||||
def test_label_with_inner_spaces_still_matches() -> None:
|
||||
"""OCR 会把「姓名」识别成「姓 名」,标签内部空格需被容忍。"""
|
||||
text = "姓 名:万镭 性 别:女 年 龄:43 岁"
|
||||
fields = extract_fields(text, MEDICAL_SPECS)
|
||||
|
||||
assert fields["姓名"] == "万镭"
|
||||
assert fields["性别"] == "女"
|
||||
assert fields["年龄"] == "43岁" # compact 删除内部空白
|
||||
|
||||
|
||||
def test_real_report_text_end_to_end() -> None:
|
||||
text = "\n".join(
|
||||
[
|
||||
"检查号:26100830064姓名:万镭",
|
||||
"性别:女",
|
||||
"年龄:43岁",
|
||||
"门诊号:701785075",
|
||||
"床号:",
|
||||
"科室:消化内科门诊",
|
||||
"镜型:CF-HQ290I",
|
||||
"临床诊断:大便习惯改变;",
|
||||
]
|
||||
)
|
||||
fields = extract_fields(text, MEDICAL_SPECS)
|
||||
|
||||
assert fields == {
|
||||
"检查号": "26100830064",
|
||||
"姓名": "万镭",
|
||||
"性别": "女",
|
||||
"年龄": "43岁",
|
||||
"门诊号": "701785075",
|
||||
"科室": "消化内科门诊",
|
||||
"镜型": "CF-HQ290I",
|
||||
"临床诊断": "大便习惯改变",
|
||||
}
|
||||
assert "床号" not in fields # 空值不返回
|
||||
|
||||
|
||||
def test_halfwidth_and_fullwidth_separators() -> None:
|
||||
text = "姓名: 万镭 检查号:26100830064"
|
||||
fields = extract_fields(text, MEDICAL_SPECS)
|
||||
|
||||
assert fields["姓名"] == "万镭"
|
||||
assert fields["检查号"] == "26100830064"
|
||||
|
||||
|
||||
def test_value_stops_at_line_break() -> None:
|
||||
text = "姓名:万镭\n性别:女"
|
||||
fields = extract_fields(text, MEDICAL_SPECS)
|
||||
|
||||
assert fields["姓名"] == "万镭"
|
||||
assert fields["性别"] == "女"
|
||||
|
||||
|
||||
def test_label_without_separator_is_not_extracted() -> None:
|
||||
fields = extract_fields("姓名 万镭", MEDICAL_SPECS)
|
||||
|
||||
assert "姓名" not in fields
|
||||
|
||||
|
||||
def test_alias_is_matched() -> None:
|
||||
fields = extract_fields("患者姓名:张三", MEDICAL_SPECS)
|
||||
|
||||
assert fields["姓名"] == "张三"
|
||||
|
||||
|
||||
def test_first_occurrence_wins() -> None:
|
||||
text = "姓名:张三\n姓名:李四"
|
||||
fields = extract_fields(text, MEDICAL_SPECS)
|
||||
|
||||
assert fields["姓名"] == "张三"
|
||||
|
||||
|
||||
def test_no_specs_returns_empty() -> None:
|
||||
assert extract_fields("姓名:万镭", []) == {}
|
||||
assert extract_fields("", MEDICAL_SPECS) == {}
|
||||
|
||||
|
||||
def test_clean_value_strips_noise() -> None:
|
||||
assert clean_value(" 万镭 ") == "万镭"
|
||||
assert clean_value("大便习惯改变;") == "大便习惯改变"
|
||||
assert clean_value(":26100830064") == "26100830064"
|
||||
assert clean_value("43 岁", compact=True) == "43岁"
|
||||
assert clean_value("43 岁") == "43 岁"
|
||||
assert clean_value("万镭\n性别:女") == "万镭 性别:女"
|
||||
assert clean_value("万镭") == "万镭"
|
||||
|
||||
|
||||
def test_build_specs_from_config_presets() -> None:
|
||||
specs = build_specs(
|
||||
[
|
||||
FieldPreset(name="姓名", aliases=["患者姓名"]),
|
||||
FieldPreset(name="年龄", compact=True),
|
||||
FieldPreset(name=""),
|
||||
]
|
||||
)
|
||||
|
||||
assert [item.name for item in specs] == ["姓名", "年龄"]
|
||||
assert specs[0].aliases == ("患者姓名",)
|
||||
assert specs[1].compact is True
|
||||
|
||||
|
||||
def test_settings_loads_extract_presets() -> None:
|
||||
settings = Settings(
|
||||
extract_fields=[{"name": "姓名", "aliases": ["患者姓名"]}],
|
||||
extract_separators=[":"],
|
||||
)
|
||||
|
||||
assert settings.extract_fields[0].name == "姓名"
|
||||
assert settings.extract_fields[0].aliases == ["患者姓名"]
|
||||
assert settings.extract_separators == [":"]
|
||||
|
||||
specs = build_specs(settings.extract_fields)
|
||||
assert extract_fields("患者姓名:万镭", specs, (":",)) == {"姓名": "万镭"}
|
||||
|
||||
|
||||
def test_value_stops_at_next_configured_label() -> None:
|
||||
"""值的边界是「下一个已配置标签」,所以相邻字段都能正确切分。"""
|
||||
specs = [FieldSpec("姓名"), FieldSpec("性别", pattern="[男女]")]
|
||||
fields = extract_fields("姓名:万镭性别:女", specs)
|
||||
|
||||
assert fields["姓名"] == "万镭"
|
||||
assert fields["性别"] == "女"
|
||||
|
||||
|
||||
def test_unconfigured_label_is_not_a_boundary() -> None:
|
||||
"""未配置的标签不构成边界,此时用 pattern 的限定量词约束形状。"""
|
||||
specs = [FieldSpec("姓名", pattern="[一-龥·]{1,2}")]
|
||||
fields = extract_fields("姓名:万镭性别:女", specs)
|
||||
|
||||
assert fields["姓名"] == "万镭"
|
||||
|
||||
|
||||
def test_pattern_mismatch_skips_field() -> None:
|
||||
specs = [FieldSpec("检查号", pattern="[A-Za-z0-9-]{1,24}")]
|
||||
fields = extract_fields("检查号:无效值", specs)
|
||||
|
||||
assert "检查号" not in fields
|
||||
|
||||
|
||||
def test_pattern_boundary_includes_match() -> None:
|
||||
specs = [FieldSpec("年龄", pattern=r"\d{1,3}\s*岁", compact=True)]
|
||||
fields = extract_fields("年龄:43 岁 镜型:CF-HQ290I", specs)
|
||||
|
||||
assert fields["年龄"] == "43岁"
|
||||
|
||||
|
||||
def test_end_pattern_is_exclusive_terminator() -> None:
|
||||
specs = [FieldSpec("临床诊断", end_pattern="[;;,,。]")]
|
||||
fields = extract_fields("临床诊断:大便习惯改变;无其他异常", specs)
|
||||
|
||||
assert fields["临床诊断"] == "大便习惯改变"
|
||||
|
||||
|
||||
def test_end_pattern_can_stop_before_next_chinese_field_without_label() -> None:
|
||||
specs = [FieldSpec("科室", end_pattern="镜型")]
|
||||
fields = extract_fields("科室:消化内科门诊镜型:CF-HQ290I", specs)
|
||||
|
||||
assert fields["科室"] == "消化内科门诊"
|
||||
|
||||
|
||||
def test_max_length_truncates_value() -> None:
|
||||
specs = [FieldSpec("科室", max_length=4)]
|
||||
fields = extract_fields("科室:消化内科门诊", specs)
|
||||
|
||||
assert fields["科室"] == "消化内科"
|
||||
|
||||
|
||||
def test_multiline_disabled_by_default() -> None:
|
||||
specs = [FieldSpec("临床诊断")]
|
||||
fields = extract_fields("临床诊断:大便习惯改变\n检查号:26100830064", specs)
|
||||
|
||||
assert fields["临床诊断"] == "大便习惯改变"
|
||||
|
||||
|
||||
def test_multiline_allows_value_across_lines() -> None:
|
||||
specs = [FieldSpec("临床诊断", multiline=True, end_pattern="[;;]")]
|
||||
fields = extract_fields("临床诊断:大便习惯改变\n无其他异常;检查号:26", specs)
|
||||
|
||||
assert fields["临床诊断"] == "大便习惯改变 无其他异常"
|
||||
|
||||
|
||||
def test_pattern_and_end_pattern_and_max_length_together() -> None:
|
||||
specs = [FieldSpec("检查号", pattern=r"\d+", end_pattern="-", max_length=6)]
|
||||
fields = extract_fields("检查号:26100830064-甲乙", specs)
|
||||
|
||||
assert fields["检查号"] == "261008"
|
||||
|
||||
|
||||
def test_build_specs_carries_new_options() -> None:
|
||||
specs = build_specs(
|
||||
[
|
||||
FieldPreset(
|
||||
name="姓名",
|
||||
pattern="[一-龥]{1,12}",
|
||||
end_pattern=";",
|
||||
max_length=12,
|
||||
multiline=True,
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
spec = specs[0]
|
||||
assert spec.pattern == "[一-龥]{1,12}"
|
||||
assert spec.end_pattern == ";"
|
||||
assert spec.max_length == 12
|
||||
assert spec.multiline is True
|
||||
assert spec._value_re is not None
|
||||
assert spec._end_re is not None
|
||||
|
||||
|
||||
def test_invalid_regex_is_rejected_at_config_load() -> None:
|
||||
import pytest
|
||||
|
||||
with pytest.raises(Exception):
|
||||
FieldPreset(name="姓名", pattern="[未闭合")
|
||||
with pytest.raises(Exception):
|
||||
FieldPreset(name="姓名", end_pattern="(unclosed")
|
||||
with pytest.raises(Exception):
|
||||
FieldPreset(name="姓名", max_length=0)
|
||||
|
||||
|
||||
def test_real_report_with_patterns() -> None:
|
||||
"""带 pattern 的完整预设:防止相邻字段粘连。"""
|
||||
specs = [
|
||||
FieldSpec("姓名", pattern="[一-龥·]{1,12}"),
|
||||
FieldSpec("检查号", pattern="[A-Za-z0-9-]{1,24}"),
|
||||
FieldSpec("性别", pattern="[男女]"),
|
||||
FieldSpec("年龄", pattern=r"\d{1,3}\s*岁", compact=True),
|
||||
FieldSpec("镜型", pattern="[A-Za-z0-9-]{1,24}"),
|
||||
FieldSpec("临床诊断", end_pattern="[;;]"),
|
||||
]
|
||||
text = "\n".join(
|
||||
[
|
||||
"年龄:43岁检查号:26100830064姓名:万镭性别:女",
|
||||
"镜型:CF-HQ290I门诊号:701785075",
|
||||
"临床诊断:大便习惯改变;无其他异常",
|
||||
]
|
||||
)
|
||||
fields = extract_fields(text, specs)
|
||||
|
||||
assert fields == {
|
||||
"年龄": "43岁",
|
||||
"检查号": "26100830064",
|
||||
"姓名": "万镭",
|
||||
"性别": "女",
|
||||
"镜型": "CF-HQ290I",
|
||||
"临床诊断": "大便习惯改变",
|
||||
}
|
||||
|
||||
|
||||
def test_occurrence_last_takes_the_final_value() -> None:
|
||||
"""页眉+页脚都出现同一标签时,可能需要取最后一次。"""
|
||||
text = "姓名:张三\n报告医生:李四\n姓名:张三丰"
|
||||
assert extract_fields(text, [FieldSpec("姓名")])["姓名"] == "张三"
|
||||
assert extract_fields(text, [FieldSpec("姓名", occurrence="last")])["姓名"] == "张三丰"
|
||||
|
||||
|
||||
def test_occurrence_last_skips_empty_trailing_value() -> None:
|
||||
text = "姓名:张三\n姓名:"
|
||||
fields = extract_fields(text, [FieldSpec("姓名", occurrence="last")])
|
||||
|
||||
assert fields["姓名"] == "张三"
|
||||
|
||||
|
||||
def test_occurrence_validated() -> None:
|
||||
import pytest
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
FieldSpec("姓名", occurrence="middle")
|
||||
|
||||
|
||||
def test_build_specs_rejects_duplicate_field_names() -> None:
|
||||
import pytest
|
||||
|
||||
with pytest.raises(ValueError, match="重复的字段名"):
|
||||
build_specs([FieldPreset(name="姓名"), FieldPreset(name="姓名")])
|
||||
|
||||
|
||||
def test_build_specs_rejects_shared_label() -> None:
|
||||
"""同一标签被两个字段共用必然取错,启动时就应报错。"""
|
||||
import pytest
|
||||
|
||||
with pytest.raises(ValueError, match="同时被字段"):
|
||||
build_specs(
|
||||
[
|
||||
FieldPreset(name="检查号"),
|
||||
FieldPreset(name="编号", aliases=["检查号"]),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def test_build_specs_allows_same_label_for_same_field() -> None:
|
||||
"""同一个字段内部主名与别名重复无害。"""
|
||||
specs = build_specs([FieldPreset(name="姓名", aliases=["姓名", "患者姓名"])])
|
||||
|
||||
assert len(specs) == 1
|
||||
assert specs[0].labels == ("患者姓名", "姓名")
|
||||
|
||||
|
||||
def test_label_linebreak_switch() -> None:
|
||||
"""OCR 把「姓名」拆成两行时,只有打开开关才能匹配。"""
|
||||
text = "姓\n名:万镭"
|
||||
|
||||
assert "姓名" not in extract_fields(text, [FieldSpec("姓名")])
|
||||
assert (
|
||||
extract_fields(text, [FieldSpec("姓名", label_linebreak=True)])["姓名"] == "万镭"
|
||||
)
|
||||
|
||||
|
||||
def test_label_linebreak_allows_at_most_one_newline() -> None:
|
||||
"""跨行只允许一个换行,避免把相隔很远的字拼成标签。"""
|
||||
text = "姓\n\n\n名:万镭"
|
||||
|
||||
assert "姓名" not in extract_fields(text, [FieldSpec("姓名", label_linebreak=True)])
|
||||
|
||||
|
||||
def test_separator_optional_switch() -> None:
|
||||
assert "姓名" not in extract_fields("姓名 万镭", [FieldSpec("姓名")])
|
||||
assert (
|
||||
extract_fields("姓名 万镭", [FieldSpec("姓名", separator_optional=True)])["姓名"]
|
||||
== "万镭"
|
||||
)
|
||||
# 带冒号时两种配置都应正常
|
||||
assert extract_fields("姓名:万镭", [FieldSpec("姓名", separator_optional=True)]) == {
|
||||
"姓名": "万镭"
|
||||
}
|
||||
|
||||
|
||||
def test_row_limits_lookup_to_one_row() -> None:
|
||||
"""row 用于避开正文或页眉里同名的干扰标签。"""
|
||||
rows = ["姓名:张三", "报告医生:李四", "正文提到姓名:王五", "姓名:张三丰"]
|
||||
text = "\n".join(rows)
|
||||
seps = (":", ":")
|
||||
|
||||
assert extract_fields(text, [FieldSpec("姓名")], seps, rows=rows)["姓名"] == "张三"
|
||||
assert (
|
||||
extract_fields(text, [FieldSpec("姓名", row=3)], seps, rows=rows)["姓名"] == "王五"
|
||||
)
|
||||
assert (
|
||||
extract_fields(text, [FieldSpec("姓名", row=4)], seps, rows=rows)["姓名"]
|
||||
== "张三丰"
|
||||
)
|
||||
|
||||
|
||||
def test_row_out_of_range_returns_nothing() -> None:
|
||||
rows = ["姓名:张三"]
|
||||
fields = extract_fields(
|
||||
"姓名:张三", [FieldSpec("姓名", row=9)], (":", ":"), rows=rows
|
||||
)
|
||||
|
||||
assert fields == {}
|
||||
|
||||
|
||||
def test_row_is_ignored_when_rows_not_provided() -> None:
|
||||
"""没有行信息时,row 限定的字段取不到值,而不是退回全文(避免误取)。"""
|
||||
fields = extract_fields("姓名:张三", [FieldSpec("姓名", row=1)], (":", ":"))
|
||||
|
||||
assert fields == {}
|
||||
|
||||
|
||||
def test_include_empty_fills_missing_fields() -> None:
|
||||
fields = extract_fields(
|
||||
"姓名:万镭",
|
||||
[FieldSpec("姓名"), FieldSpec("床号")],
|
||||
(":", ":"),
|
||||
include_empty=True,
|
||||
)
|
||||
|
||||
assert fields == {"姓名": "万镭", "床号": ""}
|
||||
|
||||
|
||||
def test_include_empty_disabled_by_default() -> None:
|
||||
fields = extract_fields("姓名:万镭", [FieldSpec("姓名"), FieldSpec("床号")])
|
||||
|
||||
assert "床号" not in fields
|
||||
|
||||
|
||||
def test_row_validated_in_config() -> None:
|
||||
import pytest
|
||||
|
||||
with pytest.raises(Exception):
|
||||
FieldPreset(name="姓名", row=0)
|
||||
with pytest.raises(Exception):
|
||||
FieldPreset(name="姓名", row=-2)
|
||||
assert FieldPreset(name="姓名", row=3).row == 3
|
||||
assert FieldPreset(name="姓名").row == -1
|
||||
|
||||
|
||||
def test_build_specs_carries_switch_options() -> None:
|
||||
spec = build_specs(
|
||||
[
|
||||
FieldPreset(
|
||||
name="姓名",
|
||||
separator_optional=True,
|
||||
label_linebreak=True,
|
||||
row=2,
|
||||
)
|
||||
]
|
||||
)[0]
|
||||
|
||||
assert spec.separator_optional is True
|
||||
assert spec.label_linebreak is True
|
||||
assert spec.row == 2
|
||||
|
||||
|
||||
def test_default_settings_have_no_presets() -> None:
|
||||
"""配置文件缺失时不应报错,且默认不抽取任何字段。"""
|
||||
assert isinstance(Settings().extract_fields, list)
|
||||
@@ -0,0 +1,39 @@
|
||||
import io
|
||||
import logging
|
||||
|
||||
from ocr_server.logging_setup import APP_LOGGER_NAME, configure_logging
|
||||
|
||||
|
||||
def test_app_logging_survives_root_level_reset() -> None:
|
||||
"""回归测试:Paddle 初始化会把 root logger 降到 WARNING。
|
||||
|
||||
应用日志不能依赖 root 级别,否则「加载引擎」之后的所有 INFO 日志
|
||||
(包括 10/20/50/80/100 进度)都会被静默过滤掉。
|
||||
"""
|
||||
configure_logging("INFO")
|
||||
handler = logging.getLogger(APP_LOGGER_NAME).handlers[0]
|
||||
original_stream = handler.stream
|
||||
stream = io.StringIO()
|
||||
handler.stream = stream
|
||||
original_root_level = logging.getLogger().level
|
||||
try:
|
||||
# 模拟 Paddle 初始化后改动 root logger
|
||||
logging.getLogger().setLevel(logging.WARNING)
|
||||
logging.getLogger("ocr_server.task").info("进度 10% 模型已就绪")
|
||||
finally:
|
||||
handler.stream = original_stream
|
||||
logging.getLogger().setLevel(original_root_level)
|
||||
|
||||
assert "进度 10% 模型已就绪" in stream.getvalue()
|
||||
|
||||
|
||||
def test_configure_logging_keeps_app_logger_level_and_stops_propagation() -> None:
|
||||
configure_logging("DEBUG")
|
||||
app_logger = logging.getLogger(APP_LOGGER_NAME)
|
||||
assert app_logger.level == logging.DEBUG
|
||||
assert app_logger.propagate is False
|
||||
assert len(app_logger.handlers) == 1
|
||||
assert app_logger.handlers[0].terminator == "\n"
|
||||
|
||||
configure_logging("WARNING")
|
||||
assert logging.getLogger(APP_LOGGER_NAME).level == logging.WARNING
|
||||
@@ -60,6 +60,67 @@ def test_queue_completes_task() -> None:
|
||||
assert queue.snapshot(file_id)[3] is not None
|
||||
|
||||
|
||||
def test_resolve_cpu_threads_supports_max_and_int() -> None:
|
||||
import os
|
||||
|
||||
from ocr_server.config import CPU_THREADS_MAX, resolve_cpu_threads
|
||||
|
||||
assert resolve_cpu_threads(4) == 4
|
||||
assert resolve_cpu_threads("max") == max(1, os.cpu_count() or 1)
|
||||
assert resolve_cpu_threads("MAX") == max(1, os.cpu_count() or 1)
|
||||
assert resolve_cpu_threads(CPU_THREADS_MAX) == max(1, os.cpu_count() or 1)
|
||||
|
||||
|
||||
def test_settings_validate_cpu_threads_and_expose_mkldnn() -> None:
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from ocr_server.config import Settings
|
||||
|
||||
assert Settings(cpu_threads=8).cpu_threads_resolved == 8
|
||||
assert Settings(cpu_threads="max").cpu_threads_resolved == max(1, os.cpu_count() or 1)
|
||||
assert Settings(enable_mkldnn=True).enable_mkldnn is True
|
||||
assert Settings(enable_mkldnn=False).enable_mkldnn is False
|
||||
|
||||
with pytest.raises(Exception):
|
||||
Settings(cpu_threads="lots")
|
||||
with pytest.raises(Exception):
|
||||
Settings(cpu_threads=0)
|
||||
|
||||
|
||||
def test_ocr_service_resolves_threads_and_stores_mkldnn() -> None:
|
||||
from ocr_server.ocr import OCRService
|
||||
|
||||
service = OCRService(cpu_threads="max", enable_mkldnn=True)
|
||||
assert service._cpu_threads == max(1, __import__("os").cpu_count() or 1)
|
||||
assert service._enable_mkldnn is True
|
||||
|
||||
plain = OCRService(cpu_threads=3, enable_mkldnn=False)
|
||||
assert plain._cpu_threads == 3
|
||||
assert plain._enable_mkldnn is False
|
||||
|
||||
|
||||
def test_document_rows_groups_by_coordinates() -> None:
|
||||
"""行文本按坐标(bbox 纵向重叠)归并,供字段抽取的 row 选项使用。"""
|
||||
recognized = [
|
||||
{"text": "检查号", "confidence": 0.9, "polygon": [[0, 0], [40, 0], [40, 10], [0, 10]]},
|
||||
{"text": "26100830064", "confidence": 0.9, "polygon": [[45, 0], [120, 0], [120, 10], [45, 10]]},
|
||||
{"text": "姓名", "confidence": 0.9, "polygon": [[0, 20], [40, 20], [40, 30], [0, 30]]},
|
||||
{"text": "万镭", "confidence": 0.9, "polygon": [[45, 20], [90, 20], [90, 30], [45, 30]]},
|
||||
]
|
||||
document = OCRTaskQueue.build_document(recognized)
|
||||
rows = OCRTaskQueue.document_rows(document)
|
||||
|
||||
assert rows == ["检查号26100830064", "姓名万镭"]
|
||||
|
||||
|
||||
def test_document_rows_empty_document() -> None:
|
||||
document = OCRTaskQueue.build_document([])
|
||||
|
||||
assert OCRTaskQueue.document_rows(document) == []
|
||||
|
||||
|
||||
def test_progress_is_monotonic_and_includes_checkpoints() -> None:
|
||||
from ocr_server.ocr import OCRService
|
||||
|
||||
|
||||
Reference in New Issue
Block a user