Tesseract-OCR批量标注及合并数据集的一种思路

版权声明:本文为博主原创文章,转载请标明原始博文地址。 https://blog.csdn.net/yuanlulu/article/details/83280100

背景

Tesseract标注训练最根本的文件是两个:tiff文件(存储图片)及box文件(标注字符值和位置)
一般tiff用命令合成,box文件用jTessBoxEditor生成。

背景知识

参考前一篇博客,可以用python读写tiff文件。

另外box每一行的格式如下(6个元素):

tag x1 y1 x2 y2 frame-num

tag即OCR识别后的字符的值,(x1,y1)是box左下角坐标,(x2, y2)是box右上角的坐标 。注意box文件的坐标系原点是图片的左下角。

frame-num是图片在tiff文件里的序号,从0开始。每一帧可以有多个标注,也可以有一个。

比如,一个标注数字的box文件部分内容如下:

2 28 2 40 18 0
0 39 2 51 18 0
8 51 2 63 18 0
3 172 2 183 18 0
0 183 2 195 18 0
2 195 2 208 18 0
4 208 2 220 18 0
1 220 2 230 18 0
3 16 2 27 18 1
2 28 2 39 18 1
0 40 2 51 18 1
1 52 3 61 18 1
2 64 2 75 18 1
0 183 2 195 18 1
7 196 2 207 18 1
1 208 2 217 18 1
7 219 2 232 18 1
3 16 2 27 18 2
2 27 2 39 18 2
0 39 2 51 18 2
1 52 2 60 18 2
2 64 2 75 18 2

一个坑

jTessBoxEditor界面上显示的box的位置,坐标系原点在图片左上角,描述box位置的4个元素是(x, y, w, h),其中(x,y)是box左上角的坐标,(w,h)是框的宽高。

同一个软件,两种不同的坐标系及box表示方法,不得不服作者。

官方说明文档这么说的:

Note that the coordinate system used in the box file has (0,0) at the bottom-left; on computer graphics devices, however, (0,0) is defined as top-left. jTessBoxEditor uses and displays in the graphics device coordinates. The edited box files are still read and written in proper format.

批量标注和合并数据集的思路

我手上有一批同样分辨率的汉字图片(有标注值),有了上面的知识,就可以将这些图片合并到一个tiff文件,并且将相应的标注信息写入一个box文件。假设图片分辨率为64X64, 那么(x1,y1,x2,y2)我就取(0,0,64,64)。

在这里插入图片描述
如果有两个tiff及box,只要把图片调整为相同的分辨率,或者用空白补齐,就可以把图片抽出来合并为一个tiff文件,并适当修改box信息,合并为一个box文件。

参考资料

Tesseract-OCR的简单使用与训练

python之tiff文件读写

猜你喜欢

转载自blog.csdn.net/yuanlulu/article/details/83280100
今日推荐