使用的是python的pytesser模块,原先想做的是图片中文识别,搞了一段时间了,在中文的识别上还是有很多问题,这里做记录分享。

  pytesserOCR in Python using the Tesseract engine from Google。是谷歌OCR开源项目的一个模块,可将图片中的文字转换成文本(主要是英文)。

  1.pytesser安装

  使用设备:win8 64位

  PyTesser使用Tesseract OCR引擎,将图像转换到可接受的格式,然后执行tesseract提取出文本信息。使用PyTesser ,你无须安装Tesseract OCR引擎,但必须要先安装PIL模块(Python Image Library,python的图形库)

  pytesser下载:http://code.google.com/p/pytesser/  若打不开,可通过百度网盘下载:http://pan.baidu.com/s/1o69LL8Y

  PIL官方下载:http://www.pythonware.com/products/pil/

  其中PIL可直接点击exe安装,pytesser无需安装,解压后可以放在python安装文件夹的\Lib\site-packages\  下直接使用(需要添加pytesser.pth)

  Ubuntu安装

sudo pip install pytesseract
sudo apt-get install tesseract-ocr

  2.pytesser源码

  通过查看pytesser.py的源码,可以看到几个主要函数:

 (1)call_tesseract(input_filename, output_filename)

  该函数调用tesseract外部执行程序,提取图片中的文本信息 

  (2)image_to_string(im, cleanup = cleanup_scratch_flag)

  该函数处理的是image对象,所以需用使用im = open(filename)打开文件,返回一个image对象。其中调用util.image_to_scratch(im, scratch_image_name)将内存中的图像文件保存为bmp,以便tesserac程序能正常处理。

  (3)image_file_to_string(filename, cleanup = cleanup_scratch_flag, graceful_errors=True)
  该函数直接使用Tesseract读取图像文件,如果图像是不相容的,会先转换成兼容的格式,然后再提取图片中的文本信息。
"""OCR in Python using the Tesseract engine from Google
http://code.google.com/p/pytesser/
by Michael J.T. O'Kelly
V 0.0.1, 3/10/07""" import Image
import subprocess import util
import errors tesseract_exe_name = 'tesseract' # Name of executable to be called at command line
scratch_image_name = "temp.bmp" # This file must be .bmp or other Tesseract-compatible format
scratch_text_name_root = "temp" # Leave out the .txt extension
cleanup_scratch_flag = False # Temporary files cleaned up after OCR operation def call_tesseract(input_filename, output_filename):
"""Calls external tesseract.exe on input file (restrictions on types),
outputting output_filename+'txt'"""
args = [tesseract_exe_name, input_filename, output_filename]
proc = subprocess.Popen(args)
retcode = proc.wait()
if retcode!=0:
errors.check_for_errors() def image_to_string(im, cleanup = cleanup_scratch_flag):
"""Converts im to file, applies tesseract, and fetches resulting text.
If cleanup=True, delete scratch files after operation."""
try:
util.image_to_scratch(im, scratch_image_name)
call_tesseract(scratch_image_name, scratch_text_name_root)
text = util.retrieve_text(scratch_text_name_root)
finally:
if cleanup:
util.perform_cleanup(scratch_image_name, scratch_text_name_root)
return text def image_file_to_string(filename, cleanup = cleanup_scratch_flag, graceful_errors=True):
"""Applies tesseract to filename; or, if image is incompatible and graceful_errors=True,
converts to compatible format and then applies tesseract. Fetches resulting text.
If cleanup=True, delete scratch files after operation."""
try:
try:
call_tesseract(filename, scratch_text_name_root)
text = util.retrieve_text(scratch_text_name_root)
except errors.Tesser_General_Exception:
if graceful_errors:
im = Image.open(filename)
text = image_to_string(im, cleanup)
else:
raise
finally:
if cleanup:
util.perform_cleanup(scratch_image_name, scratch_text_name_root)
return text if __name__=='__main__':
im = Image.open('phototest.tif')
text = image_to_string(im)
print text
try:
text = image_file_to_string('fnord.tif', graceful_errors=False)
except errors.Tesser_General_Exception, value:
print "fnord.tif is incompatible filetype. Try graceful_errors=True"
print value
text = image_file_to_string('fnord.tif', graceful_errors=True)
print "fnord.tif contents:", text
text = image_file_to_string('fonts_test.png', graceful_errors=True)
print text   
  3.pytesser使用
  
在代码中加载pytesser模块,简单的测试代码如下:
from pytesser import *
im = Image.open('fonts_test.png')
text = image_to_string(im)
print "Using image_to_string(): "
print text
text = image_file_to_string('fonts_test.png', graceful_errors=True)
print "Using image_file_to_string():"
print text

  

 识别结果如下:基本能将英文字符提取出来,但对一些复杂点的图片,比如说我尝试对一些英文论文图片进行识别,但结果实在不理想。

 

  由于在中文识别方面还有很多问题,以后再进一步研究分享。

参考:HK_JH的专栏 http://blog.csdn.net/hk_jh/article/details/8961449

Python 利用pytesser模块识别图像文字的更多相关文章

  1. python利用selenium库识别点触验证码

    利用selenium库和超级鹰识别点触验证码(学习于静谧大大的书,想自己整理一下思路) 一.超级鹰注册:超级鹰入口 1.首先注册一个超级鹰账号,然后在超级鹰免费测试地方可以关注公众号,领取1000积分 ...

  2. 利用Hough变换识别图像中的直线

    引入 近期看到2015年数学建模A题太阳影子定位中的第四问,需要根据附件中视频里的直杆的太阳影子的变化确定拍摄地点.其实确定拍摄地点这个问题并不是十分困难,因为有前三问的铺垫,我们已经得出了太阳影子长 ...

  3. Python利用os模块批量修改文件名

    初学Python.随笔记录自己的小练习. 通过查阅资料os模块中rename和renames都可以做到 他们的区别为.rename:只能修改文件名   renames:可以修改文件名,还可以修改文件上 ...

  4. python 利用tkinter模块设计出window窗口(搞笑版)

    代码如下 from tkinter import * import tkinter from tkinter import messagebox #定义了一个函数,当关闭window窗口时将会弹出一个 ...

  5. python利用twilio模块给自己发短信

    1.访问http://twilio.com/并填写注册表单.注册了新账户后,你需要验证一个手机号码,短信将发给该号码. 2.Twilio 提供的试用账户包括一个电话号码,它将作为短信的发送者.你将需要 ...

  6. [Python] 利用commands模块执行Linux shell命令

    http://blog.csdn.net/dbanote/article/details/9414133 http://zhou123.blog.51cto.com/4355617/1312791

  7. python利用scapy模块写一个TCP路由追踪和扫描存活IP的脚本

    前言: 没有前言 0x01 from scapy.all import * import sys from socket import * import os from threading impor ...

  8. python 利用csv模块导入数据

  9. 利用pytesser识别图形验证码

    简单识别 1.一般思路 验证码识别的一般思路为: 图片降噪 图片切割 图像文本输出 1.1 图片降噪 所谓降噪就是把不需要的信息通通去除,比如背景,干扰线,干扰像素等等,只剩下需要识别的文字,让图片变 ...

随机推荐

  1. 第九天 内容提供者 ContentResolver

    重点:理解ContentProvider 的作用和创建流程 1. 内容提供者,提供 其他数据库的访问. 特点       - 描述 : 它是android 四大组件之一,需要androidManife ...

  2. jquery之remove(),detach()方法详解

    一:remove()方法 remove()函数用于从文档中移除匹配的元素. 你还可以使用选择器进一步缩小移除的范围,只移除当前匹配元素中符合指定选择器的部分元素. 与detach()相比,remove ...

  3. 448. Find All Numbers Disappeared in an Array Add to List

    题目描述 题目分析 有个[1,n]的条件要充分利用起来. 题目代码 public class Solution { public List<Integer> findDisappeared ...

  4. poj2778DNA Sequence(AC自动机+矩阵乘法)

    链接 看此题前先看一下matrix67大神写的关于十个矩阵的题目中的一个,如下: 经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值    把给定的图转为邻 ...

  5. 程序设计入门——C语言 第3周编程练习 1 奇偶个数(5分)

    1 题目内容: 你的程序要读入一系列正整数数据,输入-1表示输入结束,-1本身不是输入的数据.程序输出读到的数据中的奇数和偶数的个数. 输入格式: 一系列正整数,整数的范围是(0,100000).如果 ...

  6. Hibernate 中出现 XXXX is not mapped 问题

    1.查询的不是数据库名是实体名…Entity 2.也许是你where条件后的字段名称写错了

  7. [SoapUI] 同一个Resource不同参数时,在两个step里默认打开总是同一个Resource

    当SoapUI里Projects 有两个相同的Resource,只是参数不同时,使用两个Resource创建的step默认打开的总是同一个Resource.我们应当修改method名字为不同,这是So ...

  8. shell的if判断

     shell的if判断 2012-03-16 14:53:05 分类: Python/Ruby   1 概要 有时候你需要指定shell脚本中的依靠命令的成功与否来实施不同过程的行为. if 结构允许 ...

  9. C#获取参数getParameter

    昨天遇到了这样一个问题,getParameter时,首次进入页面读取默认值时,本应该读取到“全部”这个字符. 然后在实际读取过程当中却不是这样,实际读取到的是  类的对象值 重新运行时能够读取到 “全 ...

  10. hibernate FetchType理解

    JPA定义实体之间的关系有如下几种: @OneToOne @ManyToOne @OneToMany @ManyToMany 在定义它们的时候可以通过fetch属性指定加载方式,有两个值: Fetch ...