Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpeechRecognition, AssertionError "Source must be an audio source"

Here is my code:

import speech_recognition as sr

r = sr.Recognizer()

with sr.Microphone() as source:
    print('Say Something')
    audio = r.listen(source)
    voice_data = r.record(audio)
    print(voice_data)

When I type "python main.py" on the terminal and start the program it starts to listen but doesn't get what I say. I've tried to use adjust_for_ambient_noise() instead of listen() but it also didn't change anything.

I'm using macOS Catalina and Python 3.8.1.

This is the error I get:

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    voice_data = r.record(audio)
  File "/Users/sefailyasoz/PycharmProjects/SpeechAssistant/venv/lib/python3.8/site-packages/speech_recognition/__init__.py", line 483, in record
    assert isinstance(source, AudioSource), "Source must be an audio source"
AssertionError: Source must be an audio source 

This is what I get when I use adjust_for_ambient_noise(), if I use listen, it doesn't end, it just listens, I end it with Ctrl+C.

like image 818
sefailyasoz Avatar asked Nov 07 '22 09:11

sefailyasoz


1 Answers

Well I changed my function a little bit

def record_audio(ask=False):
    with sr.Microphone() as source:
        if ask:
            turkishSiri_speak(ask)
        audio = r.listen(source)
        voice_data = ''
        try:
            voice_data = r.recognize_google(audio , language='tr-TR')
        except sr.UnknownValueError:
            turkishSiri_speak('Ne söylediğini anlayamadım')
        except sr.RequestError:
            turkishSiri_speak('Google konuşma servisinde bir problem var')
        return voice_data

but the biggest problem was a macOS problem, I had to give permission to pycharm or visual studio code to use my microphone. before the changing function it was already working after giving the permission

like image 186
sefailyasoz Avatar answered Nov 12 '22 19:11

sefailyasoz