【问题标题】:Python "Segmentation fault: 11" when running "import cv" or "import cv2"运行“import cv”或“import cv2”时的Python“分段错误:11”
【发布时间】:2015-04-24 12:41:42
【问题描述】:

我已经按照these instructions, 使用macports 安装了numpy 和opencv,但是当我尝试import cvimport cv2 时,我只是得到了段错误,我不知道为什么。

有什么建议吗?


$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Segmentation fault: 11

$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Segmentation fault: 11

抱歉,细节太少了,但这是一台新计算机,我为此安装了 macports,而这些是我用 macports 安装的唯一东西,所以我不知道为什么这不起作用.


编辑:现在我更困惑了。

查看崩溃报告我发现了这个:

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000

所以,作为一个在黑暗中的镜头,我以超级用户身份运行 python:

$ sudo python
Python 2.7.9 (default, Dec 13 2014, 15:13:49) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
>>>

作为超级用户,一切似乎都运行良好。这怎么可能?

【问题讨论】:

  • 尝试修复您的库的权限,因为我看到只有 root 可以访问它们。

标签: python macos python-2.7 opencv macports


【解决方案1】:

如果您仔细查看 python 命令的信息消息,您会发现差异。

来自越野车:

$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Segmentation fault: 11

来自工作的:

$ sudo python
Python 2.7.9 (default, Dec 13 2014, 15:13:49) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
>>>

您的机器上有两个不同版本的 Python,这可以解释您的行为。

【讨论】:

  • 好眼光,我没听懂...如何让我的常规 Python 命令指向超级用户 Python 指向的位置?
  • 嗯,我发现thisthis 正在讨论一些解决方案。也许您会发现它们很有帮助。
  • 非常感谢,defaults write com.apple.versioner.python Version 2.7.9 是我需要的命令。
【解决方案2】:

在 OSX 10.11 上遇到此问题并在各种情况下浏览了此问题的几个案例后,我意识到此问题的发生主要是由于以下独立原因:

  1. 冲突的 python 版本(多个 python);解决方案 - 卸载其中一个,获取与 opencv 兼容的那个 ("Segmentation fault" during "import cv" on Mac OS)
  2. opencv 版本问题;解决方案 - 为您获取正确的版本Python opencv feature detector causes segmentation fault
  3. 您的 numpy 版本存在问题;解决方案 - 再次卸载并安装 numpy (OpenCV - cannot find module cv2)

我尝试了所有 3 个,但第 3 个解决了我的问题。

【讨论】:

    【解决方案3】:

    使用

    cv2.ocl.setUseOpenCL(False)
    

    在代码的开头为我解决了问题。

    【讨论】:

      【解决方案4】:

      我遇到了类似的问题。

      ➜  ~ ✗ python
      Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 13:19:00)
      [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import cv2
      [1]    41233 segmentation fault  python
      

      但如果我先导入 numpy,问题就会消失。

      ➜  ~ ✗ python
      Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 13:19:00)
      [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import numpy as np
      >>> import cv2
      >>>
      

      【讨论】:

        【解决方案5】:

        对我来说,解决方案很简单

        sudo apt install python3-opencv
        

        然后安装 pip opencv 包

        sudo pip3 install opencv-python 
        

        sudo pip install opencv-python 
        

        注意:这是因为我启动了一个新的 Aws 实例。

        【讨论】:

        • sudo pip install 是错误的形式。即使在独立实例中,您也应该使用 virtualenv。它很有可能引起头痛。
        • 同意,当时我还没有意识到这一点。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多