龙空技术网

Python通过百度云接口实现图文识字

Baby球 65

前言:

当前小伙伴们对“百度云python”大概比较关怀,我们都需要剖析一些“百度云python”的相关文章。那么小编在网络上收集了一些关于“百度云python””的相关知识,希望朋友们能喜欢,同学们一起来了解一下吧!

from aip import AipOcr# -----------调用百度识字API-----------------# 定义常量APP_ID = '17815248'API_KEY = 'VRHGlFx64Mvu3v6lpVcoFMeQ'SECRET_KEY = '2saLBYMqChX94CYcRQlSClDPYvSq6cRW'# 初始化AipFace对象aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)# 打开图片def get_file_content(filePath):    with open(filePath, 'rb') as fp:        return fp.read()# 调用通用文字识别接口def basicGeneral(file):    """ 如果有可选参数 """    options = {}    options[ "detect_direction" ] = "true"  # 检测朝向    options[ "detect_language" ] = "true"  # 检测语言    result = aipOcr.basicGeneral(file, options)    return (result)# 通用文字识别(高精度版)def basicAccurate(file):    options = {}    options[ "detect_direction" ] = "true"  # 检测朝向    options[ "detect_language" ] = "true"  # 检测语言    result = aipOcr.basicAccurate(file, options)    return (result)# 识别一些网络上背景复杂,特殊字体的文字。def webImage(file):    options = {}    options[ "detect_direction" ] = "true"  # 检测朝向    options[ "detect_language" ] = "true"  # 检测语言    result = aipOcr.webImage(file, options)    return (result)def main():    file = get_file_content("F:\IMG_2423.JPG")       # 图片文件路径    result = basicGeneral(file)    for word in result['words_result']:         # print(word['words'])         with open('1.txt', 'a', encoding='utf-8') as f:            f.write(word['words'])if __name__ == '__main__':    main()

本人亲测识别率在98以上。

标签: #百度云python