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

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!

Related

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.

Pyaudio not found , also not able to install it . Need it for my virtual assistant

I am in progress of making a virtual assistant (like cortana in windows) but just after importing speechrecognition, and setting it up, it shows an error "Pyaudio not found, check installation" , or in detail, this is the whole error I am getting while I run it. :
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 108, in get_pyaudio
import pyaudio
ModuleNotFoundError: No module named 'pyaudio'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\machine_learning\raw_ai\Virtual assistant.py", line 59, in <module>
take()
File "C:\machine_learning\raw_ai\Virtual assistant.py", line 31, in take
with sr.Microphone() as source:
File "C:\Users\user\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\user\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 110, in get_pyaudio
raise AttributeError("Could not find PyAudio; check installation")
AttributeError: Could not find PyAudio; check installation
Here is my code(it runs perfectly till wish function , but when I try to speak or not even speak , it just exits the code with the error above):
import pyttsx3
import datetime
import speech_recognition as sr
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wish():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour <= 12:
speak("Good morning !")
elif hour > 12 and hour <= 5:
speak("Good Aftrenoon !")
else:
speak("Good night")
speak("How may I help you ?")
def take():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening.....")
r.pause_threshold(1)
audio = r.listen(source)
try:
print("Recognisizing...")
query = r.recognize_google(audio, Language = 'en-in')
print(f"User said : {query}\n")
except Exception as e:
print("Come Again ?")
return "None"
return query
if __name__ == "__main__":
wish()
take()
For installing Pyaudio you have to do this 👇
pip install pipwin
pipwin install PyAudio
This worked for me. Hope it works for you also

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

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

Error in Running a script: Google Speech API

I have been trying to run the following code from this link.
import io
import os
# Imports the Google Cloud client library
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
# Instantiates a client
client = speech.SpeechClient()
# The name of the audio file to transcribe
file_name = os.path.join(
os.path.dirname(__file__),
'resources',
'audio.raw')
# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file:
content = audio_file.read()
audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US')
# Detects speech in the audio file
response = client.recognize(config, audio)
for result in response.results:
print('Transcript: {}'.format(result.alternatives[0].transcript))
However, I am getting the following error when I run it using python 3.4.2 on a raspberry pi.
Traceback (most recent call last):
File "script.py", line 10, in <module>
client = speech.SpeechClient()
File "/usr/local/lib/python3.4/dist- packages/google/cloud/speech_v1/gapic/speech_client.py", line 137, in __init__
credentials=credentials,
File "/usr/local/lib/python3.4/dist-packages/google/cloud/speech_v1/gapic/transports/speech_grpc_transport.py", line 76, in __init__
channel)
File "/usr/local/lib/python3.4/dist- packages/google/api_core/operations_v1/operations_client.py", line 59, in __init__
self.operations_stub = operations_pb2.OperationsStub(channel)
AttributeError: 'module' object has no attribute 'OperationsStub'
I have been trying to get over this for about 3 days now and I have not found any solution.
Any help would be appreciated.
Not sure if you still have the problem. I went through the same thing this morning.
I, apparently, seemed to have a version of googleapis-common-protos installed which was built without GRPC support (1.6.0b6). I downgraded it to 1.5.3, restarted jupyter server and its working perfectly fine now. Please refer to this link.
Google Cloud Github

Categories

Resources