python实现图像识别功能

yipeiwu_com6年前Python基础

本文实例为大家分享了python实现图像识别的具体代码,供大家参考,具体内容如下

#! /usr/bin/env python 
 
from PIL import Image 
import pytesseract 
 
url='img/denggao.jpeg' 
image=Image.open(url) 
#image=image.convert('RGB') # RGB 
image=image.convert('L') # 灰度 
image.load() 
text=pytesseract.image_to_string(image) 
print text 
#image.show() 
 
r'''''# 
zhongwen_url = 'img/zhongwen003.png' 
import os 
fn = "aaaa" 
# sudo apt-get install tesseract 
cmd = "tesseract " + zhongwen_url + " " + fn + " -l chi_sim" 
os.system(cmd) 
 
with open(fn+".txt", "r") as f: 
  print f 
 
 
ret=os.system('cat /etc/pam.conf') 
print ret 
print '----------------------' 
ret=os.popen('cat /etc/pam.conf') 
print ret''' 
 
r''''' 
import os 
import subprocess 
 
def image_to_string(img, cleanup=True, plus=''): 
  # cleanup为True则识别完成后删除生成的文本文件 
  # plus参数为给tesseract的附加高级参数 
  subprocess.check_output('tesseract ' + img + ' ' + 
              img + ' ' + plus, shell=True) # 生成同名txt文件 
  text = '' 
  with open(img + '.txt', 'r') as f: 
    text = f.read().strip() 
  if cleanup: 
    os.remove(img + '.txt') 
  return text 
# run >>> 
# print(image_to_string('./phototest.tif')) # 打印识别出的文本,删除txt文件 
# print(image_to_string('./phototest.tif', False)) # 打印识别出的文本,不删除txt文件 
# print(image_to_string('./phototest.tif', False, '-l eng')) # 打印识别出的文本,不删除txt文件,同时提供高级参数 
 
# PyTesser废弃... 
''' 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python Numpy 自然数填充数组的实现

今天学习Numpy时,想到了一个小问题。在Numpy中,随机生成array是比较容易的,用np.random.rand即可。如下 a = np.random.rand(3,4) 可...

利用python实现AR教程

利用python实现AR教程

先了解如何利用python语言实现以平面和标记物进行姿态估计 本实验只是先实现一个简单的小例子。简单来说就是先识别出图像中的参考面,再拍摄一张目标图像,将参考面顶部的3D模型投影到目标图...

numpy.std() 计算矩阵标准差的方法

计算矩阵标准差 >>> a = np.array([[1, 2], [3, 4]]) >>> np.std(a) # 计算全局标准差 1.1180...

Python中类的创建和实例化操作示例

本文实例讲述了Python中类的创建和实例化操作。分享给大家供大家参考,具体如下: python中同样使用关键字class创建一个类,类名称第一个字母大写,可以带括号也可以不带括号; p...

如何利用python给图片添加半透明水印

如何利用python给图片添加半透明水印

前言 本文主要给大家介绍了关于python图片添加半透明水印的相关资料,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 示例代码: # coding:utf-8 f...