module 'skipthoughts' has no attribute 'load_model' - python

I was implementing the following text encoder code but was facing this error:
from codecs import EncodedFile
import os
from os import listdir
from os.path import isfile, join
import skipthoughts
import os.path
import pickle
# from keras.models import load_model
annot_dir='annot/test_annotations'
encoded_vector_dir='annot/test_encoded'
garbage='annot/test_garbage'
model=skipthoughts.load_model()
ofiles=[f_ for f_ in listdir(annot_dir) if isfile(join(annot_dir,f_))]
ofiles_1=ofiles[0: len(ofiles)//5]
ofiles_2=ofiles[len(ofiles)//5: 2*len(ofiles)//5]
ofiles_3=ofiles[2*len(ofiles)//5: 3*len(ofiles)//5]
ofiles_4=ofiles[3*len(ofiles)//5: 4*len(ofiles)//5]
ofiles_5=ofiles[4*len(ofiles)//5: 5*len(ofiles)//5]
for files in ofiles:
if os.path.exists(join(encoded_vector_dir,files))==False :
with open(join(annot_dir,files)) as f:
captions=f.read().split(',')
captions=[cap for cap in captions if len(cap.strip())>0]
try:
caption_vectors=skipthoughts.encode(model,captions)
except:
with open(join(garbage,files),mode='wb') as myfile:
pickle.dump(caption_vectors,myfile)
else:
print('skipped')
and below is the error I'm facing, if anyone has a solution if might be very helpful. Thanks.
Traceback (most recent call last):
File "\encoder.py", line 14, in <module>
model=skipthoughts.load_model()
AttributeError: module 'skipthoughts' has no attribute 'load_model'

Related

Having trouble importing cv2, outputting an importerror

Input
Trying to import these libraries and cv2 is throwing an error:
import argparse
import cv2
from datetime import datetime
import keyboard as key
import imutils
import mediapip as mp
import numpy as np
import os
import pandas as pd
from PIL import Image
import sys
import time
This is the error returned:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-ee20a49702d1> in <module>
1 import argparse
----> 2 import cv2
3 from datetime import datetime
4 import keyboard as key
5 import imutils
~/opt/anaconda3/lib/python3.8/site-packages/cv2/__init__.py in <module>
7
8 from .cv2 import *
----> 9 from .cv2 import _registerMatType
10 from . import mat_wrapper
11 from . import gapi
ImportError: cannot import name '_registerMatType' from 'cv2.cv2' (/Users/ryleigh_grove/opt/anaconda3/lib/python3.8/site-packages/cv2/cv2.cpython-38-darwin.so)

python opening .txt error file an integer is required (got type str)

I've never used from os import * So this duplicate suggestion is not valid. Please look at the code in the description.
I'm not sure why I am running into this error. I thought it would be a simple with open() method but it isn't working. I have a .txt file that I'm trying to open and I'm getting this error.
I'm not sure why it is asking for a integer vs a string to open a .txt file
import os
import spacy
import en_core_web_sm
import plac
from collections import Counter
from __future__ import unicode_literals
file = 'spacy_sample.txt'
with open(file, 'r' )as f:
'''Do something'''
TypeError Traceback (most recent call last)
<ipython-input-198-b6441a57e84e> in <module>()
----> 1 with open(file, 'r' )as f:
2 print(f)
TypeError: an integer is required (got type str)

Import .edf file directly from online archive in python

Using pyedflib to import edf files, is it possible to import datasets directly from their source? Or is it always necessary to download data and import locally?
for example, I would like to do this:
pyedflib.EdfReader("https://www.physionet.org/pn6/chbmit/chb02/chb02_02.edf")
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-64-d123ce671a2f> in <module>()
----> 1 pyedflib.EdfReader("https://www.physionet.org/pn6/chbmit/chb02/chb02_02.edf")
pyedflib/_extensions/_pyedflib.pyx in pyedflib._extensions._pyedflib.CyEdfReader.__init__()
pyedflib/_extensions/_pyedflib.pyx in pyedflib._extensions._pyedflib.CyEdfReader.open()
pyedflib/_extensions/_pyedflib.pyx in pyedflib._extensions._pyedflib.CyEdfReader.check_open_ok()
IOError: can not open file, no such file or directory
Answer received on GitHub page
import pyedflib
import os
url = "https://www.physionet.org/pn6/chbmit/chb01/chb01_01.edf"
filename = "./chb.edf"
try:
from urllib import urlretrieve # Python 2
except ImportError:
from urllib.request import urlretrieve # Python 3
urlretrieve(url,filename)
pyedflib.EdfReader(filename)
os.remove(filename)
https://github.com/holgern/pyedflib/issues/22#issuecomment-341649760

Python gTTS not accepting strings within variables

I am writing a python script to scan a photo which contains text with google vision OCR, then use Google gTTS to speak the text. Here is the code:
#BookrBasic
from os import system
from time import sleep
from pygame import mixer
from gtts import gTTS
import subprocess
def tts(speech):
tts = gTTS(text=speech, lang='en')
tts.save("/tmp/text.mp3")
subprocess.Popen(['mpg123', '-q', '/tmp/text.mp3']).wait()
def ocr(file):
out = system('python3 ~/bookrbasic/ocr.py <KEY GOES HERE> ' + file)
return out
text = ocr("~/bookrbasic/photos/canada4.jpg")
tts(text)
This is the error I recieve:
Traceback (most recent call last):
File "BookrBasic.py", line 20, in <module>
tts(text)
File "BookrBasic.py", line 11, in tts
tts = gTTS(text=speech, lang='en')
File "/usr/local/lib/python2.7/dist-packages/gtts/tts.py", line 72, in __init__
raise Exception('No text to speak')
Exception: No text to speak
Does anyone know what the issue is here?
Thanks in advance.
Never mind, I managed to fix it by writing to a file in tmp.
Here is the new code, in case anyone is having a similar issue:
#BookrBasic
import os
from os import system
from time import sleep
from pygame import mixer
from gtts import gTTS
import subprocess
def tts(speech):
tts = gTTS(text=speech, lang='en-uk')
os.mkfifio('/tmp/fifo')
tts.save("/tmp/text.mp3")
subprocess.Popen(['mpg123', '-q', '/tmp/text.mp3']).wait()
def ocr(file):
system('python3 ~/bookrbasic/ocr.py <KEY GOES HERE> ' + file + ' > /tmp/text.txt')
with open ("/tmp/text.txt", "r") as myfile:
out=myfile.read()
return out
text = ocr("~/bookrbasic/photos/canada4.jpg")
tts(text)

Alternative way of connecting to PIL library?

Usually the library PIL is connected as follows:
from PIL import ImageTk, Image
I would like to connect it this way:
import PIL
but my version does not work. Here's the code:
import os, sys
import tkinter
import PIL
main = tkinter.Tk()
catalogImg1 = 'imgs'
nameImg1 = 'n.jpg'
pathImg1 = os.path.join(catalogImg1, nameImg1)
openImg = PIL.Image.open(pathImg1)
renderImg = PIL.ImageTk.PhotoImage(openImg)
tkinter.Label(main, image=renderImg).pack()
main.mainloop()
The error message is:
Traceback (most recent call last): File
"C:\Python33\projects\PIL_IMAGETK\ImageTK_photoimage - копия.py", line
11, in
openImg = PIL.Image.open(pathImg1) AttributeError: 'module' object has no attribute 'Image'
Importing a package (PIL) does not automatically import subpackages, submodules (PIL.Image, PIL.ImageTk). (Unless the package itself do it).
Explicitly import the submodules.
Replace following line:
import PIL
with:
import PIL.Image
import PIL.ImageTk
This is because, Image is a submodule within the PIL package i.e. It is not a function or class. Importing a package does not automatically import its submodules.
If you want to use the PIL namespace, you can import the module as follows:
import PIL.Image
openImg = PIL.Image.open(pathImg1)
If you want to import all the submodules of PIL, you can do the following
from PIL import *
openImg = Image.open(pathImg1)

Categories