要想使用百度图像识别api得先注册一个百度账号,然后创建一个图像识别应用,获取其中的APPID、AK、SK
账号注册什么的就不多说了,直接开始
创建应用
创建成功
代码
# -*- coding: utf-8 -*-
# @File : img2num.py
from aip import AipOcr
import urllib.request
'''
使用百度api读取图片中的文字
'''
""" 你的 APPID AK SK """
APP_ID = 'APPID '
API_KEY = 'AK '
SECRET_KEY = 'SK'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
options = {} # 配置字典
options["language_type"] = "CHN_ENG" # 识别文字类型
options["detect_direction"] = "true" # 是否检测图片的朝向
options["detect_language"] = "true" # 是否检测语言
options["probability"] = "true" # 是否返回置信度
def get_file_content(filePath): # 本地图片
with open(filePath, 'rb') as fp:
return fp.read() # 获取图片信息
# 获取网络图片内容
def get_img_word(url):
res = urllib.request.urlopen(url)
result = client.basicGeneral(res.read(), options)
if "words_result" not in result.keys(): # 当识别失败时返回图片地址
print(result)
print("无结果")
return url
word = ""
for i in result["words_result"]:
word = i['words']
print(word)
return word
def main():
img_url = "网络图片地址"
get_img_word(img_url)
if __name__ == '__main__':
main()
图片
转换结果