1.使用pytesseract和PIL库
(1)安装pytesseract和PIL库:pycharm->settings->project interpreter
(2)安装识别引擎tesseract-ocr:点击打开链接
from PIL import Image
import pytesseract
text=pytesseract.image_to_string(Image.open('test3.png'),lang='chi_sim')
print(text)
但是识别英文和数字效果还可以,识别中文效果较差。
2.利用百度AI提供的python SDK
安装OCR Python SDK:pip install baidu-aip
百度AI python SDK文档中心:点击打开链接
(1)通用文字识别:
# -*- coding: UTF-8 -*-
from aip import AipOcr
# 定义常量
APP_ID = '11352343'
API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE'
SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os'
# 初始化AipFace对象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 读取图片
filePath = "test3.png"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 定义参数变量
options = {
'detect_direction': 'true',
'language_type': 'CHN_ENG',
}
# 调用通用文字识别接口
result = aipOcr.basicGeneral(get_file_content(filePath), options)
print(result)
words_result=result['words_result']
for i in range(len(words_result)):
print(words_result[i]['words'])
(2)表格文字识别:
# -*- coding: UTF-8 -*-
from aip import AipOcr
import time
# 定义常量
APP_ID = '11352343'
API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE'
SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os'
# 初始化AipFace对象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 读取图片
filePath = "1-2.png"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
options1={}
# 调用通用文字识别接口
result1 = aipOcr.tableRecognitionAsync(get_file_content(filePath), options1 )
requestID=result1['result'][0]['request_id']
time.sleep(20) #为了'ret_msg': '已完成'
options2 = {}
options2["result_type"] = "json"
result2=aipOcr.getTableRecognitionResult(requestID,options2)
print(result2)
str_result=str(result2)
begin=str_result.find('word')
for i in range(str_result.count('word')):
print(str_result[begin+7:str_result.find('"',begin+8)])
begin=str_result.find('word',begin+1)
识别出来的结果需要再处理一下。