python识别图片的中文手写文字(OCR识别)
ocr识别的全称是Optical Character Recognition中文光学字符识别,在日常的使用中还是比较广泛的,比如不愿意打字的朋友可以直接通过ocr将照片中的文字识别,减少了打字的时间,提高了工作效率,那么今天我们来说说如何通过python来识别照片中的中文并提取出来,我们先看看几张识别提取效果图吧。
火车票识别
身份证识别
A4打印纸文字识别
手写文字识别
是不是很强大,下面我来讲怎么识别的,上面的识别效果主要靠github上一个开源库chineseocr。
此项目基于yolo3 与crnn 实现中文自然场景文字检测及识别,采用darknet深度学习框架。主要功能如下:
★文字方向检测 0、90、180、270度检测(支持dnn/tensorflow)
★支持(darknet/opencv dnn /keras)文字检测,支持darknet/keras训练
★不定长OCR训练(英文、中英文) crnn\dense ocr 识别及训练 ,新增pytorch转keras模型代码(tools/pytorch_to_keras.py)
★支持darknet 转keras, keras转darknet, pytorch 转keras模型
★身份证/火车票结构化数据识别
★新增CNN+ctc模型,支持DNN模块调用OCR,单行图像平均时间为0.02秒以下
★CPU版本加速
★支持基于用户字典OCR识别
★新增语言模型修正OCR识别结果
★支持树莓派实时识别方案
darknet 优化版本:https://github.com/chineseocr/darknet-ocr.git
▊ 环境部署
GPU部署
conda create -n chineseocr python=3.6 pip scipy numpy jupyter ipython ##运用conda 创建python环境 source activate chineseocr git submodule init && git submodule update pip install easydict opencv-contrib-python==4.0.0.21 Cython h5py lmdb mahotas pandas requests bs4 matplotlib lxml -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install -U pillow -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install keras==2.1.5 tensorflow==1.8 tensorflow-gpu==1.8 pip install web.py==0.40.dev0 redis conda install pytorch torchvision -c pytorch pip install torch torchvision
##CPU 环境配置,支持linux\macOs conda create -n chineseocr python=3.6 pip scipy numpy jupyter ipython ##运用conda 创建python环境 source activate chineseocr cd darknet/ && make && cd .. pip install easydict opencv-contrib-python==4.0.0.21 Cython h5py lmdb mahotas pandas requests bs4 matplotlib lxml -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install -U pillow -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install web.py==0.40.dev0 redis pip install keras==2.1.5 tensorflow==1.8 mac conda install pytorch torchvision -c pytorch linux conda install pytorch-cpu torchvision-cpu -c pytorch
▊ 训练代码
ocr ctc训练数据集(压缩包解码:chineseocr)
百度网盘地址:链接: https://pan.baidu.com/s/1UcUKUUELLwdM29zfbztzdw 提取码: atwn
gofile地址:http://gofile.me/4Nlqh/uT32hAjbx 密码 https://github.com/chineseocr/chineseocr
下载编译darknet(如果直接运用opencv dnn或者keras yolo3 可忽略darknet的编译)
git clone https://github.com/pjreddie/darknet.git mv darknet chineseocr/##编译对GPU、cudnn的支持 修改 Makefile
#GPU=1 #CUDNN=1 #OPENCV=0 #OPENMP=0 make修改 darknet/python/darknet.py line 48
root = '/root/'##chineseocr所在目录 lib = CDLL(root+"chineseocr/darknet/libdarknet.so", RTLD_GLOBAL)
▊ 下载模型文件
模型文件地址:
百度网盘:https://pan.baidu.com/s/1gTW9gwJR6hlwTuyB6nCkzQ
other-links:http://59.110.234.163:9990/static/models/chineseocr/
复制文件夹中的所有文件到models目录
▊ 模型转换
pytorch ocr 转keras ocr
python tools/pytorch_to_keras.py -weights_path models/ocr-dense.pth -output_path models/ocr-dense-keras.h5darknet 转keras
python tools/darknet_to_keras.py -cfg_path models/text.cfg -weights_path models/text.weights -output_path models/text.h5keras 转darknet
python tools/keras_to_darknet.py -cfg_path models/text.cfg -weights_path models/text.h5 -output_path models/text.weights
▊ 模型选择
参考config.py文件
ocr_redis = False##是否多任务执行OCR识别加速 如果多任务,则配置redis数据库,数据库账号参考apphelper/redisbase.py ##是否启用LSTM crnn模型 ##OCR模型是否调用LSTM层 LSTMFLAG = True ocrFlag = 'torch'##ocr模型 支持 keras torch opencv版本 ##模型选择 True:中英文模型 False:英文模型 chineseModel = True## 中文模型或者纯英文模型
▊ 构建docker镜像
##下载Anaconda3 python 环境安装包(https://repo.anaconda.com/archive/Anaconda3-2019.03-Linux-x86_64.sh) 放置在chineseocr目录下 ##建立镜像 docker build -t chineseocr . ##启动服务 docker run -d -p 8080:8080 chineseocr /root/anaconda3/bin/python app.py web服务启动 cd chineseocr## 进入chineseocr目录 python app.py 8080 ##8080端口号,可以设置任意端口▊ 访问服务
http://127.0.0.1:8080/ocr
可以在这里上传图片进行识别
网友评论0