• python3 图片文字识别


      最近用到了图片文字识别这个功能,从网上搜查了一下,决定利用百度的文字识别接口。通过测试发现文字识别率还可以。下面就测试过程简要说明一下

      1、注册用户

       链接:https://login.bce.baidu.com/?account=

       少量使用,免费

      

      2、创建一个应用

      

      3、调用方式

       python SDK文档:https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.html#.E9.80.9A.E7.94.A8.E6.96.87.E5.AD.97.E8.AF.86.E5.88.AB

       首先 安装  pip install baidu-aip

        快速入门:https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.html#.E5.BF.AB.E9.80.9F.E5.85.A5.E9.97.A8

        接口说明:https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.html#.E6.8E.A5.E5.8F.A3.E8.AF.B4.E6.98.8E

       具体实现代码:

    from aip import AipOcr
    class baiduApi:
        def __init__(self,APP_ID,API_KEY,SECRET_KEY):
            '''
            """ 你的 APPID AK SK """
            APP_ID = '你的 App ID'
            API_KEY = '你的 Api Key'
            SECRET_KEY = '你的 Secret Key'
            '''
            self.client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    
    
        """ 读取图片 """
        def get_file_content(self,imageFile):
            with open(imageFile, 'rb') as fp:
                return fp.read()
    
        def getWordFromImage(self,imageFile):
            image = self.get_file_content(imageFile)
            result = self.client.basicGeneral(image)
            print(result)
    
    if __name__=="__main__":
        APP_ID='15307894'
        API_KEY='eT4rkU2i2X2quti4Z5kIl8dT'
        SECRET_KEY='UCo2WIQoMq12TR98Nm2N1PgfhWT47'
        obj = baiduApi(APP_ID,API_KEY,SECRET_KEY)
        imageFile='E:\test5.png'
        obj.getWordFromImage(imageFile)

      另外一种调用方式

      参考:https://cloud.baidu.com/doc/OCR/OCR-API.html#.E8.B0.83.E7.94.A8.E6.96.B9.E5.BC.8F.E4.BA.8C

      【如果您对于使用API调用的方式很陌生,您可以参见:【只要10分钟 快速掌握文字识别】 教程

      可以利用调用方式一

      首先获取access_token: 必须参数,参考“Access Token获取”。

      注意:access_token的有效期为30天,需要每30天进行定期更换;

      获取后access_token例如文字识别API,使用HTTPS POST发送:

    https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=24.f9ba9c5241b67688bb4adbed8bc91dec.2592000.1485570332.282335-8574074

      

    import requests
    import json
    import base64
    import os
    class baiduApi:
        def __init__(self):
            pass
    
        def getAccess_Token(self,ApiKey='aTdOkc2i4X2qutd4Z5kIl8dT',SecretKey='UCodWIQgMq1NZTRdNmMNduN1PgfhWT47'):
            url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s'%(ApiKey,SecretKey)
            txt = requests.get(url).text
            dict1 = eval(txt)
            access_token = dict1.get('access_token')
            print(access_token)
            return access_token
    
            # txt ={"refresh_token":"25.0dc809836ad8546c79547ec12b4bf9d7.315360000.1869465297.282335-15907896","expires_in":2592000,
            #  "session_key":"9mzdWESprZJdkHm0iQOcHGX2Rn2qTwQ6Q9NIKPHwTu/DC9HqDzZzo8JJRfsE4q5OnATglorWcPy9WRFLKlrptCbdDx/Dug==",
            #  "access_token":"24.c4bab673c3edbe30eb334df37d4cf434.2592000.1556697297.282335-15907896"}
            #
            # access_token = txt.get('access_token')
            # print(access_token)
    
        def getdata(self,imageFile,access_token):
            imageBase64 = None
            if os.path.exists(imageFile):
                with open(imageFile, 'rb') as f:  # 以二进制读取图片
                    data = f.read()
                    imageBase64= base64.b64encode(data).decode()
            if imageBase64:
                data1 = {'image_type': 'BASE64',
                         'image': imageBase64,
                         'group_id': 'group001',
                         'user_id': 'D001'}
    
                headers = {'Content-type': 'application/x-www-form-urlencoded'}
                url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=%s' % access_token
                # post调用方式
                response = requests.post(url, data=data1, headers=headers)
                dict1 = json.loads(response.text)
                # print(dict1)
                words_result = dict1.get('words_result')
                word = ''
                for wd in words_result:
                    word = wd.get('words')
                    print(word)
                    # print(wd.get('words'))
                # return word

       

  • 相关阅读:
    zipalign内存对齐优化
    反编译 waring...
    android.os.NetworkOnMainThreadException
    android:LayoutInflater
    Notification NotificationManager RemoteViews PendingIntent
    WebView WebViewClient WebChromeClient
    寒假1
    冻死可怕了
    一个人失眠
    军训快乐
  • 原文地址:https://www.cnblogs.com/shaosks/p/10641646.html
Copyright © 2020-2023  润新知