Is there any solution to PermissionError:[Errno13] when using voice assistant? - python

I am currently working on a voice project in python. The program works fine until I say "hello" after the program says "Hello User." It gives me the following error:
C:\Users\HP\AppData\Local\Programs\Python\Python38-32\python.exe
C:/Users/HP/.PyCharmCE2019.3/config/scratches/scratch_1.py
hello
Traceback (most recent call last):
File "C:/Users/HP/.PyCharmCE2019.3/config/scratches/scratch_1.py", line 39, in <module>
speak("Hello, how are you?")
File "C:/Users/HP/.PyCharmCE2019.3/config/scratches/scratch_1.py", line 16, in speak
tts.save(randFile)
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\site-packages\gtts\tts.py", line 294, in save
with open(str(savefile), 'wb') as f:
PermissionError: [Errno 13] Permission denied: '91252543randomtext98303551.mp3'
Here's the code:
from gtts import gTTS
import time
import speech_recognition as sr
import playsound
import os
import random
r1 = random.randint(1, 100000000)
r2 = random.randint(1, 100000000)
randFile = str(r1) + "randomtext" + str(r2) + ".mp3"
def speak(text):
tts = gTTS(text=text, lang="en")
tts.save(randFile)
playsound.playsound(randFile)
speak("Hello User!")
os.remove(randFile)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
except Exception as e:
print("Sorry, my speech service is down!")
return said
if text == 'hello':
speak("Hello, how are you?")
if text == 'what is your name?':
speak("My name is Alex")
I have seen dozens of the solutions for this error and also tried saving the audio file using "randFile" .However, none of the solutions are helpful.

Related

Voice Recognition (converting voice to text)

I have code to convert voice to written text, I want to save the written text after it's converted to files that can be accessed later, how do I do it in the following code?
import speech_recognition as sr
def main():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Please say something to start recording the lecture ")
audio = r.listen(source)
print("Recognizing Now .... ")
# recognize speech using google
try:
print("You have said \n" + r.recognize_google(audio))
print("Audio Recorded Successfully \n ")
except Exception as e:
print("Error : " + str(e))
# write audio
with open("recorded.wav", "wb") as f:
f.write(audio.get_wav_data())
if __name__ == "__main__":
main()
I tried to create another python file and run it as .txt but it saves the code not recoreded one
In your def main(): function you need to open a text file using open(), something similar to as shown:
transcript = open('transcript.txt', 'w')
after that where you are using print("You have said \n" + r.recognize_google(audio))
You have to write the data coming from r.recognize_google(audio) to the above transcript.txt text file using transcript file descriptor
transcript.write(r.recognize_google(audio))
Also you need to make sure to close the open file descriptor using transcript.close()
Modified your code slightly to show how you can do it
import speech_recognition as sr
def main():
transcript = open('transcript.txt', 'w')
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Please say something to start recording the lecture ")
audio = r.listen(source)
print("Recognizing Now .... ")
# recognize speech using google
try:
print("You have said \n" + r.recognize_google(audio))
transcript.write(r.recognize_google(audio))
print("Audio Recorded Successfully \n ")
except Exception as e:
print("Error : " + str(e))
# write audio
with open("recorded.wav", "wb") as f:
f.write(audio.get_wav_data())
f.close()
transcript.close()
if __name__ == "__main__":
main()

I keep getting a permission denial error whenever I try to access a file in a speech recognition program

I'm trying out speech recognition and using it as input for some statements while having the program "speak" back to me using the playsound and gTTS modules. But I have ran into an issue that I can't find the solution for, I tried the most common solutions but with no luck.
The program uses the playsound, speech_recognition, and gTTS modules and two functions; speak() lets the program speak back to the user using google's text to sound translation, and get_audio() that receives input from the user's microphone using speech_recognition's recognizer and microphone classes.
import os
import time
import playsound
import speech_recognition as sr
from gtts import gTTS
run = True
def speak(text):
tts = gTTS(text=text, lang="en")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
while run == True:
text = get_audio()
if "hello" in text:
speak("Hello, how are you?")
if "what are you" in text:
print("")
speak("I am a speech recognition program")
if "goodbye" in text:
speak("Talk to you later" or "Bye" or "Cya")
run = False
I have the program set up with a while loop so a conversation can play out, and it only breaks once the user says "Goodbye". The problem seems to be that the .mp3 file (voice.mp3 which is what the speak() function uses to store audio for the program to play back) can't be accessed after its creation. Both the python file and mp3 file are stored within the same folder.
Here is the error message in full:
who are you
hello
Traceback (most recent call last):
File "c:\Users\User\OneDrive\Documents\VS Code Projects\Speech Recognition\main_va.py", line 34, in <module>
speak("Hello, how are you?")
File "c:\Users\User\OneDrive\Documents\VS Code Projects\Speech Recognition\main_va.py", line 12, in speak
tts.save(filename)
File "C:\Python\Python310\lib\site-packages\gtts\tts.py", line 328, in save
with open(str(savefile), "wb") as f:
PermissionError: [Errno 13] Permission denied: 'voice.mp3'
PS C:\Users\User\OneDrive\Documents\VS Code Projects\Speech Recognition>
I received a response on the first call ("who are you"), but then the error message popped up after the second call ("hello").
Specs: python 3.10.4
playsound 1.2.2
Rest is up to date
Your solution works fine.
I just would tweak it to leave the file behind (in case you want to listen to it for testing purposes) and instead, remove it at the beginning if it exists.
Also passing filename to your function ensures nothing is hard coded
def speak(text, filename):
if os.path.exists(filename):
os.remove(filename)
tts = gTTS(text=text, lang="en")
tts.save(filename)
playsound.playsound(filename)
I found a solution that seems to work just fine; I delete the .mp3 file each time after I use it, so at the end of the speak() function I just use os.remove(filename) and then the next time it wants to say something a new file is created.
I found some other solutions saying that you should rename the filename every time you make one, but that would make too much clutter for me.
Here is the change that I made to my code, it was just a single line within the speak() function:
def speak(text):
tts = gTTS(text=text, lang="en")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
os.remove("voice.mp3")
This works perfectly for me so far, it can take in as many inputs as needed since the file is deleted and recreated every time the speak() function is used.
Again if a better and more efficient solution is suggested or found, I'll gladly take it.

Output of Speech Recognition (Python)

I am working on a project with speech recognition module and due to my different language, I want to save on text file so I can read and have proper output; but when I tried sys module to save the file it came with some errors. If you help me to fix this I`ll be appreciate
This is my code:
import speech_recognition as sr
import sys
r = sr.Recognizer()
print('How can I help you?')
def my_first_sr():
while True:
try:
with sr.Microphone() as mic:
r.adjust_for_ambient_noise(mic)
audio = r.listen(mic)
text = r.recognize_google(audio, language = 'fa-IR')
print(text)
except sr.UnknownValueError:
print('I didn`t understand!')
except sr.RequestError:
print('Sorry my service is down')
my_first_sr()
output = open('Speech Recognition.txt', 'w')
sys.stdout = output
print(text)
output.close()
You have to write the text to the file output:
import speech_recognition as sr
r = sr.Recognizer()
print('How can I help you?')
def my_first_sr():
while True:
try:
with sr.Microphone() as mic:
r.adjust_for_ambient_noise(mic)
audio = r.listen(mic)
text = r.recognize_google(audio, language = 'fa-IR')
print(text)
output = open('Speech Recognition.txt', 'w')
output.write(text)#Write the text to the file
output.close()
except sr.UnknownValueError:
print('I didn`t understand!')
except sr.RequestError:
print('Sorry my service is down')
my_first_sr()

how to fix 'permission error: [Errno 13]' in speech recognizer python 3

My problem is actually weird. My program runs good at some times but some time it speaks out what I have not asked for. for example: I said "hello" it says the time when I did not ask the time at all.
Also when it does not recognize any speech from the user it speaks out the time.
And when I ask what is the date it says the date but then it shows a error message
I have tried to switch around the if statements and i don't know much about speech recognition so did not try around much changes
import os
import time
from datetime import date
import playsound
import speech_recognition as sr
from gtts import gTTS
def speak(text):
tts = gTTS(text=text, lang="en")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
text = get_audio()
today = date.today()
t = time.localtime()
current_time = time.strftime("%H:%M", t)
d2 = today.strftime("%B %d, %Y")
if "hello" in text:
speak("hello sir")
if "date" in text:
speak("the date is: " + d2)
if "what's the time" or "what is the time" in text:
speak("The time is: " + current_time)
The error message it shows when I say ask the date:
Traceback (most recent call last):
File "d:/python programs/hello.py", line 42, in <module>
speak("The time is: " + current_time)
File "d:/python programs/hello.py", line 11, in speak
tts.save(filename)
File "E:\python\lib\site-packages\gtts\tts.py", line 248, in save
with open(str(savefile), 'wb') as f:PermissionError: [Errno 13] Permission denied: 'voice.mp3'
When it does not recognize the voice of the user and returns a exception:
it says out the time
These are all of the problems with the program.
Generate unique filename instead of "voice.mp3" every time:
import uuid
filename = str(uuid.uuid4()) + ".mp3"

Overwrite pre-existing .mp3 file in Python using gtts

I have a constraint with my syntax. I just tried python some time ago and I started it from AI. I make a bot like Jarvis to help me like opening google or youtube. From the tutorial available in Pythonspot.com it shows for Ubuntu tutorial but I use Windows. And there are some tools or plugins that do not work in windows is mpg321. I have found a replacement with mixer.music to play the sound of the AI. This works but I have constraints on the second sound, ie i make the first sound with audio.mp3 and it works then when my second voice uses the same filename ie audio.mp3 and i have constraints like this
Traceback (most recent call last):
File "D:\#AI Projects\Jarvis\Jarvis.py", line 71, in
jarvis(data)
File "D:\#AI Projects\Jarvis\Jarvis.py", line 53, in jarvis speak(ctime())
File "D:\#AI Projects\Jarvis\Jarvis.py", line 17, in speak tts.save("audio.mp3")
File "C:\Users\inialdan\AppData\Local\Programs\Python\Python36\lib\site-packages\gtts\tts.py",
line 110, in save
with open(savefile, 'wb') as f: PermissionError: [Errno 13] Permission denied: 'audio.mp3'
This is my code
#!/usr/bin/env python3
# Requires PyAudio and PySpeech.
import speech_recognition as sr
from time import ctime
import time
import os
import subprocess
from gtts import gTTS
from pygame import mixer
def speak(audioString):
print(audioString)
tts = gTTS(text=audioString, lang='en')
tts.save("audio.mp3")
mixer.init()
mixer.music.load('D:/#AI Projects/Jarvis/audio.mp3')
mixer.music.play()
def recordAudio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Try to say something!")
audio = r.listen(source)
data = ""
try:
data = r.recognize_google(audio)
print("You said : " + data)
except sr.UnknownValueError:
print("I'm Sorry, i couldn't understand what you mean ...")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return data
def jarvis(data):
CHROME = os.path.join('C:\\', 'Program Files (x86)', 'Google', 'Chrome', 'Application', 'chrome.exe')
if "jarvis" in data:
speak("Yes, sir ?")
if "what is your name" in data:
speak("You can call me, Jarvis")
if "where do you leave" in data:
speak("In your heart.")
if "how are you" in data:
speak("I am fine")
if "what time is it" in data:
speak(ctime())
if "where is" in data:
data = data.split(" ")
location = data[2]
speak("Hold on Aldan, I will show you where " + location + " is.")
os.system('taskkill /im chrome.exe')
subprocess.call([CHROME, "https://www.google.nl/maps/place/" + location + "/&"])
if "open" in data:
data = data.split(" ")
application = data[1]
speak("Hold on Aldan, I will show you " + application)
os.system('taskkill /im chrome.exe')
subprocess.call([CHROME, "https://www." + application + ".com"])
time.sleep(2)
speak("Hi Aldan, How may I assist you?")
while 1:
data = recordAudio()
jarvis(data)
I have tried it with os.remove (); to remove the audio.mp3 and rewrite it. but still failed
I have modified Speak method with the help of TemporaryFile.Click on the icon to check the Code
from googletrans import Translator
import pygame, time
import tempfile
from gtts import gTTS
from pygame import mixer
from tempfile import TemporaryFile
def speak(text, lang='en'):
"""Text to speech. For funp."""
try:
translator = Translator()
tts = gTTS(text=translator.translate(text, dest=lang).text, lang=lang)
mixer.init()
sf = TemporaryFile()
tts.write_to_fp(sf)
sf.seek(0)
mixer.music.load(sf)
mixer.music.play()
except Exception:
raise
Just create a second def that removes the filename and run that command after your speak()
def complete():
os.remove('audio.mp3')
speak("blah blah")
complete()
I did this, it worked. You may have to guess the length of the sound. For mine, 2 sec is long enough.
voice = gtts.gTTS(item)
voice.save("temp.mp3")
sound = SoundLoader.load("temp.mp3")
sound.play()
time.sleep(2)
sound.stop()
os.remove("temp.mp3")

Categories