虚拟环境中的 CV2 导入错误



我在虚拟环境中尝试了所有内容,但一次又一次"没有名为opencv的模块"。我检查了站点包文件夹,发现没有 cv2 文件夹,就像我找到的 numpy、pip 文件夹一样;但是有 1 个文件夹名为:

cv2-1.0-py2.7.egg-info

现在我该怎么办?Open CV 3.0 与 python 3.5.1 不兼容,而这个问题一次又一次地阻碍了我对 python 2.7 的阻碍。请帮忙!!

附言前面答案中提到的解决方案都不起作用:/

网站包的屏幕截图

cv2-1.0 是一个虚假的垃圾模块,里面什么都没有。

亲眼看看:https://pypi.python.org/pypi/cv2/1.0

安装后,cv2-1.0 将屏蔽 opencv 并阻止其工作。

。所以删除它:

rm -rf /usr/local/lib/python2.7/dist-packages/cv2-1.0*

如果你想import cv2,你当然想要opencv python模块。

  • 在 ubuntu 上,这不会从 pip 获得——你需要安装一个系统包:

    apt-get install python-opencv

  • 如果您使用的是virtualenv,则需要手动将opencv符号链接到virtualenv中。

    cd venv/lib/python2.7 && ln -s /usr/local/lib/python2.7/dist-packages/cv2.so
    cd venv/lib/python2.7 && ln -s /usr/local/lib/python2.7/dist-packages/cv.py
    
  • 这不太可能,但有时 pip 会遇到取决于cv2的包裹。如果不执行任何操作,这将(或运行pip install cv2 - 不推荐!)将从pypi安装冒名顶替者cv2-1.0包。要安抚 pip,请制作一个元数据文件,描述系统的 cv2 安装到 pip:

    #/usr/local/lib/python2.7/dist-packages/cv2-2.4.8.egg-info
    Metadata-Version: 1.2
    Name: cv2
    Version: 2.4.8
    

    文件名和文件中的版本号应与 apt-cache show python-opencv 中的主要版本匹配。

关于python-opencv的说明

如果你从源代码构建 opencv debian 软件包,你最终会得到 opencv-python ,它描述了更少的依赖项(特别是它缺少opencv-libs),我不使用它。也许有人可以启发我们,为什么这一切如此混乱。

在将OpenCV安装到虚拟环境中时,我还发现缺少cv2.so。 解决方法是手动复制它:cp /home/user/opencv/lib/cv2.so /home/user/venv/lib/python2.7/site-packages/cv2.so .

因此,为了完整起见,以下内容对我有用:

git clone https://github.com/Itseez/opencv
cd /home/user/opencv
mkdir release && cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE 
   -D WITH_QT=ON 
   -D WITH_OPENMP=ON 
   -D CMAKE_BUILD_TYPE=RELEASE 
   -D CMAKE_INSTALL_PREFIX=/home/user/venv/local 
   -D PYTHON2_PACKAGES_PATH=/home/user/venv/local/lib/python2.7/site-packages 
   -D INSTALL_C_EXAMPLES=ON 
   -D INSTALL_PYTHON_EXAMPLES=ON 
   -D BUILD_EXAMPLES=ON ..
make
make install
cp /home/user/opencv/lib/cv2.so /home/user/venv/lib/python2.7/site-packages/cv2.so

在你的 venv 中试试这个。它会工作

pip install opencv-python

我在带有 pip 的 OSX 上的 openenv 安装中,在 opencv 上安装时,已经卡在消息"没有名为 opencv "的一段时间了。

适用于我的设置的两种解决方法是:

1 - 绕过 virtualenv 结构,直接在 python 代码中添加 opencv 的系统路径(不优雅的方式)

import sys
sys.path.append('/usr/local/lib/python3.4/site-packages')

2 - 使用 conda 代替 pip 进行 opencv 安装

conda install -c https://conda.binstar.org/menpo opencv

更多信息在这里(我们如何在Anaconda上安装OpenCV?

对我有用的解决方案是使用 pip 安装 cv2wrap 包:

pip install cv2wrap

它对我有用(在你的虚拟环境中):

brew install opencv
如果您

使用的是MacOS,下面详述的说明和此处的说明会有所帮助(感谢Daniel Shiffman和David Haylock!)。如果该链接失效,这是一个备用位置。brentiumbrent的帖子解释了为什么会发生安装错误,但是它是用Linux编写的,所以如果你在MacOS上,你就不能使用apt-get命令。casper 的帖子涉及 CMAKE 的使用,但如果您没有安装它和/或以前从未使用过它,那么在终端中运行它时可能会出错......

无论如何,我只是从第一个站点复制了 HTML。单击"运行截图",您将获得一个中途不错的版本。 单击上面的链接之一以获取所有相同的信息,但格式更好。它以更好的格式呈现所有相同的信息(我只是复制了 HTML)。

在说明的末尾,它没有明确说明,但你需要遵循 casper 的领导,并将...opencv/SharedLibs/lib/cv2.so文件复制到你的 python 包目录中:...lib/python2.7/site-packages/ 。根据此处的建议,我从 SharedLibs 文件夹而不是 StaticLibs 文件夹中安装了 cv2.so 文件,并且它起作用了。

<div class="entry-content">
  <h2>Overview</h2>
  <p style="margin-top:-30px">Despite the wealth of information on the internet, installation guides for openCV are far and few between. Whilst we have used openCV packages in previous projects they have always been wrapped in an addon format, which obviously makes it easier to
    use but, for a forthcoming project we needed the ability to access the library directly. Briefly, openCV is a library of functions which mainly focuses on image analysis, processing and evaluation. In lamen’s terms, it allows computers to process
    and understand images and other forms of visual data.</p>
  <p>In this post we will explain how we managed to work around the mysterious installation process, and provide a simple set of instructions that will enable you to install, build and use the openCV libraries and binaries on your system.</p>
  <p>First you will need:</p>
  <ul>
    <li>Mac OSX Yosemite 10.10</li>
    <li><a href="https://itunes.apple.com/gb/app/xcode/id497799835?mt=12" target="_blank">XCode</a>
    </li>
    <li>Command Line Tools (This is done from inside XCode)</li>
    <li><a href="http://www.cmake.org/download/" target="_blank">CMake</a>
    </li>
  </ul>
  <p>Ready … lets begin!
  </p>
  <h2>Building OpenCV</h2>
  <h2>Update:</h2>
  <p style="margin-top:-30px">
    Having rebuilt openCV using the XCode again the makefile for some reason is not generated, will try and find out why. For now use the Terminal method as outlined below.
    <br>
    <strong>Attention: </strong>Steps 2 a and b document methods of installing both the static and shared libraries.
  </p>
  <p><strong>Step 1:</strong>
    <br>Download <a href="http://opencv.org/downloads.html" target="_blank">openCV</a> and unzip it somewhere on your computer. Create two new folders inside of the openCV directory, one called StaticLibs and the other SharedLibs.</p>
  <p><strong>Step 2a: Build the Static Libraries with Terminal.</strong>
    <br>To build the libraries in Terminal.
    <br>
    <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.17.51.png">
      <img class="aligncenter size-full wp-image-1118" alt="Screen Shot 2014-10-23 at 10.17.51" src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.17.51.png" width="1920" height="1201">
    </a>
  </p>
  <ul>
    <li>Open CMake.</li>
    <li style="margin-top: 5px;">Click Browse Source and navigate to your openCV folder.</li>
    <li style="margin-top: 5px;">Click Browse Build and navigate to your StaticLib Folder.</li>
    <li style="margin-top: 5px;">Click the configure button. You will be asked how you would like to generate the files. Choose Unix-Makefile from the Drop Down menu and Click OK. CMake will perform some tests and return a set of red boxes appear in the CMake Window.</li>
  </ul>
  <p>
    <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.24.01.png">
      <img class="aligncenter size-full wp-image-1118" alt="Screen Shot 2014-10-23 at 10.24.01" src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.24.01.png" width="1920" height="1201">
    </a>
    <br>You will need to uncheck and add to the following options.</p>
  <ul>
    <li>Uncheck <code>BUILD_SHARED_LIBS</code>
    </li>
    <li>Uncheck <code>BUILD_TESTS</code>
    </li>
    <li>Add an SDK path to <code>CMAKE_OSX_SYSROOT</code>, it will look something like this “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk”.</li>
    <li>Add x86_64 to <code>CMAKE_OSX_ARCHITECTURES</code>, this tells it to compile against the current system</li>
    <li>Uncheck <code>WITH_1394</code>
    </li>
    <li>Uncheck <code>WITH_FFMPEG</code>
    </li>
  </ul>
  <p>Click Configure again, then Click Generate.</p>
  <p>When the application has finished generating, Open Terminal and type the following commands.</p>
  <pre>- cd &lt;path/to/your/opencv/staticlibs/folder/&gt;
- make (This will take awhile)
- sudo make install</pre>
  <p>Enter your password.
    <br>This will install the static libraries on your computer.</p>
  <p><strong>Step 2c: Build the Shared Libraries with Terminal.</strong>
  </p>
  <ul>
    <li>Open CMake.</li>
    <li style="margin-top: 5px;">Click Browse Source and navigate to your openCV folder.</li>
    <li style="margin-top: 5px;">Click Browse Build and navigate to your SharedLib Folder.</li>
    <li style="margin-top: 5px;">Click the configure button. You will be asked how you would like to generate the files. Choose Unix-Makefile from the Drop Down menu and Click OK. CMake will perform some tests and return a set of red boxes appear in the CMake Window.</li>
  </ul>
  <p>You will need to uncheck and add to the following options.</p>
  <ul>
    <li>Check <code>BUILD_SHARED_LIBS</code>
    </li>
    <li>Uncheck <code>BUILD_TESTS</code>
    </li>
    <li>Add an SDK path to <code>CMAKE_OSX_SYSROOT</code>, it will look something like this “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk”.</li>
    <li>Add x86_64 to <code>CMAKE_OSX_ARCHITECTURES</code>, this tells it to compile against the current system</li>
    <li>Uncheck <code>WITH_1394</code>
    </li>
    <li>Uncheck <code>WITH_FFMPEG</code>
    </li>
  </ul>
  <p>Click Configure again, then Click Generate.</p>
  <p>When the application has finished generating, Open Terminal.</p>
  <pre>- cd &lt;path/to/your/opencv/SharedLibs/folder/&gt;
- make (This will take awhile)
- sudo make install</pre>
  <p>Enter your password.
    <br>This will install the shared libraries on your computer.</p>
  <h2>Make an Application </h2>
  <p style="margin-top:-30px">
    This is a very basic example, but the similar principles can be applied to other code.
    <br>For this post, lets make an application that shows two images, one normal and one that has been put through a blur filter.
    <br>
    <strong> Step 1: </strong>
  </p>
  <ul>
    <li>Create a new folder somewhere on the computer.</li>
    <li>Inside the folder, create a CMakeLists.txt file then create a BlurImage.cpp file.</li>
    <li>Then add an image file.</li>
  </ul>
  <p style="text-align: center;">For this example I’ll use this fruity number.
    <br>
    <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/fruits.jpg">
      <img class="aligncenter  wp-image-1180" alt="fruits" src="http://blogs.wcode.org/wp-content/uploads/2014/10/fruits.jpg" width="307" height="288">
    </a>
  </p>
  <p><strong>Step 2:</strong>
  </p>
  <p>Open the BlurImage.cpp in your favourite text editor and add the following text.</p>
  <pre>#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
Mat src; Mat dst;
char window_name1[] = "Unprocessed Image";
char window_name2[] = "Processed Image";
int main( int argc, char** argv )
{
    /// Load the source image
    src = imread( argv[1], 1 );
    namedWindow( window_name1, WINDOW_AUTOSIZE );
    imshow("Unprocessed Image",src);
    dst = src.clone();
    GaussianBlur( src, dst, Size( 15, 15 ), 0, 0 );
    namedWindow( window_name2, WINDOW_AUTOSIZE );
    imshow("Processed Image",dst);
    waitKey();
    return 0;
}</pre>
  <p>Save the file.</p>
  <p><strong>Step 3:&nbsp;</strong>
  </p>
  <p>Open the CMakeLists.txt file then add the following text.</p>
  <pre>cmake_minimum_required(VERSION 2.8)
project( BlurImage )
find_package( OpenCV )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( BlurImage BlurImage.cpp )
target_link_libraries( BlurImage ${OpenCV_LIBS} )
</pre>
  <p>Save the File.</p>
  <p><strong>Step 4:&nbsp;</strong>
    <br>Open Terminal and navigate to your applications directory.</p>
  <pre>- cd &lt;path/to/application/folder&gt;
- /Applications/CMake.app/Contents/bin/cmake .
- make
</pre>
  <p>This will generate both the makefile and the executable file.</p>
  <p>Then type <code>./BlurImage image.jpg </code>
  </p>
  <p>
    <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.44.56.png">
      <img src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.44.56.png" alt="Screen Shot 2014-10-23 at 13.44.56" width="1920" height="1201" class="aligncenter size-full wp-image-1184">
    </a>
  </p>
  <p>
    <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.45.52.png">
      <img src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.45.52.png" alt="Screen Shot 2014-10-23 at 13.45.52" width="1920" height="1196" class="aligncenter size-full wp-image-1185">
    </a>
  </p>
  <p>Yay … it works all you need to do is add your own .cpp file and alter the .txt file. Then follow the above commands.</p>
  <p>For more detailed examples go to the <a href="http://docs.opencv.org/doc/tutorials/tutorials.html" style="color: #920;" target="_blank">openCV Tutorial Page</a>, or check out the sample folder inside the opencv folder.</p>
  <p>Huge thank you to Daniel Shiffman, whose guide put us on the right track : <a style="color: #920;" href="http://shiffman.net/2011/01/23/how-to-build-opencv-static-libraries-mac-os-x/" target="_blank">http://shiffman.net/2011/01/23/how-to-build-opencv-static-libraries-mac-os-x/</a>
  </p>
</div>

我发现一旦我激活了虚拟环境,我必须从命令面板中选择虚拟环境的Python解释器,然后它才能找到OpenCV。显然,在此之前我没有选择任何Python解释器。

相关内容

最新更新