zoukankan      html  css  js  c++  java
  • python 调用百度ORC进行文字识别

    最近大神推荐一个新的东西,orc文字识别,是免费的,感觉特别不错,所以打算自己弄来玩玩。

    首先要自己上百度申请一个账号https://cloud.baidu.com/product/ocr.html,登陆百度云,然后添加一个应用。创建好应用以后,就跟图片中一样,其中的API KEY 和 Secret Key 要记住,等会调用的时候要用到。

    创建好应用以后,首先要调用登陆接口来获得access_token,这里要用到APIKEY 和 SECRET KEY,代码如下

    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=这里替换成你的APIKEY&client_secret=这里替换成你的Secret key'
    request = urllib2.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib2.urlopen(request)
    content = response.read()
    if (content):
      coco=json.loads(content)
      print(coco['access_token'])

    获得access_token 以后就可以调用接口,接口文档在官网的技术文档里有,我这里就只示范一下通用版的api

    如果你想要文字识别网络图片

    url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
    values = {
            'access_token':coco['access_token'],#这里是你之前获取的access_token
            'url':imgurl,#这里是你要识别的网络图片的地址
            'Content-Type':'application/x-www-form-urlencoded',
            'language_type':'ENG'  #这里我的语言设置成只识别英文
        }
    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    the_page = response.read()
    result = json.loads(the_page)
    print result['words_result']

    尝试用来识别有些网站登陆时需要输入的验证码,感觉效果还不错,但是如果字母重叠了就会有误判。

    此外,如果想要识别本地的图片,需要对图片进行base64编码。

    with open("orc.png","rb") as f:  
            # b64encode是编码,b64decode是解码  
            imagedata = base64.b64encode(f.read())
    
    url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
    values = {
            'access_token':coco['access_token'],
            'image':imagedata,
            'Content-Type':'application/x-www-form-urlencoded',
            'language_type':'ENG'
        }
    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    the_page = response.read()
    result = json.loads(the_page)
    return result['words_result']

    下面我打包了一个函数,输入参数是网络图片的地址,输出就是识别结果,小伙伴们可以直接复制过去调用

    import urllib, urllib2, sys,json,base64
    import ssl
    def orcPic(imgurl):
        appkey = '你的appkey'
        secretkey= '你的secretkey'
        # client_id 为官网获取的AK, client_secret 为官网获取的SK
        host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+appkey+'&client_secret='+secretkey
        request = urllib2.Request(host)
        request.add_header('Content-Type', 'application/json; charset=UTF-8')
        response = urllib2.urlopen(request)
        content = response.read()
        if (content):
            coco=json.loads(content)
    
        url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
        values = {
            'access_token':coco['access_token'],
            'url':imgurl,
            'Content-Type':'application/x-www-form-urlencoded',
            'language_type':'ENG'
        }
        data = urllib.urlencode(values)
        req = urllib2.Request(url, data)
        response = urllib2.urlopen(req)
        the_page = response.read()
        result = json.loads(the_page)
        return result['words_result']
  • 相关阅读:
    利用GitHub和Hexo打造免费的个人博客 coder
    Android基础——项目的文件结构(二) coder
    25个Android酷炫开源UI框架 coder
    MarkDown使用教程(In Atom) coder
    Android基础——项目的文件结构(一) coder
    25类Android常用开源框架 coder
    Android Activity启动黑/白屏原因与解决方式 coder
    我的window phone 开发第一步
    Entity Framework 4 In Action 读书笔记
    最近在制作一套ASP.NET控件,已初见雏形
  • 原文地址:https://www.cnblogs.com/NewsunLs/p/8656582.html
Copyright © 2011-2022 走看看