需要注册,每次识别成功需要0.002元/次,详细接口文档见上面路径
说明:识别度高,基本不会错,但是收费
通用图片识别接口| 接口地址 | 说明 | Content-Type | 图片格式 | 返回格式 |
|---|---|---|---|---|
| http://api.ttshitu.com/predict | 模拟表单提交 | multipart/form-data;charset=UTF-8 | Byte[] 字节流 | JSON |
| PostData 为JSON格式 | application/json;charset=UTF-8 | base64 编码 | JSON |
1.第一种方式:
请求的数据类型:Content-Type:multipart/form-data;charset=UTF-8
import requests
request_url = "http://api.ttshitu.com/predict"
params = {
"username": "图鉴注册的用户名",
"password": "图鉴注册的密码"
}
# 需要识别验证码图片名称"code.png",open(图片的路径-这里使用相对路径,以字节流binary方式读取read图片)
files = {"image": ("code.png", open("code.png", "rb"))}
response = requests.request("post", request_url, params=params, files=files)
print(response.text)
1.第二种方式:
请求的数据类型:application/json;charset=UTF-8
import base64
import requests
# 读取图片
f = open("code.png", 'rb')
request_url = "http://api.ttshitu.com/predict"
params = {
"username": "图鉴注册的用户名",
"password": "图鉴注册的密码",
# 使用base64编码读取的字节对象,返回一个字节对象,注意要转string类型,请求参数value格式要为string,最后还要加encoding
"image": str(base64.b64encode(f.read()), encoding="utf-8")
}
response = requests.request("post", url, params=params)
print(response.text)
二、百度OCR
(官方文档:https://ai.baidu.com/ai-doc/OCR/Ck3h7y2ia)
说明:每个月有免费次数,识别度没有图鉴准确
步骤一:先获取鉴权使用的Access_token,通过API Key和Secret Key获取
API Key和Secret Key:注册登录百度智能云控制台(百度智能云-登录),打开文字识别菜单项,在概览界面【领取免费资源】,然后【创建应用】,创建完后在应用列表就能看到对应的API key和Secret Key。
通过上面的API Key和Secret Key,请求后的响应内容就包含Access_token值,更多详细内容看官方文档:https://ai.baidu.com/ai-doc/OCR/1k3h7y3db
import requests
# client_id 为官网获取的API Key, client_secret 为官网获取的Secret Key
host = "https://aip.baidubce.com/oauth/2.0/token?"
'grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
response = requests.get(host)
if response:
print(response.json())
# 获取响应内容的access_token值
access_token =req.json()["access_token"]
# print(access_token)
# 高精度通用文字识别接口路径
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
# base64编码返回字节对象
f = open('code.png', 'rb')
img = base64.b64encode(f.read())
headers = {
"Content-type": "application/json;charset=UTF-8"
}
params = {'image': img}
# 在请求路径的基础上加上鉴权access_token
re_url = request_url+'?access_token=' + access_token
# print(re_url)
resp = requests.request("post", re_url, params=params, headers=headers)
print(resp.text)



