首頁 > 軟體

Python 識別錄音並轉為文字的實現

2022-03-08 13:03:37

程式功能: 用 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_2020My_AudioMy_AudioMy_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", "結束"))

到此這篇關於Python 識別錄音並轉為文字的實現的文章就介紹到這了,更多相關Python 識別錄音轉為文字內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com