100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python 通过调取百度接口进行图片OCR文字识别 高识别率

python 通过调取百度接口进行图片OCR文字识别 高识别率

时间:2018-10-11 22:35:25

相关推荐

python  通过调取百度接口进行图片OCR文字识别 高识别率

一、客户需要一个判断工作证姓名和输入的姓名是否一致的需求,用Tesseract 试了下 发现识别率太低 ,只能另寻方法 ,想起百度的api接口,

1,需要先在百度https://login. 注册一个账号

2,进入到产品服务人工智能-->文字识别

3,在其中创建一个应用,可以发现通用文字识别的日免费量是50000次,应该是可以满足一部分项目的需求

4,创建应用后 在管理应用里 会看见你创建的应用和应用对应的AK和SK

二、好了准备工作OKl,接下来直接上代码(先看下效果),注:token有效时长一般为一个月可存储重复使用

1,测试识别的图片如下

2,识别效果如下 ,准确率高达100%

3,代码如下

import base64class CodeDemo:def __init__(self, AK, SK, code_url, img_path):self.AK = AKself.SK = SKself.code_url = code_urlself.img_path = img_pathself.access_token = self.get_access_token()def get_access_token(self):"""detect_direction:为true时会检测图片朝向,默认false"""token_host = '/oauth/2.0/token?grant_type=client_credentials&client_id={ak}&client_secret={sk}'.format(ak=self.AK, sk=self.SK)header = {'Content-Type': 'application/json; charset=UTF-8'}response = requests.post(url=token_host, headers=header)content = response.json()access_token = content.get("access_token")return access_tokendef getCode(self):header = {"Content-Type": "application/x-www-form-urlencoded"}def read_img(img_path):picture = requests.get(img_path)return base64.b64encode(picture.content).decode()image = read_img(self.img_path)response = requests.post(url=self.code_url, data={"image": image, "access_token": self.access_token,'detect_direction':'true'},headers=header)return response.json()if __name__ == '__main__':AK = 'xxxxxxxxxxxxx'SK = 'xxxxxxxxxxxxxxxxxxxxxxxxx'code_url = "/rest/2.0/ocr/v1/accurate" # 百度图片识别接口地址img_paths = "http://xxxxxx" # 识别图片的地址线上链接code_obj = CodeDemo(AK=AK, SK=SK, code_url=code_url, img_path=img_paths)res = code_obj.getCode()code_list = res.get("words_result")is_pass=Falseif "error_code" not in res.keys():code_list = res.get("words_result")for i in code_list:print i['words']

好了 如上就是调用百度OCR识别 ,百度的API接口:/docs#/OCR-API-GeneralBasic/top

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。