語音識別,標註數據

切割音頻文件

from pydub import AudioSegment
from pydub.silence import split_on_silence
import os

sound = AudioSegment.from_mp3("E:/data/AcsData/zfBX/hw202003301111246_23401.wav")
loudness = sound.dBFS
outputPath = "E:/data/AcsData/zfBX/output/"
chunks = split_on_silence(sound,
                          # 以沉默500毫秒,切割音頻文件
                          min_silence_len=500,
                          # 低於45分貝的聲音過濾
                          silence_thresh=-45,
                          #爲截出的每個音頻添加多少ms無聲
                          keep_silence=400
                          )
print('總分段:', len(chunks))
for i, chunk in enumerate(chunks):
    if os.path.exists(outputPath+"chunk{0}.wav".format(i)):
        os.remove(outputPath+"chunk{0}.wav".format(i))
    chunk.export(outputPath+"chunk{0}.wav".format(i), format="wav")

智能識別音頻

使用了百度acs識別接口

from aip import AipSpeech
""" 你的 APPID AK SK """
APP_ID = '你的appid'
API_KEY = '你的AK'
SECRET_KEY = '你的SK'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 讀取文件
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

path = "E:/data/AcsData/zfBX/output" #文件夾目錄
files= os.listdir(path) #得到文件夾下的所有文件名稱
s = []
strData=""
for file in files: #遍歷文件夾
     if not os.path.isdir(file): #判斷是否是文件夾,不是文件夾纔打開
          f = open(path+"/"+file); #打開文件
          fileName = get_file_content(path+"/"+file)
          # 識別本地文件
          a = client.asr(fileName, 'wav', 16000, {
              'dev_pid': 1537,
          })
          print("識別文件:",f)
          s.append(a.get('result')) #打印結果
          strData=strData+""+str(a.get('result'))
print(s)
#文件保存
with open(path+'output.txt','w') as f:
    f.write(str(strData))

識別結果:(內容太長,只展示部分結果)
[‘餵你好。’],[‘您好,請問什麼可以幫您,幫我查一下我這個用電的戶號啊?’],[‘然後我叫那個叫李琴。’],[‘然後叫那個叫李。’],[‘李先生,請問的。’],[’ 您這邊集中一下,用戶聯繫地址綁定的銀行卡,電話號碼,這邊都是能問一下行的。’],[‘呃,就是需要說哪一些嗎?’],[‘姓名。’],[‘綁定的電話號碼,銀行卡號。’],[‘啊,我這邊呢是那個廣州市珠海區環衛街十號。’],[‘就算請問是珠海還是海珠區呢啊,海珠區。’],[‘海珠區然後呢?’]…

獲取appid等信息地址:https://console.bce.baidu.com/ai/?_=1589890413217&fromai=1#/ai/speech/app/detail~appId=262785
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章