2

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?

Community
  • 1
  • 1
Ryan
  • 1,032
  • 1
  • 10
  • 23

5 Answers5

2

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.

Ha Dang
  • 1,218
  • 1
  • 10
  • 12
  • Good eye, I didn't catch that... How can I make my regular Python command point where the super user Python points? – Ryan Feb 23 '15 at 16:56
  • Uhm, I found [this](http://sevas.github.io/2011/06/14/multiple-python-osx.html) and [this](http://superuser.com/questions/35256/how-can-i-change-the-default-python-version-on-snow-leopard) discussing about some solutions. Maybe you will find them helpful. – Ha Dang Feb 23 '15 at 17:32
  • 2
    Thanks so much, `defaults write com.apple.versioner.python Version 2.7.9` was the command I needed. – Ryan Feb 23 '15 at 18:38
1

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.

Community
  • 1
  • 1
Abhimanu Kumar
  • 1,751
  • 18
  • 20
1

Using

cv2.ocl.setUseOpenCL(False)

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

Rouzbeh
  • 2,141
  • 1
  • 11
  • 19
1

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.

Hemant c
  • 133
  • 1
  • 5
0

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