Python中利用Tesseract軟件來識別圖片中的英文與中文


OCR與Tesseract介紹  

  將圖片翻譯成文字一般被稱為光學文字識別(Optical Character Recognition,OCR)。可以實現OCR 的底層庫並不多,目前很多庫都是使用共同的幾個底層OCR 庫,或者是在上面進行定制。
  Tesseract 是一個OCR 庫,目前由Google 贊助(Google 也是一家以OCR 和機器學習技術聞名於世的公司)。Tesseract 是目前公認最優秀、最精確的開源OCR 系統。
  除了極高的精確度,Tesseract 也具有很高的靈活性。它可以通過訓練識別出任何字體(只要這些字體的風格保持不變就可以),也可以識別出任何Unicode 字符。

Tesseract的安裝與使用  

  Tesseract的Windows安裝包下載地址為: http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-4.00.00dev.exe ,下載后雙擊直接安裝即可。安裝完后,需要將Tesseract添加到系統變量中。在CMD中輸入tesseract -v, 如顯示以下界面,則表示Tesseract安裝完成且添加到系統變量中。

  識別簡體中文,需要事先安裝簡體中文語言包,下載地址為:https://github.com/tesseract-ocr/tessdata/find/master/chi_sim.traineddata ,再講chi_sim.traineddata放在C:\Program Files (x86)\Tesseract-OCR\tessdata目錄下。

Python圖像處理之圖片驗證碼識別

  利用Tesseract軟件來識別圖片中的英文與中文,本文將具體介紹如何在Python中利用Tesseract軟件來識別驗證碼(數字加字母)。

  本文將具體介紹如何利用Python的圖像處理模塊pillow和OCR模塊pytesseract來識別上述驗證碼(數字加字母)。
    我們識別上述驗證碼的算法過程如下:

    1. 將原圖像進行灰度處理,轉化為灰度圖像;
    2. 獲取圖片中像素點數量最多的像素(此為圖片背景),將該像素作為閾值進行二值化處理,將灰度圖像轉化為黑白圖像(用來提高識別的准確率);
    3. 去掉黑白圖像中的噪聲,噪聲定義為:以該點為中心的九宮格的黑點的數量小於等於4;
    4. 利用pytesseract模塊識別,去掉識別結果中的特殊字符,獲得識別結果。

  完整的Python代碼如下:

import os
import pytesseract
from PIL import Image
from collections import defaultdict

# tesseract.exe所在的文件路徑
pytesseract.pytesseract.tesseract_cmd = 'C://Program Files (x86)/Tesseract-OCR/tesseract.exe'


# 獲取圖片中像素點數量最多的像素
def get_threshold(image):
    pixel_dict = defaultdict(int)

    # 像素及該像素出現次數的字典
    rows, cols = image.size
    for i in range(rows):
        for j in range(cols):
            pixel = image.getpixel((i, j))
            pixel_dict[pixel] += 1

    count_max = max(pixel_dict.values())  # 獲取像素出現出多的次數
    pixel_dict_reverse = {v: k for k, v in pixel_dict.items()}
    threshold = pixel_dict_reverse[count_max]  # 獲取出現次數最多的像素點

    return threshold


# 按照閾值進行二值化處理
# threshold: 像素閾值
def get_bin_table(threshold):
    # 獲取灰度轉二值的映射table
    table = []
    for i in range(256):
        rate = 0.1  # 在threshold的適當范圍內進行處理
        if threshold * (1 - rate) <= i <= threshold * (1 + rate):
            table.append(1)
        else:
            table.append(0)
    return table


# 去掉二值化處理后的圖片中的噪聲點
def cut_noise(image):
    rows, cols = image.size  # 圖片的寬度和高度
    change_pos = []  # 記錄噪聲點位置

    # 遍歷圖片中的每個點,除掉邊緣
    for i in range(1, rows - 1):
        for j in range(1, cols - 1):
            # pixel_set用來記錄該店附近的黑色像素的數量
            pixel_set = []
            # 取該點的鄰域為以該點為中心的九宮格
            for m in range(i - 1, i + 2):
                for n in range(j - 1, j + 2):
                    if image.getpixel((m, n)) != 1:  # 1為白色,0位黑色
                        pixel_set.append(image.getpixel((m, n)))

            # 如果該位置的九宮內的黑色數量小於等於4,則判斷為噪聲
            if len(pixel_set) <= 4:
                change_pos.append((i, j))

    # 對相應位置進行像素修改,將噪聲處的像素置為1(白色)
    for pos in change_pos:
        image.putpixel(pos, 1)

    return image  # 返回修改后的圖片


# 識別圖片中的數字加字母
# 傳入參數為圖片路徑,返回結果為:識別結果
def OCR_lmj(img_path):
    image = Image.open(img_path)  # 打開圖片文件
    imgry = image.convert('L')  # 轉化為灰度圖

    # 獲取圖片中的出現次數最多的像素,即為該圖片的背景
    max_pixel = get_threshold(imgry)

    # 將圖片進行二值化處理
    table = get_bin_table(threshold=max_pixel)
    out = imgry.point(table, '1')

    # 去掉圖片中的噪聲(孤立點)
    out = cut_noise(out)

    # 保存圖片
    # out.save('E://figures/img_gray.jpg')

    # 僅識別圖片中的數字
    # text = pytesseract.image_to_string(out, config='digits')
    # 識別圖片中的數字和字母
    text = pytesseract.image_to_string(out)

    # 去掉識別結果中的特殊字符
    exclude_char_list = ' .:\\|\'\"?![],()~@#$%^&*_+-={};<>/¥'
    text = ''.join([x for x in text if x not in exclude_char_list])
    # print(text)

    return text


def main():
    # 識別指定文件目錄下的圖片
    # 圖片存放目錄figures
    dir = 'E://figures'

    correct_count = 0  # 圖片總數
    total_count = 0  # 識別正確的圖片數量

    # 遍歷figures下的png,jpg文件
    for file in os.listdir(dir):
        if file.endswith('.png') or file.endswith('.jpg'):
            # print(file)
            image_path = '%s/%s' % (dir, file)  # 圖片路徑

            answer = file.split('.')[0]  # 圖片名稱,即圖片中的正確文字
            recognizition = OCR_lmj(image_path)  # 圖片識別的文字結果

            print((answer, recognizition))
            if recognizition == answer:  # 如果識別結果正確,則total_count加1
                correct_count += 1

            total_count += 1

    print('Total count: %d, correct: %d.' % (total_count, correct_count))
    '''
    # 單張圖片識別
    image_path = 'E://figures/code .jpg'
    OCR_lmj(image_path)
    '''

if __name__:
    main()

運行結果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM