NotImplementedError: this is an abstract class in speech recognition - python

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()

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))

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]:

Traceback AttributeError: module 'pyaudio' has no attribute '__version__'

I intalled SppechRecognition and when I worte
import speech_recognition as sr
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
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)`
print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
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))
The above problem occurred.
The same question was asked over here, but the solution didn't help me!
AttributeError: module 'pyaudio' has no attribute '__version__'
Please help me!
My Terminal Output:-
Traceback (most recent call last):
File "c:/Users/samar/OneDrive/Desktop/MAIN/projects/Jarvis/jarvis.py", line 4, in <module>
with sr.Microphone() as source:
File "C:\Users\samar\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 79, in __init__
self.pyaudio_module = self.get_pyaudio()
File "C:\Users\samar\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 112, in get_pyaudio
if LooseVersion(pyaudio.__version__) < LooseVersion("0.2.11"):
AttributeError: module 'pyaudio' has no attribute '__version__'
When I installed the package from Unofficial Python Packagesand ran the code in the terminal. It worked!

im faceing the errors in speech recognition

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")

Categories

Resources