用pyaudio做一個聲控打開瀏覽器的小程序


聲控打開瀏覽器思路很簡單:通過麥克風錄音產生一個音頻文件-->通過調用百度大腦的api識別音頻文件中的語音並返回字符串-->通過對字符串的處理使瀏覽器做出反應

 

通過麥克風錄音並產生wav文件:


 

CHUNK = 1024
FORMAT = pyaudio.paInt16
RATE = 8000 //一般8000的采樣率能識別出人說的話
CHANNELS = 1
record_second = 5 //先設定好幾個重要的待處理參數
def record_wav(to_dir=None):
    if to_dir == None:
        to_dir='./'

    pa = pyaudio.PyAudio()   //產生pyaudio對象

    stream = pa.open(format=FORMAT,
                     channels = CHANNELS,
                     rate = RATE,
                     input=True,
                     frames_per_buffer = CHUNK)//初始化流

    sava_buffer = []

    for i in range(0,int(RATE/CHUNK*record_second)):
        audio_data = stream.read(CHUNK)
        sava_buffer.append(audio_data)//將音頻文件寫入列表

    stream.stop_stream()
    stream.close()
    pa.terminate()

    file_name = datetime.now().strftime("%Y-%m-%d_%H_%M_%S")+'.wav'

    file_path = to_dir+file_name//創建保存文件目錄


    wf = wave.open(file_path,'wb')
    wf.setframerate(RATE)
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(pa.get_sample_size(FORMAT))
    wf.writeframes(b''.join(sava_buffer))        //寫入文件

    wf.close()

    return file_path

調用百度大腦api

def baiduys(object):
    VOICE_RATE = 8000
    WAVE_FILE = object
    USER_ID = 'joker'
    WAVE_TYPE = 'wav'

    baidu_server = 'https://openapi.baidu.com/oauth/2.0/token?'
    grant_type = 'client_credentials'

  //以下兩行需要自己去申請百度開發者賬號以獲取 client_id='' client_secret = '' url = baidu_server+'grant_type='+grant_type+'&client_id='+client_id+'&client_secret='+client_secret //以上操作均為生成一個用來請求token的url res = urllib.request.urlopen(url).read() data = json.loads(res)//將json轉換為字典 token = data['access_token'] with open(WAVE_FILE,'rb') as f://打開wav音頻文件 fe = f.read() speech = base64.b64encode(fe) speech1 = speech.decode('utf-8') size = os.path.getsize(WAVE_FILE)      //將字典轉換為json用來傳輸 update = json.dumps({"format":WAVE_TYPE,"rate":VOICE_RATE,"channel":1,'token':token,'cuid':USER_ID,'speech':speech1,'len':size}) update1 = update.encode('utf-8') headers = {'Content-Type':'application/json'} url = 'https://vop.baidu.com/server_api' req = urllib.request.Request(url,update1,headers) r= urllib.request.urlopen(req)//通過上傳語音文件以獲得識別內容 t= r.read() ans =json.loads(t) //獲得的內容仍是json if ans['err_msg']=='success.': result = ans['result'][0].encode('utf-8') if result!='': return result.decode('utf-8') else: print(u'不存在文件0') else: print(u'錯誤')

處理字符串並作出反應:

def text_open_browser(text):
    url = ""
    if text:
        if len(re.split(u"谷歌",text))>1 or len(re.split('google',text))>1:
            url = 'https://www.google.com'
        elif len(re.split(u'百度',text))>1 or len(re.split('baidu',text))>1:
            url = 'https://www.baidu.com'
    if text != "":
        webbrowser.open_new_tab(url)
    else:
        print('no')

 

就是一個簡單的關鍵詞檢索並用webbrowser模塊根據關鍵詞所匹配的網址打開瀏覽器。。。。

 

完整代碼:

import base64
from datetime import datetime
import json
import os
import urllib.request

import wave
import webbrowser

import pyaudio
import re

CHUNK = 1024
FORMAT = pyaudio.paInt16
RATE = 8000
CHANNELS = 1
record_second = 5
def record_wav(to_dir=None):
    if to_dir == None:
        to_dir='./'

    pa = pyaudio.PyAudio()

    stream = pa.open(format=FORMAT,
                     channels = CHANNELS,
                     rate = RATE,
                     input=True,
                     frames_per_buffer = CHUNK)

    sava_buffer = []

    for i in range(0,int(RATE/CHUNK*record_second)):
        audio_data = stream.read(CHUNK)
        sava_buffer.append(audio_data)

    stream.stop_stream()
    stream.close()
    pa.terminate()

    file_name = datetime.now().strftime("%Y-%m-%d_%H_%M_%S")+'.wav'

    file_path = to_dir+file_name


    wf = wave.open(file_path,'wb')
    wf.setframerate(RATE)
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(pa.get_sample_size(FORMAT))
    wf.writeframes(b''.join(sava_buffer))

    wf.close()

    return file_path

def text_open_browser(text):
    url = ""
    if text:
        if len(re.split(u"谷歌",text))>1 or len(re.split('google',text))>1:
            url = 'https://www.google.com'
        elif len(re.split(u'百度',text))>1 or len(re.split('baidu',text))>1:
            url = 'https://www.baidu.com'
    if text != "":
        webbrowser.open_new_tab(url)
    else:
        print('no')

def baiduys(object):
    VOICE_RATE = 8000
    WAVE_FILE = object
    USER_ID = 'joker'
    WAVE_TYPE = 'wav'

    baidu_server = 'https://openapi.baidu.com/oauth/2.0/token?'
    grant_type = 'client_credentials'
    client_id=''
    client_secret = ''

    url = baidu_server+'grant_type='+grant_type+'&client_id='+client_id+'&client_secret='+client_secret

    res = urllib.request.urlopen(url).read()

    data = json.loads(res)

    token = data['access_token']

    with open(WAVE_FILE,'rb') as f:
        fe = f.read()
        speech =  base64.b64encode(fe)
        speech1 = speech.decode('utf-8')

        size = os.path.getsize(WAVE_FILE)


        update = json.dumps({"format":WAVE_TYPE,"rate":VOICE_RATE,"channel":1,'token':token,'cuid':USER_ID,'speech':speech1,'len':size})

        update1 = update.encode('utf-8')

        headers = {'Content-Type':'application/json'}

        url = 'https://vop.baidu.com/server_api'

        req = urllib.request.Request(url,update1,headers)

        r= urllib.request.urlopen(req)


        t= r.read()

        ans =json.loads(t)

        if ans['err_msg']=='success.':
            result = ans['result'][0].encode('utf-8')

            if result!='':
                return result.decode('utf-8')
            else:
                print(u'不存在文件0')
        else:
            print(u'錯誤')







if __name__ =='__main__':
    to_dir = './'
    file_path = record_wav(to_dir)
    file_path1 = 'C:\\Users\eexf\PycharmProjects\mcc'+file_path

    text = baiduys(file_path1)
    print(text)

    text_open_browser(text)
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM