现在百度AI可以调用图像识别,有一些免费的可以玩一下。地址
1.需要的库
import base64
import urllib
from typing import BinaryIO
from urllib.parse import urlencode
from urllib import request
import requests
from urllib.request import urlopen
import json
2.获取token
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=百度后台获取的AK&client_secret=百度后台获取的SK'
headers = {
'Content-Type': 'application/json;charset=UTF-8'
}
res = requests.get(url=host, headers=headers).json()
access_token=res['access_token']
AK、SK获取地址
3.获取数据
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=' + access_token
f: BinaryIO = open(r'图片地址', 'rb')
op: bytes=f.read()
imgR = base64.b64encode(op)
params={'image':imgR}
params2 = urllib.parse.urlencode(params).encode(encoding='UTF8')
request = request.Request(url, params2)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urlopen(request)
content: object = response.read()
result: object=content.decode()
print(result)
4.解析json
json1=json.loads(result)
jsonArray=json1['words_result']
for i in range (len(jsonArray)):
words=jsonArray[i]['words']
print(words)