1. 程式人生 > >linux安裝tesseract以及python呼叫tesseract-ocr

linux安裝tesseract以及python呼叫tesseract-ocr

安裝tesseract-orc前必要的依賴:

sudo apt-get install libpng12-dev
sudo apt-get install libjpeg62-dev
sudo apt-get install libtiff4-dev
sudo apt-get install gcc
sudo apt-get install g++
sudo apt-get install automake

安裝tessact-ocr:

sudo apt-get install tesseract-ocr

安裝語言檔案(英語,中文簡體):

sudo apt-get install tesseract-ocr-eng
sudo apt-get install tesseract-ocr-chi-sim

需要什麼語言庫,名字可以去列表找:

可以選擇安裝tesseract-ocr的圖形前端gimagereader:

sudo add-apt-repository ppa:sandromani/gimagereader
sudo apt-get update
sudo apt-get install gimagereader

tesseract只能在命令列下執行,基本使用方法如下:

tesseract imagename outputbase

封裝使用:

import subprocess

def img_tostr(img, clean = True, plus = ''):
# clean為True則識別完成後刪除生成的文字檔案
# plus引數為給tesseract的附加高階引數
    subprocess.call('tesseract'+ img + '' + img + '' + plus , shell=True)
    text=file(img + '.txt').read().strip()
    if cleanup:
        subprocess.remove(img + '.txt')
    return text

呼叫函式:

print (image_to_string('test.jpg'))
print (image_to_string('2.jpg', False, '-l eng'))
print (image_to_string('2.jpg', False, '-l chi_sim'))