python图片识别文字

# -*- coding: utf-8 -*-
__author__ = 'Administrator'
__time__ = '2018-09-10 下午 3:55'


import urllib,requests,re,json,base64,io,sys
import os
import time

sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')

#调用接口
def invokApi(*parameter,**headers):
    response  = requests.post(*parameter,**headers).text
    return response

#解析json获取access_token
def getAccess_token(response):
    token_json = json.loads(response)
    access_token = token_json['access_token']
    return access_token
#循环读取文件夹中的文件
def ReadFileList():
    caseList1 = [] #定义数组
    path = r'd:\pic'
    for line in path:#打开文本并读取文本值
       if line != '\n' and not line.startswith("#"):#判断筛选空格符和#号
        caseList1.append(line.replace("\n", ""))#插入到数组并每行空格符
        #path = r'd:\pic'
        #filenames = os.listdir(path)
        #for filename in filenames:
        #return ReadFileList
    for r in range(10):
     r=r+1
    for c in caseList1:#循环数组
     time.sleep(1)
     return c
     print(c)

if __name__=="__main__":
#【接口参数】获取AccessToken,具体参数说明可参看百度AI的API文档
    token_url = 'https://aip.baidubce.com/oauth/2.0/token'
    grant_type = 'client_credentials'
    client_id = '9aCY0A19pxq7uGX8t19dXriK'
    client_secret = 'CPY6dvLCWs3ZsIFp7R7LZ7U7zBhXVeNk'
    token_parameter = {'grant_type':grant_type,'client_id':client_id,'client_secret':client_secret}

#获取access_token
    access_token = getAccess_token(invokApi(token_url,token_parameter))

#【接口参数】百度通用文字识别
    words_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
    headers = {'Content-Type':'application/x-www-form-urlencoded'}
    print(ReadFileList())

    picture = '2018091004.jpg'
    # for picture in ReadFileList():
       # ss=json.loads(ReadFileList['picture'])
        #print(ss)

        #二进制方式打开图文件
    f = open('D:/pic/'+ picture, 'rb')
        #图像base64编码


    img = base64.b64encode(f.read())
    img_parameter = {'image':img,'access_token':access_token}

#获取识别结果并打印
    recognition_word = invokApi(words_url,img_parameter,headers=headers)
   # print(recognition_word)

    d = json.loads(recognition_word)
    for wr in d['words_result']:
        print(wr['words'])
         #保存到文件
        fout = open("d:/a.txt", "a", encoding="utf-8")
        fout.write(wr['words']+'\n')
        fout.close()


   # printWords(getWords(recognition_word))
   # printWords(getWords(recognition_word))

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值