Introducción
El último Python para hacer reconocimiento de texto OCR_Código fuente de Baidu , ahora se ha actualizado a la versión GUI
instalación
Vi al funcionario para saber que hay instalación de pip. . Busqué el SDK durante mucho tiempo
Método uno pip
pip install baidu-aip
Método dos sdk
Después de descargar el Python SDK , descomprímalo y ejecute setup.py descomprimido
python setup.py install
Crea una aplicación, obtén la clave
Abra Baidu Cloud, regístrese para la autenticación de nombre real,
ingrese a la consola, haga clic en reconocimiento de texto, cree una
aplicación y copie ID + clave en la aplicación creada
Empezar a reconocer
Texto a reconocer
Versión básica V0.0.0
# 利用百度OCR在线识别图片文本
from aip import AipOcr
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
if __name__ == "__main__":
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
image = get_file_content('tmp.PNG')
""" 调用通用文字识别, 图片参数为本地图片 """
res = client.basicGeneral(image);
print( res)
Para imágenes, condiciones de Baidu: datos de imagen, codificación base64, codificación base64 no requiere más de 4M, al menos el lado más corto de 15px, el lado más largo máximo 4096px, admite el formato jpg / png / bmp
basicGeneral () Devuelve la interpretación del formato
爱之初体验
Puede ver que los resultados devueltos son un poco desordenados. .
Versión mínima básica V0.0.0.1
Los datos devueltos anteriormente no se han resuelto todavía, solucionémoslos y reparemos a continuación
# 利用百度OCR在线识别图片文本
from aip import AipOcr
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
""" 待识别图片的路径 """
filepath = 'tmp.PNG'
""" 读取图片的编码 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
""" 将所有的文字都合并到一起 """
def get_text_concat(resdict):
strx=""
for tex in resdict["words_result"]:
strx+=tex["words"]
return(strx)
if __name__ == "__main__":
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
image = get_file_content(filepath)
""" 调用通用文字识别, 图片参数为本地图片 """
res = client.basicGeneral(image);
text = get_text_concat(res)
print( text)
Versión básica mejorada V0.0.1
Soporte para guardar archivos, manejo de excepciones
# 利用百度OCR在线识别图片文本
from aip import AipOcr
""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
""" 待识别图片的路径 """
filepath = 'tmp.PNG'
# 是否保存结果到文件中,True为保存,False为不保存,直接输出
isFileSave = True
# 若保存,保存路径为
saveFilePath = 'OcrText.txt'
# 是否保留上次文件中的内容,True为保存之前的内容,并在之后添加本次内容
# False为删除上次的内容,只保留本次识别结果
isLastSave = True
""" 读取图片的编码 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
""" 将所有的文字都合并到一起 """
def get_text_concat(resdict):
strx=""
for tex in resdict["words_result"]:
strx+=tex["words"]
return(strx)
""" 将数据data保存到filePath中 """
def save_text(filePath, data, isLastSave = True):
if(isLastSave):
with open(filePath, 'a', encoding='utf-8') as f:
f.write('\n'+ data)
else:
with open(filePath, 'w', encoding='utf-8') as f:
f.write(data)
if __name__ == "__main__":
print("OCR识别开始")
""" 调用通用文字识别, 图片参数为本地图片 """
try:
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
image = get_file_content(filepath)
res = client.basicGeneral(image)
text = get_text_concat(res)
if(isFileSave):
save_text(saveFilePath, text, isLastSave)
print("识别文字已保存到文件中")
else:
print( text)
print("OCR识别成功,程序结束")
except Exception as e:
print("OCR识别失败!!!程序非正常结束\n详情:",e)
Admite arrastrar imágenes. ¿
Procesar continuamente varias imágenes? ?