OCR光学字符识别--STN-OCR 测试
1、同文章中建议的使用ubuntu-python隔离环境,真的很好用
参照:http://blog.topspeedsnail.com/archives/5618
启动虚拟环境:
source env/bin/activate
退出虚拟环境:
deactivate
注意:下面的操作全部都要在隔离环境中完成
2、搭建虚拟环境
pip install -r(requests)应该是安装request中所有的包
pip install Cython == 0.26
sudo apt-get install python3-dev
editdistance == 0.3.13、
3、
参照,编译百度warpctc
http://blog.csdn.net/amds123/article/details/73433926
git clone
https://github.com/baidu-research/warp-ctc.git
cd warp-ctc
mkdir build
cd build
cmake ..
make
sudo make install
执行文章中snt-orc
mxnet/metrics/ctc` and run `python setup.py build_ext --inplace`
4、
编译MXNET:
git clonr --recursive mxnet
cd mxnet
git tag
git checkout v0.9.3
按照论文中的方法编译失败,只能下载新版本编译
新版本编译步骤参考:https://www.bbsmax.com/A/A7zgqGk54n/
安装依赖:
$ sudo apt-get install -y build-essential git
$ sudo apt-get install -y libopenblas-dev
$ sudo apt-get install -y libopencv-dev
git clone --recursive https://github.com/dmlc/mxnet.git
cd mxnet
cp make/*.ck ./(编译选项文件)
vim *(按需修改编译文件)文章要求加入warpctc
https://mxnet.incubator.apache.org/tutorials/speech_recognition/baidu_warp_ctc.html
make -j4
5、
编译python接口参照
http://blog.csdn.net/zziahgf/article/details/72729883
编译 MXNet的Python API:
安装所需包
sudo apt-get install -y python-dev python-setuptools python-numpy
cd python
sudo python setup.py install
6、
下载stn-orc网络:https://github.com/Bartzi/stn-ocr
这个网络感觉跟FCN使用差不多,应该不需要什么格外操作
7、
下载model
https://bartzi.de/research/stn-ocr
中的文本识别:会有model文件夹,测试数据集
model文件夹中有两个文件
*.params是模型文件,*.json应该是网络描述文件
测试数据集中有图片文件夹,gt文件,还有一个不知道是什么用
还需要一个文件stn-orc网络中data文件对应‘文本’中应有个char_map文件,后面需要
模型预测代码就是stn-orc文件下的eva的py代码,看名字就知道,不过由于之前下载的是新版本,跟文中不同,所以使用这里的py文件没有运行成功,仿照文件自己写了一个简单的测试文件:
import matplotlib.pyplot as plt import argparse
import csv
import json
import os
from collections import namedtuple from PIL import Image import editdistance
import mxnet as mx
import numpy as np from callbacks.save_bboxes import BBOXPlotter
from metrics.ctc_metrics import strip_prediction
from networks.text_rec import SVHNMultiLineCTCNetwork
from operations.disable_shearing import *
from utils.datatypes import Size Batch = namedtuple('Batch', ['data']) #后缀都不能加的,程序自己添加,似乎同时加载两个文件
sym,arg_params,aux_params = mx.model.load_checkpoint('./testxt/model/model',2)
#这里面应该是训练的参数
#print(arg_params)
net, loc, transformed_output, size_params = SVHNMultiLineCTCNetwork.get_network((1,1,64,200),Size(50,50),46,2,23)
output = mx.sym.Group([loc, transformed_output, net]) #靠 在这里预定义的话,TMD,soft 层怎么办?
mod = mx.mod.Module(output,context=mx.cpu(),data_names=['data',
'softmax_label',
'l0_forward_init_h_state',
'l0_forward_init_c_state_cell',
'l1_forward_init_h_state',
'l1_forward_init_c_state_cell' ],label_names=[])
mod.bind(for_training=False,grad_req='null',data_shapes=[
('data',(1,1,64,200)),
('softmax_label', (1,23)),
('l0_forward_init_h_state', (1, 1, 256)),
('l0_forward_init_c_state_cell', (1, 1, 256)),
('l1_forward_init_h_state', (1, 1, 256)),
('l1_forward_init_c_state_cell', (1, 1, 256))
])
arg_params['l0_forward_init_h_state'] = mx.nd.zeros((1, 1, 256))
arg_params['l0_forward_init_c_state_cell'] = mx.nd.zeros((1, 1, 256))
arg_params['l1_forward_init_h_state'] = mx.nd.zeros((1, 1, 256))
arg_params['l1_forward_init_c_state_cell'] = mx.nd.zeros((1, 1, 256))
mod.set_params(arg_params, aux_params) #看看怎么加载label
#一个映射文件,类似caffe中的label,在下面循环中用到
with open('/home/lbk/python-env/stn-ocr/mxnet/testxt/ctc_char_map.json') as char_map_file:
char_map = json.load(char_map_file)
reverse_char_map = {v: k for k, v in char_map.items()}
print(len(reverse_char_map)) with open('/home/lbk/python-env/stn-ocr/mxnet/testxt/icdar2013_eval/one_gt.txt') as eval_gt:
reader = csv.reader(eval_gt,delimiter=';')
for idx,line in enumerate(reader):
file_name = line[0]
label = line[1].strip()
gt_word = label.lower()
print(gt_word)
#这一步又是干什么的
#dict.get(key,default)查找,不存在返回default
label = [reverse_char_map.get(ord(char.lower()),reverse_char_map[9250]) for char in gt_word]
label+=[reverse_char_map[9250]]*(23-len(label))
#print(label)
the_image = Image.open(file_name)
the_image = the_image.convert('L')
the_image = the_image.resize((200,64), Image.ANTIALIAS)
image = np.asarray(the_image, dtype=np.float32)[np.newaxis, np.newaxis, ...]
image/=255
temp = mx.nd.zeros((1,1,256))
label = mx.nd.array([label])
image = mx.nd.array(image)
print(type(temp),type(label))
input_batch = Batch(data=[image,label,temp,temp,temp,temp]) mod.forward(input_batch,is_train=False)
print(len(mod.get_outputs()))
print('0000',mod.get_outputs()[2])
predictions = mod.get_outputs()[2].asnumpy()
predicted_classes = np.argmax(predictions,axis=1)
print(len(predicted_classes))
print(predicted_classes) predicted_classes = strip_prediction(predicted_classes, int(reverse_char_map[9250]))
predicted_word = ''.join([chr(char_map[str(p)]) for p in predicted_classes]).replace(' ', '')
print(predicted_word) distance = editdistance.eval(gt_word, predicted_word)
print("{} - {}\t\t{}: {}".format(idx, gt_word, predicted_word, distance)) results = [prediction == label for prediction, label in zip(predicted_word, gt_word)]
print(results)
补充:
学习MXNET:
http://www.infoq.com/cn/articles/an-introduction-to-the-mxnet-api-part04
http://blog.csdn.net/yiweibian/article/details/72678020
http://ysfalo.github.io/2016/04/01/mxnet%E4%B9%8Bfine-tune/
http://shuokay.com/2016/01/01/mxnet-memo/
OCR光学字符识别--STN-OCR 测试的更多相关文章
- Ocrad.js – JS 实现 OCR 光学字符识别
Ocrad.js 相当于是 Ocrad 项目的纯 JavaScript 版本,使用 Emscripten 自动转换.这是一个简单的 OCR (光学字符识别)程序,可以扫描图像中的文字回文本. 不像 G ...
- 非黑即白--谷歌OCR光学字符识别
# coding=utf-8 #非黑即白--谷歌OCR光学字符识别 # 颜色的世界里,非黑即白.computer表示深信不疑. # 今天研究一下OCR光学识别庞大领域中的众多分支里的一个开源项目的一个 ...
- [Xcode 实际操作]七、文件与数据-(22)使用OCR光学字符识别技术识别银行卡号码
目录:[Swift]Xcode实际操作 本文将演示如何使用光学字符识别技术,识别信用卡上的卡号. OCR技术是光学字符识别的缩写(Optical Character Recognition), 是通过 ...
- 6 个优秀的开源 OCR 光学字符识别工具
转自:http://sigvc.org/bbs/thread-870-1-1.html 纸张在许多地方已日益失宠,无纸化办公谈论40多年,办公环境正限制纸山的生成.而过去几年,无纸化办公的概念发生了显 ...
- 开源OCR光学字符识别
纸张在 许多地方已日益失宠,无纸化办公谈论40多年,办公环境正限制纸山的生成.而过去几年,无纸化办公的概念发生了显着的转变.在计算机软件的帮助 下,包含大量重要管理数据和资讯的文档可以更方便的以电子形 ...
- IT行业新名词--透明手机/OCR(光学字符识别)/夹背电池
透明手机 机身设计的一大关键部分是可替换玻璃的使用,利用导电技术,在看不到线路的环境下,让LED发光. 这样的玻璃内含液晶分子,对于内容的显示则是通过电流对分子的刺激来实现.当手机断电后,分子位置会随 ...
- 【OCR技术系列一】光学字符识别技术介绍
注:此篇内容主要是综合整理了光学字符识别 和OCR技术系列之一]字符识别技术总览,详情见文末参考文献 什么是 OCR? OCR(Optical Character Recognition,光学字符识别 ...
- Tesseract:简单的Java光学字符识别
1.1 介绍 开发具有一定价值的符号是人类特有的特征.对于人们来说识别这些符号和理解图片上的文字是非常正常的事情.与计算机那样去抓取文字不同,我们完全是基于视觉的本能去阅读它们. 另一方面,计算机的工 ...
- 光学字符识别OCR
1.功能: 光学字符识别(OCR,Optical Character Recognition)是指对文本资料进行扫描,然后对图像文件进行分析处理,获取文字及版面信息的过程 2.典型应用: 名片扫描 3 ...
随机推荐
- [CF665F]Four Divisors
题目大意: 给定$n(n\leq10^{11})$,求$\displaystyle\sum_{i=1}^n[\tau(i)=4]$. 思路: 设$p,q$为不相等的质数,则满足$\tau(i)=4$的 ...
- 转:Maven项目中获取classpath和资源文件的路径
假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么java文件夹和resources文件夹在运行时就是class ...
- java.lang.NoSuchMethodError: main Exception in thread "main" ===Exception
java.lang.NoSuchMethodError: mainException in thread "main" 出现该异常是因为在之前我的项目中自定义了一个String类, ...
- 各语言最原始数据库访问组件封装DBHelper
源码:https://github.com/easonjim/DBHelper bug提交:https://github.com/easonjim/DBHelper/issues 每个语言放在不同的分 ...
- linux基础学习6
daemon 可以理解成为service 两大类: stand_alone:此 daemon 可以自行单独启动服务,加载到内存后就一直占用内存与系统资源:如 www的httpd ,ftp的vsft ...
- 使用LeakCanary遇到的问题 就是不弹出来
今天楼主遇到引用LeakCanary时代码跟官网一样但是就不弹出来.楼主新建项目就可以正常使用.楼主郁闷半天,现在终于整出来了. 楼主主工程app引用module为thirdParty,本想为了整洁三 ...
- openfire常见几类插件开发研究与总结
openfire 的插件可以访问所有openfire的API,这给我们的插件实现提供了巨大的灵活性. 以下介绍几类比较常用的插件集成方式: 基于源码XMPP协议的插件 比如:IQHandler,常用来 ...
- centos网络配置实例
1.配置DNS vim /etc/resolv.conf nameserver 192.168.0.1 nameserver 8.8.8.8 nameserver 8.8.4.4 2.配置网关 r ...
- DFRobot万物互联大赛第二轮
前言 最近放在阳台的花草被啥东西给吃了,然后厨房挂在墙上的小虾米也不知道咋的被抓破吃光了(我怀疑是隔隔壁两条泰迪),所以打算做个简单的项目,教训一下偷吃贼.时间比较仓促,内容比较多,能力有比较有限,好 ...
- 96Boards扩展板 STM32 B96B-F446VE 牛刀小试
前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的链接.图片显示,请访问我的博客原文: http://www.cnblog ...