简体   繁体   English

OpenCV Python - 没有名为 cv2 的模块(再次)

[英]OpenCV Python - No module named cv2 (again)

I know that this question has already been asked here a few times, but since no answer helped me so far, I'm posting it here again.我知道这个问题已经在这里被问过几次了,但由于到目前为止没有答案对我有帮助,我再次在这里发布。

What I did:我做了什么:

I cloned the repository https://github.com/Itseez/opencv , created and changed to the build directory and compiled OpenCV using the following commands.我克隆了存储库https://github.com/Itseez/opencv ,创建并更改到构建目录并使用以下命令编译 OpenCV。

cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_TBB=ON -D BUILD_opencv_python2=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D CUDA_ARCH_BIN="2.0 3.0 3.5" ..
make
sudo make install

Everything compiled fine.一切都编译得很好。 Afterwards I added /usr/local/lib to /etc/ld.so.conf.d/opencv.conf and performed sudo ldconfig .之后我将/usr/local/lib/etc/ld.so.conf.d/opencv.conf并执行sudo ldconfig Finally, I added最后,我补充说

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

to my local bashrc.到我本地的 bashrc。 Everything worked fine and I also can execute the cpp examples, but as soon as I try a python example or try using OpenCV in python myself, I get:一切正常,我也可以执行 cpp 示例,但是一旦我尝试使用 python 示例或尝试在 python 中使用 OpenCV,我就会得到:

ImportError: No module named cv2导入错误:没有名为 cv2 的模块

Things I tried based on other posts:我根据其他帖子尝试过的事情:

  1. Manually copy cv2.so to /usr/local/lib/python2.7/dist-packages : Did not work because I cannot find cv2.so anywhere.手动将cv2.so复制到/usr/local/lib/python2.7/dist-packages :不起作用,因为我在任何地方都找不到 cv2.so。

  2. Missing numpy or multiple versions of numpy: No problem here, because I only have one version of numpy.缺少 numpy 或多个版本的 numpy:这里没问题,因为我只有一个版本的 numpy。

Additional:额外的:

I also tried the same steps with the most recent sources from the homepage: same result.我还使用主页上的最新来源尝试了相同的步骤:相同的结果。

I found several ressources saying that the correct flag is BUILD_NEW_PYTHON_SUPPORT now and not BUILD_PYTHON_SUPPORT , like it was in earlier version.我发现几个资源说正确的标志现在是BUILD_NEW_PYTHON_SUPPORT而不是BUILD_PYTHON_SUPPORT ,就像在早期版本中一样。 Since both didn't work for me, I used BUILD_opencv_python2 .由于两者都不适合我,我使用了BUILD_opencv_python2 Is that correct?那是对的吗?

So, what could be the problem here?那么,这里可能有什么问题呢? I mean, except the fact that there is no cv2.so anywhere on my machine.我的意思是,除了我的机器上没有 cv2.so 的事实。 Btw, I use Ubuntu 14.04.顺便说一句,我使用 Ubuntu 14.04。

Thanks in advance.提前致谢。

I'm not sure if this is also the case if you compile from source but when you use the self-extractor you need to copy a file containting the Python bindings, as @Malik Brahimi pointed out.正如@Malik Brahimi 指出的那样,我不确定如果您从源代码编译是否也是这种情况,但是当您使用自解压器时,您需要复制一个包含 Python 绑定的文件。 Did you do this step?你做了这一步吗?

  1. Goto opencv\\build\\python\\2.7 folder转到 opencv\\build\\python\\2.7 文件夹
  2. Copy cv2.pyd to C:\\Python27\\lib\\site-packeges将 cv2.pyd 复制到 C:\\Python27\\lib\\site-packeges

Here is a tutorial this is taken from and that worked for me with both Anaconda on Ubuntu 14.04 and WinPython. 是一个取自 Ubuntu 14.04 和 WinPython 上的 Anaconda 的教程。 On Linux just copy the file to the same folder of your python distribution, ie Python27/lib/site-packeges在 Linux 上,只需将文件复制到 Python 发行版的同一文件夹中,即 Python27/lib/site-packeges

I did run in the same trouble.我确实遇到了同样的麻烦。 But looking at (and following) this tutorial it seems like splitting up the python environment did the trick.但是查看(和遵循) 教程,似乎拆分 python 环境可以解决问题。

I just installed OpenCV 3.0 on Ubuntu 14.04.我刚刚在 Ubuntu 14.04 上安装了 OpenCV 3.0。 It's working.它正在工作。

According to the official tutorial , install OpenCV.根据官方教程安装OpenCV。

#!/bin/bashd
#https://help.ubuntu.com/community/OpenCV
version="$(wget -q -O - http://sourceforge.net/projects/opencvlibrary/files/opencv-unix | egrep -m1 -o '\"[0-9](\.[0-9]+)+' | cut -c2-)"
echo "Installing OpenCV" $version
mkdir OpenCV
cd OpenCV
echo "Removing any pre-installed ffmpeg and x264"
sudo apt-get -qq remove ffmpeg x264 libx264-dev
echo "Installing Dependenices"
sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg cmake qt5-default checkinstall
echo "Downloading OpenCV" $version
wget -O OpenCV-$version.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download
echo "Installing OpenCV" $version
unzip OpenCV-$version.zip
cd opencv-$version
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j2
sudo checkinstall
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
echo "OpenCV" $version "ready to be used"

在此处输入图片说明 Use the following code for testing:使用以下代码进行测试:

import cv2
img = cv2.imread("/home/xiao/Desktop/test.png")
cv2.imshow("opencv",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Just change your CMakeChahce file to write python include files, and lib dir to it.只需更改您的CMakeChahce文件以写入 python 包含文件,并将 lib 目录写入它。 you may use find / -name "Python.h" to find your python directory.您可以使用find / -name "Python.h"来查找您的 python 目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM