astromandarin commited on
Commit
a9aab5c
·
verified ·
1 Parent(s): 20fc05e

Card: crops of real lines with their sources, whole-page reading (read_page, --page), what was taken from han2ids

Browse files
.gitattributes CHANGED
@@ -39,3 +39,4 @@ figures/examples.png filter=lfs diff=lfs merge=lfs -text
39
  figures/ids.png filter=lfs diff=lfs merge=lfs -text
40
  figures/rare-characters.png filter=lfs diff=lfs merge=lfs -text
41
  figures/real-papers.png filter=lfs diff=lfs merge=lfs -text
 
 
39
  figures/ids.png filter=lfs diff=lfs merge=lfs -text
40
  figures/rare-characters.png filter=lfs diff=lfs merge=lfs -text
41
  figures/real-papers.png filter=lfs diff=lfs merge=lfs -text
42
+ figures/real-lines.png filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -29,7 +29,7 @@ tags:
29
  ## 它读什么,不读什么
30
 
31
  - **读**:论文、著作里排印出来的一行横排文字(正文、脚注),包括其中的扩展区汉字、隶定字、造字。
32
- - **不读整页**:要先把页面切成行。测量用的论文行高约 100–150 像素,专项测试的行高约 96 像素。
33
  - **不读原始材料**:甲骨、金文、简帛的照片和拓片不在它的范围里,它读的是论著里印出来的字。
34
  - 竖排、手写没有测过。高大于宽的图被当作竖排,不开 IDS,只按 OpenGlyph-OCR-0 读。
35
 
@@ -101,6 +101,7 @@ cd OpenGlyph-OCR-IDS
101
  pip install -r requirements.txt
102
  python -m openglyph_ocr_ids line.png # 每行图输出一行 JSON
103
  python -m openglyph_ocr_ids line.png --options # 连同第二选项
 
104
  ```
105
 
106
  ```python
@@ -110,11 +111,14 @@ ocr = load() # 本仓库所在的目录
110
  out = read_line(ocr, "line.png")
111
  print(out["text"])
112
  print(atoms(out["text"])) # [('text', '其'), ('glyph', 'u2ff0-u79c0-u6208'), ('ids', '⿰…'), …]
 
 
 
113
  ```
114
 
115
  - **只有 MLX 版。** 读一行要四套权重轮流上场,还要按图把字切成部件再读,这一整套是在 MLX 上写、也在 MLX 上量的,暂时没有 PyTorch 版。只要 OpenGlyph-OCR-0 那一部分的话,它有 `transformers` 版。
116
  - 版本要照 `requirements.txt` 装(mlx 0.32.3、mlx-vlm 0.3.12、transformers 5.18.0、tokenizers 0.23.2)。底模是 16 位浮点,换了版本算出来的数会有细微出入,下面的成绩就不一定原样重现。torch 和 torchvision 只用来预处理图像(装了它们,图像的预处理才与测量时逐像素相同)。
117
- - **发布前核对过**:这个文件夹里的代码搭出的模型,每个数组、每张表都与测量所用的模型逐位相同(1,330 项);拿它重读测试里的 190 行,每行的输出、每个拼字处的记录都与测量时的读法相同,另有 32 行连同第二选项也相同。
118
 
119
  ## 成绩
120
 
@@ -135,6 +139,21 @@ print(atoms(out["text"])) # [('text', '其'), ('glyph', 'u2ff0-u79c0
135
 
136
  注:论文里含难字的片段:严格按写出的字符串算,OpenGlyph-OCR-IDS 整段全对 275 段,字错率 1.94%。两者逐行相比,论文整行里只有 OpenGlyph-OCR-IDS 读对的 21 行,只有 OpenGlyph-OCR-0 读对的 6 行;论文里含难字的片段里只有 OpenGlyph-OCR-IDS 读对的 24 段,只有 OpenGlyph-OCR-0 读对的 0 段;一本书的扫描页里只有 OpenGlyph-OCR-IDS 读对的 7 行,只有 OpenGlyph-OCR-0 读对的 1 行。
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  ### 生僻字
139
 
140
  ![生僻字读对的比例](figures/rare-characters.png)
@@ -205,7 +224,7 @@ print(atoms(out["text"])) # [('text', '其'), ('glyph', 'u2ff0-u79c0
205
  - **慢**,见上。
206
  - 拼字只认 `⿰⿱⿲⿳⿴⿵⿶⿷⿸⿹⿺⿻` 十二个结构符。
207
  - 什么时候开 IDS、拼出的 IDS 采用与否,由几道门槛决定(`reading.json`)。门槛是在另一批调试用的行上选的,从没看过测试;换一类材料,最合适的门槛未必相同。
208
- - 整页读只试过一页,没有当成本事来量。
209
 
210
  ## 它是怎么来的
211
 
@@ -220,6 +239,18 @@ print(atoms(out["text"])) # [('text', '其'), ('glyph', 'u2ff0-u79c0
220
 
221
  训练开口的那一套,用的是程序排印的拼字行,加上 OpenGlyph-OCR-0 训练时用过的几类行,以守住旧有的本事(其中 3,000 行是从 26 篇论文电子版裁出的真实行);拆字读部件的用同一批行,部件按排字程序的记录切开,另把行里一些有码位的字也切开来练习。拼字的只用程序排印的行:用部件拼出的字(含左右、上下、包围、三部件、嵌套)嵌在论文的句子里,GlyphWiki 上按 IDS 命名的字形,以及按部件拼写的生僻字,先后训练了七轮。测试用的四篇论文和那本书都不在任何一套的训练材料里;专项测试里那 300 个只能写成 IDS 的字,是在全部训练用的拼字之外另造的,也没有在训练行里出现过。本仓库不含任何训练或测试数据。
222
 
 
 
 
 
 
 
 
 
 
 
 
 
223
  ## 仓库里有什么
224
 
225
  | 文件 | 内容 |
@@ -234,7 +265,7 @@ print(atoms(out["text"])) # [('text', '其'), ('glyph', 'u2ff0-u79c0
234
  | `token-counts.json` | 每个字要底模分词器的几个记号(据此分常用字与生僻字) |
235
  | `reading.json` | 开口与采用的门槛(测量时所用的) |
236
  | `openglyph_ocr_ids/` | 读一行的代码(MLX) |
237
- | `figures/` | 本页的图 |
238
  | `requirements.txt` | 测量时所用的各库版本 |
239
 
240
  另有 `release.json`,记着每个文件的 SHA-256 和来历。
@@ -249,6 +280,8 @@ print(atoms(out["text"])) # [('text', '其'), ('glyph', 'u2ff0-u79c0
249
 
250
  **What it is.** The next version of [OpenGlyph-OCR-0](https://huggingface.co/OpenGlyph/OpenGlyph-OCR-0), an OCR model for single lines of printed Chinese scholarly text dense with rare characters (Xiaomi-OCR-0, 0.8B, with LoRA, a retrained vision tail and 159,052 added output tokens). It adds two things. (1) A character that has neither a code point nor a GlyphWiki glyph is written as an Ideographic Description Sequence, `<ids>⿰…</ids>`. (2) Wherever the reader writes a rare character or a glyph token, the glyph is also spelt from its parts as an IDS; where that spelling is sure enough, the character it spells is written (an IDS that spells an encoded character becomes that character). On the 398 whole lines of four papers that gave no training line, lines read exactly right rise from 332 (OpenGlyph-OCR-0) to 347 and the character error rate falls from 0.90% to 0.70%; on 313 pieces of those papers' lines that hold hard characters (4 to 16 characters each), pieces read exactly right rise from 252 to 276 and the error rate falls from 2.89% to 1.88%.
251
 
 
 
252
  **How it reads a line.** Four sets of weights in one model: the reader (OpenGlyph-OCR-0, unchanged) writes the line; the opener (14 added tokens and rank-8 LoRA in language layers 18–23) says where an IDS is due; at such a place, and wherever the reader itself writes a rare character or a glyph token, the glyph is read two more ways — unfolded (located by the reader's attention, cut into parts by a program, the parts read by a rank-8 LoRA part reader) and spelt whole (the speller: rank-64 LoRA in language layers 18–23 and in all twelve vision blocks and the merger, its own structure-token rows and per-step part rows, about 34.8 million weights). An IDS both routes write alike stands; otherwise the speller's stands if it is sure enough by a gate set per kind of place; otherwise the reader's own character stands. The gates were chosen on tuning lines, never on a test. With `options=True` the reading also returns a second option at every place that has one (the reading not taken where the routes were asked; the reader's second choice where its lead is under 4 logits); the text is the same with and without.
253
 
254
  **Measured** (November 2026, MLX form; "base" is Xiaomi-OCR-0 as released, with its own prompt; an IDS that draws exactly the answer's glyph counts as right — strict counts are given in the Chinese tables where they differ):
@@ -267,6 +300,10 @@ print(atoms(out["text"])) # [('text', '其'), ('glyph', 'u2ff0-u79c0
267
 
268
  **Known defects.** Nested composites are rarely right (2 of 54 on the special test), surrounds 8 of 29, three-part 7 of 22; composites with a part never seen in training 18 of 52. It sometimes spells a GlyphWiki glyph it has a token for as a wrong IDS (ebag-family glyphs 22 / 60 against OpenGlyph-OCR-0's 42 / 60; scanned-page glyphs 14 / 27 against 16 / 27), and leaves stray IDS where no composite is (57 on the special test; on the real sets 4 in the 398 whole lines of four papers, 3 in the 313 pieces of lines with hard characters, 3 in the 93 lines of four scanned book pages). It is several times slower than OpenGlyph-OCR-0. Only the MLX form exists (the reading runs four weight sets and cuts glyphs with a program; it was written and measured in MLX); OpenGlyph-OCR-0 alone has a `transformers` form.
269
 
270
- **Before release** the package in this repository was checked against the code that was measured: every array and table of the model it builds is bit for bit the measured one (1,330 entries), and 190 test lines read with it come out exactly as they were measured, every note of every place included (32 more with the second options).
 
 
 
 
271
 
272
  **Licence.** Apache-2.0. `base/` is Xiaomi-OCR-0, redistributed unchanged under its Apache-2.0 licence. `canon.json` is derived from CJKVI IDS (from CHISE IDS, under CHISE's terms, GPLv2), BabelStone's IDS table (free for any use) and GlyphWiki data, and is used under the terms of those sources. Not a product of Xiaomi or of the authors of Xiaomi-OCR-0, and not endorsed by them.
 
29
  ## 它读什么,不读什么
30
 
31
  - **读**:论文、著作里排印出来的一行横排文字(正文、脚注),包括其中的扩展区汉字、隶定字、造字。
32
+ - **整页也能读,但推荐一行一行读。** 我们拿一页排印的论文试过(单栏横排,22 行,691 项,行内有一处古文字摹形插图;答案是逐字对着原图录的,凡读法有出入处都放大原图核过):整页一次读完,错 8 项(1.16%),两次测得用时 33 秒和 16 秒;切成 22 行一行行读,错 5 项(0.72%),13 个生僻字全对。整页读时,拼字的两条路用不上——它们按一行里的位置找字,整页上找不准——所以整页读出来的就是 OpenGlyph-OCR-0 的读法;要用上 IDS 和按部件拼生僻字,就切成行读。整页读用 `read_page`(见下文),不写换行。只试过这一页,多栏、表格、竖排的页面没有试过。测量用的论文行高约 100–150 像素,专项测试的行高约 96 像素。
33
  - **不读原始材料**:甲骨、金文、简帛的照片和拓片不在它的范围里,它读的是论著里印出来的字。
34
  - 竖排、手写没有测过。高大于宽的图被当作竖排,不开 IDS,只按 OpenGlyph-OCR-0 读。
35
 
 
101
  pip install -r requirements.txt
102
  python -m openglyph_ocr_ids line.png # 每行图输出一行 JSON
103
  python -m openglyph_ocr_ids line.png --options # 连同第二选项
104
+ python -m openglyph_ocr_ids page.png --page # 整页一次读(按 OpenGlyph-OCR-0 的读法,不写 IDS)
105
  ```
106
 
107
  ```python
 
111
  out = read_line(ocr, "line.png")
112
  print(out["text"])
113
  print(atoms(out["text"])) # [('text', '其'), ('glyph', 'u2ff0-u79c0-u6208'), ('ids', '⿰…'), …]
114
+
115
+ page = read_page(ocr, "page.png") # 整页一次读;切成行用 read_line 读,效果更好
116
+ print(page["text"], page["complete"]) # complete 为 False 时,加大 max_tokens(默认 2048)
117
  ```
118
 
119
  - **只有 MLX 版。** 读一行要四套权重轮流上场,还要按图把字切成部件再读,这一整套是在 MLX 上写、也在 MLX 上量的,暂时没有 PyTorch 版。只要 OpenGlyph-OCR-0 那一部分的话,它有 `transformers` 版。
120
  - 版本要照 `requirements.txt` 装(mlx 0.32.3、mlx-vlm 0.3.12、transformers 5.18.0、tokenizers 0.23.2)。底模是 16 位浮点,换了版本算出来的数会有细微出入,下面的成绩就不一定原样重现。torch 和 torchvision 只用来预处理图像(装了它们,图像的预处理才与测量时逐像素相同)。
121
+ - **发布前核对过**:这个文件夹里的代码搭出的模型,每个数组、每张表都与测量所用的模型逐位相同(1,330 项);拿它重读测试里的 190 行,每行的输出、每个拼字处的记录都与测量时的读法相同,另有 32 行连同第二选项也相同;整页试读的那一页,用它整页读,也与试读时一字不差。
122
 
123
  ## 成绩
124
 
 
139
 
140
  注:论文里含难字的片段:严格按写出的字符串算,OpenGlyph-OCR-IDS 整段全对 275 段,字错率 1.94%。两者逐行相比,论文整行里只有 OpenGlyph-OCR-IDS 读对的 21 行,只有 OpenGlyph-OCR-0 读对的 6 行;论文里含难字的片段里只有 OpenGlyph-OCR-IDS 读对的 24 段,只有 OpenGlyph-OCR-0 读对的 0 段;一本书的扫描页里只有 OpenGlyph-OCR-IDS 读对的 7 行,只有 OpenGlyph-OCR-0 读对的 1 行。
141
 
142
+ ### 真实论文里的几行
143
+
144
+ ![真实论文里的几行](figures/real-lines.png)
145
+
146
+ 图中六行的出处(截图只用来说明模型读得怎样,著作权归原作者和出版者):
147
+
148
+ 1. 何義軍:《戰國陶文綜合研究》,吉林大學博士學位論文,2023 年 12 月,第 192 頁。
149
+ 2. 陈晶晶:《清华简典型楚文字及相关问题研究》,河北大学博士学位论文,2025 年 5 月,第 36 页。
150
+ 3. 沈奇石:《「隼」字異讀考論》,《臺大中文學報》第 84 期,2024 年 3 月,第 27 頁(全文第 1–36 頁),DOI:10.6281/NTUCL.202403_(84).0001。
151
+ 4. 鄔可晶:《甲骨金文語文論稿》,上海:上海古籍出版社,2024 年 9 月,第 1 頁(書頁掃描)。
152
+ 5. 同上,第 4 頁(書頁掃描)。
153
+ 6. 何義軍:《戰國陶文綜合研究》,第 514 頁。
154
+
155
+ 测试用的四篇论文,除上面三篇外,还有一篇 2025 年的学位论文;我们不知道��是否已经公开,这里不列名,也不用它的截图。那本书的四页扫描,是《甲骨金文語文論稿》的第 1–4 頁。
156
+
157
  ### 生僻字
158
 
159
  ![生僻字读对的比例](figures/rare-characters.png)
 
224
  - **慢**,见上。
225
  - 拼字只认 `⿰⿱⿲⿳⿴⿵⿶⿷⿸⿹⿺⿻` 十二个结构符。
226
  - 什么时候开 IDS、拼出的 IDS 采用与否,由几道门槛决定(`reading.json`)。门槛是在另一批调试用的行上选的,从没看过测试;换一类材料,最合适的门槛未必相同。
227
+ - 整页读只试过一页(见上文):整页读时用不上拼字,比切成行读多错几处。
228
 
229
  ## 它是怎么来的
230
 
 
239
 
240
  训练开口的那一套,用的是程序排印的拼字行,加上 OpenGlyph-OCR-0 训练时用过的几类行,以守住旧有的本事(其中 3,000 行是从 26 篇论文电子版裁出的真实行);拆字读部件的用同一批行,部件按排字程序的记录切开,另把行里一些有码位的字也切开来练习。拼字的只用程序排印的行:用部件拼出的字(含左右、上下、包围、三部件、嵌套)嵌在论文的句子里,GlyphWiki 上按 IDS 命名的字形,以及按部件拼写的生僻字,先后训练了七轮。测试用的四篇论文和那本书都不在任何一套的训练材料里;专项测试里那 300 个只能写成 IDS 的字,是在全部训练用的拼字之外另造的,也没有在训练行里出现过。本仓库不含任何训练或测试数据。
241
 
242
+ ## 对 han2ids 的借鉴
243
+
244
+ [han2ids](https://github.com/mkpoli/han2ids)(mkpoli,MIT 许可,模型在 [Hugging Face](https://huggingface.co/mkpoli))是一个只读一个字的图、把它写成 IDS 的小模型:约 3,500 万个参数,ViT 编码器加四层解码器,在九万多个有码位的字的字图上训练,标签是 BabelStone 的 IDS。2026 年 11 月我们读过它的设计,也在我们的测试上试过它。借鉴的是这几点:
245
+
246
+ 1. **部件要有自己的输出。** han2ids 给每个部件一个自己的记号;我们的模型写部件时用的是整字的输出行,不少部件要两三个记号才写得出来。我们因此给拼字的加了部件的输出行:为写部件的每一步另学一行,只在拼 IDS 时起作用(见上文“它是怎么来的”第 4 条)。
247
+ 2. **可训练的分量。** han2ids 的全部权重都参与训练;我们当时的拼字只有约 260 万个 LoRA 权重。对比之后,拼字的 LoRA 秩从 8 提到了 64(约 2,080 万个权重)。
248
+ 3. **怎样算写对。** han2ids 的评测把同一方向上的嵌套看作同一个结构(⿱⿱ABC 和 ⿳ABC 画出来相同);我们后来定下的计法——画出来是同一个字就算对——与此一致。
249
+
250
+ 此外,我们拿它当过一回尺子:把我们测试里的拼字按框一个个切出来给它读(它只读单字,要先告诉它字在哪里),和我们的模型在整行里自己找字、自己拼相比。当时的一版拼字,部件都练过的 600 个字,han2ids 写对 238 个,我们 272 个;带有训练里从未出现的部件的 400 个字,han2ids 232 个,我们 132 个。在没见过的部件上差距很大,这也是本模型现在的毛病之一。
251
+
252
+ 我们没有用它的代码、权重或数据,本仓库里没有 han2ids 的任何东西。两者设计上的根本不同在于:han2ids 读一个字,要给框;我们的模型读整行,自己决定哪里该写 IDS,其余照常写字。
253
+
254
  ## 仓库里有什么
255
 
256
  | 文件 | 内容 |
 
265
  | `token-counts.json` | 每个字要底模分词器的几个记号(据此分常用字与生僻字) |
266
  | `reading.json` | 开口与采用的门槛(测量时所用的) |
267
  | `openglyph_ocr_ids/` | 读一行的代码(MLX) |
268
+ | `figures/` | 本页的图(其中真实论文的截图出处见上文) |
269
  | `requirements.txt` | 测量时所用的各库版本 |
270
 
271
  另有 `release.json`,记着每个文件的 SHA-256 和来历。
 
280
 
281
  **What it is.** The next version of [OpenGlyph-OCR-0](https://huggingface.co/OpenGlyph/OpenGlyph-OCR-0), an OCR model for single lines of printed Chinese scholarly text dense with rare characters (Xiaomi-OCR-0, 0.8B, with LoRA, a retrained vision tail and 159,052 added output tokens). It adds two things. (1) A character that has neither a code point nor a GlyphWiki glyph is written as an Ideographic Description Sequence, `<ids>⿰…</ids>`. (2) Wherever the reader writes a rare character or a glyph token, the glyph is also spelt from its parts as an IDS; where that spelling is sure enough, the character it spells is written (an IDS that spells an encoded character becomes that character). On the 398 whole lines of four papers that gave no training line, lines read exactly right rise from 332 (OpenGlyph-OCR-0) to 347 and the character error rate falls from 0.90% to 0.70%; on 313 pieces of those papers' lines that hold hard characters (4 to 16 characters each), pieces read exactly right rise from 252 to 276 and the error rate falls from 2.89% to 1.88%.
282
 
283
+ **Whole pages.** A whole page can be read in one go (`read_page`, or `--page` on the command line), but line by line reads better. On the one page tried (a printed paper page, single column, 22 lines, 691 items), the page read whole had 8 items wrong (1.16%), the same page cut into its 22 lines 5 (0.72%). Read whole, the IDS routes are not used — they find a glyph by its place within one line — so a page read whole is read as OpenGlyph-OCR-0 reads it.
284
+
285
  **How it reads a line.** Four sets of weights in one model: the reader (OpenGlyph-OCR-0, unchanged) writes the line; the opener (14 added tokens and rank-8 LoRA in language layers 18–23) says where an IDS is due; at such a place, and wherever the reader itself writes a rare character or a glyph token, the glyph is read two more ways — unfolded (located by the reader's attention, cut into parts by a program, the parts read by a rank-8 LoRA part reader) and spelt whole (the speller: rank-64 LoRA in language layers 18–23 and in all twelve vision blocks and the merger, its own structure-token rows and per-step part rows, about 34.8 million weights). An IDS both routes write alike stands; otherwise the speller's stands if it is sure enough by a gate set per kind of place; otherwise the reader's own character stands. The gates were chosen on tuning lines, never on a test. With `options=True` the reading also returns a second option at every place that has one (the reading not taken where the routes were asked; the reader's second choice where its lead is under 4 logits); the text is the same with and without.
286
 
287
  **Measured** (November 2026, MLX form; "base" is Xiaomi-OCR-0 as released, with its own prompt; an IDS that draws exactly the answer's glyph counts as right — strict counts are given in the Chinese tables where they differ):
 
300
 
301
  **Known defects.** Nested composites are rarely right (2 of 54 on the special test), surrounds 8 of 29, three-part 7 of 22; composites with a part never seen in training 18 of 52. It sometimes spells a GlyphWiki glyph it has a token for as a wrong IDS (ebag-family glyphs 22 / 60 against OpenGlyph-OCR-0's 42 / 60; scanned-page glyphs 14 / 27 against 16 / 27), and leaves stray IDS where no composite is (57 on the special test; on the real sets 4 in the 398 whole lines of four papers, 3 in the 313 pieces of lines with hard characters, 3 in the 93 lines of four scanned book pages). It is several times slower than OpenGlyph-OCR-0. Only the MLX form exists (the reading runs four weight sets and cuts glyphs with a program; it was written and measured in MLX); OpenGlyph-OCR-0 alone has a `transformers` form.
302
 
303
+ **Real lines and their sources.** The card shows crops of real lines, each with its source: 何義軍《戰國陶文綜合研究》(Jilin University PhD thesis, 2023), 陈晶晶《清华简典型楚文字及相关问题研究》(Hebei University PhD thesis, 2025), 沈奇石《「隼」字異讀考論》(*臺大中文學報* 84, 2024, DOI 10.6281/NTUCL.202403_(84).0001), 鄔可晶《甲骨金文語文論稿》(Shanghai Guji, 2024). The fourth test paper, a 2025 thesis, is not named or shown: we do not know that it is public.
304
+
305
+ **What we took from han2ids.** [han2ids](https://github.com/mkpoli/han2ids) (MIT) reads ONE glyph picture into an IDS. From it we took the idea that parts need outputs of their own (hence the speller's part rows), the comparison of trainable size that made us raise the speller's LoRA rank from 8 to 64, and its convention that nesting along one axis is one structure (as our "same picture counts as right"). We also used it as a yardstick: given each composite's box, it wrote 238 of 600 test composites with practised parts against 272 for an earlier version of our speller reading whole lines, and 232 of 400 with parts never seen in training against our 132. No code, weights or data of han2ids are in this model or repository.
306
+
307
+ **Before release** the package in this repository was checked against the code that was measured: every array and table of the model it builds is bit for bit the measured one (1,330 entries), and 190 test lines read with it come out exactly as they were measured, every note of every place included (32 more with the second options); the page of the whole-page trial, read whole with it, comes out letter for letter as in the trial.
308
 
309
  **Licence.** Apache-2.0. `base/` is Xiaomi-OCR-0, redistributed unchanged under its Apache-2.0 licence. `canon.json` is derived from CJKVI IDS (from CHISE IDS, under CHISE's terms, GPLv2), BabelStone's IDS table (free for any use) and GlyphWiki data, and is used under the terms of those sources. Not a product of Xiaomi or of the authors of Xiaomi-OCR-0, and not endorsed by them.
figures/real-lines.png ADDED

Git LFS Details

  • SHA256: 3257beb6a5318c4995366766453fb0572b6590ea62b69ab4d2a1812ffcfa3a68
  • Pointer size: 131 Bytes
  • Size of remote file: 839 kB
openglyph_ocr_ids/__init__.py CHANGED
@@ -6,7 +6,7 @@ and characters no catalogue has. (MLX, Apple silicon. Run from a local copy of
6
  from openglyph_ocr_ids import load, read_line, atoms
7
  ocr = load() # the folder that holds this package; or load('/path/to/the/folder')
8
  out = read_line(ocr, 'line.png')
9
- print(out['text']) # 其字作<ids>⿰阝令</ids>,與<gw:u2ff0-u79c0-u6208>同
10
  out = read_line(ocr, 'line.png', options=True)
11
  print(out['options']) # the second option of every place that has one
12
 
@@ -16,8 +16,9 @@ What comes back (read_line):
16
  ids every IDS the model wrote and what it spells: a coded character (a rare character read by its parts), a glyph
17
  with a token, or a new glyph
18
  options with options=True: beside the text, the other reading of every place that has one
19
- The picture: one horizontal line of print as cropped from a page (the lines measured are about 60 to 130 pixels high).
20
- It is not a page reader: cut the page into lines first.
 
21
 
22
  How a line is read. Four sets of weights work in one model built on Xiaomi-OCR-0: the reader (OpenGlyph-OCR-0, unchanged)
23
  writes the line; the opener says where a glyph needs an IDS; at such a place, and wherever the reader itself writes a rare
@@ -28,6 +29,6 @@ spell (the speller), unfold_cut and unfold_search (the cuts), canon (canonical f
28
  The versions matter: this was measured with mlx 0.32.3, mlx-vlm 0.3.12, transformers 5.18.0, tokenizers 0.23.2
29
  (requirements.txt); torch and torchvision are there for the pictures only (with torchvision, transformers prepares them
30
  as they were prepared when the model was measured)."""
31
- from .build import load,read_line,readable,atoms
32
 
33
- __all__=['load','read_line','readable','atoms']
 
6
  from openglyph_ocr_ids import load, read_line, atoms
7
  ocr = load() # the folder that holds this package; or load('/path/to/the/folder')
8
  out = read_line(ocr, 'line.png')
9
+ print(out['text']) # 𤪷也庾參與<gw:u2ff1-u65e5-u82f1><gw:ebag_s093-073><ids>⿰拍嵩</ids>了燒造磚瓦的工作
10
  out = read_line(ocr, 'line.png', options=True)
11
  print(out['options']) # the second option of every place that has one
12
 
 
16
  ids every IDS the model wrote and what it spells: a coded character (a rare character read by its parts), a glyph
17
  with a token, or a new glyph
18
  options with options=True: beside the text, the other reading of every place that has one
19
+ The picture: one horizontal line of print as cropped from a page (the lines measured are about 100 to 150 pixels high
20
+ in the papers, 96 in the special test). A whole page can be read in one go with read_page(ocr, 'page.png') (or
21
+ --page): it is then read as OpenGlyph-OCR-0 reads, without IDS; cutting the page into lines reads better.
22
 
23
  How a line is read. Four sets of weights work in one model built on Xiaomi-OCR-0: the reader (OpenGlyph-OCR-0, unchanged)
24
  writes the line; the opener says where a glyph needs an IDS; at such a place, and wherever the reader itself writes a rare
 
29
  The versions matter: this was measured with mlx 0.32.3, mlx-vlm 0.3.12, transformers 5.18.0, tokenizers 0.23.2
30
  (requirements.txt); torch and torchvision are there for the pictures only (with torchvision, transformers prepares them
31
  as they were prepared when the model was measured)."""
32
+ from .build import load,read_line,read_page,readable,atoms
33
 
34
+ __all__=['load','read_line','read_page','readable','atoms']
openglyph_ocr_ids/__main__.py CHANGED
@@ -1,19 +1,26 @@
1
- """python -m openglyph_ocr_ids LINE.png [LINE2.png ...] [--options [LOGITS]] [--folder FOLDER]
2
 
3
- Prints one JSON object a line: the picture, the text, the IDS written (what each spells), the second options when asked
4
- for, whether the model ended the line itself, and the seconds it took."""
 
5
  import argparse,json
6
- from .build import load,read_line
7
 
8
  def main():
9
  p=argparse.ArgumentParser(prog='python -m openglyph_ocr_ids',description='Read lines of printed text with OpenGlyph-OCR-IDS (MLX).')
10
- p.add_argument('images',nargs='+',help='one picture per line of text')
11
  p.add_argument('--options',nargs='?',type=float,const=True,default=False,metavar='LOGITS',
12
  help='give the second options beside the text (LOGITS: the reader\'s lead under which a place nobody asked about gets one; default 4)')
 
 
13
  p.add_argument('--folder',default=None,help='the folder of the release (default: the one that holds this package)')
14
  a=p.parse_args();ocr=load(a.folder)
 
15
  for path in a.images:
16
- out=read_line(ocr,path,options=a.options)
 
 
 
17
  keep={'image':path,'text':out['text'],'ids':out['ids'],**({'options':[{k:o[k] for k in ('at_text','stands_text','other_text','other_from','lead') if k in o} for o in out['options']]} if 'options' in out else {}),
18
  'complete':out['complete'],'seconds':round(out['seconds'],2)}
19
  print(json.dumps(keep,ensure_ascii=False),flush=True)
 
1
+ """python -m openglyph_ocr_ids LINE.png [LINE2.png ...] [--options [LOGITS]] [--page] [--max-tokens N] [--folder FOLDER]
2
 
3
+ Prints one JSON object a picture: the picture, the text, the IDS written (what each spells), the second options when
4
+ asked for, whether the model ended the line itself, and the seconds it took. With --page every picture is read as a
5
+ whole page (read_page: as OpenGlyph-OCR-0 reads, no IDS; better results come from cutting the page into lines)."""
6
  import argparse,json
7
+ from .build import load,read_line,read_page
8
 
9
  def main():
10
  p=argparse.ArgumentParser(prog='python -m openglyph_ocr_ids',description='Read lines of printed text with OpenGlyph-OCR-IDS (MLX).')
11
+ p.add_argument('images',nargs='+',help='pictures: one line of text each (or a whole page each, with --page)')
12
  p.add_argument('--options',nargs='?',type=float,const=True,default=False,metavar='LOGITS',
13
  help='give the second options beside the text (LOGITS: the reader\'s lead under which a place nobody asked about gets one; default 4)')
14
+ p.add_argument('--page',action='store_true',help='read each picture as a whole page (as OpenGlyph-OCR-0 reads: no IDS); line by line reads better')
15
+ p.add_argument('--max-tokens',type=int,default=None,dest='max_tokens',help='the most tokens written for one picture (default: 256 a line, 2048 a page)')
16
  p.add_argument('--folder',default=None,help='the folder of the release (default: the one that holds this package)')
17
  a=p.parse_args();ocr=load(a.folder)
18
+ if a.page and a.options:p.error('--options: line by line only')
19
  for path in a.images:
20
+ if a.page:
21
+ out=read_page(ocr,path,max_tokens=a.max_tokens or 2048)
22
+ print(json.dumps({'image':path,'text':out['text'],'complete':out['complete'],'seconds':round(out['seconds'],2)},ensure_ascii=False),flush=True);continue
23
+ out=read_line(ocr,path,options=a.options,max_tokens=a.max_tokens or 256)
24
  keep={'image':path,'text':out['text'],'ids':out['ids'],**({'options':[{k:o[k] for k in ('at_text','stands_text','other_text','other_from','lead') if k in o} for o in out['options']]} if 'options' in out else {}),
25
  'complete':out['complete'],'seconds':round(out['seconds'],2)}
26
  print(json.dumps(keep,ensure_ascii=False),flush=True)
openglyph_ocr_ids/build.py CHANGED
@@ -192,3 +192,16 @@ def atoms(text):
192
  elif text.startswith('<ids>',i) and text.find('</ids>',i+5)>0:end=text.find('</ids>',i+5);out.append(('ids',text[i+5:end]));i=end+6
193
  else:out.append(('text',text[i]));i+=1
194
  return out
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  elif text.startswith('<ids>',i) and text.find('</ids>',i+5)>0:end=text.find('</ids>',i+5);out.append(('ids',text[i+5:end]));i=end+6
193
  else:out.append(('text',text[i]));i+=1
194
  return out
195
+
196
+ def read_page(ocr,image,max_tokens=2048):
197
+ """Read a whole page -- or any picture of several lines -- in one go, as OpenGlyph-OCR-0 reads it: no place is opened
198
+ for an IDS and no glyph is spelt. The two routes find a glyph by its place within ONE line and cannot do so on a
199
+ page; on the page tried, asking the speller there cost an item more. Cut a page into lines and read them with
200
+ read_line when the rare characters and the glyphs without a code point matter: on that page, read whole it had 8
201
+ of 691 items wrong, cut into its 22 lines 5. No line breaks are written. Returns the text, whether the model
202
+ ended the page itself (`complete`; else raise max_tokens), the seconds, and everything the reading noted."""
203
+ u=ocr.unfolding;kept=(u.offset,u.open_hard,u.ask_under,u.second_under)
204
+ u.offset=-1e9;u.open_hard=None;u.ask_under=None;u.second_under=None # nothing opens: the reader alone
205
+ try:r=u.predict({'image':str(image),'prompt':HYBRID_PROMPT},max_tokens,options=False)
206
+ finally:u.offset,u.open_hard,u.ask_under,u.second_under=kept
207
+ return {'text':r['prediction'],'raw_text':r['raw_prediction'],'complete':not r['possibly_truncated'],'seconds':r['seconds'],'details':r}
release.json CHANGED
@@ -13,7 +13,8 @@
13
  "checks_before_release": {
14
  "arrays_tables_and_settings_bit_for_bit_as_measured": 1330,
15
  "lines_read_as_measured": 190,
16
- "lines_read_as_measured_with_second_options": 32
 
17
  },
18
  "settings": {
19
  "top": 3,
@@ -55,8 +56,8 @@
55
  "bytes": 2622
56
  },
57
  "README.md": {
58
- "sha256": "5c6211ecf5ce0b029927aac47ceab64b95dab1bcc60680d150da8221c2d536ff",
59
- "bytes": 20887
60
  },
61
  "base/chat_template.jinja": {
62
  "sha256": "273d8e0e683b885071fb17e08d71e5f2a5ddfb5309756181681de4f5a1822d80",
@@ -105,6 +106,10 @@
105
  "sha256": "8a1322e3673768ff72b61eb242a146d329b43056d48f429975bd668a3ce8e8c8",
106
  "bytes": 247972
107
  },
 
 
 
 
108
  "figures/real-papers.png": {
109
  "sha256": "f279e55806221e08954770f1c5b8c4fe9dddaa32a36d54df29b997c05fd024a1",
110
  "bytes": 204165
@@ -125,18 +130,18 @@
125
  "from": "OpenGlyph/OpenGlyph-OCR-0, the same file"
126
  },
127
  "openglyph_ocr_ids/__init__.py": {
128
- "sha256": "bb9267b788405ae228b67a21f4916bc5d4eada1168f37c3d0b18cc2dd1492d32",
129
- "bytes": 2582,
130
  "from": "release/ids/port.py (the research code, ported)"
131
  },
132
  "openglyph_ocr_ids/__main__.py": {
133
- "sha256": "59989ab7c0309393049012991a1a7854c378b93de84a7cef4acb3c0a095d9a78",
134
- "bytes": 1447,
135
  "from": "release/ids/port.py (the research code, ported)"
136
  },
137
  "openglyph_ocr_ids/build.py": {
138
- "sha256": "080499911e245c14c7d398c5c08217d4dbb84b470c76f1c18e786dc199d9dc19",
139
- "bytes": 15111,
140
  "from": "release/ids/port.py (the research code, ported)"
141
  },
142
  "openglyph_ocr_ids/canon.py": {
 
13
  "checks_before_release": {
14
  "arrays_tables_and_settings_bit_for_bit_as_measured": 1330,
15
  "lines_read_as_measured": 190,
16
+ "lines_read_as_measured_with_second_options": 32,
17
+ "whole_page_read_as_in_the_trial": true
18
  },
19
  "settings": {
20
  "top": 3,
 
56
  "bytes": 2622
57
  },
58
  "README.md": {
59
+ "sha256": "8e51e37f54a3b8ef211ea5b0bfc32f82fd193a92c6b3c252895aed070d7d4b96",
60
+ "bytes": 27002
61
  },
62
  "base/chat_template.jinja": {
63
  "sha256": "273d8e0e683b885071fb17e08d71e5f2a5ddfb5309756181681de4f5a1822d80",
 
106
  "sha256": "8a1322e3673768ff72b61eb242a146d329b43056d48f429975bd668a3ce8e8c8",
107
  "bytes": 247972
108
  },
109
+ "figures/real-lines.png": {
110
+ "sha256": "3257beb6a5318c4995366766453fb0572b6590ea62b69ab4d2a1812ffcfa3a68",
111
+ "bytes": 838509
112
+ },
113
  "figures/real-papers.png": {
114
  "sha256": "f279e55806221e08954770f1c5b8c4fe9dddaa32a36d54df29b997c05fd024a1",
115
  "bytes": 204165
 
130
  "from": "OpenGlyph/OpenGlyph-OCR-0, the same file"
131
  },
132
  "openglyph_ocr_ids/__init__.py": {
133
+ "sha256": "daebffe5530c8de9c6cbbb17ea7b5e608a1eed4a9d5a46f1dd3385649c8a87db",
134
+ "bytes": 2805,
135
  "from": "release/ids/port.py (the research code, ported)"
136
  },
137
  "openglyph_ocr_ids/__main__.py": {
138
+ "sha256": "227445d926245710fc42568a4cad207f064bf30f72cf38d5965e6be6a1bc6237",
139
+ "bytes": 2337,
140
  "from": "release/ids/port.py (the research code, ported)"
141
  },
142
  "openglyph_ocr_ids/build.py": {
143
+ "sha256": "d6de061897e217e75def7e77f9028c1281d3df159eb0b18dfee680dd8b549718",
144
+ "bytes": 16336,
145
  "from": "release/ids/port.py (the research code, ported)"
146
  },
147
  "openglyph_ocr_ids/canon.py": {