PaddleOCR 初尝试
建议环境
- conda 虚拟环境
- python3.10
- paddlepaddle-gpu==2.4
- cuda 10.2
- paddleocr 分支 release 2.6
ocr识别
- 简单的文字识别 例子:
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"函数 {func.__name__} 运行时间为 {elapsed_time:.5f} 秒")
return result
return wrapper
@timer
def ocrFn(img_path, res_dir=None):
ocr = PaddleOCR(use_angle_cls=True, lang="ch", use_gpu=False)
resUrl = os.path.basename(img_path)
file_name, file_ext = os.path.splitext(resUrl)
resName = file_name + '.txt'
if os.path.exists(img_path):
ocr_res = ocr.ocr(img_path, cls=True)
if res_dir is not None:
with open(os.path.join(res_dir, resName), 'w') as file_test:
for idx, res in enumerate(ocr_res):
file_test.write(str(res[1]) + '\n')
return ocr_res
else:
raise ValueError("图片路径错误")
ser 模型和 re 模型训练
- 使用 paddleLabel 进行 标注数据 (系统: macos m1)
PPOCRLabel --lang ch --kie True
- 标注时会有关键词 说法 建议 写成固定的几个后续需要改
如 header question answer other 导出标注结果后
数据样子:
image/custom_1.jpg [{"transcription": "xxxxxx", "points": [[170, 1215], [1048, 1186], [1056, 1317], [168, 1358]], "difficult": false, "key_cls": "TEST"}]
将上面的数据进行修改, 这里具有主观意见在里面
custom_1.jpg [{"transcription": "xxxxxx", "points": [[170, 1215], [1048, 1186], [1056, 1317], [168, 1358]], "id": 0, "label": "header", "linking": []}]
以上面的数据为例子 简单解释一下
custom_1.jpg PPOCRLabel 导出的结果在 Label.txt 文件中,通过自己的修改我要自己给数据进行 label 标注 以及 id 编号,linking 关系绑定等,
linking关系的绑定可以去看一下paddleOCR官方是怎么说的
总得说如果自己要进行 ser 和 re 模型的训练 数据集方面博主目前还没发现有什么自动生成工具,还是需要有自己手动去修改数据
kie对的应文档
3. 特别注意
ValueError: (InvalidArgument) The shape of tensor assigned value must match the shape of target shape: [512, 3], but now shape is [513, 3]
如果在训练re模型时遇到以上错误,请检查数据集是否存在一下问题
- 数据的标注块是否大于 3
- 是否至少包含一对linking 关系的映射
- linking 关系的映射是否合理,如:question -> answer 这样才对
- 检查数据中格式是否错误
注意: 文本文件中默认请将图片路径和图片标签用 \t
分割,如用其他方式分割将造成训练报错。
报错如下
error happened with msg: Traceback (most recent call last):
File "/Users/xxx/xxxDesktop/xxx/ai/xxx/ppocr/data/simple_dataset.py", line 144, in __getitem__
label = substr[1]
IndexError: list index out of range
但是这个是可以更改的,如下
# /ppocr/data/simple_dataset.py 35行 这里 \t 可以修改成其他方便分割的符号,不然会在 训练 ser 和 re 的时候都会报错
self.delimiter = dataset_config.get('delimiter', '\t')