【问题标题】:cv2 import error in virtual environment虚拟环境中的cv2导入错误
【发布时间】:2016-05-02 19:02:14
【问题描述】:

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

cv2-1.0-py2.7.egg-info

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

附注先前答案中提到的解决方案均无效:/

Screenshot of Site-Packages

【问题讨论】:

  • 模块名可能有误,试试import cvimport cv2
  • 请提供更多信息,即您遵循的是哪个指南?安装步骤?

标签: python python-2.7 opencv installation opencv3.0


【解决方案1】:

在将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

【讨论】:

    【解决方案2】:

    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),我不使用它。也许有人能告诉我们为什么这一切都是一团糟。

    【讨论】:

      【解决方案3】:

      如果您使用的是 MacOS,下面详述并取自 here 的说明可以提供帮助(感谢 Daniel Shiffman 和 David Haylock!)。如果该链接失效,this 另一个位置。 brentiumbrent 的帖子解释了为什么会出现安装错误,但它是在 Linux 中编写的,因此如果您使用的是 MacOS,则无法使用 apt-get 命令。 casper 的帖子涉及到 CMAKE 的使用,但如果您没有安装它和/或以前从未使用过它,那么在终端中运行它时可能会出错...

      无论如何,我只是从第一个站点复制了 HTML。点击“run snippit”,你会得到一个不错的版本。单击上面的链接之一以获取所有相同的信息,但格式更好。它提供了所有相同的信息,但格式更好(我只是复制了 HTML)。

      在说明的最后,并没有明确说明,但您需要遵循 casper 的 引导,并将 ...opencv/SharedLibs/lib/cv2.so 文件复制到您的 python 包目录:...lib/python2.7/site-packages/ .根据here 的建议,我从 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>

      【讨论】:

        【解决方案4】:

        在使用 pip 在 OSX 上安装 opencv 时,我一直停留在消息“没有名为 opencv 的模块”上。

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

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

        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
        

        更多信息在这里 (how could we install opencv on anaconda?)

        【讨论】:

          【解决方案5】:

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

          pip install cv2wrap

          【讨论】:

            【解决方案6】:

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

            brew install opencv
            

            【讨论】:

              【解决方案7】:

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

              pip install opencv-python

              【讨论】:

                猜你喜欢
                • 2022-07-01
                • 1970-01-01
                • 2019-09-24
                • 1970-01-01
                • 1970-01-01
                • 2023-01-14
                • 2020-07-13
                • 1970-01-01
                • 2017-01-09
                相关资源
                最近更新 更多