Skip to content

Instantly share code, notes, and snippets.

@R97416032
Created July 19, 2019 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save R97416032/6ee48f45b996faaaef4d6d147df9d215 to your computer and use it in GitHub Desktop.
Save R97416032/6ee48f45b996faaaef4d6d147df9d215 to your computer and use it in GitHub Desktop.
基于百度api实现图像文字识别
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