资源经验分享OCR百度api,python实现图像文字识别

OCR百度api,python实现图像文字识别

2019-10-08 | |  119 |   0

原标题:OCR百度api,python实现图像文字识别

原文来自:CSDN      原文链接:https://blog.csdn.net/weixin_45706382/article/details/102185893


from urllib.request import Request,urlopen
# client_id 为官网获取的AK, client_secret 为官网获取的SK
url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【API Key】&client_secret=【Secret Key】'
from fake_useragent import UserAgent
headers = {'User-Agent':UserAgent().random}
request = Request(url,headers=headers)
response = urlopen(request)
content = response.read().decode()
if (content):
    print(content)
with open('access_token.txt','w+') as f:
    f.write(content)
 
 
获取  access_token
import requests
import base64
from fake_useragent import UserAgent
headers = {'User-Agent':UserAgent().random}
access_token = ''
api_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/webimage?{}'.format(access_token)
img_url = 'http://qidian.qpic.cn/qidian_common/349573/c290ad5bb680b5078f57fc68a0f7531a/0'
i = requests.get(img_url,headers=headers).content
with open('a.jpg','wb')as f:
    f.write(i)
    f.close()
img = open('a.jpg','rb').read()
print(img)
image = base64.b64encode(img)
#编码一定要转换
print(image)
data = {
    'image':image,
    'access_token':access_token
}
#                  str类型      {}类型     {}类型
r = requests.post(api_url,headers=headers,data=data).text
print(r)

免责声明:本文来自互联网新闻客户端自媒体,不代表本网的观点和立场。

合作及投稿邮箱:E-mail:editor@tusaishared.com

上一篇:一分钟了解:a*算法

下一篇:结合OpenCV与TensorFlow进行人脸识别

用户评价
全部评价

热门资源

  • Python 爬虫(二)...

    所谓爬虫就是模拟客户端发送网络请求,获取网络响...

  • TensorFlow从1到2...

    原文第四篇中,我们介绍了官方的入门案例MNIST,功...

  • TensorFlow从1到2...

    “回归”这个词,既是Regression算法的名称,也代表...

  • 机器学习中的熵、...

    熵 (entropy) 这一词最初来源于热力学。1948年,克...

  • TensorFlow2.0(10...

    前面的博客中我们说过,在加载数据和预处理数据时...