python SpeechRecognition,每隔n秒推送一次输出,而不是等待麦克风输入停止

时间:2019-05-01 14:26:38

标签: python nlp speech-recognition

我正在研究一种语音检测器,该语音检测器搜索某些单词并将其使用Web套接字发送到Web服务器。我正在将python speechReconition库与Google语音API配合使用。问题是输出仅在我们停止讲话时才出现,但我希望它每隔'n'秒给出一次输出。假设5秒。 PS:我尚未实现Web套接字部分 谢谢

import speech_recognition as sr
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import re
import nltk
nltk.download('punkt')
from nltk.corpus import stopwords 
from nltk.tokenize import word_tokenize 
from nltk.stem import PorterStemmer 

import asyncio
import websockets



inp=[]
out=[]
count=0
icount=0
y=0

ps=PorterStemmer()
while(True==True):
    r=sr.Recognizer()
    with sr.Microphone() as source:
         print('Speak Anything')
         audio=r.listen(source)
         try:
                #voice input phase
            text=r.recognize_google(audio)  
            print(r.energy_threshold)
            #text=input()
            inp.append(text)
            #x=text.split(' ')
            #print(x)
            #pre processing phase
            stop_words = set(stopwords.words('english')) 
            word_tokens = word_tokenize(text) 
            filtered_sentence = [w for w in word_tokens if not w in stop_words] 
            filtered_sentence = [] 

            for w in word_tokens: 
                w=ps.stem(w)
                print(w)
                if w not in stop_words: 
                    filtered_sentence.append(w) 
            print(filtered_sentence)
            icount=0
            #detection phase
            for i in filtered_sentence:
                print(i)
                if i=="one" or i=="two" or i=="three"  :
                    print('match')
                    icount+=1
                    y= 0
                else:
                    y=1
            #if y == 1:
             #   out.append(0)
            #else:
            out.append(icount)
            count+=1
            if count>4:
                break
            print(inp,out)


         except sr.UnknownValueError :
            print('error')
         except sr.RequestError as e:
            print(e)
print(out)

#inference phase 
for i in range(len(out)):
    inp[i]=i
print(inp)
plt.bar(inp,out)




0 个答案:

没有答案