本文實例為大家分享了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廢棄...
'''
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。