Based OCR recognition Baidu cloud (Python)

July 3, 2019 morning, Baidu AI developer conference, a youth from Shanxi, bottle of mineral water will be poured on the body of Robin Li, also from Shanxi.

You can recall https://b23.tv/av57665929/p1, people really surprised, since such a large meeting people so easily accessible chiefs.

(Source network)

OCR recognition preparations

Baidu cloud really is paradise test interface, the interface a lot of free, of course, there are limits on the amount, but it is completely sufficient for personal use, what face recognition, MQTT servers, voice recognition and so on, everything it

Look at the amount of free OCR recognition

first step:

Create a character recognition application, which is to apply a character recognition Baidu cloud user interface

 

Step two:

Open technical documentation, covering today's popular programming languages

https://cloud.baidu.com/doc/OCR/s/ejwvxzls6/

third step:

Installation OCR Python SDK, OCR Python SDK directory structure

├── README.md
├── aip                   //SDK目录
│   ├── __init__.py       //导出类
│   ├── base.py           //aip基类
│   ├── http.py           //http请求
│   └── ocr.py //OCR
└── setup.py              //setuptools安装

Support Python Version: 2.7 + 3 +.

Installation command:

In the win10 environment through the installation you need to turn off the current cmd cmd window re-opens cmd window command python program before they achieve the correct

pip install baidu-aip

Test code

Universal character recognition

from aip import AipOcr

#更换为自己的注册信息
APP_ID = '---'
API_KEY = '---'
SECRET_KEY = '---'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)#创建连接
fp=open("tu2.png","rb").read()#打开并读取文件内容
res=client.basicGeneral(fp)#普通
#print(res)

#将所有的文字都合并到一起
strx=""
for tex in res["words_result"]:#遍历结果
    strx+=tex["words"]#每一行
print(strx)#输出内容

通用文字识别(高精度版)

from aip import AipOcr

#更换为自己的注册信息
APP_ID = '---'
API_KEY = '---'
SECRET_KEY = '---'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)#创建连接
fp=open("tu2.png","rb").read()#打开并读取文件内容
#res=client.basicGeneral(fp)#普通
res=client.basicAccurate(fp)#高精度
#print(res)

#将所有的文字都合并到一起
strx=""
for tex in res["words_result"]:#遍历结果
    strx+=tex["words"]#每一行
print(strx)#输出内容

 URL图片地址方式

from aip import AipOcr

#更换为自己的注册信息
APP_ID = '----'
API_KEY = '----'
SECRET_KEY = '-----'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)#创建连接
url = "https://img2018.cnblogs.com/blog/1485202/201907/1485202-20190705210445649-2093672772.png"
res=client.basicGeneralUrl(url)#普通
#print(res)

#将所有的文字都合并到一起
strx=""
for tex in res["words_result"]:#遍历结果
    strx+=tex["words"]
print(strx)#输出内容

注意

图片格式(image):

图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式

URL格式(url):

图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,当image字段存在时url字段失效

语言设置(language_type)

识别语言类型,默认为CHN_ENG

可设置:- CHN_ENG:中英文混合;- ENG:英文;- POR:葡萄牙语;- FRE:法语;- GER:德语;- ITA:意大利语;- SPA:西班牙语;- RUS:俄语;- JAP:日语;- KOR:韩语;

图像倒置设置(detect_direction)

是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:- true:检测朝向;- false:不检测朝向

是否检测语言(detect_language)

是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语),值为flase或者true

是否返回识别结果中每一行的置信度(probability)

值为flase或者true

参数添加举例

""" 如果有可选参数 """
options = {}
options["detect_direction"] = "true"
options["detect_language"] = "true"

""" 带参数调用网络图片文字识别, 图片参数为远程url图片 """
client.webImageUrl(url, options)

 更多参考:

https://cloud.baidu.com/doc/OCR/s/Rjwvxzm3n

Guess you like

Origin www.cnblogs.com/dongxiaodong/p/11140680.html