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

亡梦爱人 提交于 2019-12-06 13:38:17

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.

Abhimanu Kumar

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.

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.

Using

cv2.ocl.setUseOpenCL(False)

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

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