在py2app生成的应用程序中包含opencv

时间:2011-11-10 18:23:21

标签: python opencv py2app

我正在使用安装了OS 10.7.2和Apple的默认python 2.7.1的mac。我通过源安装了opencv 2.3.1,并确认import cv可以在python中运行。我创建了一个最小的python脚本(保存为“cvTest.py”),顶部只有import cv而没有别的,然后我使用了py2app(通过终端命令py2applet --make-setup cvTest.py; python setup.py py2app)。当我将生成的独立应用程序移动到未安装opencv的其他计算机(mac,OS 10.7.2)并尝试运行应用程序时,它会崩溃,并通过console.app提供以下错误:

11-11-10 1:17:38.591 PM [0x0-0xcb9cb9].org.pythonmac.unspecified.cvTest: Traceback (most recent call last):
11-11-10 1:17:38.591 PM [0x0-0xcb9cb9].org.pythonmac.unspecified.cvTest:   File "/Users/ra/Downloads/cvTest.app/Contents/Resources/__boot__.py", line 103, in <module>
11-11-10 1:17:38.591 PM [0x0-0xcb9cb9].org.pythonmac.unspecified.cvTest:     _argv_emulation()
11-11-10 1:17:38.591 PM [0x0-0xcb9cb9].org.pythonmac.unspecified.cvTest:   File "/Users/ra/Downloads/cvTest.app/Contents/Resources/__boot__.py", line 101, in _argv_emulation
11-11-10 1:17:38.591 PM [0x0-0xcb9cb9].org.pythonmac.unspecified.cvTest:     _get_argvemulator().mainloop()
11-11-10 1:17:38.591 PM [0x0-0xcb9cb9].org.pythonmac.unspecified.cvTest:   File "/Users/ra/Downloads/cvTest.app/Contents/Resources/__boot__.py", line 40, in mainloop
11-11-10 1:17:38.591 PM [0x0-0xcb9cb9].org.pythonmac.unspecified.cvTest:     stoptime = Evt.TickCount() + timeout
11-11-10 1:17:38.591 PM [0x0-0xcb9cb9].org.pythonmac.unspecified.cvTest: AttributeError: 'module' object has no attribute 'TickCount'
11-11-10 1:17:38.661 PM cvTest: cvTest Error
11-11-10 1:17:39.888 PM com.apple.launchd.peruser.502: ([0x0-0xcb9cb9].org.pythonmac.unspecified.cvTest[49524]) Exited with code: 255

然后我创建了一个简单的bash脚本,让我深入了解独立应用程序并从终端运行应用程序的二进制文件:

#!/bin/bash
CMD=$0
PROGDIR=$(echo $CMD|perl -pe 's|/[^/]*$||')
cd "$PROGDIR"
"./cvTest.app/Contents/MacOS/cvTest"

执行此操作会产生以下错误:

Traceback (most recent call last):
  File "/Users/ra/Downloads/cvTest.app/Contents/Resources/__boot__.py", line 137, in <module>
    _run('cvTest.py')
  File "/Users/ra/Downloads/cvTest.app/Contents/Resources/__boot__.py", line 134, in _run
    execfile(path, globals(), globals())
  File "/Users/ra/Downloads/cvTest.app/Contents/Resources/cvTest.py", line 1, in <module>
    import cv
  File "cv.pyc", line 1, in <module>
ImportError: No module named cv2.cv
2011-11-10 13:18:26.239 cvTest[49578:707] cvTest Error

[Process completed]

那么,这里发生了什么?为什么我收到错误,有没有办法将opencv与py2app创建的应用捆绑在一起,以便import cv有效?

3 个答案:

答案 0 :(得分:0)

说实话我不确定。但我正在开发一个基于计算机视觉的开源项目(它包装OpenCV)等(http://www.simplecv.org)。我们遇到的一个问题是跨平台部署,因为我们需要一键安装程序。

我们尝试在Windows上使用Innosetup并遇到很多问题。我厌倦了,所以我开始写自己的。基本上这个程序的功能是一个引导程序。你下载我们的exe,运行它,然后它拉入它需要的文件/库列表并开始安装它们。因此,在我们的案例中,我们可以在安装了零库的机器上安装我们的软件。它首先执行python,然后是setuptools,然后是opencv等。

基本上我作为开源发布的应用程序将试图成为innosetup的替代品,但是跨平台。如果您想现在看到它(大约85%),您可以在以下位置查看: https://github.com/ingenuitas/SimpleCV/blob/installer/install/simplecv-installer.py

我计划在完成后分叉这个项目,因此它可以是一个通用的安装程序,你只需要提供一个清单文件。 这是现有的清单: https://github.com/ingenuitas/SimpleCV/blob/installer/install/manifest.json

答案 1 :(得分:0)

问题是py2app在包中构建了完全不同的Python发行版。该分发应该是自包含的,因此它需要包含应用程序在运行时可能使用的所有库(包括OpenCV)。不幸的是,py2app似乎错过了dist / helloworld.app / Contents / Resources / lib / python2.7 / lib-dynload /所需的cv2.so文件(参见http://pythonhosted.org/py2app/implementation.html)。

以下是OpenCV的工作安装:

bash-3.2$ ls -1 /usr/local/lib/python2.7/site-packages/cv*
/usr/local/lib/python2.7/site-packages/cv.py
/usr/local/lib/python2.7/site-packages/cv.pyc
/usr/local/lib/python2.7/site-packages/cv2.so

所以理论上,将cv2.so复制到dist / helloworld.app / Contents / Resources / lib / python2.X / lib-dynload /应该可以解决问题。

不幸的是,它引入了另一个问题:

bash-3.2$ dist/main_wx.app/Contents/MacOS/main_wx 
Segmentation fault: 11

cv2.so需要包含大量依赖项:

bash-3.2$ otool -L /usr/local/Cellar/opencv/2.4.5/lib/python2.7/site-packages/cv2.so 
/usr/local/Cellar/opencv/2.4.5/lib/python2.7/site-packages/cv2.so:
        /usr/local/lib/python2.7/site-packages/cv2.so (compatibility version 0.0.0, current version 0.0.0)
        /usr/local/opt/python/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.0)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_core.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_flann.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_imgproc.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_video.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_ml.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_highgui.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_features2d.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_calib3d.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_photo.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_objdetect.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_contrib.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_legacy.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_gpu.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_ocl.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/local/Cellar/opencv/2.4.5/lib/libopencv_nonfree.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
        /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
        /System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

我不知道如何处理它们,但我感觉如果没有它们,什么都不会起作用。我要离开这里,希望别人可以用它来找到解决方案。

答案 2 :(得分:0)

查找并添加依赖项到py2app包可以这样做:

import os, sys, glob

frameworks = []
opencvLibs = glob.glob(os.path.join(sys.exec_prefix, 'lib', 'libopencv*.2.4.dylib'))
frameworks.extend(opencvLibs)

setup(app=['myApp'],
    options=dict(py2app=dict(
       frameworks=frameworks,
       includes = ['cv2',.....],
       packages = [........],
       ....
       )
    )

但不幸的是,这并没有完全解决问题。对我来说,看起来cv2.so已经用@ rpath / Python的静态链接编译,但在你的pyapp包中它需要一个相对链接,以便应用程序可以移动。 : - (