了解更多关注微信公众号“木下学Python”吧~
原文:https://blog.csdn.net/zjkpy_5/article/details/85173167
通过图片
import base64
import urllib3, base64
import json
from urllib.parse import urlencode
access_token = 'your'
http = urllib3.PoolManager()
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=' + access_token
f = open('image.png', 'rb')
参数image:图像base64编码
img = base64.b64encode(f.read())
params = {'image': img}
对base64数据进行urlencode处理
params = urlencode(params)
request = http.request('POST',
url,
body=params,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
对返回的byte字节进行处理。Python3输出位串,而不是可读的字符串,需要进行转换
result = str(request.data, 'utf-8')
返回参数json序列化处理
res = json.loads(result)
res = res['words_result'][0]['words']
print(res)
通过url
import base64
import urllib3,base64
import json
from urllib.parse import urlencode
access_token='your'
http=urllib3.PoolManager()
url='https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token='+access_token
img_url = 'https://raw.githubusercontent.com/Python3WebSpider/TestTess/master/image.png'
params={'url':img_url}
对base64数据进行urlencode处理
params=urlencode(params)
request=http.request('POST',
url,
body=params,
headers={'Content-Type':'application/x-www-form-urlencoded'})
对返回的byte字节进行处理。Python3输出位串,而不是可读的字符串,需要进行转换
result = str(request.data,'utf-8')
返回参数json序列化处理
res = json.loads(result)
print(res)
封装上述两种方法
import base64
import urllib3, base64
import json
from urllib.parse import urlencode
class CaptchaRecognition():
"""
调用百度云 api 识别验证码
"""
access_token = 'yours' #替换成你的
http = urllib3.PoolManager()
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=' + access_token
def __init__(self,image = '',image_url = ''):
"""
初始化图片文件对象和图片的 url
:param image: 识别图片的文件
:param image_url:识别图片的 url
"""
self.image = image
self.image_url = image_url
def byImage(self):
"""
通过图片识别
:return:
"""
f = open(self.image, 'rb')
# 参数image:图像base64编码
img = base64.b64encode(f.read())
params = {'image': img}
# 对base64数据进行urlencode处理
params = urlencode(params)
request = http.request('POST',
url,
body=params,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
# 对返回的byte字节进行处理。Python3输出位串,而不是可读的字符串,需要进行转换
result = str(request.data, 'utf-8')
# 返回参数json序列化处理
res = json.loads(result)
res = res['words_result'][0]['words']
return res
def byImageUrl(self):
"""
通过图片的 url 识别
:return:
"""
img_url = self.image_url
params = {'url': img_url}
# 对base64数据进行urlencode处理
params = urlencode(params)
request = http.request('POST',
url,
body=params,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
# 对返回的byte字节进行处理。Python3输出位串,而不是可读的字符串,需要进行转换
result = str(request.data, 'utf-8')
# 返回参数json序列化处理
res = json.loads(result)
return res
调用
from baiduyun import baiduyun
r = baiduyun.CaptchaRecognition(image='image.png')
res=r.byImage()
print(res)