一:自然語言處理
# -*- coding: utf-8 -*- # @Author : FELIX # @Date : 2018/5/18 9:47
# pip install baidu-aip from aip import AipNlp """ 你的 APPID AK SK 從百度開發者平台申請 """ APP_ID = '' API_KEY = '' SECRET_KEY = '' client = AipNlp(APP_ID, API_KEY, SECRET_KEY) text='百度是一家高科技公司' print(client.lexer(text)) text = "今天天氣怎么樣?" """ 調用依存句法分析 """ print(client.depParser(text)) """ 如果有可選參數 """ options = {} options["mode"] = 1 """ 帶參數調用依存句法分析 """ print(client.depParser(text, options)) word = "張飛" """ 調用詞向量表示 """ print(client.wordEmbedding(word)) text = "床前明月光" """ 調用DNN語言模型 """ print(client.dnnlm(text)) word1 = "北京" word2 = "上海" """ 調用詞義相似度 """ print(client.wordSimEmbedding(word1, word2)) """ 如果有可選參數 """ options = {} """ 帶參數調用詞義相似度 """ print(client.wordSimEmbedding(word1, word2, options)) text1 = "浙富股份" text2 = "萬事通自考網" """ 調用短文本相似度 """ print(client.simnet(text1, text2)) """ 如果有可選參數 """ options = {} options["model"] = "CNN" """ 帶參數調用短文本相似度 """ print(client.simnet(text1, text2, options)) text = "三星電腦電池不給力" """ 調用評論觀點抽取 """ print(client.commentTag(text)) """ 如果有可選參數 """ options = {} options["type"] = 13 """ 帶參數調用評論觀點抽取 """ print(client.commentTag(text, options)) text = "蘋果是一家偉大的公司" """ 調用情感傾向分析 """ print(client.sentimentClassify(text)) title = "iphone手機出現“白蘋果”原因及解決辦法,用蘋果手機的可以看下" content = "如果下面的方法還是沒有解決你的問題建議來我們門店看下成都市錦江區紅星路三段99號銀石廣場24層01室。" """ 調用文章標簽 """ print(client.keyword(title, content)) title = "歐洲冠軍杯足球賽" content = "歐洲冠軍聯賽是歐洲足球協會聯盟主辦的年度足球比賽,代表歐洲俱樂部足球最高榮譽和水平,被認為是全世界最高素質、最具影響力以及最高水平的俱樂部賽事,亦是世界上獎金最高的足球賽事和體育賽事之一。" """ 調用文章分類 """ print(client.topic(title, content))
二:圖像識別
# -*- coding: utf-8 -*- # @Author : FELIX # @Date : 2018/5/18 11:32 from aip import AipImageClassify """ 你的 APPID AK SK """ APP_ID = '' API_KEY = '' SECRET_KEY = '' client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY) """ 讀取圖片 """ def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() image = get_file_content('ma.jpg') """ 調用通用物體識別 """ print(client.advancedGeneral(image)) """ 調用菜品識別 """ print(client.dishDetect(image)) """ 調用車輛識別 """ print(client.carDetect(image)) """ 調用logo商標識別 """ print(client.logoSearch(image)) """ 調用動物識別 """ print(client.animalDetect(image)) """ 調用植物識別 """ print(client.plantDetect(image))
三:文字識別
# -*- coding: utf-8 -*- # @Author : FELIX # @Date : 2018/5/18 10:36 from aip import AipOcr """ 你的 APPID AK SK """ APP_ID = '' API_KEY = '' SECRET_KEY = '' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # """ 讀取圖片 """ # def get_file_content(filePath): # with open(filePath, 'rb') as fp: # return fp.read() # # image = get_file_content('code.jpg') # # """ 調用通用文字識別, 圖片參數為本地圖片 """ # print(client.basicGeneral(image)) # # """ 如果有可選參數 """ # options = {} # options["language_type"] = "CHN_ENG" # options["detect_direction"] = "true" # options["detect_language"] = "true" # options["probability"] = "true" # # """ 帶參數調用通用文字識別, 圖片參數為本地圖片 """ # print(client.basicGeneral(image, options)) # url = "https//www.x.com/sample.jpg" # # """ 調用通用文字識別, 圖片參數為遠程url圖片 """ # client.basicGeneralUrl(url) # # """ 如果有可選參數 """ # options = {} # options["language_type"] = "CHN_ENG" # options["detect_direction"] = "true" # options["detect_language"] = "true" # options["probability"] = "true" # # """ 帶參數調用通用文字識別, 圖片參數為遠程url圖片 """ # client.basicGeneralUrl(url, options) """ 讀取圖片 """ def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() image = get_file_content('code.jpg') """ 調用通用文字識別(含位置高精度版) """ print(client.accurate(image)) """ 調用網絡圖片文字識別, 圖片參數為本地圖片 """ print(client.webImage(image)) print(client.basicGeneral(image)) # """ 如果有可選參數 """ # options = {} # options["recognize_granularity"] = "big" # options["detect_direction"] = "true" # options["vertexes_location"] = "true" # options["probability"] = "true" # # """ 帶參數調用通用文字識別(含位置高精度版) """ # print(client.accurate(image, options))
四:語音文字識別
# -*- coding: utf-8 -*- # @Author : FELIX # @Date : 2018/5/18 10:08 from aip import AipSpeech """ 你的 APPID AK SK """ APP_ID = '' API_KEY = '' SECRET_KEY = '' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # result = client.synthesis('你好百度', 'zh', 1, { # 'vol': 5, # 'per':3, # }) # # # 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼 # if not isinstance(result, dict): # with open('auido.mp3', 'wb') as f: # f.write(result) # # 讀取文件 def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() # 識別本地文件 text=client.asr(get_file_content('auido.wav'), 'pcm', 16000, { 'dev_pid': 1536, }) print(text)