百度云 ai接口 图片文字识别

文档中心

http://ai.baidu.com/docs#/

文字识别api文档

http://ai.baidu.com/docs#/OCR-API/e1bd77f3

通用文字识别对于背景比较简单,最好是纯色的效果最好

登陆后进入文字识别

创建项目,可以看到功能还是挺多的

使用通用识别对下面图片的识别结果,效果还是很不错的

 

{"log_id": 2993169583859972978, "words_result_num": 13,
 "words_result": [{"words": "8:01"}, 
                  {"words": "A4G L"}, 
                  {"words": "罗永浩"},
                  {"words": "一个集体光着屁股的"}, 
                  {"words": "皇帝方阵"},
                  {"words": "缓缓走过来的时候"}, 
                  {"words": "做了裁缝铺老板的孩子"},
                  {"words": "应该如何在内心崩溃的情况下"},
                  {"words": "保持本色"},
                  {"words": "并正常开展业务"}, 
                  {"words": "还有2天"},
                  {"words": "锤子科技2017秋季新品发布会|2017.11.0719:30"},
                  {"words": "知乎@黄"}]}

对于聊天类图片识别效果,背景图片的影响还是很大的

{"log_id": 598706437923341846, "words_result_num": 7,
 "words_result": [{"words": "a睡觉了"}, 
                  {"words": "回"}, 
                  {"words": "睡吧睡吧睡叭睡叭"}, 
                  {"words": "隔"}, 
                  {"words": "我亲爱的宝贝(a··@)"},
                  {"words": "爸爸的双手(=^V^=)轻轻"}, 
                  {"words": "摇着你"}]}

代码

import requests
import base64
ak = 'ak'
sk = 'sk'
# 根据ak和sk 获取 access_token
host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={ak}&client_secret={sk}'

ret = requests.get(host).json()
print(ret)
headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
}

# 将图片转为base64 编码
img_path = 'img2.jpg'
with open(img_path, mode='rb') as f:
    img_base64 = bytes.decode(base64.b64encode(f.read()))

img_url = "http://pic.58pic.com/58pic/11/63/45/30e58PICwVM.jpg"

# 数据可以使用base64格式或者网页url
data = {
    'image': img_base64
    # 'url': img_url
}
url = f"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={ret['access_token']}"
print(data)
print(url)
ans = requests.post(url, data=data, headers=headers).text
print(ans)

参数说明

HTTP 方法:POST

请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic

URL参数:

参数
access_token 通过API Key和Secret Key获取的access_token,参考“Access Token获取

Header如下:

参数
Content-Type application/x-www-form-urlencoded

Body中放置请求参数,参数详情如下:

请求参数

参数 是否必选 类型 可选值范围 说明
image 和url二选一 string - 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,当image字段存在时url字段失效
url 和image二选一 string - 图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,当image字段存在时url字段失效,不支持https的图片链接
language_type false string CHN_ENG、ENG、
POR、FRE、
GER、ITA、
SPA、RUS、
JAP、KOR
识别语言类型,默认为CHN_ENG。可选值包括:
- CHN_ENG:中英文混合;
- ENG:英文;
- POR:葡萄牙语;
- FRE:法语;
- GER:德语;
- ITA:意大利语;
- SPA:西班牙语;
- RUS:俄语;
- JAP:日语;
- KOR:韩语

猜你喜欢

转载自my.oschina.net/ahaoboy/blog/1814925