Created
July 19, 2019 07:22
-
-
Save R97416032/6ee48f45b996faaaef4d6d147df9d215 to your computer and use it in GitHub Desktop.
基于百度api实现图像文字识别
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.parse | |
import urllib.request | |
import sys | |
import ssl | |
import base64 | |
access_token = '' | |
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=' + access_token | |
# 二进制方式打开图文件 | |
f = open(r'C:\Users\R\Desktop\QQ截图20190309100002.png', 'rb') | |
# 参数image:图像base64编码 | |
img = base64.b64encode(f.read()) | |
params = {"image": img} | |
params = urllib.parse.urlencode(params) | |
request = urllib.request.Request(url, params) | |
request.add_header('Content-Type', 'application/x-www-form-urlencoded') | |
response = urllib.request.urlopen(request) | |
content = response.read() | |
if (content): | |
print(content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment