I am trying to transcribe text from an audio file with the speech recognition module in python. However, whenever I try to run the code, the module throws an OSError. I have disabled the lines of code that say “try” and “except”, since the code returns “Sorry... run again...” if I don’t. I have made sure that the audio sample is 16 bit, but even then I can’t get it to work. I am running Pythonista on an iPad Air, but I get the same results when using a MacBook Air. Is there a problem with software compatibility? I am also new to python, so can anybody help me with this issue?
The code:
#import library
import os
import speech_recognition as sr
import wave
# Initialize recognizer class (for recognizing the speech)
r = sr.Recognizer()
# Reading Audio file as source
# listening the audio file and store in audio_text variable
with sr.AudioFile('audio1.wav') as source:
audio_text = r.listen(source)
# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
#try:
# using google speech recognition
text = r.recognize_google(audio_text)
print('Converting audio transcripts into text ...')
print(text)
#except:
#print('Sorry.. run again...')
The Traceback:
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/34AFF89C-1113-4021-BF04-27338D54F87B/Pythonista3/Documents/program/program.py", line 21, in <module>
text = r.recognize_google(audio_text)
File "/private/var/mobile/Containers/Shared/AppGroup/34AFF89C-1113-4021-BF04-27338D54F87B/Pythonista3/Documents/site-packages-3/speech_recognition/__init__.py", line 828, in recognize_google
convert_width=2 # audio samples must be 16-bit
File "/private/var/mobile/Containers/Shared/AppGroup/34AFF89C-1113-4021-BF04-27338D54F87B/Pythonista3/Documents/site-packages-3/speech_recognition/__init__.py", line 445, in get_flac_data
flac_converter = get_flac_converter()
File "/private/var/mobile/Containers/Shared/AppGroup/34AFF89C-1113-4021-BF04-27338D54F87B/Pythonista3/Documents/site-packages-3/speech_recognition/__init__.py", line 1196, in get_flac_converter
raise OSError("FLAC conversion utility not available - consider installing the FLAC command line application by running `apt-get install flac` or your operating system's equivalent")
OSError: FLAC conversion utility not available - consider installing the FLAC command line application by running `apt-get install flac` or your operating system's equivalent
Related
I try to use speech synthesis in Python 3.9.13 [MSC v.1929 64 bit (AMD64)].
If I understand well, pyttxs3 is the dedicated module (thank you in advance for any possible alternative!), which I installed successfully:
c:\>pip install -U pyttsx3
Requirement already satisfied: pyttsx3 in c:\users\...\python39\site-packages (2.90)
Requirement already satisfied: comtypes in c:\users\...\python39\site-packages (from pyttsx3) (1.1.11)
Requirement already satisfied: pywin32 in c:\users\...\python39\site-packages (from pyttsx3) (304)
Requirement already satisfied: pypiwin32 in c:\users\...\python39\site-packages (from pyttsx3) (223)
but I cannot initialize the engine: when I do
>>> e = pyttsx3.init()
Traceback (most recent call last):
File "C:\Users\...\Python39\site-packages\pyttsx3\__init__.py", line 20, in init
eng = _activeEngines[driverName]
File "C:\Program Files\...\lib\weakref.py", line 137, in __getitem__
o = self.data[key]()
KeyError: None
During handling of the above exception, another exception occurred:
...
File "C:\Users\...\Python39\site-packages\pyttsx3\drivers\sapi5.py", line 10, in <module>
import pythoncom
File "C:\Users\...\Python39\site-packages\pythoncom.py", line 2, in <module>
import pywintypes
ModuleNotFoundError: No module named 'pywintypes'
I noticed that part of the scripts are in my Users\...\AppData\Local\Packages\PythonSoftwareFoundation... and some are in "C:\Program Files\WindowsApps\PythonSoftwareFoundation.... I wonder whether that might be the reason for the errors.
How could I fix this? (I also found that there are other modules, speech and pyttsx, which however appear to be for Python 2.7: they produce Syntax error: print "..." - did you mean print("...")? Is there a Python 3 version of speech or any other alternative?)
pyttxs3 is a little (actually very) glitched for example after i removed a part of my code which involved speaking something it still spoke my old code i removed the whole pyttxs3 from my code it still didn't work at last i had to uninstall the package. I used this function. You need to install pygame (it is a game making package but we will use it) and gtts i.e google text to speech
import os
from pygame import *
from gtts import *
def speak(text: str):
try:
tts_ = gTTS(text=text, lang='en')
ran = random.randint(0, 1000000)
audio_file = 'audio-' + str(ran) + '.mp3'
tts_.save(audio_file)
# Starting the mixer
mixer.init()
# Loading the song
mixer.music.load("C:///Users/roopa/PycharmProjects/pokemon game/" + audio_file)
# Start playing the song
mixer.music.play()
clock = time.Clock()
# infinite loop
while mixer.music.get_busy():
clock.tick(60)
mixer.music.unload()
os.remove(audio_file)
except gTTSError:
print('unknown error')
Roop's answer didn't work out completely for me due to an error "Failed loading libmpg123-0.dll", but it enabled me to find the following working solution:
import gtts # text to mp3 file
import random # for random file name generation
import playsound # to play mp3 file
import os # to remove audio file
def say(text: str, block = True, lang = 'en'):
"""Text-to-speech synthesis using google TTS. If block=True,
waits until the text is spoken. If False, return a cleanup
function to delete the temporary audio file."""
audio_file = f'audio-{random.randint(0, 1000000)}.mp3'
gtts.gTTS(text, lang = lang).save(audio_file)
playsound.playsound(audio_file, block = block)
if block:
os.remove(audio_file)
else:
print(f"Playing sound, don't forget to do: os.remove('{audio_file}')"
"\n(You can do so by calling the returned function.)")
return(lambda: os.remove(audio_file))
if __name__=='__main__':
cleanup = say("Good morning, Max!")
if cleanup: # if block = True, function returns None
import time
time.sleep(10)
cleanup()
(It appears to be a known bug of playsound that the sound isn't played to the end if the script ends too early, therefore I added the sleep(10), but obviously you can do other stuff instead.)
That said, the question regarding pyttsx3 and pywin32 remains open.
So I am trying to make a download tool to download stuff via a link here is a snippet from my code
if download_choice == "14":
print("Downloading test file to Desktop...")
myfile14 = requests.get(url14)
open('c:/users/%userprofile%/desktop/', 'wb').write(myfile14.content)
I want to make a program that can download stuff via a link. I want it to run on all PCs not only on mine. Here is the error:
Traceback (most recent call last): File "C:\Users\David\Desktop\Windows Download Tool\Python\WDT.py", line 100, in <module> open('c:/users/%userprofile%/desktop/', 'wb').write(myfile14.content) FileNotFoundError: [Errno 2] No such file or directory: 'c:/users/%userprofile%/desktop/'
Python does not use %userprofile% to get the username of the executing user.
To achieve this, you need to import the package os and run it's getlogin function. This returns a string with the current username
Example:
import os
username = os.getlogin()
if download_choice == "14":
print("Downloading test file to Desktop...")
myfile14 = requests.get(url14)
open(f'C:/Users/{username}/desktop/', 'wb').write(myfile14.content)
I am using an f-string for opening the file, since it is preferred by PEP-8 (it's just correct code-styling)
Attention: This only works on a windows machine
I have installed PyExifTool (https://smarnach.github.io/pyexiftool/). The installation was successful. However, when I try to run the example code provided there:
import exiftool
files = ["test.jpg"]
with exiftool.ExifTool() as et:
metadata = et.get_metadata_batch(files)
for d in metadata:
print("{:20.20} {:20.20}".format(d["SourceFile"],
d["EXIF:DateTimeOriginal"]))
I am getting this error:
Traceback (most recent call last):
File "extract_metadata_03.py", line 5, in <module>
metadata = et.get_metadata_batch(files)
File "c:\Python38\lib\site-packages\exiftool.py", line 264, in get_metadata_batch
return self.execute_json(*filenames)
File "c:\Python38\lib\site-packages\exiftool.py", line 256, in execute_json
return json.loads(self.execute(b"-j", *params).decode("utf-8"))
File "c:\Python38\lib\site-packages\exiftool.py", line 227, in execute
inputready,outputready,exceptready = select.select([fd],[],[])
OSError: [WinError 10093] Either the application has not called WSAStartup, or WSAStartup failed
I have tried with exiftool.exe Version 11.91 stand-alone Windows executable (from https://exiftool.org/) in my path as well as installing exiftool using Oliver Betz's ExifTool Windows installer (https://oliverbetz.de/pages/Artikel/ExifTool-for-Windows)
I have tried two separate Python installations (Python 3.8 and also Python 2.7) with the same behaviour.
Any assistance with this or suggestions for troubleshooting would be greatly appreciated.
You get the error because the select.select() used in exiftool.py is not compatible with Windows. To solve this you can manually add the following to exiftool.py:
if sys.platform == 'win32':
# windows does not support select() for anything except sockets
# https://docs.python.org/3.7/library/select.html
output += os.read(fd, block_size)
else:
# this does NOT work on windows... and it may not work on other systems... in that case, put more things to use the original code above
inputready,outputready,exceptready = select.select([fd],[],[])
for i in inputready:
if i == fd:
output += os.read(fd, block_size)
Source: https://github.com/sylikc/pyexiftool/commit/03a8595a2eafc61ac21deaa1cf5e109c6469b17c
I'm trying to run a speech recognition using the Speech Recognition Project
I installed SpeechRecognition as illustrated. My code ran correctly for a few times.
I was trying to input different files. Now I started getting the following error:
import speech_recognition as sr
Traceback (most recent call last):
File "<ipython-input-1-a4d5c9aae5d0>", line 1, in <module>
import speech_recognition as sr
File "/Users/Sashank/Documents/Deep_Learning_A_Z/Personal Projects/Speech recognition/speech_recognition.py", line 7, in <module>
r = sr.Recognizer()
AttributeError: module 'speech_recognition' has no attribute 'Recognizer'
The confusing thing is that I'm only executing the first line of the code, which is to import the library. And it returns the error.
import speech_recognition as sr
And the error seems to be corresponding to the next line of code, which I've not yet executed:
r = sr.Recognizer()
I'm new to both programming as well as to python. I'm using spyder3. I've restarted the kernel a few times. I tried to install SpeechRecognition again on terminal. I closed and opened spyder also a few times, but now facing the same error again and again.
Please help.
Full Code:
# Speech Recognition
# Importing Library
import speech_recognition as sr
# Creating a recognition object
r = sr.Recognizer()
# Extracting the audio & removing ambient noice
audio_file = sr.AudioFile('ambient_noise_recording.wav')
with audio_file as source:
r.adjust_for_ambient_noise(source)
audio = r.record(source)
# Recognize the audio
r.recognize_google(audio)
File "/Users/Sashank/Documents/Deep_Learning_A_Z/Personal Projects/Speech recognition/speech_recognition.py", line 7, in
Your name of the file is speech_recognition.py and python is looking not to the speech_recognition module but search the Recognizer in your module (file).
You need simple to rename your module (file).
For example from speech_recognition.py to sp_recog.py
I'm having a problem that I can't excel file.
I was using swapy+pywinauto.
program export excel file with different name (ex. time..)
I used swapy to close the export excel.
from pywinauto.application import Application
app = Application().Start(cmd_line=u'"C:\\Program Files\\Microsoft Office\\Office14\\EXCEL.EXE" \\dde')
xlmain = app.XLMAIN
xlmain.Wait('ready')
xlmain.Close()
app.Kill_()
but got error below.
Traceback (most recent call last):
File "D:/23007.py", line 54, in <module>
xlmain.Wait('ready')
WaitUntil(timeout, retry_interval, lambda: self.__check_all_conditions(check_method_names))
File "C:\Python35\lib\site-packages\pywinauto\timings.py", line 308, in WaitUntil
raise err
pywinauto.timings.TimeoutError: timed out
Process finished with exit code 1
Why do you use app.XLMAIN? Is the title of window similar to XLMAIN? Usually the title is <file name> - Excel so that pywinauto can handle it so: xlmain = app["<file name> - Excel"].
Obviously Wait('ready') raised an exception because the window with title "XLMAIN" or similar is not found.
Generally I would recommend using pyWin32 standard module win32com.client to work with Excel (through standard COM interface). See the second answer here for example: Driving Excel from Python in Windows