IdCardOcr 0.1.0

dotnet add package IdCardOcr --version 0.1.0
                    
NuGet\Install-Package IdCardOcr -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="IdCardOcr" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="IdCardOcr" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="IdCardOcr" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add IdCardOcr --version 0.1.0
                    
#r "nuget: IdCardOcr, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package IdCardOcr@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=IdCardOcr&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=IdCardOcr&version=0.1.0
                    
Install as a Cake Tool

IdCardOcr

English

IdCardOcr 是一个纯 C# 的中国居民身份证 OCR 库,可从身份证照片和扫描件中识别文字行,并提取正反面字段。项目仅使用 CPU 推理,无需原生依赖。核心库提供 .NET 8 和 .NET 10 构建,模型包兼容 .NET 8 及以上版本,图像解码包 IdCardOcr.Imaging 需要 .NET 10。

项目从 SimdPaddleOCR 提取身份证识别所需的组件,使用经过手工 SIMD 优化的托管 SimdOnnx 引擎运行 PP-OCRv6 模型。组件取舍见上游来源说明。

项目包

项目 内容
IdCardOcr 核心库,包含推理引擎、OCR 流程、IdCardReader、字段解析器和约 1 MB 的文字行方向模型,无外部依赖。
IdCardOcr.Models.Tiny 内置 PP-OCRv6 tiny 文本检测和识别模型(约 6.3 MB),速度快,适合清晰的扫描件和照片。
IdCardOcr.Models.Small 内置 PP-OCRv6 small 文本检测和识别模型(约 31 MB),更适合模糊、反光、光线不足或含生僻字的图像。
IdCardOcr.Imaging 可选图像解码包,通过纯托管的 Sdcb.SimdCodecs(MIT 许可证)解码 JPEG、PNG、WebP 等格式,并处理 EXIF 方向信息。需要 .NET 10。

使用方法

以下命令安装 Tiny 模型和图像解码包,核心库会随依赖安装。使用 Small 模型时,将第一行的包名换成 IdCardOcr.Models.Small。如果自行解码图像,只需安装模型包。

dotnet add package IdCardOcr.Models.Tiny
dotnet add package IdCardOcr.Imaging
using IdCardOcr;
using IdCardOcr.Models;

// 也可使用 SmallModel.Default。读取器支持并发调用,建议创建一次后复用。
using var reader = new IdCardReader(TinyModel.Default);

IdCardOcrResult r = reader.ReadFile("card.jpg"); // 需要 IdCardOcr.Imaging
// 如果自行解码图像,可以直接传入像素数据。
// IdCardOcrResult r = reader.Read(pixels, width, height, stride, IdCardPixelFormat.Bgr24);

if (r.Front is { } f)
    Console.WriteLine($"{f.Name} {f.Sex} {f.Ethnicity} {f.BirthDate} {f.Address} {f.IdNumber} 号码校验通过={f.IdNumberValid}");
if (r.Back is { } b)
    Console.WriteLine($"{b.IssuingAuthority} {b.ValidFrom}–{(b.IsLongTerm ? "长期" : b.ValidUntil?.ToString())}");

// 原始文字行按阅读顺序排列,每行包含四角坐标和置信度。
foreach (TextLine line in r.Lines)
    Console.WriteLine($"{line.Confidence:F2} {line.Text}");

通过 IdCardReaderOptions 可以关闭自动旋转(AutoRotate),调整检测输入长边上限(DetectionMaxSide,默认 960 像素),限制文字行识别的并行度(MaxParallelLines),或跳过字段解析(ParseFields)。

运行命令行示例时传入一个或多个图片路径。默认使用 tiny 模型,加入 --small 可切换至 small 模型。

dotnet run -c Release --project samples/IdCardOcr.Cli -- card.jpg

字段解析与方向校正

  • 根据文字的主方向校正倾斜,再合并同一视觉行上的检测框。字段标签和值即使被检测为两个框也能合并,多行住址会拼接成一个字段。
  • 提取字段时会忽略斜向或尺寸过大的覆盖文字,例如印章以及「样本」「仅供…使用」水印。
  • 按 GB 11643 校验公民身份号码的校验码(IdNumberValid),并纠正常见的字母与数字混淆,例如把 O 改为 0、把 I 或 l 改为 1。性别或出生日期缺失时,可从通过校验的号码中补全。
  • 可识别旋转了 90°、180° 或 270° 的证件,无需额外模型。首轮识别会转正竖长文字框,并结合文字框的形状和方向模型的翻转结果推断整张证件的朝向。证件转正后会再执行一轮 OCR。若第二轮发现 90° 与 270° 判反,会再旋转 180° 并执行第三轮。Rotation 记录证件在输入图像中的顺时针旋转角度。
  • 可处理约 ±15° 的轻微倾斜。约 30° 至 60° 的大角度倾斜超出当前处理范围。

平台与性能

  • x86-64 支持 AVX-512、AVX2+FMA 和 SSE,运行时根据 CPU 能力选择实现,并可回退到 Vector<T> 或标量实现。
  • ARM64 目前处于预览阶段。项目保留了上游的 NEON 内核,相关 CI 测试结果仅供参考。
  • 支持 Windows、Linux 和 macOS,无需原生二进制文件,兼容 .NET 裁剪和 AOT 编译。

下表为支持 AVX2 的笔记本 CPU 在 WSL2 上的实测结果。测试使用 1568×1003 像素的合成证件图,识别耗时在模型加载后单独测量。

操作 tiny small
加载模型 约 0.2 秒 约 0.15–0.3 秒
识别人像面 约 70–80 毫秒 约 115–130 毫秒

macOS ARM64 基准

Apple M4 Max(16 核)、macOS 26.6.2,使用原生 ARM64 Release 构建和默认 OCR 配置(检测长边上限 960 像素、自动方向校正、字段解析)。输入为 tests/data/generated/front_scan.png(1568×1003 像素)。JIT 使用 .NET 10.0.12(SDK 10.0.401),NativeAOT 使用 .NET 10.0.15(SDK 10.0.404)。

每种模型在 10 个独立的 CLI 进程中各构造一次 IdCardReader,取加载耗时的中位数。识别在另一个进程中复用同一读取器,预热 10 次后连续测量 50 次。识别耗时取自 IdCardOcrResult.Elapsed,计时范围从输入已解码的像素到字段解析完成。

运行方式 操作 tiny small
JIT 模型加载中位数 118 ms 123.5 ms
JIT 人像面识别中位数 25.5 ms 59 ms
JIT 人像面识别 P95 33 ms 82 ms
NativeAOT 模型加载中位数 3 ms 8 ms
NativeAOT 人像面识别中位数 24 ms 59 ms
NativeAOT 人像面识别 P95 27 ms 68 ms

测试

dotnet test --project tests/IdCardOcr.Tests -c Release
# 关闭硬件内在函数,测试回退路径。
DOTNET_EnableHWIntrinsic=0 dotnet test --project tests/IdCardOcr.Tests -c Release

测试包含解析器单元测试和端到端测试,覆盖平整扫描件、倾斜照片、暗光照片、国徽面、倒置证件、像素格式及并发调用。tests/data/generated 下的证件图均为带水印的合成图片,个人信息均为虚构。

许可证

项目采用 Apache-2.0 许可证。模型和上游项目的归属信息见第三方声明。

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on IdCardOcr:

Package Downloads
IdCardOcr.Imaging

IdCardOcr 的可选图像解码包。通过纯托管的 Sdcb.SimdCodecs 解码 JPEG、PNG 等格式,并处理 EXIF 方向信息。

IdCardOcr.Models.Small

IdCardOcr 的 PP-OCRv6 small 模型包,内置中文文本检测和识别模型。

IdCardOcr.Models.Tiny

IdCardOcr 的 PP-OCRv6 tiny 模型包,内置中文文本检测和识别模型。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 59 12/1/2026