Python 百度图片识别实例

接口地址:http://yuyin.baidu.com/docs/tts/196

使用前安装baidu api包: pip install baidu-aip

from aip import AipOcr
import re
import os


imagePath = "d:\\images\\"
filesoure = imagePath
def baiduduqu(filesoure,filename):
   APP_ID = '你的 App ID'
   API_KEY = '你的 App KEY'
   SECRET_KEY = '你的 App SECRET_KEY'
   client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
   dakai = open(filename,'rb')
   duqu = dakai.read()
   message = client.basicGeneral(duqu)
   for duqu in message.get('words_result'):
      print(duqu.get('words'))
 
def filename (fielsoure,filetype):
   import os
   pathDir = os.listdir(filesoure)
   for allDir in pathDir:
      child = os.path.join('%s%s' % (filesoure,allDir))
      print(child)
      baiduduqu(filesoure,child)
def run():
   import os
   os.chdir(filesoure)
   for i in os.listdir(os.getcwd()):
      postfix = os.path.splitext(i)[1]
      if postfix == '.jpeg' or postfix == '.png':
         filename(filesoure,postfix)
 
if __name__ == '__main__':
   run()

 

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:Python 百度图片识别实例