File size: 3,168 Bytes
1edc459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<p align="center">
  <a href="README_ZH.md">δΈ­ζ–‡</a> | <a href="README.md">English</a>
</p>

# DocLayout Page Number Detection

A YOLO-based dataset and model for detecting page numbers in document images.

## Introduction

This dataset is built from the [DocLayNet-v1.2](https://huggingface.co/datasets/ds4sd/DocLayNet-v1.2) parquet files:

- 14,184 images selected from `misc/selected_pngs.txt` were annotated. See that file for the corresponding parquet list. Some images share similar page number layouts and were not all annotated; others have no page numbers.
- 4,899 images annotated in total: `box_single/` (4,816 images) and `box_multi/` (83 images). Note: decide whether to use `box_multi/`, as it may mislead the model.
- 334 images without page numbers included as negative examples under `no_boxes/`.
- 1,026 images from Chinese-format books selected from [HuggingFace](https://huggingface.co/datasets/wqzh/Gaokao-Admission-Chart-Segment) or [ModelScope](https://www.modelscope.cn/datasets/wqzh117/Gaokao-Admission-Chart-Segment) are under `pipe-adp/`.

## Labels

```
β”œβ”€β”€ DocLayout-PageNumDet/
    β”œβ”€β”€ labels/
    β”‚   β”œβ”€β”€ box_single/
    β”‚   β”‚   β”œβ”€β”€ image_1.txt
    β”‚   β”‚   β”œβ”€β”€ image_2.txt
    β”‚   β”‚
    β”‚   β”œβ”€β”€ no_boxes/
    β”‚       β”œβ”€β”€ image_a.txt (empty txt file)
    β”‚       β”œβ”€β”€ image_b.txt
...
```

## Models

```
DocLayout-PageNumDet/models
    β”œβ”€β”€ ch_PP-OCRv3_rec_infer.onnx            (PaddleOCR recognition model)
    β”œβ”€β”€ ppocr_keys_v1.txt                     (OCR character set)
    β”œβ”€β”€ yolo11-medium-page-best.pt            (YOLO11 medium, .pt format)
    β”œβ”€β”€ yolo11-medium-page-best-quant-ir9.onnx (ONNX quantized)
    β”œβ”€β”€ yolo11-nano-page-best.pt
    β”œβ”€β”€ yolo11-nano-page-best-quant-ir9.onnx
    β”œβ”€β”€ yolo11-small-page-best.pt
    └── yolo11-small-page-best-quant-ir9.onnx
```
For better detection performance, consider model ensemble.

## Usage

Process a single image: detect page number boxes, run OCR, and return:

```
[[x1, y1, x2, y2, conf, text], ...]
```

- List length = number of detected boxes.
- Each tuple = box coordinates, confidence, and recognized text (page number).
- Cropped page number images are saved in `./tmp/`.

```shell
# python src/det_and_ocr_pt.py <image_or_dir>
# python src/det_and_ocr_onnx.py <image_or_dir>
# python src/det_and_ocr_onnx_wo_opencv.py <image_or_dir>   # without OpenCV dependency

# single image
CUDA_VISIBLE_DEVICES=1 python src/det_and_ocr_pt.py examples/00-80T-80_433.png
CUDA_VISIBLE_DEVICES=1 python src/det_and_ocr_onnx.py examples/00-80T-80_433.png

# directory
CUDA_VISIBLE_DEVICES=1 python src/det_and_ocr_pt.py examples/
CUDA_VISIBLE_DEVICES=1 python src/det_and_ocr_onnx.py examples/
```

## ONNX Export

Export `.pt` models to ONNX with quantization:

```shell
python src/export_onnx.py
```

### ONNX File Size Comparison

```
37M     models/yolo11-small-page-best-ir9.onnx
37M     models/yolo11-small-page-best.onnx
37M     models/yolo11-small-page-best.onnx.data
19M     models/yolo11-small-page-best.pt
9.3M    models/yolo11-small-page-best-quant-ir9.onnx
```