python opencv import error for 2.7 - python

I tried to import the cv2 package in windows and I got the error:
>>> import numpy
>>> numpy.version.version
'1.6.1'
>>> import cv2
RuntimeError: module compiled against API version 0xa but this version of numpy is 0x6
Traceback (most recent call last):
File "", line 1, in
import cv2
ImportError: numpy.core.multiarray failed to import
I have also tried numpy verson 1.11.1 but it didn't work.

You should ensure that you have a single version of numpy installed, assuming you are not working within a virtualenv. Python may still be loading the old numpy package. If that is the case, you will need to remove the old version, or, to make things easier in the long run, use a virtualenv with only the packages you require.
You can check the numpy path:
import numpy
print numpy.__path__

Related

Cannot import name 'imread', 'imresize', 'imsave' anaconda (windows)

I am trying to import image to python but end up getting the same error again and again.
I have tried most of the solutions mentioned about this problem on multiple discussion threads but none of them have solved this issue of mine.
I have installed Pillow but still the error prevails. Would really appreciate some guidance on how to resolve this issue.
from scipy.misc import imread, imresize, imsave
import numpy as np
ImportError Traceback (most recent call last)
<ipython-input-6-c8bc16b68368> in <module>
----> 1 from scipy.misc import imread, imresize, imsave
2 import numpy as np
ImportError: cannot import name 'imread'
The library you're looking scipy.misc.imread is deprecated. You can use imageio. First install Pillow and then install imageio
pip install Pillow imageio
Then you can use,
imageio.imread('image1.png')
imageio.imread('image2.png', array)
But you cannot resize in imageio. Use numpy for that.
What is your version? I think scipy.misc.imread is deprecated. Use imageio
import imageio
image = imageio.imread('pic.png')

How can I Fix an ImportError

So I am stuck on how can I fix the import error for imresize. I installed all the things I needed like installing Pillow but I haven't been able to find a solution. Also the code that I trying to run is from this github link https://github.com/CSAILVision/IBD
Also side note I do not own this code nor was not the original arthor of it. This is for a research project that I am apart of. In addition I try asked the original arthor with a fix but haven't heard back from them at all.
Installing Pillow, Replacing with code: from scipy.misc.pilutil import imread, installing Pillow-3.3.1-cp27-cp27m-win32.whl, Unninstall Pil and installing Pillow, Reinstalling Pillow.
from util.image_operation import *
from PIL import Image
import numpy as np
from imageio import imresize, imread
from visualize.plot import random_color
from torch.autograd import Variable as V
import torch
I expect it to run but print out the image but it doesn't.
Traceback (most recent call last):
File "test.py", line 4, in <module>
from loader.model_loader import loadmodel
File "/home/joshuayun/Desktop/IBD/loader/model_loader.py", line 5, in <module>
from util.feature_operation import hook_feature, hook_grad
File "/home/joshuayun/Desktop/IBD/util/feature_operation.py", line 6, in <module>
from imageio import imresize, imread, imsave
ImportError: cannot import name 'imresize'
If you want to work with the code as is, i would recommend creating a new environment, and installing an old version of scipy (0.19.1 should do the trick). In your new environment, assuming you are using conda, do:
conda install scipy==0.19.1
If you use pip instead:
pip install scipy==0.19.1
Since scipy 1.3.0rc1 resize has been removed.
I had the same problem, I had scipy 1.4. I did the following and it worked -
pip install scipy==1.1.0

import error OpenCV on Jupyter Notebook (but it works in Ipython on Terminal..)

I'm having an interesting error where I could import cv2 in Ipython on Terminal but cannnot import the library on Jupyter notebook. I checked the kernel but I'm using the same kernel (Anaconda python2.7)
Would someone has any idea how to fix this?
Error:
ImportErrorTraceback (most recent call last)
<ipython-input-2-52da0154cfe4> in <module>()
----> 1 import cv2
2 import numpy as np
ImportError: No module named cv2
$import os
$os.sys.path
['',
'/Users/kn/anaconda2/envs/python2/lib/python27.zip',
'/Users/kn/anaconda2/envs/python2/lib/python2.7',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/plat-darwin',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/plat-mac',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/lib-tk',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/lib-old',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/lib-dynload',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/site-packages',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/site-packages/Sphinx-1.4.6-py2.7.egg',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/site-packages/aeosa',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/site-packages/setuptools-27.2.0-py2.7.egg',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/site-packages/IPython/extensions',
'/Users/kn/.ipython']
$which python
/Users/kn/anaconda2/envs/python2/bin/python
Create a one-line opencv.pth file under /Users/kn/anaconda2/lib/python2.7/site-packages.
The content of that file is like:
/usr/local/opt/opencv3/lib/python2.7/site-packages
This will append the opencv3 path under all conda environments permanently. I think it's better than just make cv2 available for python2 environment.

AttributeError: 'module' object (scipy) has no attribute 'misc'

I updated from ubuntu 12.04 to ubuntu 12.10 and the python module I have written suddenly no longer works with the error message that the module scipy does not have the attribute 'misc'. This worked previously. I am still using python 2.7 after the update. Here is where the code crashes
import scipy
scipy.misc.imsave(slice,dat)
Any ideas?
>>> import scipy
>>> scipy.misc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'misc'
>>>
>>>
>>> import scipy.misc
>>> scipy.misc.imsave
<function imsave at 0x19cfa28>
>>>
Which seems to be quite common with scipy.
Because you cannot directly use the misc module from scipy without explicitly import it. Here is the way of loading scipy.misc:
import scipy.misc
#Load the Lena image into an array, (yes scipy does have a lena function)
lena = scipy.misc.lena()
...
imread is depreciated after version 1.2.0 !
So to solve the problem I had to install 1.1.0 version.
pip install scipy==1.1.0
You need to explicitly import scipy.misc as:
import scipy.misc
You need to install the package pillow (formerly known as PIL), if not already installed. For image manipulation functions of scipy.misc such as imread() or imsave() to function correctly, pillow has to be installed.
To verify, either run your code again or type the below command:
scipy.misc.imread
I had a similar problem. In my case, I was trying to import comb from scipy.misc, which had been depreciated in scipy 1.0.0 (see ref here). Thus, I was inevitabely getting AttributeError: module 'scipy.misc' has no attribute 'comb'.
Replacing scipy.misc.comb with scipy.special.comb fixed the issue.

ImportError when installing NumPy for Python 2.7

EDIT: after reading this http://projects.scipy.org/numpy/ticket/1322 it seems that the NumPy version I am using doesn't work with Mac OS 10.5.x. Does anyone have access to a version of NumPy that works with Mac OS 10.5? I can't get it to compile either.
Original post ...
I am trying to use NumPy, but I'm having difficulty installing it. I'm using the installer that is available on sourceforge.
NumPy v1.5.0 (Py2.7)
Python 2.7
Mac OS 10.5.8 (PPC)
After running the installer (all I did was double-click the .pkg and follow the instructions), I try to import it ...
>>> import numpy
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import numpy
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nump/__init__.py", line 153, in <module>
import random
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/random/__init__.py", line 87, in <module>
from mtrand import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/random/mtrand.so, 2): Symbol not found: _fopen$UNIX2003
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/random/mtrand.so
Expected in: /usr/lib/libSystem.B.dylib
Does anyone have experience with this or have advice on how to fix it?
Thanks!
You can find discussion about current problems with Mac OS on the numpy mailing list, and I would recommend to discuss installation problems there, since that's were the developers are that try to fix the problems.
for example
http://groups.google.com/group/numpy/browse_thread/thread/a0ceb45b58feca2b#
and this is OS 10.5 specific
http://groups.google.com/group/numpy/browse_thread/thread/de75279785d56a25/2bfbb96e6d6c0a2e
You could join the effort, since they don't seem to have many OS 10.5 users available for testing.

Categories

Resources