①javascript
②html
③ java
④python
將 文本 轉換成 音頻jquery
from aip import AipSpeech """ 你的 APPID AK SK """ # 接口信息 APP_ID = '16027159' API_KEY = 'AZoiGc2udGqk9psiylinOZrU' SECRET_KEY = '9pa87qO9kkLiCxFjp2vCp9MRvM5LUPgV' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis('人的一切痛苦,本質都是對本身無能的憤怒', 'zh', 1, { 'vol': 8, "spd": 4, "pit": 6, "per": 3 }) # 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼 if not isinstance(result, dict): with open('auido.mp3', 'wb') as f: # 將合成數據放入 auido.mp3 文件中 f.write(result)
將音頻 轉換成 文本web
from aip import AipSpeech import os """ 你的 APPID AK SK """ APP_ID = '16027159' API_KEY = 'AZoiGc2udGqk9psiylinOZrU' SECRET_KEY = '9pa87qO9kkLiCxFjp2vCp9MRvM5LUPgV' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # 讀取文件 def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() # 識別本地文件 res = client.asr(get_file_content('name.pcm'), 'pcm', 16000, { 'dev_pid': 1536, }) print(res.get("result")[0])
①安裝ffmpegajax
②配置環境變量json
③基本結構flask
import os from aip import AipSpeech """ 你的 APPID AK SK """ APP_ID = '16027154' API_KEY = '5a8u0aLf2SxRGRMX3jbZ2VH0' SECRET_KEY = 'UAaqS13z6DjD9Qbjd065dAh0HjbqPrzV' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # 讀取文件 def get_file_content(filePath): os.system(f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm")
# 格式轉換命令 with open(f"{filePath}.pcm", 'rb') as fp: return fp.read() res = client.asr(get_file_content('name.wav'), 'pcm', 16000, { #pcm格式進行識別率高 'dev_pid': 1536, }) print(res.get("result")[0])
import os from aip import AipSpeech """ 你的 APPID AK SK """ APP_ID = '16027159' API_KEY = 'AZoiGc2udGqk9psiylinOZrU' SECRET_KEY = '9pa87qO9kkLiCxFjp2vCp9MRvM5LUPgV' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) def text2audio(text): #⑥ result = client.synthesis(text, 'zh', 1, { 'vol': 8, "spd": 4, "pit": 6, "per": 3 }) # 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼 if not isinstance(result, dict): with open('auido.mp3', 'wb') as f: f.write(result) return "auido.mp3" def audio2text(filepath): #② res = client.asr(get_file_content('name.wav'), 'pcm', 16000, { 'dev_pid': 1536, }) return res.get("result")[0] def get_file_content(filePath): #③ os.system(f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm") with open(f"{filePath}.pcm", 'rb') as fp: return fp.read() text= audio2text('name.wav') # ① if text == "你叫什麼": #④ filname = text2audio("個人名字是張三") #⑤ os.system(filname) #⑦
import os from aip import AipSpeech,AipNlp #導入模塊 """ 你的 APPID AK SK """ APP_ID = '16027159' API_KEY = 'AZoiGc2udGqk9psiylinOZrU' SECRET_KEY = '9pa87qO9kkLiCxFjp2vCp9MRvM5LUPgV' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) client_nlp = AipNlp(APP_ID, API_KEY, SECRET_KEY) #定義新對象 def text2audio(text): result = client.synthesis(text, 'zh', 1, { 'vol': 8, "spd": 4, "pit": 6, "per": 3 }) # 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼 if not isinstance(result, dict): with open('auido.mp3', 'wb') as f: f.write(result) return "auido.mp3" def audio2text(filepath): res = client.asr(get_file_content('name.wav'), 'pcm', 16000, { 'dev_pid': 1536, }) return res.get("result")[0] def get_file_content(filePath): os.system(f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm") with open(f"{filePath}.pcm", 'rb') as fp: return fp.read() text= audio2text('name.wav') score = client_nlp.simnet("你的名字是什麼",text).get('score') # 比較倆個對象,獲取類似度 print(score) if score >= 0.58: # 0.58 是分界線(是否是一個意思) filname = text2audio("個人名字是張三") os.system(filname) else: print('結束')
①
②
③
{ "reqType":0, "perception": { "inputText": { "text": "附近的酒店" }, "inputImage": { "url": "imageUrl" }, "selfInfo": { "location": { "city": "北京", "province": "北京", "street": "信息路" } } }, "userInfo": { "apiKey": "", "userId": "" } }
①必填
②至少有一個
③ 可不寫
④ 必填
URL = "http://openapi.tuling123.com/openapi/api/v2" import requests data = { "perception": { "inputText": { "text": "你叫什麼" } }, "userInfo": { "apiKey": "2acbb36ddfcc41b5ae0deb65428eea84", "userId": "123" } } res = requests.post("http://openapi.tuling123.com/openapi/api/v2",json=data) # print(res.content) # print(res.text) print(res.json())
import os from aip import AipSpeech,AipNlp """ 你的 APPID AK SK """ APP_ID = '16027159' API_KEY = 'AZoiGc2udGqk9psiylinOZrU' SECRET_KEY = '9pa87qO9kkLiCxFjp2vCp9MRvM5LUPgV' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) client_nlp = AipNlp(APP_ID, API_KEY, SECRET_KEY) def text2audio(text): result = client.synthesis(text, 'zh', 1, { 'vol': 8, "spd": 4, "pit": 6, "per": 3 }) # 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼 if not isinstance(result, dict): with open('auido.mp3', 'wb') as f: f.write(result) return "auido.mp3" def audio2text(filepath): res = client.asr(get_file_content('name.wav'), 'pcm', 16000, { 'dev_pid': 1536, }) return res.get("result")[0] def get_file_content(filePath): os.system(f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm") with open(f"{filePath}.pcm", 'rb') as fp: return fp.read() def goto_tl(text,uid): URL = "http://openapi.tuling123.com/openapi/api/v2" import requests data = { "perception": { "inputText": { "text": "天氣怎樣" } }, "userInfo": { "apiKey": "2acbb36ddfcc41b5ae0deb65428eea84", "userId": "123" } } data["perception"]["inputText"]["text"] = text # 替換請求信息 data["userInfo"]["userId"] = uid # 替換標識 res = requests.post(URL, json=data) # print(res.content) # print(res.text) print(res.json()) return res.json().get("results")[0].get("values").get("text") text= audio2text('name.wav') score = client_nlp.simnet("今每天氣怎樣",text).get('score') print(score) if score >= 0.58: filname = text2audio("個人名字是張三") os.system(filname) answer = goto_tl(text,"qiaoxiaoqiang") # 類似度小於0.58 時走圖靈機器人 filename = text2audio(answer) os.system(filename)
①導入三個文件
② webtoy.html 文件中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>我是玩具</title> </head> <body> <audio controls id="player" autoplay></audio> <p></p> <button onclick="start_reco()">錄音</button> <button onclick="stop_reco()">發送錄音</button> </body> <script type="text/javascript" src="/static/Recorder.js"></script> <script type="text/javascript" src="/static/jquery-3.3.1.min.js"></script> <script type="text/javascript"> var serv = "http://127.0.0.1:9527"; var reco = null; var audio_context = new AudioContext();//音頻內容對象 navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); navigator.getUserMedia({audio: true}, create_stream, function (err) { console.log(err) }); function create_stream(user_media) { // audio 麥克風和揚聲器 var stream_input = audio_context.createMediaStreamSource(user_media);//媒體流容器 reco = new Recorder(stream_input); } function start_reco() { reco.record(); } function stop_reco() { reco.stop(); reco.exportWAV(function (wav_file) { console.log(wav_file); // Blob 對象 var formdata = new FormData(); // form 表單 {key:value} formdata.append("reco",wav_file); // form - input type="file" name="reco" formdata.append("username","Alexander.DSB.Li"); // form input type="text / password 編輯框" name="username" value = "Alexander.DSB.Li" $.ajax({ url: serv + "/uploader", type: 'post', processData: false, contentType: false, data: formdata, dataType: 'json', success: function (data) { console.log(data); document.getElementById("player").src = serv + "/getaudio/" +data.filename; } }) });//異步 reco.clear(); } </script> </html>
③調用文件 app.py
import os from flask import Flask,render_template,jsonify,request,send_file from uuid import uuid4 # from flask_cors import CORS from FAQ import audio2text, client_nlp, text2audio, goto_tl app = Flask(__name__) # CORS(app) @app.route("/") def webtoy(): return render_template("WebToy.html") @app.route("/uploader",methods=["POST","GET"]) def uploader(): filename = f"{uuid4()}.wav" file = request.files.get("reco") file.save(filename) text = audio2text(filename) # 天然語言處理 LowB score = client_nlp.simnet("你叫什麼名字", text).get("score") print(score) if score >= 0.75: filename = text2audio("個人名字叫銀角大王八") else: answer = goto_tl(text, "qiaoxiaoqiang") filename = text2audio(answer) return jsonify({"code":0,"msg":"文件上傳成功","filename":filename}) @app.route("/getaudio/<filename>") def getaudio(filename): return send_file(filename) if __name__ == '__main__': app.run("127.0.0.1",9527,debug=True)
④ 調用文件用到的函數存在 FAQ.py文件中
import os from uuid import uuid4 from aip import AipSpeech,AipNlp """ 你的 APPID AK SK """ APP_ID = '16027154' API_KEY = '5a8u0aLf2SxRGRMX3jbZ2VH0' SECRET_KEY = 'UAaqS13z6DjD9Qbjd065dAh0HjbqPrzV' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) client_nlp = AipNlp(APP_ID, API_KEY, SECRET_KEY) # res = client_nlp.simnet("你叫什麼名字","你的名字是什麼") # print(res) def text2audio(text): filename = f"{uuid4()}.mp3" result = client.synthesis(text, 'zh', 1, { "spd": 4, 'vol': 5, "pit": 8, "per": 4 }) # 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼 if not isinstance(result, dict): with open(filename, 'wb') as f: f.write(result) return filename def audio2text(filepath): res = client.asr(get_file_content(filepath), 'pcm', 16000, { 'dev_pid': 1536, }) print(res) return res.get("result")[0] def get_file_content(filePath): os.system(f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm") with open(f"{filePath}.pcm", 'rb') as fp: return fp.read() def goto_tl(text,uid): URL = "http://openapi.tuling123.com/openapi/api/v2" import requests data = { "perception": { "inputText": { "text": "你叫什麼名字" } }, "userInfo": { "apiKey": "be41cf8596a24aec95b0e86be895cfa9", "userId": "123" } } data["perception"]["inputText"]["text"] = text data["userInfo"]["userId"] = uid res = requests.post(URL, json=data) # print(res.content) # print(res.text) print(res.json()) return res.json().get("results")[0].get("values").get("text")