(Jan-28-2018, 06:14 AM)snippsat Wrote: [ -> ]A couple of alternatives,a demo with Click and Fire.
Thanks, I will keep that in mind. :)
(Jan-28-2018, 06:42 AM)egslava Wrote: [ -> ]I don't have python 3 now to check it, so I'd appreciate if you tell me whether it works in Python 3 :)
#!/usr/bin/env python3
import sys
print ( sys.argv[sys.argv.index('--filename') + 1] )
Output:
$ python3 test3.py
Traceback (most recent call last):
File "test3.py", line 5, in <module>
print ( sys.argv[sys.argv.index('--filename') + 1] )
ValueError: '--filename' is not in list
$ python3 test3.py output3.wav
Traceback (most recent call last):
File "test3.py", line 5, in <module>
print ( sys.argv[sys.argv.index('--filename') + 1] )
ValueError: '--filename' is not in list
$ python3 test3.py --filename output3.wav
output3.wav
(Jan-28-2018, 08:48 AM)wavic Wrote: [ -> ]import argparse
parser = argparse.ArgumentParser()
parser.add_argument('filename', type=argparse.FileType('br'), help='File name') # open the directly for reading
# parser.add_argument('filename', help='File name') # or just as an argument
args = parser.parse_args()
while True:
chunk = args.filename.read(4096) # it is opened for reading already by argparse
if chunk:
# do something
else:
break
## if it is just an argument
# with open(args.filename, 'br') as in_file:
# read it
# do something
This will make the file name argument mandatory.
The good thing? You have a help system so you could just:
$ script.py -h
It works even if you miss the 'help' argument while adding the argument to the parser.
Or if you miss the argument it will throw the proper message.
Thanks for the argparse example. I have modified it to include the code for speech recognition
#!/usr/bin/env python3
import argparse
import speech_recognition as sr
import sys
parser = argparse.ArgumentParser()
parser.add_argument('filename', type=argparse.FileType('br'), help='File name') # open the directly for reading
# parser.add_argument('filename', help='File name') # or just as an argument
args = parser.parse_args()
while True:
#print ( sys.argv[sys.argv.index('--filename') + 1] )
print("The audio filename is %s." % args.filename)
chunk = args.filename.read(4096) # it is opened for reading already by argparse
AUDIO_FILE = args.filename
if chunk:
# use the audio file as the audio source
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source) # read the entire audio file
# recognize speech using Sphinx
try:
print("Sphinx thinks you said " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
else:
break
## if it is just an argument
# with open(args.filename, 'br') as in_file:
# read it
# do something
The help works fine
Output:
$ python3 test2.py -h
usage: test2.py [-h] filename
positional arguments:
filename File name
optional arguments:
-h, --help show this help message and exit
when I ran that test script, there were many errors, but I feel 99% of them are because the filename is not being evaluated correctly. (Most of the errors came from the speech recognition side of things).
Output:
$ python3 test2.py output3.wav
The audio filename is <_io.BufferedReader name='output3.wav'>.
Traceback (most recent call last):
File "/home/********/.local/lib/python3.5/site-packages/speech_recognition/__init__.py", line 203, in __enter__
self.audio_reader = wave.open(self.filename_or_fileobject, "rb")
File "/usr/lib/python3.5/wave.py", line 499, in open
return Wave_read(f)
File "/usr/lib/python3.5/wave.py", line 163, in __init__
self.initfp(f)
File "/usr/lib/python3.5/wave.py", line 130, in initfp
raise Error('file does not start with RIFF id')
wave.Error: file does not start with RIFF id
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/********/.local/lib/python3.5/site-packages/speech_recognition/__init__.py", line 208, in __enter__
self.audio_reader = aifc.open(self.filename_or_fileobject, "rb")
File "/usr/lib/python3.5/aifc.py", line 890, in open
return Aifc_read(f)
File "/usr/lib/python3.5/aifc.py", line 340, in __init__
self.initfp(f)
File "/usr/lib/python3.5/aifc.py", line 305, in initfp
raise Error('file does not start with FORM id')
aifc.Error: file does not start with FORM id
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/********/.local/lib/python3.5/site-packages/speech_recognition/__init__.py", line 234, in __enter__
self.audio_reader = aifc.open(aiff_file, "rb")
File "/usr/lib/python3.5/aifc.py", line 890, in open
return Aifc_read(f)
File "/usr/lib/python3.5/aifc.py", line 340, in __init__
self.initfp(f)
File "/usr/lib/python3.5/aifc.py", line 303, in initfp
chunk = Chunk(file)
File "/usr/lib/python3.5/chunk.py", line 63, in __init__
raise EOFError
EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test2.py", line 23, in <module>
with sr.AudioFile(AUDIO_FILE) as source:
File "/home/********/.local/lib/python3.5/site-packages/speech_recognition/__init__.py", line 236, in __enter__
raise ValueError("Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format")
ValueError: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format
Have done a fair bit of looking at other examples of argparse, but still not able to get the variable
args.filename evaluated properly. Yet the one liner with using the sys module works okay.
Hmm, I changed 2 instances of "args.filename" to "args.filename.name" , and it works.
#!/usr/bin/env python3
import argparse
import speech_recognition as sr
import sys
parser = argparse.ArgumentParser()
parser.add_argument('filename', type=argparse.FileType('br'), help='File name') # open the directly for reading
# parser.add_argument('filename', help='File name') # or just as an argument
args = parser.parse_args()
while True:
#print ( sys.argv[sys.argv.index('--filename') + 1] )
print("The audio filename is %s." % args.filename.name)
chunk = args.filename.read(4096) # it is opened for reading already by argparse
AUDIO_FILE = args.filename.name
if chunk:
# use the audio file as the audio source
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source) # read the entire audio file
# recognize speech using Sphinx
try:
print("Sphinx thinks you said " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
else:
break
## if it is just an argument
# with open(args.filename, 'br') as in_file:
# read it
# do something
but it is stuck in a loop, ..lol. I will see about the 'while' maybe. :)
No loop now, and removed some code, ..short and simple
#!/usr/bin/env python3
import argparse
import speech_recognition as sr
parser = argparse.ArgumentParser()
parser.add_argument('filename', type=argparse.FileType('br'), help='File name') # open the directly for reading
args = parser.parse_args()
print("The audio filename is %s." % args.filename.name)
AUDIO_FILE = args.filename.name
# use the audio file as the audio source
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source) # read the entire audio file
# recognize speech using Sphinx
try:
print("Sphinx thinks you said " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))