在做文字识别的时候,往往由于自己的rec训练数据过少,需要自己生成一些数据来加入训练。这里我使用 TextRecognitionDataGenerator 来生成自己所需的数据。
1.使用方法:
下载代码仓库:
git clone https://github.com/Belval/TextRecognitionDataGenerator
# 安装一下依赖库
pip3 install -r requirements.txt
pip3 install -r requirements-hw.txt
cd TextRecognitionDataGenerator/trdg
python run.py -c 10
即可生成10个文本行样本,保存在 out 目录下。
2.遇到的错误:
在运行 python run.py -c 10 时,出现如下错误:
AttributeError: module 'PIL.Image' has no attribute 'Resampling'
原因是PIL.Image 没有 Resampling 这个属性。
3.解决方法:
先升级一下自己的 pillow 版本:
pip install --upgrade pillow -i https://pypi.tuna.tsinghua.edu.cn/simple
自己的版本由 7.1.0 升级到了8.4.0,然后再运行 python run.py -c 10 发现还是有相同的错误,原因是自己的8.4.0版本还是没有这个Resampling 属性。但是pillow不能再往上升级了,因为自己用的是python3.6版本,python3.6最大支持 pillow 8.4.0
进一步解决问题:
打开 trdg/data_generator.py 文件,找到133行左右,修改代码:
去掉 Image.Resampling.LANCZOS和Image.Resampling.NEAREST 中的 Resampling 即可。
resized_img = distorted_img.resize(
(new_width, size - vertical_margin), Image.Resampling.LANCZOS
)
resized_mask = distorted_mask.resize(
(new_width, size - vertical_margin), Image.Resampling.NEAREST
)
# 改成
resized_img = distorted_img.resize(
(new_width, size - vertical_margin), Image.LANCZOS
)
resized_mask = distorted_mask.resize(
(new_width, size - vertical_margin), Image.NEAREST
)