Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import requests
- import json
- import os
- import time
- with open ("0.wav", "rb") as f:
- data = f.read()
- apiKey = 'f8c3d232-7420-4be7-9a53-3efa2f50ac3b'
- #f8c3d232-7420-4be7-9a53-3efa2f50ac3b
- # Step 1
- headers = {
- "X-API-KEY": apiKey
- }
- payloads = {
- 'Action':'connect'
- }
- url = 'https://iot.cht.com.tw/apis/CHTIoT/chtlasr/v2/MyServlet/tlasr'
- response = requests.request("POST", url, headers=headers, params=payloads)
- print(response.status_code)
- #print(response.text)
- asr_id = json.loads(response.text)['AsrReferenceId']
- # Step2
- '''
- mp3path = "10384880080604.wav" #可以改回 sys.argv[1]
- binarydata = removeWAVHeader(mp3path) #改用 wav 移除 header 模式讀取
- '''
- for i in range (1):
- headers = {
- "X-API-KEY": apiKey,
- "Content-type": "application/octet-stream",
- "Accept": "application/json"
- }
- payloads = {
- 'Action':'syncData',
- 'AsrReferenceId' : asr_id,
- 'ByteNum' : len(data),#os.path.getsize(mp3path), #ByteNum 應該是取的檔案的 length
- 'SpeechEnd' : 'y' #當作一個結束,除非使用迴圈繼續 post, 這裡當作測試一次就結束
- }
- # 送出音頻資料用 binary data 方式送出,不是 file
- response = requests.post(url, data=data, headers=headers, params=payloads)
- print(response.status_code)
- #print(response.text) # 要在結束 {SpeechEnd:'n'} 的時候看才會有 Result
- words = json.loads(response.text)['Result']
- print("結果: "+words)
- i = i+1
- '''
- payloads = {
- 'Action':'syncData',
- 'AsrReferenceId' : asr_id,
- 'ByteNum' : len(data), #os.path.getsize(mp3path), #ByteNum 應該是取的檔案的 length
- 'SpeechEnd' : 'y' #當作一個結束,除非使用迴圈繼續 post, 這裡當作測試一次就結束
- }
- response = requests.post(url, data=data, headers=headers, params=payloads)
- print(response.status_code)
- print(response.text) # 要在結束 {SpeechEnd:'n'} 的時候看才會有 Result
- '''
- # Step 3
- def stopRcg():
- headrs = {
- "X-API-KEY": apiKey
- }
- payloads = {
- 'Action':'stopRcg',
- 'AsrReferenceId' : asr_id
- }
- response = requests.request("POST", url, headers=headers, params=payloads)
- print(response.status_code)
- print(response.text)
- stopRcg()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement