speech recognition not working in my code - python

I am making a simple speech recognition system for a project purpose. I am following a youtube video where the following code was working in that system but when I tried It is showing me error
This is the code
import speech_recognition as sr
with sr.AudioFile('/content/male.wav') as source:
audio = r.record(source)
And this is the error
NameError Traceback (most recent call last)
<ipython-input-52-428b394f05e3> in <module>()
1 import speech_recognition as sr
2 with sr.AudioFile('/content/male.wav') as source:
----> 3 audio = r.record(source)
NameError: name 'r' is not defined
This is the full code
pip install SpeechRecognition
import speech_recognition as sr
catch = sr.Recognizer()
songss = sr.AudioFile('/content/male.wav')
print(type(songss))
import speech_recognition as sr
with sr.AudioFile('/content/male.wav') as source:
audio = r.record(source)

The r is not defined. It should be defined as:
r = sr.Recognizer()
Hope this will work.

Related

send Youtube videos to telegram

i wrote this program :
import scrapetube
import hashlib
import telegram
import requests
import json
from pytube import YouTube
from time import sleep
import os
bot_token = 'my token'
bot_chatID = '#PinkPanthersShow'
bot = telegram.Bot(token=f'{bot_token}')
c_videos = scrapetube.get_channel("UC1fIyfhQtm1fSljyKBf2uKA")
videos = list()
for video in c_videos:
videos.append(f"https://www.youtube.com/watch?v={str(video['videoId'])}")
videos.reverse()
def upload_video(title, desc):
bot.send_video(chat_id=bot_chatID, video=open(fr'{title}.mp4', 'rb'),caption=f"{desc}", supports_streaming=True)
for i in list(range(len(videos))):
yt = YouTube(videos[i])
print(f"{yt.title} : {videos[i]}")
ys = yt.streams.get_highest_resolution()
ys.download()
upload_video(str(yt.title), str(yt.description))
#print (list(videos)[0])
#links.reverse()
#print(links)
#print(title)
#print(videos)
input()
When my program gets to the video The Pink Panther Show Episode 4 - Dial" P "for Pink: https://www.youtube.com/watch?v=tOaakSDwuZE, it gives this error:
Traceback (most recent call last):
File "g:\yta\bot.py", line 43, in <module>
upload_video(str(yt.title), str(yt.description))
File "g:\yta\bot.py", line 37, in upload_video
bot.send_video(chat_id=bot_chatID, video=open(fr'{title}.mp4', 'rb'),caption=f"{desc}", supports_streaming=True)
OSError: [Errno 22] Invalid argument: 'The Pink Panther Show Episode 4 - Dial "P" for Pink.mp4'
What can I do to avoid this error?
If you want To send All videos Of a Youtube channel to telegram, you can use ytt, which is a module I have written:
https://nimafanniasl.github.io/py_youtube_to_telegram/
If you want to use an app , You can use ytt app, but if you want to use it in python, you can use ytt module, example:
pip install ytt
python app:
from ytt import ytt
ytt.start_all(YouTube_channel_id,telegram_bot_token,bot_chatID,send_channel_or_group_id=None)
# :)
More information: https://github.com/nimafanniasl/py_youtube_to_telegram/tree/main/module
from pytube import YouTube
from telebot import TeleBot
yt = YouTube("https://www.youtube.com/watch?v=lOUsz0ggDsk")
video = yt.streams.filter(file_extension="mp4").get_by_resolution("360p").download("./tmp")
bot = TeleBot("Telegram Token")
bot.send_video(230967203, open(video, 'rb'))

Error 263 for command: open Welcome.mp3 The specified device is not open or is not recognized by MCI

import speech_recognition as sr
import requests
from gtts import gTTS
from playsound import playsound
import os
import subprocess
bot_message = ""
message = ""
myobj = gTTS(text="Hello I am Shilpa Sheety Speak Anything I am Listening", lang='en', tld='com.au')
myobj.save("starting.mp3")
playsound("starting.mp3")
while bot_message !="Bye":
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
message = r.recognize_google(audio)
print("You said : {}".format(message))
except:
print("Sorry Could not recognize your voice")
if len(message) == 0:
continue
print("Sending Message Now")
r = requests.post("http://localhost:5002/webhooks/rest/webhook", json={'message':message})
print("Bot Says,", end=' ')
for i in r.json():
bot_message = i['text']
print(f"{i['text']}")
myobj = gTTS(text=bot_message)
myobj.save("Welcome.mp3")
playsound("Welcome.mp3")
In above program I am playing welcome.mp3 in a loop. It is working fine for first 2 iterations but in 3rd iteration of for loop I am getting the following error:
Error 263 for command:
open Welcome.mp3
The specified device is not open or is not recognized by MCI.
Error 263 for command:
close Welcome.mp3
The specified device is not open or is not recognized by MCI. Failed to close the file: Welcome.mp3 Traceback (most recent call last): File "Voice_bot.py", line 31, in <module>
playsound("Welcome.mp3") File "C:\Users\DJ9004\anaconda4\lib\site-packages\playsound.py", line 72, in _playsoundWin
winCommand(u'open {}'.format(sound)) File "C:\Users\DJ9004\anaconda4\lib\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundException(exceptionMessage) playsound.PlaysoundException:
Error 263 for command:
open Welcome.mp3
The specified device is not open or is not recognized by MCI.*
It worked for me when I uninstalled playsound module and installed sn older version like this:
pip uninstall playsound
pip install playsound==1.2.2
It works when I install the version of playsound by uninstalling the previous version like this
pip uninstall playsound
pip install playsound==1.2.2
Try it
I had the same issue, it looks like the file is still being saved when you are trying to open it.
I added a couple of lines and it worked just fine:
for i in r.json():
bot_message = i['text']
print(f"{i['text']}")
myobj = gTTS(text=bot_message)
os.remove('Welcome.mp3') #LINE ADDED
myobj.save("Welcome.mp3")
time.sleep(1) #LINE ADDED
playsound("Welcome.mp3")
I had the same error as you and not finding answers I started to do tests. The way I found is not very practical, but it works for me. In a new file I wrote this piece of code (For the example we will call the 'function_sound_file' file):
from playsound import playsound
def function_sound():
playsound('complete/path/file.wav')
And in the file where I had the problem, I call the function I created after importing it (below).
from function_sound_file import function_sound
function_sound()
I tried an illogical solution but it worked every time.
just change the name of your audio file to 'audio.mp3'
and don't forget to close it by using "os.close('audio.mp3')" .
The code below never worked:
from gtts import gTTS
import os
#greetings
def start():
tts = gTTS(text="hi, its kate here! how may i help you?", lang='en' ,
slow=False)
tts.save('lol.mp3')
from playsound import playsound
playsound('lol.mp3')
os.remove('lol.mp3')
start()
But it worked every time:
from gtts import gTTS
import os
#greetings
def start():
tts = gTTS(text="hi, its kate here! how may i help you?", lang='en' ,
slow=False)
tts.save('audio.mp3')
from playsound import playsound
playsound('audio.mp3')
os.remove('audio.mp3')
start()
As you can see I just changed 'lol.mp3' to 'audio.mp3'.
Hope it works.

pepper robot speech Recognition

import os
import sys
import time
from naoqi import ALProxy
from naoqi import ALBroker
from naoqi import ALModule
import speech_recognition as sr
record = ALProxy("ALAudioRecorder", "192.168.8.104", 9559)
record.stopMicrophonesRecording()
print('Start recording...')
# tts.say("start recording...")
record.startMicrophonesRecording('/home/nao/recordings/cameras/maha1', 'wav', 16000, (0,0, 1, 0))
time.sleep(5)
record.stopMicrophonesRecording()
audio_player_service = ALProxy("ALSpeechRecognition", "192.168.8.104", 9559)
recognizer = sr.Recognizer()
audio_file_ = sr.AudioFile('/home/nao/recordings/cameras/maha1.wav') #problem is here
print(type(audio_file_))
with audio_file_ as source:
audio_file = recognizer.record(source, duration = 8.0)
result = recognizer.recognize_google(audio_data=audio_file, language="ar")
print(result)
this is my code I have to retrieve the audio recording from pepper cloud but it's seems it can't see this path because it's running on my laptop, so how can I retrieve the audio file from pepper cloud
You may have a look at the Pepper Controller Python library which contains the speech recognition implementation (see robot.py: listen()).

why speech recognition is not recognizing any sound

I'm working on a Virtual Assistant project that recognizes a speech then convert it to a text and follow instructions according to the speech.
now, my issue is the speech never recognize any word I say, there are no errors popping. however, when I print the speech recognized it is always blank.
please see the below code:
import wikipedia
import webbrowser
import speech_recognition as sr
import pyttsx3
import subprocess
import os
from gtts import gTTS
import datetime
import warnings
import calendar
import random
import pyaudio
#ignore warnings
warnings.filterwarnings('ignore')
def record_audio():
#record
r = sr.Recognizer() #creating recognizer object
#open the mic and record
with sr.Microphone() as source:
print('say somthing!')
audio = r.listen(source)
#use google speech recognition
data = ''
try:
date = r.recognize_google(audio)
print('you said: '+data)
except sr.UnknownValueError:
print('google cant understand the audio !')
except sr.RequestError as e:
print('request results from google speech recognition service error '+ e)
return data
record_audio()
the output is always as per the below:
you said :
what I have tried to solve this:
r.adjust_for_ambient_noise(source, duration=1)
in terminal: pip install pipwin
none of the above worked.
indentations are fine, it is just the formatting of the thread.
There is a typo in your code, that's why you will never know what google understood.
data = r.recognize_google(audio)

Python MP3 Player

I Am Trying To Play An MP3 File On Python , But I Can't Find The Right Module!
I've Tried This:
import os
os.startfile('hello.mp3')
But I Just Got The Error:
Traceback (most recent call last):
File "/Applications/Youtube/text 2 speech/test.py", line 2, in <module>
os.startfile('hello.mp3')
AttributeError: 'module' object has no attribute 'startfile'
I Have Also Tried This:
import vlc
p = vlc.MediaPlayer("file:hello.mp3")
p.play()
But I Get The Error:
Traceback (most recent call last):
File "/Applications/Youtube/text 2 speech/test.py", line 1, in <module>
import vlc
ImportError: No module named 'vlc'
But I Still Can't Find The Right Module. Could Someone Please Help?
You will need to install the vlc.py module from https://wiki.videolan.org/Python_bindings
The absolute bare bones for this would be something like:
import vlc
Inst = vlc.Instance()
player = Inst.media_player_new()
Media = Inst.media_new_path('/home/rolf/vp1.mp3')
player.set_media(Media)
player.play()
Although you would need to check player.get_state() to see if it is still running, paused, stopped etc and player.stop() to stop the audio once started
As far as I am aware os.startfile() is only available for the windows operating system.
Using the play command line instruction (Linux and probably OS X)
import os
os.system('play /home/rolf/vp1.mp3')
You could also look at Gstreamer, although the documentation could do with some improvement.
import gst
player = gst.element_factory_make("playbin")
player.set_property("uri", "file:///home/james/vp1.mp3")
player.set_state(gst.STATE_PLAYING)
play is stopped with player.set_state(gst.STATE_NULL),
pause with player.set_state(gst.STATE_PAUSED)
Note: Avoid tutorials for Gstreamer version 0.10, search instead for version 1.0
Both vlc and Gstreamer allow you to play audio and video, although, in my opinion, vlc in simpler to use but Gstreamer is more flexible.
import pygame
from pygame import mixer
def play_music():
global paused
if (paused):
mixer.music.unpause()
paused = False
else:
music = choose_music[0]
mixer.music.load(music)
mixer.music.play()
show_details(music)
musicpage.mainloop()

Categories

Resources