OpenCV 2.0 和 Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2195441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV 2.0 and Python
提问by Jive Dadson
I cannot get the example Python programs to run. When executing the Python command "from opencv import cv" I get the message "ImportError: No module named _cv". There is a stale _cv.pyd in the site-packages directory, but no _cv.py anywhere. See step 5 below.
我无法运行示例 Python 程序。执行 Python 命令“from opencv import cv”时,我收到消息“ImportError: No module named _cv”。site-packages 目录中有一个陈旧的 _cv.pyd,但任何地方都没有 _cv.py。请参阅下面的步骤 5。
MS Windows XP, VC++ 2008, Python 2.6, OpenCV 2.0
MS Windows XP、VC++ 2008、Python 2.6、OpenCV 2.0
Here's what I have done.
这是我所做的。
- Downloaded and ran the MS Windows installer for OpenCV2.0.
- Downloaded and installed CMake
- Downloaded and installed SWIG
- Ran CMake. After unchecking "ENABLE_OPENMP" in the CMake GUI, I was able to build OpenCV using INSTALL.vcproj and BUILD_ALL.vcproj. I do not know what the difference is, so I built everything under both of those project files. The C example programs run fine.
- Copied contents of OpenCV2.0/Python2.6/lib/site-packages to my installed Python2.6/lib/site-packages directory. I notice that it contains an old _cv.pyd and an old libcv.dll.a.
- 下载并运行 OpenCV2.0 的 MS Windows 安装程序。
- 下载并安装 CMake
- 下载并安装 SWIG
- 跑 CMake。在 CMake GUI 中取消选中“ENABLE_OPENMP”后,我能够使用 INSTALL.vcproj 和 BUILD_ALL.vcproj 构建 OpenCV。我不知道有什么区别,所以我在这两个项目文件下构建了所有内容。C 示例程序运行良好。
- 将 OpenCV2.0/Python2.6/lib/site-packages 的内容复制到我安装的 Python2.6/lib/site-packages 目录中。我注意到它包含一个旧的 _cv.pyd 和一个旧的 libcv.dll.a。
采纳答案by Roger Metor
After Step 1 (Installer) just copy the content of C:\OpenCV2.0\Python2.6\Lib\site-packages
to C:\Python26\Lib\site-packages
(standard installation path assumed).
That's all.
在第 1 步(安装程序)之后,只需复制C:\OpenCV2.0\Python2.6\Lib\site-packages
to的内容C:\Python26\Lib\site-packages
(假定为标准安装路径)。就这样。
If you have a webcam installed you can try the camshift.demo in C:\OpenCV2.0\samples\python
如果您安装了网络摄像头,您可以尝试使用 camshift.demo C:\OpenCV2.0\samples\python
The deprecated stuff (C:\OpenCV2.0\samples\swig_python
) does not work at the moment as somebody wrote above. The OpenCV People are working on it. Here is the full picture:
C:\OpenCV2.0\samples\swig_python
正如上面有人写的那样,已弃用的东西 ( ) 目前不起作用。OpenCV 人员正在研究它。这是完整的图片:
31/03/10 (hopefully) Next OpenCV Official Release: 2.1.0 is due March 31st, 2010. link://opencv.willowgarage.com/wiki/Welcome/Introduction#Announcements
04/03/10 [james]rewriting samples for new Python 5:36 PM Mar 4th
via API link://twitter.com/opencvlibrary12/31/09 We've gotten more serious about OpenCV's software engineering. We now have a full C++ and Python interface. link://opencv.willowgarage.com/wiki/OpenCV%20Monthly
9/30/09 Several (actually, most) SWIG-based Python samples do not work correctly now. The reason is this problem is being investigated and the intermediate update of the OpenCV Python package will be released as soon as the problem is sorted out. link://opencv.willowgarage.com/wiki/OpenCV%20Monthly
31/03/10(希望如此)下一个 OpenCV 正式版本:2.1.0 将于 2010 年 3 月 31 日发布。link://opencv.willowgarage.com/wiki/Welcome/Introduction#Announcements
10 年 4 月 3 日 [james] 3 月 4 日下午 5:36
通过 API 链接为新 Python 重写示例://twitter.com/opencvlibrary12/31/09 我们对 OpenCV 的软件工程变得更加认真。我们现在有一个完整的 C++ 和 Python 接口。链接://opencv.willowgarage.com/wiki/OpenCV%20Monthly
9/30/09 几个(实际上是大多数)基于 SWIG 的 Python 示例现在无法正常工作。原因是这个问题正在调查中,一旦问题解决,OpenCV Python包的中间更新就会发布。链接://opencv.willowgarage.com/wiki/OpenCV%20Monthly
回答by Bear
You should install opencv using python dist utils. This can be accomplished by going to the opencv directory, and typing:
您应该使用 python dist utils 安装 opencv。这可以通过转到 opencv 目录并键入:
python setup.py install
Edit:
编辑:
openCV uses distutils, but somewhat indirectly. On UNIX the installation is more along the lines of:
openCV 使用 distutils,但有点间接。在 UNIX 上,安装更多的是:
configure
make -j 8
make install
For Windows, the easiest approach is probably to download the installer, much as the easiest approach for Linux is to use a package manager to install the application. You can't just copy OpenCV into the python path and have things work, as it relies on a number of C libraries which need to be compiled.
对于 Windows,最简单的方法可能是下载安装程序,就像对于 Linux 最简单的方法是使用包管理器来安装应用程序一样。您不能只是将 OpenCV 复制到 python 路径中并使其正常工作,因为它依赖于许多需要编译的 C 库。
I would imagine that the windows installer would install binaries (rather than needing to compile everything, and it probably is bad to use the installer and compile everything yourself). In any event, do not copy the files your self, either do a make install, or use the installer.
我想 Windows 安装程序会安装二进制文件(而不是需要编译所有内容,使用安装程序并自己编译所有内容可能很糟糕)。在任何情况下,都不要自己复制文件,要么进行 make install,要么使用安装程序。
Good luck with OpenCV, and I hope you enjoy using it!
祝您使用 OpenCV 好运,希望您喜欢使用它!
回答by dirkjot
As Roger already explained, the situation was quite confusing for a while. The reason was that opencv moved from a C only implementation to a mixed C and C++ implementation in opencv2. This causes headaches for all wrappers, including the python ones. So please don't complain that the python guys "keep breaking things", because they didn't. (btw, the only ever major incompatibility in python was between 2 and 3, and this was well advertised ).
正如罗杰已经解释过的那样,情况一度相当混乱。原因是 opencv 在 opencv2 中从仅 C 实现转变为混合 C 和 C++ 实现。这会导致所有包装器(包括 python 包装器)头疼。所以请不要抱怨蟒蛇人“不断破坏东西”,因为他们没有。(顺便说一句,python 中唯一的主要不兼容性是在 2 和 3 之间,并且宣传得很好)。
The good news is that somebody is trying to fix this mess by wrapping opencv2 with boost. I haven't used it extensively yet, but it is looking very good right now. http://code.google.com/p/pyopencv/It works in python2 right now, but it is already 99% percent compatible with python3.
好消息是有人试图通过用 boost 包装 opencv2 来解决这个问题。我还没有广泛使用它,但它现在看起来非常好。 http://code.google.com/p/pyopencv/它现在可以在 python2 中运行,但它已经 99% 与 python3 兼容。
PS it is not good style to keep a log of your progress in replies to a stackoverflow question: Add a reply when you have solved it.
PS 在对 stackoverflow 问题的回复中记录您的进度不是一种好风格:在您解决问题后添加回复。
回答by Thuy
I have posted a entry to setup OpenCV for Python in Windows: http://luugiathuy.com/2011/02/setup-opencv-for-python/It is for OpenCV 2.2 but I think it wors for previous version also.
我已经发布了在 Windows 中为 Python 设置 OpenCV 的条目:http: //luugiathuy.com/2011/02/setup-opencv-for-python/它适用于 OpenCV 2.2,但我认为它也适用于以前的版本。