Kitt-AI / snowboy

Future versions with model training module will be maintained through a forked version here: https://github.com/seasalt-ai/snowboy
Other
3.09k stars 997 forks source link

Does google recogniotion api lock microphone? #406

Open Nickkk1124 opened 6 years ago

Nickkk1124 commented 6 years ago

Hi, I hope to be able to perform snowboy wakeup word detection after running google speech recognition api, but I execute the following code and cannot perform the snowboy detection part after speech recognition.

-- coding: utf-8 --

import snowboydecoder import sys import signal import speech_recognition as sr import os from nluapi import NluAPISample import json import tempfile from gtts import gTTS from pygame import mixer import time import pyaudio import wave from mutagen.mp3 import MP3

TOP_DIR = os.path.dirname(os.path.abspath(file)) DETECT_DING = os.path.join(TOP_DIR, "resources/ding.wav") DETECT_DONG = os.path.join(TOP_DIR, "resources/dong.wav")

interrupted = False

def signal_handler(signal, frame): global interrupted interrupted = True

def detectedCallback(fname=DETECT_DING): print('recording audio...', end='', flush=True)

偵測到喚醒詞發出ding

ding_wav = wave.open(fname, 'rb') ding_data = ding_wav.readframes(ding_wav.getnframes()) audio = pyaudio.PyAudio() stream_out = audio.open( format=audio.get_format_from_width(ding_wav.getsampwidth()), channels=ding_wav.getnchannels(), rate=ding_wav.getframerate(), input=False, output=True) stream_out.start_stream() stream_out.write(ding_data) time.sleep(0.2) stream_out.stop_stream() stream_out.close() audio.terminate()

def audioRecorderCallback(fname): print("converting audio to text") r1 = sr.Recognizer() with sr.AudioFile(fname) as source: audio = r1.record(source) # read the entire audio file

recognize speech using Google Speech Recognition

try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
    sentence1 = r1.recognize_google(audio, language='zh-TW')
    print(sentence1)
    #olaminlu(sentence1)
    os.remove(fname)
    #remindspeak_dong()
    #looprecognition()
except sr.UnknownValueError:
    print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
    print("Could not request results from Google Speech Recognition service; {0}".format(e))
global interrupted
interrupted = True
print('123')
print(interrupted)

def interrupt_callback(): global interrupted return interrupted

model = sys.argv[1]

capture SIGINT signal, e.g., Ctrl+C

signal.signal(signal.SIGINT, signal_handler)

r2 = sr.Recognizer() with sr.Microphone() as source: r2.adjust_for_ambient_noise(source) audio = r2.listen(source) sentence2 = r2.recognize_google(audio, language='zh-TW') print(sentence2)

detector = snowboydecoder.HotwordDetector(model, sensitivity=0.38) print('Listening... Press Ctrl+C to exit')

main loop

detector.start(detected_callback=detectedCallback, audio_recorder_callback=audioRecorderCallback, interrupt_check=interrupt_callback, sleep_time=0.01)

detector.terminate()

chenguoguo commented 6 years ago

You have to make sure that Google releases the microphone after running.