sefailyasoz
sefailyasoz

Reputation: 69

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.

Upvotes: 5

Views: 6627

Answers (2)

sefailyasoz
sefailyasoz

Reputation: 69

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

Upvotes: 2

Mohammed
Mohammed

Reputation: 311

You are using the function record() here, which is used to capture data from a file, you should use the listen() function instead when capturing microphone input.

import speech_recognition as sr

r = sr.Recognizer()

with sr.Microphone() as source:
    print('Say Something')
    audio = r.listen(source)
    print(audio) #This is just a speech_recognition.AudioData object
    text = r.recognize_google(audio) #Speech to text google recognizer
    print(text) #This is what you actually said

Upvotes: 1

Related Questions