web-sdk-pp-detection 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,149 @@
1
+ # web-sdk-pp-detection
2
+
3
+ [English](#english) | [在线 Demo](https://chenmohan123.github.io/web-sdk-PP-Detection/)
4
+
5
+ 基于 ONNX Runtime Web 的浏览器端 PP-Detection 版面分析 SDK,支持 PC、移动端与各类 H5 页面。
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ pnpm add web-sdk-pp-detection
11
+ ```
12
+
13
+ 也可以使用 `npm install web-sdk-pp-detection`。
14
+
15
+ ## 快速开始
16
+
17
+ ```ts
18
+ import { createPPDetection } from "web-sdk-pp-detection";
19
+
20
+ const detector = await createPPDetection({
21
+ model: "https://models.example.com/pp-detection/manifest.json",
22
+ backend: "auto",
23
+ precision: "auto"
24
+ });
25
+ const result = await detector.detect(file, {
26
+ threshold: 0.5,
27
+ classThresholds: {
28
+ formula: 0.4,
29
+ table: 0.55,
30
+ text: 0.6
31
+ }
32
+ });
33
+ console.log(result.detections, result.runtime, result.timings);
34
+ await detector.dispose();
35
+ ```
36
+
37
+ `classThresholds` 按 manifest 标签名称覆盖置信度过滤阈值,未配置的类别回退到 `threshold`。全局 `threshold` 仍用于 mask 二值化和多边形提取。未知类别名称或超出 `0` 到 `1` 的值会被拒绝。
38
+
39
+ 当前发布版本内置 PicoDet 1.0.1 FP32 stable manifest;使用默认模型时可以省略 `model`。自定义模型仍应传入经过验证的 runtime manifest 或清单对象。
40
+
41
+ 模型初始化耗时可以通过 `detector.loadTimings` 查看。`totalMs` 是初始化总耗时,同时提供 `modelDownloadMs`(网络下载)、`modelCacheReadMs`(缓存读取)、`integrityMs`(SHA-256 完整性校验)和 `sessionMs`(ONNX Runtime Session 创建)。发布版本内置 PicoDet 1.0.1 FP32 stable manifest;使用自定义模型时请传入已验证的 runtime manifest 或清单对象。
42
+
43
+ ## 运行后端与精度
44
+
45
+ - `backend: "auto"` 优先使用 WebGPU;只有显式设置 `allowFallback: true` 时,WebGPU 会话失败才会尝试 WASM(CPU)。也可手动指定 `"webgpu"` 或 `"wasm"`。
46
+ - `precision: "auto"` 选择清单中可用的默认稳定精度;当前默认 PicoDet 仅验证 FP32,因此不会臆测切换到 FP16。已验证其他精度时可手动指定 `"fp16"`、`"int8"` 等。
47
+ - 默认 PicoDet 1.0.1 FP32 已发布可下载的 stable ONNX 资产,并通过 Linux WASM 与 Windows NVIDIA WebGPU 七张 fixture 验证;FP16、INT8、INT4 和 FP8 仍需独立证据。
48
+ - 使用默认模型时,显式请求清单中未声明的组合会抛出 `CAPABILITY_UNSUPPORTED`,不会改写无效组合;自定义清单可在单独验证后声明其他组合。上游模型是 float32,不支持 FP64;FP32 约为 FP16 两倍大小并可能更慢、更占显存。
49
+ - `detect` 可接收图片 Blob/File、Canvas/ImageData、`HTMLVideoElement` 或单帧 `VideoFrame`。摄像头和视频播放的权限、帧率控制由宿主页面负责;每次提交一帧后应等待 Promise 完成,并在停止媒体时调用 `dispose()`。
50
+
51
+ ## 自定义模型
52
+
53
+ 通过 `model` 传入微调模型的 Custom manifest URL 或 manifest 对象。自定义 manifest 必须遵循仓库中的模型契约,并为每个模型文件提供大小、SHA-256、精度及后端兼容信息。
54
+
55
+ ```ts
56
+ const detector = await createPPDetection({
57
+ model: "https://models.example.com/pp-detection/manifest.json"
58
+ });
59
+ ```
60
+
61
+ ## 资源管理
62
+
63
+ 模型加载进度通过 `onProgress` 回调提供。`phase: "model"`、`status: "progress"` 事件中的 `loadedBytes` 和可选的 `totalBytes` 仅表示网络下载字节,不代表完整初始化百分比,也不包含完整性校验或 Session 创建。响应没有 `Content-Length` 时 `totalBytes` 可能缺失,缓存、内存或自定义二进制模型也可能不产生字节进度。可捕获结构化的 `PPDetectionError` 并读取 `code` 与 `details`。SDK 支持模型缓存;可以通过 detector 的缓存方法查询或清理。使用结束后必须调用 `dispose()` 释放 Worker、ONNX Runtime session 与 GPU/CPU 资源。
64
+
65
+ ## 微信环境
66
+
67
+ 支持微信公众号页面以及微信内嵌浏览器中的 H5/WebView 集成。当前不宣称支持 native Mini Program 原生小程序直接推理;原生小程序需要通过 WebView 承载 H5 页面或使用服务端推理。
68
+
69
+ ## 完整文档
70
+
71
+ - [中文文档](https://github.com/chenmohan123/web-sdk-PP-Detection#readme)
72
+ - [English documentation](https://github.com/chenmohan123/web-sdk-PP-Detection/blob/main/README.en.md)
73
+ - [在线 Demo](https://chenmohan123.github.io/web-sdk-PP-Detection/)
74
+ - [示例目录](https://github.com/chenmohan123/web-sdk-PP-Detection/tree/main/examples)
75
+
76
+ ## English
77
+
78
+ A browser-first PP-Detection document layout analysis SDK powered by ONNX Runtime Web for desktop, mobile, and H5 pages.
79
+
80
+ ### Installation
81
+
82
+ ```bash
83
+ pnpm add web-sdk-pp-detection
84
+ ```
85
+
86
+ `npm install web-sdk-pp-detection` is also supported.
87
+
88
+ ### Quick start
89
+
90
+ ```ts
91
+ import { createPPDetection } from "web-sdk-pp-detection";
92
+
93
+ const detector = await createPPDetection({
94
+ model: "https://models.example.com/pp-detection/manifest.json",
95
+ backend: "auto",
96
+ precision: "auto"
97
+ });
98
+ const result = await detector.detect(file, {
99
+ threshold: 0.5,
100
+ classThresholds: {
101
+ formula: 0.4,
102
+ table: 0.55,
103
+ text: 0.6
104
+ }
105
+ });
106
+ console.log(result.detections, result.runtime, result.timings);
107
+ await detector.dispose();
108
+ ```
109
+
110
+ `classThresholds` overrides confidence filtering for matching manifest label names and falls back to `threshold` for unspecified classes. The global `threshold` still controls mask binarization and polygon extraction. Unknown class names and values outside `0` through `1` are rejected.
111
+
112
+ The release includes a built-in PicoDet 1.0.1 FP32 stable manifest, so `model` may be omitted for the default model. Pass a verified runtime or custom manifest when using another model.
113
+
114
+ Detailed initialization timings are available through `detector.loadTimings`. `totalMs` is the full initialization duration. The additive fields `modelDownloadMs`, `modelCacheReadMs`, `integrityMs`, and `sessionMs` separate network download, cache reads, SHA-256 verification, and Session creation. The release includes the PicoDet 1.0.1 FP32 stable manifest; pass a verified runtime or custom model manifest when using another model.
115
+
116
+ ### Backend and precision
117
+
118
+ - `backend: "auto"` prefers WebGPU; only `allowFallback: true` permits a failed WebGPU session to try WASM (CPU). Use `"webgpu"` or `"wasm"` for an explicit choice.
119
+ - `precision: "auto"` selects the available default stable precision from the manifest. The default PicoDet 1.0.1 validates FP32 only, so the SDK does not guess an FP16 switch. When another precision has been validated, select it explicitly with `"fp16"`, `"int8"`, and so on.
120
+ - The default PicoDet 1.0.1 FP32 variant is a downloadable stable ONNX asset and has passed seven-fixture validation on Linux WASM and Windows NVIDIA WebGPU. FP16, INT8, INT4, and FP8 still require independent release-source and backend evidence.
121
+ - `detect` accepts image Blob/File, Canvas/ImageData, `HTMLVideoElement`, or a single `VideoFrame`. Hosts own camera/video permissions and frame pacing; await each frame Promise and call `dispose()` when media stops.
122
+ - Explicit pairs absent from the default manifest throw `CAPABILITY_UNSUPPORTED` instead of rewriting an invalid pair. The upstream model is float32, not FP64; FP64 inference is unsupported. FP32 is about twice the size of FP16 and may be slower or use more GPU memory.
123
+
124
+ ### Custom models
125
+
126
+ Pass a fine-tuned model's Custom manifest URL or manifest object through `model`. Each manifest variant must declare its byte size, SHA-256 digest, precision, and compatible backends.
127
+
128
+ ```ts
129
+ const detector = await createPPDetection({
130
+ model: "https://models.example.com/pp-detection/manifest.json"
131
+ });
132
+ ```
133
+
134
+ ### Resource management
135
+
136
+ Use `onProgress` for model loading progress. On `phase: "model"`, `status: "progress"` events, `loadedBytes` and the optional `totalBytes` describe network-transfer bytes only; they are not an overall initialization percentage and exclude integrity verification and Session creation. `totalBytes` can be absent without a `Content-Length` response header, while cache, memory, or custom binary model sources may emit no byte progress. Structured failures are exposed as `PPDetectionError` with `code` and `details`. Model cache entries can be listed or cleared through the detector. Always call `dispose()` when finished to release the Worker, ONNX Runtime session, and GPU/CPU resources.
137
+
138
+ ### WeChat environments
139
+
140
+ WeChat official-account pages and other H5/WebView integrations are supported. Native Mini Program inference is not claimed; a native Mini Program should host the H5 experience in a WebView or use server-side inference.
141
+
142
+ ### Documentation
143
+
144
+ - [Chinese documentation](https://github.com/chenmohan123/web-sdk-PP-Detection#readme)
145
+ - [English documentation](https://github.com/chenmohan123/web-sdk-PP-Detection/blob/main/README.en.md)
146
+ - [Live Demo](https://chenmohan123.github.io/web-sdk-PP-Detection/)
147
+ - [Examples](https://github.com/chenmohan123/web-sdk-PP-Detection/tree/main/examples)
148
+
149
+ Apache-2.0