Python SpeechRecognition, нажимайте вывод для каждых 'n' секунд, а не ожидая остановки ввода микрофона - PullRequest
0 голосов
/ 01 мая 2019

Я работаю над голосовым детектором, который ищет определенные слова и отправляет их на веб-сервер с помощью веб-сокетов.Я использую библиотеку Python speechReconition с Google Voice API.проблема в том, что вывод приходит только тогда, когда мы перестаем говорить, но я хочу, чтобы он выдавал выходные данные каждые 'n' секунд.скажем 5 секунд.PS: я еще не реализовал часть веб-сокета. Спасибо

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)




...