python利用自动识别写模块_Python 利用pytesser模块识别图像文字
使用的是python的pytesser模块,原先想做的是图片中文识别,搞了一段时间了,在中文的识别上还是有很多问题,这里做记录分享。
可将图片中的文字转换成文本(主要是英文)。
1.pytesser安装
使用设备:win8 64位
PyTesser使用Tesseract OCR引擎,将图像转换到可接受的格式,然后执行tesseract提取出文本信息。使用PyTesser ,你无须安装Tesseract OCR引擎,但必须要先安装PIL模块(Python Image Library,python的图形库)
其中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利用自动识别写模块_Python 利用pytesser模块识别图像文字相关推荐
- python利用什么写模板_Python利用逻辑回归分类实现模板
Logistic Regression Classifier逻辑回归主要思想就是用最大似然概率方法构建出方程,为最大化方程,利用牛顿梯度上升求解方程参数. 优点:计算代价不高,易于理解和实现. 缺点: ...
- python使用爬虫写一个自己的翻译器(带图像界面)
python使用爬虫写一个自己的翻译器(带图像界面) 大家好,我叫亓官劼(qí guān jié ),在CSDN中记录学习的点滴历程,时光荏苒,未来可期,加油~博客地址为:亓官劼的博客,B站昵称为 ...
- python利用自动识别写模块_序章:资料预处理(python3.6 可用fortran unformatted sequencial data读取模块)...
首先我只是一个接触Python约半年的菜鸟,开这一个专栏的目的主要是记录自己所学,以及实践的一些有用的东西,顺便分享一些自己写的公用代码段以方便具有同样想法的朋友. 既然是序章我就多写一些吧,我本人对 ...
- python笔记手写照片_Python处理手写笔记
下载W3Cschool手机App,0基础随时随地学编程导语 利用Python实现手写笔记的压缩与增强. 至于起因大概是: 这个内容很有趣... --> 说了等于没说.T_T 相关文件 密码: 4 ...
- 简述python中怎样导入模块_Python中导入模块的两种模式,import
import import pandas import pandas as pd 使用函数方式:.(),或者.() 比如 pandas.read_csv("data/stock.csv&qu ...
- cmd导入python模块_Python如何导入模块
为了在CMD中的任何位置打开python程序,将pyhon的安装目录导入到环境变量中,如我的安装目录D:\Python27,桌面计算机--->右键属性--->高级系统设置-->环境变 ...
- python验证码识别模块_Python图像处理之验证码识别
在上一篇博客Python图像处理之图片文字识别(OCR)中我们介绍了在Python中如何利用Tesseract软件来识别图片中的英文与中文,本文将具体介绍如何在Python中利用Tesseract软件 ...
- python必学的模块_Python常用的模块
模块和包 1.1模块介绍模块定义:一系列功能的集合体 模块使用: import导入模块 或者 from ... import... 导入模块 模块分类:内置模块 自定义模块 第三方模块 模块加载顺序: ...
- python中自带的模块_python中的模块详解
概念 python中的模块是什么?简而言之,在python中,一个文件(以".py"为后缀名的文件)就叫做一个模块,每一个模块在python里都被看做是一个独立的文件.模块可以被项 ...
最新文章
- Keras训练神经网络进行分类并使用GridSearchCV进行参数寻优
- 我在 CMU 的八年博士生涯...........
- Linux下安装Java(JDK8)
- 国内比较好的python中文教材-最好的Python入门教材是哪本?
- 九大排序算法,你会几个?
- 同济大学计算机直博,放弃直博浙江大学,选择保研同济大学!理性的他,做出这样的选择...
- 下一代Asp.net开发规范OWIN(3)—— Middleware
- 频率统计表用c语言_空间矢量脉宽调制建模与仿真(基于C语言的SIMULINK仿真模型 | 基于SVPWM模块的仿真)...
- 【Java 系列笔记】语法基础 + Spring + Spring MVC + SpringBoot + 数据结构
- python入门书?
- 图像文字识别易语言代码
- Eclipse| Eclipse安装中文版本教程
- iphone163邮件服务器设置,怎样在iphone上设置网易免费企业邮箱收发邮件
- python神经网络模型调用后预测值不变_用R语言实现神经网络预测股票实例
- Neo4j CQL语法
- ncbi blast MATLAB,NCBI在线BLAST使用方法与结果详解
- Android控件 SeekBar
- python 双重循环处理
- Elastic-Job开发指南
- python数据可视化书籍推荐_数据可视化的优秀入门书籍有哪些?