【问题标题】:ImportError: No module named cv2 in ubuntu 16.04 Anaconda2ImportError:ubuntu 16.04 Anaconda2 中没有名为 cv2 的模块
【发布时间】:2017-08-09 09:40:49
【问题描述】:

我知道这是一个常见问题,但我先尝试了所有方法。

以下显示了问题以及我尝试过的方法:

$ python
Python 2.7.13 |Anaconda 4.3.0 (64-bit)| (default, Dec 20 2016, 23:09:15) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import caffe
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>> import numpy
>>> print numpy.__version__
1.11.3
>>> 

我试过的方法: 1.确认numpy版本;

  1. 确认python版本为2.7

  2. 检查opencv版本;

$ dpkg -l | grep libopencv

  1. 通过以下方式安装 opencv $conda install -c menpo opencv=2.4.11

在上面的第4步之后,可以正确导入cv2但无法导入Caffe。 caffe 安装是之前实施的,它通过了所有检查。就像'make all'; '进行运行测试';和“制作pycaffee”。现在它在“make all”和“make pycaffee”中都失败了。所以这里是错误信息:

$ python 
Python 2.7.13 |Anaconda 4.3.0 (64-bit)| (default, Dec 20 2016, 23:09:15) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import cv2
>>> import caffe
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/cocadas/Program/caffe-master/python/caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
  File "/home/cocadas/Program/caffe-master/python/caffe/pycaffe.py", line 13, in <module>
    from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
ImportError: No module named _caffe
>>> exit()

以下是conda列表。

$ conda list
# packages in environment at /home/cocadas/anaconda2:
#
_license                  1.1                      py27_1  
alabaster                 0.7.9                    py27_0  
anaconda                  4.3.0               np111py27_0  
anaconda-client           1.6.0                    py27_0  
anaconda-navigator        1.4.3                    py27_0  
argcomplete               1.0.0                    py27_1  
astroid                   1.4.9                    py27_0  
astropy                   1.3                 np111py27_0  
babel                     2.3.4                    py27_0  
backports                 1.0                      py27_0  
backports_abc             0.5                      py27_0  
beautifulsoup4            4.5.3                    py27_0  
bitarray                  0.8.1                    py27_0  
blaze                     0.10.1                   py27_0  
bokeh                     0.12.4                   py27_0  
boto                      2.45.0                   py27_0  
bottleneck                1.2.0               np111py27_0  
bzip2                     1.0.6                         3  
cairo                     1.14.8                        0  
cdecimal                  2.3                      py27_2  
cffi                      1.9.1                    py27_0  
chardet                   2.3.0                    py27_0  
chest                     0.2.3                    py27_0  
click                     6.7                      py27_0  
cloudpickle               0.2.2                    py27_0  
clyent                    1.2.2                    py27_0  
cmake                     3.6.3                         0  
colorama                  0.3.7                    py27_0  
conda                     4.3.14                   py27_0  
conda-build               2.1.7                    py27_0  
conda-env                 2.6.0                         0  
conda-verify              2.0.0                    py27_0  
configobj                 5.0.6                    py27_0  
configparser              3.5.0                    py27_0  
contextlib2               0.5.4                    py27_0  
cryptography              1.7.1                    py27_0  
curl                      7.52.1                        0  
cycler                    0.10.0                   py27_0  
cython                    0.25.2                   py27_0  
cytoolz                   0.8.2                    py27_0  
dask                      0.13.0                   py27_0  
datashape                 0.5.4                    py27_0  
dbus                      1.10.10                       0  
decorator                 4.0.11                   py27_0  
dill                      0.2.5                    py27_0  
dlib                      19.4.0                    <pip>
docutils                  0.13.1                   py27_0  
entrypoints               0.2.2                    py27_0  
enum34                    1.1.6                    py27_0  
et_xmlfile                1.0.1                    py27_0  
expat                     2.1.0                         0  
fastcache                 1.0.2                    py27_1  
filelock                  2.0.7                    py27_0  
flask                     0.12                     py27_0  
flask-cors                3.0.2                    py27_0  
fontconfig                2.12.1                        2  
freetype                  2.5.5                         2  
funcsigs                  1.0.2                    py27_0  
functools32               3.2.3.2                  py27_0  
futures                   3.0.5                    py27_0  
get_terminal_size         1.0.0                    py27_0  
gevent                    1.2.1                    py27_0  
glib                      2.50.2                        1  
greenlet                  0.4.11                   py27_0  
grin                      1.2.1                    py27_3  
gst-plugins-base          1.8.0                         0  
gstreamer                 1.8.0                         0  
h5py                      2.6.0               np111py27_2  
harfbuzz                  0.9.39                        2  
hdf5                      1.8.17                        1  
heapdict                  1.0.0                    py27_1  
icu                       54.1                          0  
idna                      2.2                      py27_0  
imagesize                 0.7.1                    py27_0  
ipaddress                 1.0.18                   py27_0  
ipykernel                 4.5.2                    py27_0  
ipython                   5.1.0                    py27_0  
ipython_genutils          0.1.0                    py27_0  
ipywidgets                5.2.2                    py27_1  
isort                     4.2.5                    py27_0  
itsdangerous              0.24                     py27_0  
jbig                      2.1                           0  
jdcal                     1.3                      py27_0  
jedi                      0.9.0                    py27_1  
jinja2                    2.9.4                    py27_0  
jpeg                      9b                            0  
jsonschema                2.5.1                    py27_0  
jupyter                   1.0.0                    py27_3  
jupyter_client            4.4.0                    py27_0  
jupyter_console           5.0.0                    py27_0  
jupyter_core              4.2.1                    py27_0  
lazy-object-proxy         1.2.2                    py27_0  
leveldb                   0.194                     <pip>
libffi                    3.2.1                         1  
libgcc                    4.8.5                         2  
libgfortran               3.0.0                         1  
libiconv                  1.14                          0  
libpng                    1.6.27                        0  
libsodium                 1.0.10                        0  
libtiff                   4.0.6                         3  
libxcb                    1.12                          1  
libxml2                   2.9.4                         0  
libxslt                   1.1.29                        0  
llvmlite                  0.15.0                   py27_0  
locket                    0.2.0                    py27_1  
lxml                      3.7.2                    py27_0  
markupsafe                0.23                     py27_2  
matplotlib                2.0.0               np111py27_0  
mistune                   0.7.3                    py27_0  
mkl                       2017.0.1                      0  
mkl-service               1.1.2                    py27_3  
mpmath                    0.19                     py27_1  
multipledispatch          0.4.9                    py27_0  
nbconvert                 4.2.0                    py27_0  
nbformat                  4.2.0                    py27_0  
ncurses                   5.9                          10  
networkx                  1.11                     py27_0  
nltk                      3.2.2                    py27_0  
nose                      1.3.7                    py27_1  
notebook                  4.3.1                    py27_0  
numba                     0.30.1              np111py27_0  
numexpr                   2.6.1               np111py27_2  
numpy                     1.11.3                   py27_0  
numpydoc                  0.6.0                    py27_0  
odo                       0.5.0                    py27_1  
olefile                   0.44                      <pip>
opencv                    2.4.11                 nppy27_0    menpo
openpyxl                  2.4.1                    py27_0  
openssl                   1.0.2k                        0  
pandas                    0.19.2              np111py27_1  
partd                     0.3.7                    py27_0  
patchelf                  0.9                           0  
path.py                   10.0                     py27_0  
pathlib2                  2.2.0                    py27_0  
patsy                     0.4.1                    py27_0  
pcre                      8.39                          1  
pep8                      1.7.0                    py27_0  
pexpect                   4.2.1                    py27_0  
pickleshare               0.7.4                    py27_0  
pillow                    4.0.0                    py27_0  
pip                       9.0.1                    py27_1  
pixman                    0.34.0                        0  
pkginfo                   1.4.1                    py27_0  
ply                       3.9                      py27_0  
prompt_toolkit            1.0.9                    py27_0  
protobuf                  3.2.0                     <pip>
psutil                    5.0.1                    py27_0  
ptyprocess                0.5.1                    py27_0  
py                        1.4.32                   py27_0  
pyasn1                    0.1.9                    py27_0  
pycairo                   1.10.0                   py27_0  
pycosat                   0.6.1                    py27_1  
pycparser                 2.17                     py27_0  
pycrypto                  2.6.1                    py27_4  
pycurl                    7.43.0                   py27_2  
pyflakes                  1.5.0                    py27_0  
pygments                  2.1.3                    py27_0  
pylint                    1.6.4                    py27_1  
pyopenssl                 16.2.0                   py27_0  
pyparsing                 2.1.4                    py27_0  
pyqt                      5.6.0                    py27_2  
pytables                  3.3.0               np111py27_0  
pytest                    3.0.5                    py27_0  
python                    2.7.13                        0  
python-dateutil           1.5                       <pip>
python-dateutil           2.6.0                    py27_0  
python-gflags             3.1.1                     <pip>
pytz                      2016.10                  py27_0  
pyyaml                    3.12                     py27_0  
pyzmq                     16.0.2                   py27_0  
qt                        5.6.2                         3  
qtawesome                 0.4.3                    py27_0  
qtconsole                 4.2.1                    py27_1  
qtpy                      1.2.1                    py27_0  
readline                  6.2                           2  
redis                     3.2.0                         0  
redis-py                  2.10.5                   py27_0  
requests                  2.12.4                   py27_0  
rope                      0.9.4                    py27_1  
ruamel_yaml               0.11.14                  py27_1  
scandir                   1.4                      py27_0  
scikit-image              0.12.3              np111py27_1  
scikit-learn              0.18.1              np111py27_1  
scipy                     0.18.1              np111py27_1  
seaborn                   0.7.1                    py27_0  
setuptools                27.2.0                   py27_0  
simplegeneric             0.8.1                    py27_1  
singledispatch            3.4.0.3                  py27_0  
sip                       4.18                     py27_0  
six                       1.10.0                   py27_0  
snowballstemmer           1.2.1                    py27_0  
sockjs-tornado            1.0.3                    py27_0  
sphinx                    1.5.1                    py27_0  
spyder                    3.1.2                    py27_0  
sqlalchemy                1.1.5                    py27_0  
sqlite                    3.13.0                        0  
ssl_match_hostname        3.4.0.2                  py27_1  
statsmodels               0.6.1               np111py27_1  
subprocess32              3.2.7                    py27_0  
sympy                     1.0                      py27_0  
terminado                 0.6                      py27_0  
tk                        8.5.18                        0  
toolz                     0.8.2                    py27_0  
tornado                   4.4.2                    py27_0  
traitlets                 4.3.1                    py27_0  
unicodecsv                0.14.1                   py27_0  
wcwidth                   0.1.7                    py27_0  
werkzeug                  0.11.15                  py27_0  
wheel                     0.29.0                   py27_0  
widgetsnbextension        1.2.6                    py27_0  
wrapt                     1.10.8                   py27_0  
xgboost                   0.6a2                     <pip>
xlrd                      1.0.0                    py27_0  
xlsxwriter                0.9.6                    py27_0  
xlwt                      1.2.0                    py27_0  
xz                        5.2.2                         1  
yaml                      0.1.6                         0  
zeromq                    4.1.5                         0  
zlib                      1.2.8                         3  

在conda中安装cv2后编译caffe的错误信息。

$make clean
$make all
...
AR -o .build_release/lib/libcaffe.a
LD -o .build_release/lib/libcaffe.so.1.0.0-rc5
/usr/bin/ld: cannot find -ltbb
collect2: error: ld returned 1 exit status
Makefile:574: recipe for target '.build_release/lib/libcaffe.so.1.0.0-rc5' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc5] Error 1

制作pycaffe的错误提示

$ make pycaffe
LD -o .build_release/lib/libcaffe.so.1.0.0-rc5
/usr/bin/ld: cannot find -ltbb
collect2: error: ld returned 1 exit status
Makefile:574: recipe for target '.build_release/lib/libcaffe.so.1.0.0-rc5' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc5] Error 1
cocadas@cocadas-ThinkPad-W540:~/Program/caffe-master$ 

由于缺少ltbb,我安装包

$sudo apt-get install libtbb-dev

然后“找不到-ltbb”的问题就消失了。但随后它抱怨

.build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)'

【问题讨论】:

标签: python opencv ubuntu anaconda


【解决方案1】:

在使用 opencv 2.4.11 构建 caffe 1.0.0-rc5 时,我遇到了错误:

[ 81%] Linking CXX executable compute_image_mean
../lib/libcaffe.so.1.0.0-rc5: undefined reference to `cv::imencode(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&, std::vector<unsigned char, std::
allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
../lib/libcaffe.so.1.0.0-rc5: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
collect2: error: ld returned 1 exit status
tools/CMakeFiles/compute_image_mean.dir/build.make:130: recipe for target 'tools/compute_image_mean' failed
make[2]: *** [tools/compute_image_mean] Error 1
CMakeFiles/Makefile2:625: recipe for target 'tools/CMakeFiles/compute_image_mean.dir/all' failed
make[1]: *** [tools/CMakeFiles/compute_image_mean.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 81%] Linking CXX executable convert_imageset

然后我将opencv从2.4.11升级到opencv 3。

conda install -c menpo opencv=3

然后构建顺利。

(root) root@625fbd309a17:~/caffe-rc5/build# PYTHONPATH=~/caffe-rc5/build/install/python/ python -c 'import caffe; print caffe.__version__'
1.0.0-rc5

我不确定,但 opencv_imgecodecs.so 中可能存在缺少的函数,它在 2.4.11 中未提供,但在 >= 3.0.0 中提供

【讨论】:

    【解决方案2】:

    对于importindg cv2,我使用了

    pip install opencv-python

    试一试,也许会奏效。

    【讨论】:

      猜你喜欢
      • 2014-06-04
      • 1970-01-01
      • 2019-11-30
      • 2017-10-29
      • 2019-04-24
      • 2019-08-17
      • 2018-05-07
      • 2021-06-24
      • 2018-04-01
      相关资源
      最近更新 更多