ocr 文字区域检测及识别

# coding=utf-

from PIL import Image, ImageFilter, ImageEnhance
from skimage.filters import threshold_otsu
import skimage.morphology as sm
from skimage.measure import regionprops
import matplotlib.pyplot as plt
import numpy as np
import pytesseract
import re
import os
import time
import logging
logging.basicConfig(level=logging.INFO, format="%(message)s", filename='train_output.log') class ShopCert(object):
def cut_region(self, img):
"""
先按规则缩小搜索范围
"""
w, h = img.size
if h<:
factor = max(, 1600.0/h)
newsize = int(w*factor), int(h*factor)
img = img.resize(newsize, Image.ANTIALIAS)
if w<h:
box = (w*0.4, h*0.18, w*0.96, h*0.6)
else:
box = (w*0.1, h*0.18, w*0.96, h*0.9)
return img.crop(box) def detect_text(self, img):
"""
检测字符区域
"""
imgM = np.array(img.convert('L'))
imgM = * (imgM < threshold_otsu(imgM))
imgM = sm.binary_closing(imgM, np.ones((, )))
imgM = sm.remove_small_objects(imgM, )
label_img = sm.label(imgM)
imgList = []
for region in regionprops(label_img):
minr, minc, maxr, maxc = region.bbox
w, h = (maxc-minc), (maxr-minr)
if h > w * 0.2:
continue
box = minc-, minr-, maxc+, maxr+
imgList.append(img.crop(box))
return imgList def clear_noise(self, box):
"""
降噪处理
"""
box = box.convert('L')
# box = box.point(lambda x: if x< else x)
box = box.point(lambda x: if x> else x)
box = ImageEnhance.Contrast(box).enhance(2.5)
return box def predict(self, fname, lang='eng'):
"""
ocr 识别
"""
img = Image.open(fname)
# 先大致缩小范围
region = self.cut_region(img)
# 候选字符区域
# region = self.clear_noise(region)
boxList = self.detect_text(region)
# 遍历识别
for box in boxList:
box = self.clear_noise(box)
w, h = box.size
if float(w)/h > 12.5:
res = pytesseract.image_to_string(box, lang='chi_sim', config='-psm 7')
else:
res = pytesseract.image_to_string(box, lang='eng', config='-psm 7')
res = re.sub('\s', '', res) # 去除中间空白
res = re.findall(r'[0-9][A-Z0-9]{13,20}', res) # -20位
for line in res:
line = line.strip()
if line.find(u'年')>:
continue
print 'line', line
if len(line)> :
box.save('img/clearNoise/%s_%s.jpg' % (fname.split('/')[-].split('.')[], line))
return line
else:
print 'error line', line
return 'error' def show_pic(path='img/origin2/'):
fnames = [os.path.join(path, fname) for fname in os.listdir(path)]
for i, fname in enumerate(fnames, ):
print fname
img = Image.open(fname)
# img.save('./tesseract-train/cert.normal.exp%d.ttf' % i)
img = ImageEnhance.Contrast(img).enhance(2.0)
img = img.filter(ImageFilter.MedianFilter).convert('L')
plt.figure(figsize=(, ), dpi=)
plt.imshow(img, plt.cm.gray)
plt.title(fname.split('/')[-]+'_%d' % i)
plt.show()if __name__ == '__main__':
test = ShopCert()
path = 'img/origin2/'
fnames = [os.path.join(path, fname) for fname in os.listdir(path) if fname.endswith('jpg')]
fnames.sort() arguments = 'mode: L; enhance:2.0; h:0.5; dh:0.15'
logging.info('%s' % arguments)
logging.info("%s: %s" % ('imgname', 'result'))
start_time = time.time()
cnt =
for idx, fname in enumerate(fnames, ):
print idx, fname
y_true = fname.split('/')[-].split('_')[]
y_pred = test.predict(fname)
if y_true == y_pred:
cnt +=
print fname
else:
print '***'*
print 'error'
logging.info("%s: %s" % (fname, y_pred))
print 'y_true', y_true
print 'y_pred', y_pred
acc = float(cnt)/idx
print acc, cnt
print '=='*, idx
logging.info('%.3f %d/%d' % (acc, cnt, idx))
print 'cost time: ', time.time()-start_time
logging.info('accuracy: %.2f' % acc)

最新文章

  1. (JS+CSS)实现图片放大效果
  2. 【python】with的实现方法
  3. Android 本地/网路下载图片实现放大缩小
  4. &quot;Accepted today?&quot;[HDU1177]
  5. ajax操作时用于提高用户体验的两段备用代码
  6. windows 给ping加时间
  7. C# 多线程写文件,时常写不成功
  8. 在Html中使用Requirejs进行模块化开发
  9. ExtJs 可查询的下拉框
  10. System-Defined Device Setup Classes Available to Vendors
  11. bzoj2396: 神奇的矩阵
  12. javascript实现播放音乐
  13. Oracle数据文件管理
  14. java_method_日期方法
  15. [SDOI2009]虔诚的墓主人
  16. 在Windows上使用Docker运行.NetCore
  17. Java同步(Synchronization)
  18. 关于linux中用vi新建立一个.c文件无法保存,显示E212错误的时候
  19. spring — jdbc 配置文件的设置
  20. JS-构造函数2

热门文章

  1. python------Json与pickle数据序列化
  2. 有关O_APPEND标志和lseek()的使用
  3. java 彻底理解 byte char short int float long double
  4. skipper filter 扩展开发
  5. npx:npm包执行器
  6. 在外网访问家里面的电脑 和 DMZ
  7. 在linux环境下,php语法出错,怎样让php编译后提示编译错误,错误在哪?
  8. 3、Sql-Ora-01033:oracle initialization or shutdown in progress
  9. spring-AOP框架(基于配置文件的方式配置AOP)
  10. 中文自然语言处理工具HanLP源码包的下载使用记录