天天看點

百度雲 OCR 識别圖檔驗證碼

作業系統:Mac OS

Python版本:3.7.2

OCR:百度雲

遇到的問題:

API測試過程中,遇到API Resopnse 為圖檔驗證碼的情況,需要對圖檔進行識别得到text code,進行斷言或者下一步操作。

驗證碼圖檔:

百度雲 OCR 識别圖檔驗證碼

直接使用OCR識别圖檔結果為:

/usr/local/bin/python3.7 /Users/test.py
-----> hci

Process finished with exit code 0
           

由于圖檔帶有幹擾線且文本不規則,是以出現識别錯誤的情況。

解決方案:

  • 對原圖檔進行“灰階轉換”處理
  • 二值化
  • 百度雲OCR識别(點選檢視如何使用)

Python代碼實作

from PIL import Image
from aip import AipOcr

# 填入百度OCR API 提供的參數
config = {
    'appId': '---',
    'apiKey': '---',
    'secretKey': '---'
}

client = AipOcr(**config)


""" 1.将圖檔進行降噪處理, 通過二值化去掉後面的背景色并加深文字對比度 """
def processing_image(img_file, standard=127.5):
    img = Image.open(img_file)

    # 灰階轉換
    _image = img.convert('L')

    # 二值化: 根據門檻值 standard, 将所有像素都置為 0(黑色) 或 255(白色), 便于接下來的分割
    pixels = _image.load()
    for x in range(_image.width):
        for y in range(_image.height):
            if pixels[x, y] > standard:
                pixels[x, y] = 255
            else:
                pixels[x, y] = 0
    return _image


def get_file_content(file_path):
    with open(file_path, 'rb') as fp:
        return fp.read()


""" 2.将處理後的圖檔另存為b.png """
image_b = processing_image('a.png')
image_b.save('b.png')
# image_b.show()

""" 3. 通過百度OCR識别b.png"""
image = get_file_content('b.png')
result = client.basicAccurate(image)

text = '\n'.join([w['words'] for w in result['words_result']])
text = text.replace(' ', '')
print('----->', text)
           

結果列印

/usr/local/bin/python3.7 /Users/test.py
----->  hxciV

Process finished with exit code 0
           

大功搞成!

繼續閱讀