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))

 

你可能感兴趣的:(python)