文本检测和识别 代码结构梳理

摘要:
最近,我学习了OCR的一些基本知识,包括对象检测和自然语言处理。恰好,数字中国有相关的竞争:https://www.datafountain.cn/competitions/334/details/rule所以我想练习。事实上,我发现我不熟悉数据标签处理和整个检测和识别过程,从头开始仍然非常困难。幸运的是,有一些基线以前已经被大家伙打开,可以参考、检测和识别

前言:

最近学习了一些OCR相关的基础知识,包含目标检测和自然语言处理。

正好,在数字中国有相关的比赛:

https://www.datafountain.cn/competitions/334/details/rule

所以想动手实践一下,实际中发现,对于数据标签的处理和整个检测和识别的流程并不熟悉,自己从头去搞还是有很大难度。

幸好,有大佬们之前开源的一些baseline可以参考,有检测的也有识别的,对于真真理解OCR识别是有帮助的。

1)最初baseline AdvancedEAST + CRNN
https://github.com/Tianxiaomo/Cultural_Inheritance-Recognizing_Chinese_Calligraphy_in_Multiple_Scenarios

2)一个新的baseline:EAST + ocr_densenet

https://github.com/DataFountainCode/huawei_code_share

还有最原始的开源的EAST 源码,advanced EAST源码

https://github.com/argman/EAST

https://github.com/huoyijie/AdvancedEAST

CRNN 源码

https://github.com/bgshih/crnn

以及densenet 等,都是很好的学习资源

https://github.com/yinchangchang/ocr_densenet

PART1: EAST 

下面,先对EAST 的整个代码进行梳理:
训练样本格式:

img_1.jpg

img_1.txt

img_2.jpg

img_2.txt

(这个可以用第二个baseline中的convert_to_txt.py 实现)

即训练集包含图像以及图像对应的标注信息(4个位置坐标和文字)

python multigpu_train.py --gpu_list=0 --input_size=512 --batch_size_per_gpu=14 --checkpoint_path=/tmp/east_icdar2015_resnet_v1_50_rbox/ 
--text_scale=512 --training_data_path=/data/ocr/icdar2015/ --geometry=RBOX --learning_rate=0.0001 --num_readers=24 
--pretrained_model_path=/tmp/resnet_v1_50.ckpt

训练完成之后们就可以进行测试
python eval.py --test_data_path=./tmp/test_image/ --gpu_list=0 --checkpoint_path=./tmp/east_icdar2015_resnet_v1_50_rbox/ --output_dir=./tmp/output/
加载已经训练好的模型进行测试


bug解决:
1、lanms 无法完成编译,将Makefile中的Python3 替换为 Python即可make:
I modify the file lanms/Makefile ,change the python3-config to python-config

CXXFLAGS = -I include -std=c++11 -O3 $(shell python3-config --cflags)
LDFLAGS = $(shell python3-config --ldflags)

2、在测试输出时出现

Traceback (most recent call last):
  File "eval.py", line 194, in <module>
    tf.app.run()
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 126, in run
    _sys.exit(main(argv))
  File "eval.py", line 160, in main
    boxes, timer = detect(score_map=score, geo_map=geometry, timer=timer)
  File "eval.py", line 98, in detect
    boxes = lanms.merge_quadrangle_n9(boxes.astype('float32'), nms_thres)
  File "/work/ocr/EAST/lanms/__init__.py", line 12, in merge_quadrangle_n9
    from .adaptor import merge_quadrangle_n9 as nms_impl
ImportError: dynamic module does not define module export function (PyInit_adaptor)

nms_locality.nms_locality() is a python implemention, its much slower than c++ code, if just want to test, you can use it, these two methods should provide the same result.

When I change the lanms.merge_quadrangle_n9() in eval.py to nms_locality.nms_locality() There's no error. 

C++版本实现调用有问题,直接用Python的实现,这里只是慢一点,结果都是一样的;

PART2: CRNN

 参考源码:https://github.com/bai-shang/OCR_TF_CRNN_CTC

训练方法:

1)转换数据,对应图像和标签

For example: image_list.txt

90kDICT32px/1/2/373_coley_14845.jpg coley
90kDICT32px/17/5/176_Nevadans_51437.jpg nevadans

Note: make sure that images can be read from the path you specificed, such as:

path/to/90kDICT32px/1/2/373_coley_14845.jpg
path/to/90kDICT32px/17/5/176_Nevadans_51437.jpg
.......


命令行转换为tfrecord:

python tools/create_crnn_ctc_tfrecord.py
--image_dir ./data/ --anno_file ./data/train.txt --data_dir ./tfrecords/
--validation_split_fraction 0.1

问题:

1)最初bug:TypeError: None has type NoneType, but expected one of: int, long

是因为有未定义的字,也就是不在字典中的字,所以在字典中,字典不完整,单独加未在字典中的编码 "<undefined>": 6736

而且在原代码中:

def _string_to_int(label):
# convert string label to int list by char map
char_map_dict = json.load(open(FLAGS.char_map_json_file, 'r'))

int_list = []
for c in label:
int_list.append(char_map_dict.get(c,6736))    # 增加新的分类6736

2) python2 中会遇到许多编码的问题,建议换成Python3

def _bytes_feature(value):
    if type(value) is str:
        value = value.encode('utf-8')
    if sys.version_info[0] > 2:
        value = value # convert string object to bytes
    if not isinstance(value, list):
        value = [value]
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))

  

 代码调试的时候,一步步打印中间结果,分析问题原因:

try:

    print (tf.train.Feature(int64_list=tf.train.Int64List(value=value)))

except:
    print(value)

免责声明:文章转载自《文本检测和识别 代码结构梳理》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇用HBuilderX 打包 vue 项目 为 App 的步骤在linux系统下检查postgresql数据库安装,登录数据库及简单的查看数据库下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python购物车程序

购物车程序主要实现的功能如下      1. 启动程序后,用户通过账号密码登录,然后打印商品列表。 2. 允许用户根据商品编号购买商品。 3. 用户选择商品后,检测余额是否足够,够就直接扣款,不够就提醒充值。 4. 可随时退出,退出时,打印已购买的商品和余额。 插入一张图,这是理想的功能,下面的代码并没有实现这么多功能,需要加上登陆小程序。以后有时间我会实...

python图片黑白化

#!/usr/bin/env python#-*- coding:utf-8 -*- from PIL importImage im = Image.open(r"C:Userswangshaowei6Desktopwm.gif") #(将图片转换为8位像素模式) 和RGB模式相似 im.convert("P") his =im.histogram(...

Ansible介绍与安装使用

Ansible 介绍与安装 目录 Ansible 介绍与安装 Ansible的定义 Ansible的基础架构 Ansible的程序目录结构 Anisible特性 注意事项 安装Ansible 自定义清单(配置组内成员) Anisble实现管理方式 Ansible常用模块 ping 模块 user 模块 command/raw/shell 模块 scr...

pyinstaller打包python源程序访问hive

1.需求   使用hvie server一段时间后,业务部门需要自己不定时的查询业务数据,之前这一块都是他们提需求我们来做,后来发现这样重复一样的工作放在我们这边做是在没有效率,遂提出给他们工具或者web UI自助查询,当然hive有自己的hwi可以通过网页UI进行自助查询,但是这对不懂sql的业务人员有点不太友好,目前有没时间去修改hwi的UI,所以还是...

Python自动化学习笔记(二)——Jmeter连接数据库、添加压力机、Charles抓包、Python初识

1.Jmeter连接Mysql数据库 1.1添加jar包 选中测试计划,添加jar包 也可以直接将jar包复制到jmeter的lib目录 1.2添加mysql连接信息 测试计划-配置元件-JDBC Connection Configuration,添加Mysql连接信息 Database URL:jdbc:mysql://ip:port/dbname?u...

python读写excel

Python操作excel表,需要用到两个库:xlrd和xlwt 1、获取excel表Book对象实例:   book = xlrd.open_workbook(filepath) 2、获取excel的Sheet对象   sheet_nums = book.nsheets    获取Sheet对象个数   sheet = book.sheet_by_ind...