Python “Segmentation fault: 11” when running “import cv” or “import cv2”

泄露秘密 提交于 2019-12-22 13:58:14

问题


I have installed numpy and opencv using macports as per these instructions, but when I try import cv or import cv2 I just get the segfault and I have no idea why.

Any suggestions?


$ 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

Sorry there are so few details, but this is a new computer and I installed macports on it just for this, and these are the only things I've installed with macports, so I have no idea why this isn't working.


Edit: Now I'm More Confused.

Looking through the crash report I found this:

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

So, as a shot in the dark, I ran python as super user:

$ 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
>>>

As super user, everything appears to run fine. How is this possible?


回答1:


If you look closely to the info message of your python command, you will see the difference.

From the buggy one:

$ 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

From the working one:

$ 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
>>>

You have two different versions of Python on your machine which might explain the behaviors you got.




回答2:


After hitting this problem on OSX 10.11 and trawling through several cases of this problem in various contexts, I realized that this problem happens due to following independent reasons mostly:

  1. conflicting python versions (more than one pythons); solution - uninstall one of them, get the one thats compatible with opencv ("Segmentation fault" during "import cv" on Mac OS)
  2. opencv version issue; solution - get the right version for you Python opencv feature detector causes segmentation fault
  3. issue with your numpy version; solution - uninstall and install numpy again (OpenCV - cannot find module cv2)

I tried all 3 but number 3 solved my issue.




回答3:


For me the solution was simply

sudo apt install python3-opencv

and then install pip opencv package

sudo pip3 install opencv-python 

or

sudo pip install opencv-python 

Note: This was because I have launched a new Aws instance.




回答4:


Using

cv2.ocl.setUseOpenCL(False)

at the beginning of the code solved the problem for me.




回答5:


I encountered the similar problem.

➜  ~ ✗ 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

But if I import numpy first, the problem will go away.

➜  ~ ✗ 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
>>>


来源:https://stackoverflow.com/questions/28669587/python-segmentation-fault-11-when-running-import-cv-or-import-cv2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!