Having trouble importing cv2, outputting an importerror - python

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)

Related

module 'skipthoughts' has no attribute 'load_model'

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'

ModuleNotFoundError: No module named 'u8darts'

I am trying to use darts library to forecast price using transformer neural nets. through ubuntu20.4 terminal and python 3.10.
The below is my code, where I just calling and Importing necessary libraries:
from os.path import dirname, basename
from os import getcwd
import sys
def fix_pythonpath_if_working_locally():
"""Add the parent path to pythonpath if current working dir is darts/examples"""
cwd = getcwd()
if basename(cwd) == 'examples':
sys.path.insert(0, dirname(cwd))
fix_pythonpath_if_working_locally()
# Commented out IPython magic to ensure Python compatibility.
# %load_ext autoreload
# %autoreload 2
# %matplotlib inl
import pandas as pd
import numpy as np
import datetime
import time
from datetime import datetime
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import shutil
import os
import seaborn as sns
import torch
from torch import nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm_notebook as tqdm
import u8darts
from u8darts import TimeSeries
from u8darts.dataprocessing.transformers import Scaler
from u8darts.models import TransformerModel, ExponentialSmoothing
from u8darts.metrics import mape
from u8darts.utils.statistics import check_seasonality, plot_acf
import warnings
warnings.filterwarnings("ignore")
import logging
But I have got this error:
Traceback (most recent call last):
File "/vol/0/home/kherad/reihane/darts.py", line 59, in <module>
import u8darts
ModuleNotFoundError: No module named 'u8darts'
How can I fix this error?(I have use pip install u8darts in terminal and installed successfully)

Importing PIL ImageFont is giving a py3 import error

I'm trying to import a PIL ImageFont but when I do that, it gives me the following error:
<ipython-input-16-ef225ec0d8fd> in <module>()
----> 1 from PIL import ImageFont
/usr/local/lib/python3.6/dist-packages/PIL/ImageFont.py in <module>()
27
28 from . import Image
---> 29 from ._util import isDirectory, isPath, py3
30 import os
31 import sys
ImportError: cannot import name 'py3'
I can't find any similar issues on the internet related with this.

python just import fails but works with from

Why does this not work:
import matplotlib.pyplot as plt
import os
import skimage
camera = skimage.io.imread(os.path.join(skimage.data_dir, 'camera.png'))
#plt.show(io.imshow(camera))
But using from skimage import io does. So this works:
import matplotlib.pyplot as plt
import os
import skimage # I still need to import skimage to get the data_dir
from skimage import io
camera = io.imread(os.path.join(skimage.data_dir, 'camera.png'))
#plt.show(io.imshow(camera))
I thought
import skimage
skimage.io."something"
Was equivalent to
from skimage import io
io."something"
I thought
import skimage
skimage.io."something"
Was equivalent to
from skimage import io
io."something"
It's not.
import skimage
causes python to look for the skimage module. Maybe there's a __init__.py that sets up what becomes visible and what is done when you import that module.

Can not save file using the below python code. Error: numpy.ndarray object has no attribute 'save'

import os
import sys
import numpy as np
import scipy
import pylab
import pymorph
import mahotas
import matplotlib.pyplot as plt
import Image
from scipy import ndimage
from pymorph import regmax
from PIL import Image
path='all_images'
for file in os.listdir(path):
current = os.path.join(path, file)
extension = os.path.splitext(current)[-1]
fileType = extension.upper()
print(current)
if os.path.isfile(current):
img = mahotas.imread(current)
imgf = ndimage.gaussian_filter(img, 8)
pylab.gray()
imgf.save('dnaa.gif')
Can not save file using the below python code. Error: numpy.ndarray object has no attribute 'save'. Can anyone help how to save file using pylab. I guss the last line of the code has some issue.
Use mahotas.imsave('dnaa.gif', imgf) instead. The NumPy array you get from gaussian_filter doesn't have save functionality built in.

Categories