網站首頁 編程語言 正文
程式功能: 用 UI 界面,點擊界面上的“開始識別”來錄音(調用百度云語音接口),并自動將結果顯示在界面的文本框中
Time: 2022/03/06
Author: Xiaohong
功能:Python 更改目錄下 目錄及文件的 順序命名
項目的文件結構方式:
1. PyQt5 UI 文件:? My_Audio_Record_cloud.ui
2. PyQt5 UI 文件轉換生成的 PY 文件:? My_Audio_Record_cloud_Ui.py
3. PyQt5 UI 文件對應的 Class 文件:? My_Audio_Record_cloud_class.py
4. 通用的消息顯示 文件(在My_Audio_Record_cloud_class.py 中被調用):? FangMessage.py
?本例為實驗室產品,不具備直接使用,支持的語音錄入長度也較短
主程序界面如下:
主程序 My_Audio_Record_cloud_class.py:
# -*- coding: utf-8 -*- ''' 程式功能: 用 UI 界面,點擊界面上的“開始識別”來錄音,并自動將結果顯示在界面的文本框中 Time: 2022/03/06 Author: Xiaohong ''' import wave # pip3 install wave import My_Audio_Record_cloud_Ui as my_audio_record_cloud from pyaudio import PyAudio, paInt16 # 直接用pip安裝的pyaudio不支持3.7 # 若安裝失敗的話,下載對應的whl 文件 https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio from PyQt5 import QtGui, QtCore, QtWidgets from PyQt5.QtWidgets import ( QApplication, QMainWindow, QDialog, QSplashScreen, QToolButton, QToolTip, QWidget, QMessageBox, QAction, QFileDialog, ) # from PyQt5.QtWidgets import ( # QApplication, # QWidget, # ) import sys, os, json, pycurl, urllib import urllib.request from FangMessage import FangMessage class Audio_record_cloud_class(QMainWindow, my_audio_record_cloud.Ui_MainWindow): def __init__(self, parent=None): super().__init__() self.child = my_audio_record_cloud.Ui_MainWindow() self.child.setupUi(self) self.file_name = "" self.child.pushButton.clicked.connect(self.my_start) # self.child.pb_play.clicked.connect(self.play_audio) # 錄音文件參數 self.framerate = 8000 self.NUM_SAMPLES = 2000 self.channels = 1 self.sampwidth = 2 # 錄音時長參數 self.TIME = 5 # 播放文件參數 self.chunk = 1024 # 設置默認的錄音文件名 # 當前目錄+test+當前的時間ID+'.wav' def init_file_name(self): file_path = os.getcwd() file_name = 'test' + self.get_timeseq() + '.wav' file_wav = os.path.join(file_path, file_name) self.file_name = file_wav # self.child.lineEdit.setText(self.file_name) # print(file_wav) return file_wav # 獲取當前的時間ID def get_timeseq(self): import time now = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) return now # 開始錄音 def Start_record(self): self.init_file_name() pa = PyAudio() stream = pa.open( format=paInt16, channels=1, rate=self.framerate, input=True, frames_per_buffer=self.NUM_SAMPLES, ) my_buf = [] count = 0 while count <= self.TIME * 4: string_audio_data = stream.read(self.NUM_SAMPLES) my_buf.append(string_audio_data) count += 1 print("..") # print('begin:') # print(my_buf) self.save_wave_file(self.file_name, my_buf) stream.close() FangMessage1 = FangMessage() FangMessage1.runY('完成', '已完成錄音', 'OK') # 保存聲音文件 def save_wave_file(self, filename, data): wf = wave.open(filename, 'wb') wf.setnchannels(self.channels) wf.setsampwidth(self.sampwidth) wf.setframerate(self.framerate) for i in data: wf.writeframes(i) wf.close() # 獲取 百度返回結果,并 Print def dump_res(self, buf): print(buf) my_temp = json.loads(buf) my_list = my_temp['result'] self.child.textBrowser.setText(my_list[0]) print(my_list[0]) # 訪問 百度云語音 網站,根據自己申請的應用Key 獲取本次訪問的 Token def get_token(self): apiKey = "XXXXXXXXXXXXXXXXXXXXXXX" secretKey = "YYYYYYYYYYYYYYYYYYYYYYYYY" auth_url = ( "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey ) # print(auth_url) res = urllib.request.urlopen(auth_url) json_data = res.read() # print(json_data) # print('.....') # print(json.loads(json_data)) return json.loads(json_data)['access_token'] # 訪問 百度云語音 網站,根據 Token,上傳 wav 文件 def use_cloud(self, token): fp = wave.open(self.file_name, 'rb') nf = fp.getnframes() print('sampwidth:', fp.getsampwidth()) print('framerate:', fp.getframerate()) print('channels:', fp.getnchannels()) f_len = nf * 2 audio_data = fp.readframes(nf) cuid = "4d36e972-e325-11ce-bfc1-08002be10318" print('token:') print(token) srv_url = ( 'http://vop.baidu.com/server_api' + '?cuid=' + cuid + '&token=' + token ) http_header = ['Content-Type:audio/pcm;rate=8000', 'Content-Length:%d' % f_len] c = pycurl.Curl() c.setopt(pycurl.URL, str(srv_url)) c.setopt(c.HTTPHEADER, http_header) c.setopt(c.POST, 1) c.setopt(c.CONNECTTIMEOUT, 80) c.setopt(c.TIMEOUT, 80) c.setopt(c.WRITEFUNCTION, self.dump_res) c.setopt(c.POSTFIELDS, audio_data) c.setopt(c.POSTFIELDSIZE, f_len) c.perform() def my_start(self): print('OK') self.Start_record() self.use_cloud(self.get_token()) if __name__ == "__main__": app = QApplication(sys.argv) myWin = Audio_record_cloud_class() myWin.show() sys.exit(app.exec_())
Ui 轉化py文件如下:My_Audio_Record_cloud_Ui.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'd:\vscode_2020\My_Audio\My_Audio\My_Audio_Record_cloud.ui' # # Created by: PyQt5 UI code generator 5.15.0 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(558, 525) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget) self.textBrowser.setGeometry(QtCore.QRect(30, 50, 501, 351)) self.textBrowser.setObjectName("textBrowser") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(40, 420, 75, 23)) self.pushButton.setObjectName("pushButton") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(40, 460, 491, 16)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(30, 30, 161, 16)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(180, 10, 111, 31)) font = QtGui.QFont() font.setFamily("Agency FB") font.setPointSize(18) font.setBold(True) font.setWeight(75) self.label_3.setFont(font) self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(480, 20, 54, 12)) self.label_4.setObjectName("label_4") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(450, 420, 75, 23)) self.pushButton_2.setObjectName("pushButton_2") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 558, 23)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) self.pushButton_2.clicked.connect(MainWindow.close) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "開始識別")) self.label.setText(_translate("MainWindow", "說明:點擊“開始識別”按鈕來錄音,并通過百度語音的功能,自動將結果顯示在文本框中")) self.label_2.setText(_translate("MainWindow", "語音識別的結果:")) self.label_3.setText(_translate("MainWindow", "語音識別")) self.label_4.setText(_translate("MainWindow", "v20220306")) self.pushButton_2.setText(_translate("MainWindow", "結束"))
原文鏈接:https://blog.csdn.net/leader_ww/article/details/123317768
相關推薦
- 2022-04-08 Python裝飾器中@property使用詳解_python
- 2022-12-10 Qt界面中滑動條的實現方式_C 語言
- 2022-06-16 Python中的Super用法示例詳解_python
- 2022-09-13 Android?Studio實現智能聊天_Android
- 2022-04-23 arguments獲取當前所在函數
- 2022-06-07 進行數據處理的6個?Python?代碼塊分享_python
- 2022-10-26 Go?語言數據結構之雙鏈表學習教程_Golang
- 2022-07-21 IDEA報錯Error running ‘Application‘: Command line is
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支