gTTS Doesn't Save MP3 Files - python

I want to create an artificial intelligence program using Python. I found out that I need gTTS but it doesn't save mp3 files. Help me, please.
#gtts is imported
def talkToMe(audio, lgg = 'en'):
#print(audio)
tts = gTTS(text = audio, lang = lgg)
#tts.save('audio.mp3') #doesn't work
with open("audio.mp3") as fp: #doesn't work
tts.write_to_fp(fp)
os.system('mpg123\mpg123.exe audio.mp3')
Traceback (most recent call last):
File "C:\Users\zigzag\Desktop\gtts_test1\main.py", line 9, in <module>
talkToMe("hello")
File "C:\Users\zigzag\Desktop\gtts_test1\main.py", line 7, in talkToMe
tts.write_to_fp(fp)
File "B:\Python36\lib\site-packages\gtts\tts.py", line 187, in write_to_fp
part_tk = self.token.calculate_token(part)
File "B:\Python36\lib\site-packages\gtts_token\gtts_token.py", line 28, in calculate_token
seed = self._get_token_key()
File "B:\Python36\lib\site-packages\gtts_token\gtts_token.py", line 62, in _get_token_key
a = re.search("a\\\\x3d(-?\d+);", tkk_expr).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

from gtts import gTTS
#gtts is imported
audio = 'Hello , I am Tina, your digital assistant'
def talkToMe(audio, lgg = 'en'):
#print(audio)
tts = gTTS(text = audio, lang = lgg)
tts.save('audio.mp3') #doesn't work
return None
talkToMe(audio , lgg ='en')
save the above file as text_2_speech.py (as an e.g.).i have tried it and it is working fine. from command line type python -W ignore text_2_speech.py and then the file is saved as audio.mp3 in the current directory.

Related

How to Display .raw Dataset?

I'm trying to write a script to display the images in the file burned_wood_with_tape_1664x512x256_12bit.raw from this website: https://figshare.com/articles/SSOCT_test_dataset_for_OCTproZ/12356705
for a research project. However, I can't find a way to display the images in this .raw dataset.
This is the software I have, using other questions on StackOverflow:
import rawpy
import imageio
path = "Datasets/burned_wood_with_tape_1664x512x256_12bit.raw"
for item in path:
item_path = path + item
raw = rawpy.imread(item_path)
rgb = raw.postprocess()
rawpy.imshow(rgb)
But I get this error:
Traceback (most recent call last):
File "[ENTER PATH]", line 7, in <module>
raw = rawpy.imread(item_path)
File "[ENTER PATH]\lib\site-packages\rawpy\__init__.py", line 20, in imread
d.open_file(pathOrFile)
File "rawpy\_rawpy.pyx", line 404, in rawpy._rawpy.RawPy.open_file
File "rawpy\_rawpy.pyx", line 914, in rawpy._rawpy.RawPy.handle_error
rawpy._rawpy.LibRawIOError: b'Input/output error'
The data that you have is not a ".raw" file. It is a dataset that can be used with the "Virtual OCT System" of OCTproZ (https://github.com/spectralcode/OCTproZ/). The "rawpy" library is not useful in this case. That library works for ".raw" photos.

Phone Book Access Profile pabp linux

I try to execute the file 'pbapclient.py'. I downloaded the files from https://github.com/bmwcarit/pypbap , but the console prompt show me some errors. I want to print/save all my contact from my android phone(I've got htc one m9).I run antergos. I try for at least 4 month... thanks for the help.
This is the prompt
[mattia#LinuxMattia pypbap-master]$ python3 pbapclient.py
Traceback (most recent call last): File "pbapclient.py", line 22, in
<module> from PyOBEX import client File "/usr/lib/python3.7/site-
packages/PyOBEX/client.py", line 489, in <module>
class SyncClient(Client): File "/usr/lib/python3.7/site-
packages/PyOBEX/client.py", line 491, in SyncClient
def connect(self, header_list = (headers.Target("IRMC-SYNC"),)):
File "/usr/lib/python3.7/site-packages/PyOBEX/headers.py", line 33, in
__init__
self.data = self.encode(data)
File "/usr/lib/python3.7/site-packages/PyOBEX/headers.py", line 48, in
encode return struct.pack(">BH", self.code, len(data) + 3) + data
TypeError: can't concat str to bytes
Thanks for the help
The installation guide (https://github.com/bmwcarit/pypbap#installation) tells you that the program needs to be run with python2, however you were using python3.

Failed to download file using pafy

I am using Python 2.7 and pafy to download audio file from youtube
import pafy
video = pafy.new("https://www.youtube.com/watch?v=dcNlEn1LrrE")
print video.m4astreams
filename = video.m4astreams[0].download(quiet=False)
I get the following error:
Traceback (most recent call last):
File "E:\work\Python\2017\pafy\work_with_pafy.py", line 27, in <module>
filename = video.m4astreams[0].download(quiet=False)#.encode('utf-8')
File "c:\python27\lib\site-packages\pafy\backend_shared.py", line 586, in download
filename = self.generate_filename(meta=meta, max_length=256-len('.temp'))
File "c:\python27\lib\site-packages\pafy\backend_shared.py", line 458, in generate_filename
return xenc(filename)
File "c:\python27\lib\site-packages\pafy\util.py", line 63, in xenc
return utf8_replace(stuff) if not_utf8_environment else stuff
File "c:\python27\lib\site-packages\pafy\util.py", line 57, in utf8_replace
txt = txt.encode(sse, "replace").decode(sse)
TypeError: encode() argument 1 must be string, not None
Please Help!
Thanks in advance.
I have found the solution.
The problem is solved by replacing one string in util.py file C:\Python27\Lib\site-packages\pafy\util.py
I replaced that string in util.py:
txt = txt.encode(sse, "replace").decode(sse)
by this one:
txt = txt.encode('utf-8')
After that file successfully downloaded without any problems.

File 'tesseract.log' is Missing (Python 2.7, Windows)

I'm trying to write an OCR script with Python (2.7, Windows OS) to get text from images. First I've downloaded PyTesser and extracted it to Python27/Lib/site-packages as 'pytesser' and I've installed tesseract with pip install tesseract . Then I wrote the following script as self.py:
from PIL import Image
from pytesser.pytesser import *
image_file = 'C:/Users/blabla/test.png'
im = Image.open(image_file)
text = image_to_string(im)
text = image_file_to_string(image_file)
text = image_file_to_string(image_file, graceful_errors=True)
print text
But I'm getting the following error:
Traceback (most recent call last):
File "C:/Users/blabla/self.py", line 7, in <module>
text = image_file_to_string(image_file)
File "C:\Python27\lib\site-packages\pytesser\pytesser.py", line 44, in image_file_to_string
call_tesseract(filename, scratch_text_name_root)
File "C:\Python27\lib\site-packages\pytesser\pytesser.py", line 24, in call_tesseract
errors.check_for_errors()
File "C:\Python27\lib\site-packages\pytesser\errors.py", line 10, in check_for_errors
inf = file(logfile)
IOError: [Errno 2] No such file or directory: 'tesseract.log'
And yes, there's no 'tesseract.log' file anywhere. What should I do? How should I solve this problem?
Thank you in advance.
Note: I've changed the line tesseract_exe_name from pytesser.py from tesseract to C:/Python27/Lib/site-packages/pytesser/tesseract but it doesn't work.
Edit: Alright, I've just runned teseract.exe that is in 'pytesser' and it created the 'tesseract.log' file but I'm still getting same error.
I've changed the line from def check_for_errors(logfile = "tesseract.log"): to def check_for_errors(logfile = "C:/Python27/Lib/site-packages/pytesser/tesseract.log"): in ../pytesser/errors.py and it worked.

tokenize error in python

I have this sample program from python-docx library example-extracttext.py to extract text from a docx file.
#!/usr/bin/env python
"""
This file opens a docx (Office 2007) file and dumps the text.
If you need to extract text from documents, use this file as a basis for your
work.
Part of Python's docx module - http://github.com/mikemaccana/python-docx
See LICENSE for licensing information.
"""
import sys
from docx import opendocx, getdocumenttext
if __name__ == '__main__':
try:
document = opendocx(sys.argv[1])
newfile = open(sys.argv[2], 'w')
except:
print(
"Please supply an input and output file. For example:\n"
" example-extracttext.py 'My Office 2007 document.docx' 'outp"
"utfile.txt'"
)
exit()
# Fetch all the text out of the document we just created
paratextlist = getdocumenttext(document)
# Make explicit unicode version
newparatextlist = []
for paratext in paratextlist:
newparatextlist.append(paratext.encode("utf-8"))
# Print out text of document with two newlines under each paragraph
newfile.write('\n\n'.join(newparatextlist))
It runs fine but when I put another program called tokenize.py (given just below) in the same directory.
import nltk.data
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
fo = open(sys.argv[1], "r")
data = fo.read()
print '\n-----\n'.join(tokenizer.tokenize(data))
It gives following error.
Traceback (most recent call last):
File "./example-extracttext.py", line 14, in <module>
from docx import opendocx, getdocumenttext
File "/usr/local/lib/python2.7/dist-packages/docx-0.2.1-py2.7.egg/docx.py", line 12, in <module>
from lxml import etree
File "parsertarget.pxi", line 4, in init lxml.etree (src/lxml/lxml.etree.c:178742)
File "/usr/lib/python2.7/inspect.py", line 39, in <module>
import tokenize
File "/home/sriram/NLP_TOOLS/EDITING_TOOL/NLP/sriram_work/tokenize.py", line 3, in <module>
import nltk.data
File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 106, in <module>
from decorators import decorator, memoize
File "/usr/local/lib/python2.7/dist-packages/nltk/decorators.py", line 176, in <module>
#decorator
File "/usr/local/lib/python2.7/dist-packages/nltk/decorators.py", line 154, in decorator
if inspect.isclass(caller):
AttributeError: 'module' object has no attribute 'isclass'
Please tell me how to resolve this. I want to use both the programs in a single shell script.

Categories