[python3.6] 运用tf实现自然场景文字检测,keras/pytorch实现ctpn+crnn+ctc实现不定长场景文字OCR识别

Overview

本文基于tensorflow、keras/pytorch实现对自然场景的文字检测及端到端的OCR中文文字识别

update20190706

  • 为解决本项目中对数学公式预测的准确性,做了其他的改进和尝试,效果还不错,https://github.com/xiaofengShi/Image2Katex 希望能有所帮助,另外,这几月换了工作,并且转了方向,还是cv方向,不过不做ocr相关了,目前主要做显著目标检测以及搜索意图相关,对repo的提问回答较慢,请见谅。

实现功能

  • 文字方向检测 0、90、180、270度检测
  • 文字检测 后期将切换到keras版本文本检测 实现keras端到端的文本检测及识别
  • 不定长OCR识别

环境部署

Bash
##GPU环境
sh setup.sh
## CPU环境
sh setup-cpu.sh
##CPU python3环境
sh setup-python3.sh

使用环境:python3.6+tensorflow1.7+cpu/gpu

模型训练

  • 一共分为3个网络 1. 文本方向检测网络-Classify(vgg16)
  • 2. 文本区域检测网络-CTPN(CNN+RNN)
  • 3. EndToEnd文本识别网络-CRNN(CNN+GRU/LSTM+CTC)

文字方向检测-vgg分类

基于图像分类,在VGG16模型的基础上,训练0、90、180、270度检测的分类模型.
详细代码参考angle/predict.py文件,训练图片8000张,准确率88.23%

模型地址BaiduCloud

文字区域检测CTPN

关于ctpn网络,网上有很多对其进行介绍讲解的,算法是2016年提出的,在印书体识别用的很多,本人也写过一篇相应的博文深度学习-TextDetection,在文章中结合本repo的代码对ctpn的原理进行了详细的讲解。CTPN网路结构如下

ctpn_model

ctpn是一种基于目标检测方法的文本检测模型,在本repo的ctpn中anchor的设置为固定宽度,高度不同,相关代码如下:

def generate_anchors(base_size=16, ratios=[0.5, 1, 2],
                     scales=2 ** np.arange(3, 6)):
    heights = [11, 16, 23, 33, 48, 68, 97, 139, 198, 283]
    widths = [16]
    sizes = []
    for h in heights:
        for w in widths:
            sizes.append((h, w))
    return generate_basic_anchors(sizes)

基于这种设置,ctpn只能检测水平方向的文本,如果想要ctpn可以支持垂直文本检测,可以在anchor生成函数上进行修改。更详细的内容可以参考博客讲解。

OCR 端到端识别:CRNN

ocr识别采用GRU+CTC端到到识别技术,实现不分隔识别不定长文字

提供keras 与pytorch版本的训练代码,在理解keras的基础上,可以切换到pytorch版本,此版本更稳定

为什么使用ctc

ctc是一种解码机制,在使用ctpn提取到待检测文本行之后,我们要识别提取到的区域内的文本内容,目前广泛存在两种解码机制。

一种是seq2seq机制,输入的是图像,经过卷积编码之后再使用RNN解码,为了提高识别的准确率,一般会加入attention机制。

另一种就是ctc解码机制,但是对于ctc解码要满足一个前提,那就是输入序列的长度不小于输出序列的长度。ctc主要用于序列解码,我们不需要对序列中的每个元素进行标记,只需要知道输入序列对应的整个label是什么即可,针对ocr项目,也就是输入一张图像上面写着“欢迎来到中国”这几个字,我们只需要是这几个字,而没必要知道这几个字在输入图像中所在的具体位置,实际上如果知道每个字所在的位置,就是单字符识别了,的确会降低任务的复杂多,但是现实中我们没有这么多标记号位置的数据,这个时候CTC就显得很重要了。关于ctc解码机制,本人同样谢了一个对应的博客CTC算法原理,在文章中进行了详细的讲解,,如有疑问,请提交提问。

本repo中使用的是CNN+RNN+CTC的机制,实际上可以使用CNN+CTC的机制,CNN推荐选择densenet或者resnet

使用说明

使用预训练测试

运行demo.py  写入测试图片的路径即可,如果想要显示ctpn的结果,修改文件./ctpn/ctpn/other.py 的draw_boxes函数的最后部分,cv2.inwrite('dest_path',img),如此,可以得到ctpn检测的文字区域框以及图像的ocr识别结果

使用自己的数据训练

1 对ctpn进行训练

  • 定位到路径--./ctpn/ctpn/train_net.py
  • 预训练的vgg网络路径VGG_imagenet.npy 将预训练权重下载下来,pretrained_model指向该路径即可, 此外整个模型的预训练权重checkpoint
  • ctpn数据集还是百度云 数据集下载完成并解压后,将.ctpn/lib/datasets/pascal_voc.py 文件中的pascal_voc 类中的参数self.devkit_path指向数据集的路径即可

2 对crnn进行训练

  • keras版本 ./train/keras_train/train_batch.py model_path--指向预训练权重位置 MODEL_PATH---指向模型训练保存的位置 keras模型预训练权重
  • pythorch版本./train/pytorch-train/crnn_main.py
parser.add_argument(
    '--crnn',
    help="path to crnn (to continue training)",
    default=预训练权重的路径,看你下载的预训练权重在哪啦)
parser.add_argument(
    '--experiment',
    help='Where to store samples and models',
    default=模型训练的权重保存位置,这个自己指定)

pytorch预训练权重

识别结果展示

文字检测及OCR识别结果

ctpn原始图像1 =========================================================== ctpn检测1 =========================================================== ctpn+crnn结果1

主要是因为训练的时候,只包含中文和英文字母,因此很多公式结构是识别不出来的

看看纯文字的

ctpn原始图像2 =========================================================== ctpn检测2 =========================================================== ctpn+crnn结果2

未完待续

tensorflow版本crnn,计划尝试当前的各种trick(dropuout,bn,learning_decay等)

可以看到,对于纯文字的识别结果还是阔以的呢,感觉可以在crnn网络在加以改进,现在的crnn中的cnn有点浅,
并且rnn层为单层双向+attention,目前正在针对这个地方进行改动,使用迁移学习,以restnet为特征提取层,
使用多层双向动态rnn+attention+ctc的机制,将模型加深,目前正在进行模型搭建,结果好的话就发上来,不好的话只能凉凉了~~~~

训练数据集补充

列举可用于文本检测和识别领域模型训练的一些大型公开数据集, 不涉及仅用于模型fine-tune任务的小型数据集。

Chinese Text in the Wild(CTW)

该数据集包含32285张图像,1018402个中文字符(来自于腾讯街景), 包含平面文本,凸起文本,城市文本,农村文本,低亮度文本,远处文本,部分遮挡文本。图像大小2048*2048,数据集大小为31GB。以(8:1:1)的比例将数据集分为训练集(25887张图像,812872个汉字),测试集(3269张图像,103519个汉字),验证集(3129张图像,103519个汉字)。

文献链接:https://arxiv.org/pdf/1803.00085.pdf 
数据集下载地址:https://ctwdataset.github.io/

Reading Chinese Text in the Wild(RCTW-17)

该数据集包含12263张图像,训练集8034张,测试集4229张,共11.4GB。大部分图像由手机相机拍摄,含有少量的屏幕截图,图像中包含中文文本与少量英文文本。图像分辨率大小不等。

http://mclab.eic.hust.edu.cn/icdar2017chinese/dataset.html
文献:http://arxiv.org/pdf/1708.09585v2

ICPR MWI 2018 挑战赛

大赛提供20000张图像作为数据集,其中50%作为训练集,50%作为测试集。主要由合成图像,产品描述,网络广告构成。该数据集数据量充分,中英文混合,涵盖数十种字体,字体大小不一,多种版式,背景复杂。文件大小为2GB。

https://tianchi.aliyun.com/competition/information.htm?raceId=231651&_is_login_redirect=true&accounttraceid=595a06c3-7530-4b8a-ad3d-40165e22dbfe

Total-Text

该数据集共1555张图像,11459文本行,包含水平文本,倾斜文本,弯曲文本。文件大小441MB。大部分为英文文本,少量中文文本。训练集:1255张 测试集:300

http://www.cs-chan.com/source/ICDAR2017/totaltext.zip
http:// arxiv.org/pdf/1710.10400v

Google FSNS(谷歌街景文本数据集)

该数据集是从谷歌法国街景图片上获得的一百多万张街道名字标志,每一张包含同一街道标志牌的不同视角,图像大小为600*150,训练集1044868张,验证集16150张,测试集20404张。

http://rrc.cvc.uab.es/?ch=6&com=downloads
http:// arxiv.org/pdf/1702.03970v1

COCO-TEXT

该数据集,包括63686幅图像,173589个文本实例,包括手写版和打印版,清晰版和非清晰版。文件大小12.58GB,训练集:43686张,测试集:10000张,验证集:10000张

http://arxiv.org/pdf/1601.07140v2
https://vision.cornell.edu/se3/coco-text-2/

Synthetic Data for Text Localisation

在复杂背景下人工合成的自然场景文本数据。包含858750张图像,共7266866个单词实例,28971487个字符,文件大小为41GB。该合成算法,不需要人工标注就可知道文字的label信息和位置信息,可得到大量自然场景文本标注数据。

下载地址:http://www.robots.ox.ac.uk/~vgg/data/scenetext/
文献:http://www.robots.ox.ac.uk/~ankush/textloc.pdf
Code: https://github.com/ankush-me/SynthText (英文版)
Code https://github.com/wang-tf/Chinese_OCR_synthetic_data(中文版)

Synthetic Word Dataset

合成文本识别数据集,包含9百万张图像,涵盖了9万个英语单词。文件大小为10GB

http://www.robots.ox.ac.uk/~vgg/data/text/

Caffe-ocr中文合成数据

数据利用中文语料库,通过字体、大小、灰度、模糊、透视、拉伸等变化随机生成,共360万张图片,图像分辨率为280x32,涵盖了汉字、标点、英文、数字共5990个字符。文件大小约为8.6GB

https://pan.baidu.com/s/1dFda6R3

参考

Owner
xiaofeng
Algorithm Engineer
xiaofeng
aardio的opencv库

opencv_aardio dll库下载地址:https://github.com/xuncv/opencv-plugin/releases import cv2 img = cv2.imread("./images/Lena.jpg",1) img = cv2.medianBlur(img,5)

71 Dec 31, 2022
利用Paddle框架复现CRAFT

CRAFT-Paddle 利用Paddle框架复现CRAFT CRAFT 本项目基于paddlepaddle框架复现CRAFT,并参加百度第三届论文复现赛,将在2021年5月15日比赛完后提供AIStudio链接~敬请期待 参考项目: CRAFT: Character-Region Awarenes

QuanHao Guo 2 Mar 07, 2022
Primary QPDF source code and documentation

QPDF QPDF is a command-line tool and C++ library that performs content-preserving transformations on PDF files. It supports linearization, encryption,

QPDF 2.2k Jan 04, 2023
https://arxiv.org/abs/1904.01941

Character-Region-Awareness-for-Text-Detection- https://arxiv.org/abs/1904.01941 Train You can train SynthText data use python source/train_SynthText.p

DayDayUp 120 Dec 28, 2022
End-to-end pipeline for real-time scene text detection and recognition.

Real-time-Scene-Text-Detection-and-Recognition-System End-to-end pipeline for real-time scene text detection and recognition. The detection model use

Fangneng Zhan 89 Aug 04, 2022
DouZero is a reinforcement learning framework for DouDizhu - 斗地主AI

[ICML 2021] DouZero: Mastering DouDizhu with Self-Play Deep Reinforcement Learning | 斗地主AI

Kwai 3.1k Jan 05, 2023
Make OpenCV camera loops less of a chore by skipping the boilerplate and getting right to the interesting stuff

camloop Forget the boilerplate from OpenCV camera loops and get to coding the interesting stuff Table of Contents Usage Install Quickstart More advanc

Gabriel Lefundes 9 Nov 12, 2021
make a better chinese character recognition OCR than tesseract

deep ocr See README_en.md for English installation documentation. 只在ubuntu下面测试通过,需要virtualenv安装,安装路径可自行调整: git clone https://github.com/JinpengLI/deep

Jinpeng 1.5k Dec 28, 2022
Usando o Amazon Textract como OCR para Extração de Dados no DynamoDB

dio-live-textract2 Repositório de código para o live coding do dia 05/10/2021 sobre extração de dados estruturados e gravação em banco de dados a part

hugoportela 0 Jan 19, 2022
This is a Computer vision package that makes its easy to run Image processing and AI functions. At the core it uses OpenCV and Mediapipe libraries.

CVZone This is a Computer vision package that makes its easy to run Image processing and AI functions. At the core it uses OpenCV and Mediapipe librar

CVZone 648 Dec 30, 2022
This Repository contain Opencv Projects in python

Python-Opencv OpenCV OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was

Yash Sakre 2 Nov 06, 2021
The virtual calculator will be above the live streaming from your camera

The virtual calculator is above the live streaming from my camera usb , the program first detect my hand and in each frame calculate the distance between two finger ,if the distance is lower than the

gasbaoui mohammed al amine 5 Jul 01, 2022
governance proposal to make fei redeemable for eth

Feil Proposal 🌲 Abstract Migrate all ETH from Fei protocol-controlled value into Yearn ETH Vault. Allow redemptions of outstanding FEI for yvETH. At

13 Mar 31, 2022
textspotter - An End-to-End TextSpotter with Explicit Alignment and Attention

An End-to-End TextSpotter with Explicit Alignment and Attention This is initially described in our CVPR 2018 paper. Getting Started Installation Clone

Tong He 323 Nov 10, 2022
A semi-automatic open-source tool for Layout Analysis and Region EXtraction on early printed books.

LAREX LAREX is a semi-automatic open-source tool for layout analysis on early printed books. It uses a rule based connected components approach which

162 Jan 05, 2023
This is the official PyTorch implementation of the paper "TransFG: A Transformer Architecture for Fine-grained Recognition" (Ju He, Jie-Neng Chen, Shuai Liu, Adam Kortylewski, Cheng Yang, Yutong Bai, Changhu Wang, Alan Yuille).

TransFG: A Transformer Architecture for Fine-grained Recognition Official PyTorch code for the paper: TransFG: A Transformer Architecture for Fine-gra

Ju He 307 Jan 03, 2023
This repo contains a script that allows us to find range of colors in images using openCV, and then convert them into geo vectors.

Vectorizing color range This repo contains a script that allows us to find range of colors in images using openCV, and then convert them into geo vect

Development Seed 9 Jul 27, 2022
learn how to use Gesture Control to change the volume of a computer

Volume-Control-using-gesture In this project we are going to learn how to use Gesture Control to change the volume of a computer. We first look into h

Diwas Pandey 49 Sep 22, 2022
Ready-to-use OCR with 80+ supported languages and all popular writing scripts including Latin, Chinese, Arabic, Devanagari, Cyrillic and etc.

EasyOCR Ready-to-use OCR with 80+ languages supported including Chinese, Japanese, Korean and Thai. What's new 1 February 2021 - Version 1.2.3 Add set

Jaided AI 16.7k Jan 03, 2023
A fastai/PyTorch package for unpaired image-to-image translation.

Unpaired image-to-image translation A fastai/PyTorch package for unpaired image-to-image translation currently with CycleGAN implementation. This is a

Tanishq Abraham 120 Dec 02, 2022