Skip to content

Commit 290361a

Browse files
authored
Add files via upload
1 parent 5f1a273 commit 290361a

34 files changed

+1863
-0
lines changed

speech_recognition/LICENSE-FLAC.txt

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

speech_recognition/LICENSE.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Copyright (c) 2014-2017, Anthony Zhang <[email protected]>
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5+
6+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7+
8+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9+
10+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11+
12+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

speech_recognition/MANIFEST.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
graft speech_recognition
2+
graft reference
3+
recursive-exclude speech_recognition *.pyc
4+
include README.rst
5+
include LICENSE.txt
6+
include LICENSE-FLAC.txt

speech_recognition/README.rst

Lines changed: 381 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python3
2+
3+
import speech_recognition as sr
4+
5+
# obtain path to "english.wav" in the same folder as this script
6+
from os import path
7+
AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "english.wav")
8+
# AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "french.aiff")
9+
# AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "chinese.flac")
10+
11+
# use the audio file as the audio source
12+
r = sr.Recognizer()
13+
with sr.AudioFile(AUDIO_FILE) as source:
14+
audio = r.record(source) # read the entire audio file
15+
16+
# recognize speech using Sphinx
17+
try:
18+
print("Sphinx thinks you said " + r.recognize_sphinx(audio))
19+
except sr.UnknownValueError:
20+
print("Sphinx could not understand audio")
21+
except sr.RequestError as e:
22+
print("Sphinx error; {0}".format(e))
23+
24+
# recognize speech using Google Speech Recognition
25+
try:
26+
# for testing purposes, we're just using the default API key
27+
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
28+
# instead of `r.recognize_google(audio)`
29+
print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
30+
except sr.UnknownValueError:
31+
print("Google Speech Recognition could not understand audio")
32+
except sr.RequestError as e:
33+
print("Could not request results from Google Speech Recognition service; {0}".format(e))
34+
35+
# recognize speech using Google Cloud Speech
36+
GOOGLE_CLOUD_SPEECH_CREDENTIALS = r"""INSERT THE CONTENTS OF THE GOOGLE CLOUD SPEECH JSON CREDENTIALS FILE HERE"""
37+
try:
38+
print("Google Cloud Speech thinks you said " + r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS))
39+
except sr.UnknownValueError:
40+
print("Google Cloud Speech could not understand audio")
41+
except sr.RequestError as e:
42+
print("Could not request results from Google Cloud Speech service; {0}".format(e))
43+
44+
# recognize speech using Wit.ai
45+
WIT_AI_KEY = "INSERT WIT.AI API KEY HERE" # Wit.ai keys are 32-character uppercase alphanumeric strings
46+
try:
47+
print("Wit.ai thinks you said " + r.recognize_wit(audio, key=WIT_AI_KEY))
48+
except sr.UnknownValueError:
49+
print("Wit.ai could not understand audio")
50+
except sr.RequestError as e:
51+
print("Could not request results from Wit.ai service; {0}".format(e))
52+
53+
# recognize speech using Microsoft Azure Speech
54+
AZURE_SPEECH_KEY = "INSERT AZURE SPEECH API KEY HERE" # Microsoft Speech API keys 32-character lowercase hexadecimal strings
55+
try:
56+
print("Microsoft Azure Speech thinks you said " + r.recognize_azure(audio, key=AZURE_SPEECH_KEY))
57+
except sr.UnknownValueError:
58+
print("Microsoft Azure Speech could not understand audio")
59+
except sr.RequestError as e:
60+
print("Could not request results from Microsoft Azure Speech service; {0}".format(e))
61+
62+
# recognize speech using Microsoft Bing Voice Recognition
63+
BING_KEY = "INSERT BING API KEY HERE" # Microsoft Bing Voice Recognition API keys 32-character lowercase hexadecimal strings
64+
try:
65+
print("Microsoft Bing Voice Recognition thinks you said " + r.recognize_bing(audio, key=BING_KEY))
66+
except sr.UnknownValueError:
67+
print("Microsoft Bing Voice Recognition could not understand audio")
68+
except sr.RequestError as e:
69+
print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))
70+
71+
# recognize speech using Houndify
72+
HOUNDIFY_CLIENT_ID = "INSERT HOUNDIFY CLIENT ID HERE" # Houndify client IDs are Base64-encoded strings
73+
HOUNDIFY_CLIENT_KEY = "INSERT HOUNDIFY CLIENT KEY HERE" # Houndify client keys are Base64-encoded strings
74+
try:
75+
print("Houndify thinks you said " + r.recognize_houndify(audio, client_id=HOUNDIFY_CLIENT_ID, client_key=HOUNDIFY_CLIENT_KEY))
76+
except sr.UnknownValueError:
77+
print("Houndify could not understand audio")
78+
except sr.RequestError as e:
79+
print("Could not request results from Houndify service; {0}".format(e))
80+
81+
# recognize speech using IBM Speech to Text
82+
IBM_USERNAME = "INSERT IBM SPEECH TO TEXT USERNAME HERE" # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
83+
IBM_PASSWORD = "INSERT IBM SPEECH TO TEXT PASSWORD HERE" # IBM Speech to Text passwords are mixed-case alphanumeric strings
84+
try:
85+
print("IBM Speech to Text thinks you said " + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))
86+
except sr.UnknownValueError:
87+
print("IBM Speech to Text could not understand audio")
88+
except sr.RequestError as e:
89+
print("Could not request results from IBM Speech to Text service; {0}".format(e))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
3+
# NOTE: this example requires PyAudio because it uses the Microphone class
4+
5+
import time
6+
7+
import speech_recognition as sr
8+
9+
10+
# this is called from the background thread
11+
def callback(recognizer, audio):
12+
# received audio data, now we'll recognize it using Google Speech Recognition
13+
try:
14+
# for testing purposes, we're just using the default API key
15+
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
16+
# instead of `r.recognize_google(audio)`
17+
print("Google Speech Recognition thinks you said " + recognizer.recognize_google(audio))
18+
except sr.UnknownValueError:
19+
print("Google Speech Recognition could not understand audio")
20+
except sr.RequestError as e:
21+
print("Could not request results from Google Speech Recognition service; {0}".format(e))
22+
23+
24+
r = sr.Recognizer()
25+
m = sr.Microphone()
26+
with m as source:
27+
r.adjust_for_ambient_noise(source) # we only need to calibrate once, before we start listening
28+
29+
# start listening in the background (note that we don't have to do this inside a `with` statement)
30+
stop_listening = r.listen_in_background(m, callback)
31+
# `stop_listening` is now a function that, when called, stops background listening
32+
33+
# do some unrelated computations for 5 seconds
34+
for _ in range(50): time.sleep(0.1) # we're still listening even though the main thread is doing other things
35+
36+
# calling this function requests that the background listener stop listening
37+
stop_listening(wait_for_stop=False)
38+
39+
# do some more unrelated things
40+
while True: time.sleep(0.1) # we're not listening anymore, even though the background thread might still be running for a second or two while cleaning up and stopping
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
3+
# NOTE: this example requires PyAudio because it uses the Microphone class
4+
5+
import speech_recognition as sr
6+
7+
# obtain audio from the microphone
8+
r = sr.Recognizer()
9+
with sr.Microphone() as source:
10+
r.adjust_for_ambient_noise(source) # listen for 1 second to calibrate the energy threshold for ambient noise levels
11+
print("Say something!")
12+
audio = r.listen(source)
13+
14+
# recognize speech using Google Speech Recognition
15+
try:
16+
# for testing purposes, we're just using the default API key
17+
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
18+
# instead of `r.recognize_google(audio)`
19+
print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
20+
except sr.UnknownValueError:
21+
print("Google Speech Recognition could not understand audio")
22+
except sr.RequestError as e:
23+
print("Could not request results from Google Speech Recognition service; {0}".format(e))
39.1 KB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#JSGF V1.0;
2+
3+
/**
4+
* JSGF Grammar for English counting example
5+
*/
6+
7+
grammar counting;
8+
9+
public <counting> = ( <digit> ) +;
10+
11+
<digit> = one | two | three | four | five | six | seven ;
236 KB
Binary file not shown.

0 commit comments

Comments
 (0)