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

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

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

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

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

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

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

1,测试识别的图片如下

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

3,代码如下

import base64
class CodeDemo:
  def __init__(self, AK, SK, code_url, img_path):
    self.AK = AK
    self.SK = SK
    self.code_url = code_url
    self.img_path = img_path
    self.access_token = self.get_access_token()

  def get_access_token(self):
    token_host = 'https://aip.baidubce.com/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_token

  def 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},headers=header)
    return response.json()


if __name__ == '__main__':
  AK = 'xxxxxxxxxxxxx'
  SK = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
  code_url = "https://aip.baidubce.com/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=False
  if "error_code" not in res.keys():
    code_list = res.get("words_result")
    for i in code_list:
      print i['words']

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章