Giter Club home page Giter Club logo

baidu_ocr's Introduction

BaiduOcr

百度 OCR 客户端,对百度公开的 API 提供更易用的 Python 封装。

百度 OCR API 说明

目前百度已经在其 APIStore 上提供了个人免费版和企业版两个版本的 OCR API,除 URL 不同外,API 是一样的。

下面是对 API 本身的说明。

  • 接口地址:

    免费版: http://apis.baidu.com/apistore/idlocr/ocr

    企业版: http://apis.baidu.com/idl_baidu/baiduocrpay/idlocrpaid

  • 请求方法: POST
  • 参数说明:
    参数名类型必填参数位置描述可用值
    fromdevicestringyesbodyParam来源设备android, iPhone, pc
    clientipstringyesbodyParam客户端出口ip
    detecttypestringyesbodyParamOCR服务类型LocateRecognize, Recognize, Locate, SingleCharRecognize
    languagetypestringyesbodyParam待检测的文字类型CHN_ENG, ENG, JAP, KOR
    imagetypestringyesbodyParam图片资源类型1: 经过BASE64处理的字符串; 2: 图片源文件
    imagestringyesbodyParam图片资源,300K以下,JPEG 格式
    apikeystringyesheaderAPI 授权代码
  • 返回值

    返回结果为 json 格式,对其中各字段的说明如下:

    字段值类型值的含义
    errNumunicode string结果状态,为 ‘0’ 时表示有结果且正常;否则出错(错误码见 API 详情页面)
    errMsgunicode string错误消息,当 errNum 不为 ‘0’ 时用于分析出错原因
    querySignunicode string本次请求的唯一性标识(暂时无用)
    retDatalist实际结果

    其中 retData 的至根据参数中的 detecttype 的不同而不同,下面对其进行具体说明

    • detecttype 为 LocateRecognize 时,retData 的内容为:
      [
          {
              u'word': u'xxxxx',    # 识别出的文字
              u'rect': {            # 识别出的文字区域信息
                  u'left': u'91',
                  u'width': u'782',
                  u'top': u'32',
                  u'height': u'24',
              }
          }
      ]
              
    • detecttype 为 Recognize 时,retData 的内容格式与 detecttype 为 LocateRecognize 时相同
    • detecttype 为 Locate 时,retData 的内容为图像上 从上到下 每一行文字的区域信息,如下所示
      [
          {u'rect': {
              u'left': u'91'
              u'width': u'782',
              u'top': u'32',
              u'height': u'24',
          }},
          ...
      ]
              
    • detecttype 为 SingleCharRecognize 时,retData 中为 多个候选的字符及其置信度 ,如下所示:
      [
          {
              u'word': u'g',
              u'prob': u'0.341407'
          },
          {
              u'word': u'p',
              u'prob': u'0.190242'
          },
          ....
      ]
              

安装

项目当前只在 Github 上托管,可通过以下方法进行安装

pip install git+https://github.com/Linusp/baidu_ocr.git

使用

命令行工具 bocr 使用:

usage: bocr [-h] [-i INPUT] [-s SERVICE] [-l LANG] [-t TYPE]

Recognize text from picuture.

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        specify picture want to recognize
  -s SERVICE, --service SERVICE
                        choose service from LocateRecognize(0), Recognize(1,
                        default), Locate(2), SingleCharRecognize(3)
  -l LANG, --lang LANG  specify language of text to be detected,
                        chn_eng(default) or eng
  -t TYPE, --type TYPE  set the api type, online or test(default)

命令行使用需要将 API Key(从前文提到的两个版本的 API 页面获取)写入到 HOME 目录下的 .bocr_key 文件中。

Python 库调用示例:

from baiduocr import BaiduOcr

API_KEY = 'your api key'
client = BaiduOcr(API_KEY, 'test')  # 使用个人免费版 API,企业版替换为 'online'

# client.ping()
res = client.recog('http://lyj.fj61.net/upload/2011-11/11110912327265.jpg',
                   service='Recognize', lang='CHN_ENG')

已知的一些问题

  1. 图片中文字为纯英文时,若将 lang 参数设置为 CHN_ENG/JAP,会得到以下错误(使用 datas/eng_with_image.jpg):
    {u'errNum': u'-20006', u'retData': None, u'querySign': None, u'errMsg': u'receive or parse error'}
        

    该错误号未在 APIStore 上进行说明

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.