im faceing the errors in speech recognition - python

import speech_recognition as sr
import pyaudio
r=sr.Recognizer()
with sr.Microphone()as source:
print(" speak :")
audio=r.listen(source)
try:
output=r.recognize_google(audio)
print(" you said :".format(output))
except:
print(" i cont recognize what u said place spaek clear")
ERRORS:
Traceback (most recent call last):
File "d:/codes/speechreg.py", line 5, in <module>
with sr.Microphone()as source:
File "C:\Users\bjman\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 141, in __enter__
input=True, # stream is an input stream
File "C:\Users\bjman\AppData\Local\Programs\Python\Python37\lib\site-packages\pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "C:\Users\bjman\AppData\Local\Programs\Python\Python37\lib\site-packages\pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9999] Unanticipated host error

Have you tried to enable access to your microphone? Try enabling microphone using this guid.
I think your code didn't print the output. try to change the following line,
print(" you said :{}".format(output))
And this code will perform as your desire.You can try it as well!
import speech_recognition as sr
import pyaudio
r=sr.Recognizer()
with sr.Microphone()as source:
print(" speak :")
audio=r.listen(source)
try:
value = r.recognize_google(audio)
if str is bytes:
result = u"{}".format(value).encode("utf-8")
else:
result = "{}".format(value)
with open("outputs.txt","a") as f:
f.write(result)
print(result)
except:
print(" i cont recognize what u said place spaek clear")

Related

Background listing using speech_recognition python library

I am trying to implement a background listing using speech_recognition python library. Following code use for that. When I run this code I got the following error message. How can I solve this issue?
Error MSG:
RuntimeError: Decoder_set_kws returned -1
Python library - https://github.com/Uberi/speech_recognition#readme
Code:
import speech_recognition as sr
import time
r = sr.Recognizer()
# Words that sphinx should listen closely for. 0-1 is the sensitivity
# of the wake word.
keywords = [("google", 1), ("hey google", 1), ]
source = sr.Microphone(device_index = device_id, sample_rate = sample_rate, chunk_size = chunk_size)
def callback(recognizer, audio): # this is called from the background thread
try:
speech_as_text = recognizer.recognize_sphinx(audio, keyword_entries=keywords)
print(speech_as_text)
# Look for your "Ok Google" keyword in speech_as_text
if "google" in speech_as_text or "hey google":
recognize_main()
except sr.UnknownValueError:
print("Oops! Didn't catch that")
def recognize_main():
print("Recognizing Main...")
audio_data = r.listen(source)
# interpret the user's words however you normally interpret them
def start_recognizer():
print("Main...")
r.listen_in_background(source, callback)
time.sleep(1000000)
start_recognizer()
Full Error MSG:
Exception in thread Thread-6: Traceback (most recent call last):
File "C:\Users\aa\anaconda3\envs\env1\lib\threading.py", line 926, in
_bootstrap_inner
self.run() File "C:\Users\aa\anaconda3\envs\env1\lib\threading.py", line 870, in run
self._target(*self.args, **self.kwargs) File "C:\Users\aa\anaconda3\envs\env1\lib\site-packages\speech_recognition_init.py",
line 616, in threaded_listen
if running[0]: callback(self, audio) File "", line 16, in callback
speech_as_text = recognizer.recognize_sphinx(audio, keyword_entries=keywords) File
"C:\Users\aa\anaconda3\envs\env1\lib\site-packages\speech_recognition_init.py",
line 683, in recognize_sphinx
decoder.set_kws("keywords", f.name) File "C:\Users\aa\anaconda3\envs\env1\lib\site-packages\pocketsphinx\pocketsphinx.py",
line 309, in set_kws
return _pocketsphinx.Decoder_set_kws(self, name, keyfile) RuntimeError: Decoder_set_kws returned -1

Speech recognition error while reading data from an audio file

This is my code:
import speech_recognition as sr
r = sr.Recognizer()
file = sr.AudioFile('E:/music/jack.wav')
with file as source:
audio_file = r.record(source,duration=20)
print(r.recognize_google(source))
I am getting this error:
[Running] python -u "e:\Visual studio code\file-1.py"
Traceback (most recent call last):
File "e:\Visual studio code\file-1.py", line 8, in <module>
print(r.recognize_google(source))
File "C:\Users\asus\AppData\Roaming\Python\Python39\site-packages\speech_recognition\__init__.py", line 822, in recognize_google
assert isinstance(audio_data, AudioData), "``audio_data`` must be audio data"
AssertionError: ``audio_data`` must be audio data
How can I fix this?
instead of giving source to input of r.rcognize_google you should give audio_file for processing try it once!
import speech_recognition as sr
r = sr.Recognizer()
file = sr.AudioFile('E:/music/jack.wav')
with file as source:
audio_file = r.record(source,duration=20)
print(r.recognize_google(audio_file))

Use spech_recognition to write down system audio

I am trying to use Python speech_recognition to get an input from the system audio and then to print it as output. Unfortunately, I'm having some problems with the devices list. In fact, it seems that speech_recognition only recognizes microphones as devices of input.
My idea is the following: I am very slow at writing down notes of important videocalls, so I would like to have Python speech_recognition write them down for me so I can catch up with missing pieces. Do you think it is possible? How?
This is my code until now:
import pyaudio
import speech_recognition as sr
r=sr.Recognizer()
r.energy_threshold=4000
for index, name in enumerate(sr.Microphone.list_microphone_names()):
print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))
# returns a list of 15 devices (microphone, system speakers, headphones...)
with sr.Microphone(device_index=4) as source:
audio = r.listen(source)
# index = 4 is my headphones
try:
print("Speech was:" + r.recognize_google(audio))
except LookupError:
print('Speech not understood')
When just looking at it, it seems good. But when running it, it cannot recognize my headphones (system audio) as devices and returns the following errors:
Traceback (most recent call last):
File "...", line 10, in <module>
with sr.Microphone(device_index=4) as source:
File "...\speech_recognition\__init__.py", line 141, in __enter__
input=True, # stream is an input stream
File "...pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "...pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9998] Invalid number of channels
and when putting "normal microphone" as input:
line 858, in recognize_google
if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise UnknownValueError()
speech_recognition.UnknownValueError
Can you help me figure this out please?
The microphone error is given when the result of the audio wasn't understood as words. You can fix that by adding a try/except around the statement with "except [the name of the import].UnknownValueError. This will result in whatever is put under except to be run if the "UnknownValueError" error is encountered. As for the headphones, I believe the issue is that you are using an output device for an input function.

I am tryna make an virtual assistant and facing some errors about GoogleAPI

My computer is Windows 10 version:10.0.19041 python version:3.9
My code:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
voice = r.recognize_google(audio)
print(voice)
Traceback
Traceback (most recent call last):
File "C:\Users\mt7da\Alexa.py", line 7, in <module>
voice = r.recognize_google(audio)
File "C:\Users\mt7da\AppData\Local\Programs\Python\Python39\lib\site-packages\speech_recognition\__init__.py", line 858, in recognize_google
if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise UnknownValueError()
speech_recognition.UnknownValueError
```*
*İf you need ```__init__.py``` it is here`[http://file:///C:/Users/mt7da/AppData/Local/Programs/Python/Python39/Lib/site-packages/speech_recognition/__init__.py][1]`**
[1]:

NotImplementedError: this is an abstract class in speech recognition

I got some code from google for speech recognition when i try to run that code i'm getting "NotImplementedError" please see the below code and help me.I'm using Mac.
import speech_recognition as sr
r = sr.Recognizer()
with sr.Recognizer() as source:
print("Speak Anything")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print("you said:{}".format(text))
except NotImplementedError:
print("Sorry could not recognise your voice")
Traceback (most recent call last):
File "", line 4, in
with sr.Recognizer() as source:
File "/Users/chiku/anaconda3/lib/python3.5/site-packages/speech_recognition/init.py", line 51, in enter
raise NotImplementedError("this is an abstract class")
NotImplementedError: this is an abstract class
Traceback (most recent call last):
File "", line 4, in
with sr.Recognizer() as source:
File "/Users/chiku/anaconda3/lib/python3.5/site-packages/speech_recognition/init.py", line 51, in enter
raise NotImplementedError("this is an abstract class")
NotImplementedError: this is an abstract class
In the line above, you instantiate a Recognizer object, then you try to use the uninstatiated class in the problem line. Should that be
with r as source:
...
I have the below code and it gives me same error i.e
Traceback (most recent call last):
File "/Users/../Listen.py", line 25, in
print(Listen())
^^^^^^^^
File "/Users/../Listen.py", line 8, in Listen
with sr.Recognizer() as source:
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/speech_recognition/init.py", line 51, in enter
raise NotImplementedError("this is an abstract class")
NotImplementedError: this is an abstract class
import speech_recognition as sr
from googletrans import Translator
def Listen():
r = sr.Microphone()
with sr.Recognizer() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='hi')
except:
return ""
query = str(query).lower()
return query
Listen()

Categories

Resources