30 January
2009

Getting c++ to use a custom python framework

This post addresses how to get the OpenCV vision package with Python bindings to compile on OS X, such that you can use a version of Python other than the one that is supplied with the original operating system installation. It's very hacky and not for casual reading, but my help some person questing for help.


Dudek faces detected
Detected faces, using opencv, Viola-Jones algorithm (boosting)


Detecting a sleeply dudek with opencv
Face and eyes detected




The problem is that OpenCV insists on being linked with the standard default installation of Python on OS X. On the other hand, many (most?) techie people using Python will have installed an alternative newer release (for instance via "fink" or "macports").


When you compile OpenCV and try to use it with your custom-installed Python (e.g. version 2.5.2 instead of the stock 2.5.1), you get an error as follows:


% python
>>> import opencv
Fatal Python error: Interpreter not initialized (version mismatch?)
Abort


Using the standard Python is a workaround, but for me it wasn't acceptable. Unfortunately, since I had no prior experience with cmake or opencv, this was problematic. Here's how to fix it. Despite many only-semi-correct discussions of this on the net, it's fairly easy to fix, once you realize the source of the problem.


On OS X (or, in fact, the core operating system kernel, Darwin), many packages are installed using Frameworks. They group together libraries and headers. The C compiler on Darwin has been extended to take a -framework PATH flag, and by default this causes the linker to look in the system directories, but not user-installed directories. You need to provide an extra -F DIRECTORY flag to specific the location of your user-installed frameworks (e.g. "-F /opt/local/Library/Frameworks/").


I never quite figureed out the "right" place to insert the flag before starting a build, but after a failed build you can just insert it into
interfaces/swig/python/CMakeFiles/_highgui.dir/link.txt
The right place is probably interfaces/swig/python/CMakeLists.txt
using something like


set(CMAKE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -F/opt/local/Library/Frameworks")


but once I got it working I didn't want to waste more time on a really clean fix (instead, I wasted time writing it up here).


When you run make, in order to assure it's working you can set the system frameworks to be temporarily inaccessible to verify the right places are being searched. Do this with the chmod command. Afterwards, you can put things back the way they were (mode 755).
I suggest you test your patch as follows:


sudo chmod 000 /System/Library/Frameworks/Python.framework/
cmake .....
sudo make install
sudo chmod 755 /System/Library/Frameworks/Python.framework/



As an aside, I was also confronted by the error:


opencv/src/highgui/cvcap_ffmpeg.cpp:70:28: error: ffmpeg/swscale.h: No such file or directory

this can be fixed by the hack:

cp -r /opt/local/include/libswscale/* /opt/local/include/ffmpeg



By Gregory Dudek at | Read (1) or Leave a comment |    
Comments
Re: Opencv with Python under mac OS X

Nice topic :-)

Here is an example of OpenCV with C++ for webcam / camera : http://www.geckogeek.fr/lire-le-flux-dune-webcam-camera-video-avec-opencv.html

Good week !

[ Thanks. Note the web site in in French. -gd ]

Posted by: Vinz at March 19,2010 10:04
Trackbacks
Please send trackback to:/blog/150/tbping
Build OpenCV and python interface on Mac OS X

The standatd guide to build open computer vision library is written on OpenCV wiki. How to use openCV in X code is described in this link (Korean). It's simple. Mac OS X is UNIX os using Darwin kernel. But building python interface was difficult. So I g..

Posted by: 세상을 놀라게 하자! at May 05,2009 03:09
Build OpenCV and python interface on Mac OS X

The standatd guide to build open computer vision library is written on OpenCV wiki. How to use openCV in X code is described in this link (Korean). It's simple. Mac OS X is UNIX os using Darwin kernel. But building python interface was difficult. So I g..

Posted by: 세상을 놀라게 하자! at May 05,2009 03:09
Mac OS X에서 OpenCV설치하고 Python 연결하기

기본적인 방법들은 OpenCV wiki에 적혀져 있는 방법대로 하면 C언어에서 사용할 수는 있게 된다. 그리고 이것을 사용하는데 X code에서 이용하는 방법은 X code에서 OpenCV 이용하기를 따라가면 된다. 물론 그냥 Unix에서 하듯이 Make만들고 해도 된다. 결국 Mac OS X도 Darwin이란 Kernel을 사용하고 있는 Unix 이기 때문이다. 그러나 Python interface는 쉽게 되지 않는 것을 확인할 수 있었다. 그래서..

Posted by: 세상을 놀라게 하자! at May 05,2009 03:10
elelrelm

trocrorotrbo

Posted by: boctaboboc at May 20,2009 02:27
Post your own response

Each comment is manually screened for the presence of appropriate and substantive content, due to a constant onslaught of comment-spam. This means there may be a delay before your comment appears.


(Some kind of name is required, will be visible)

Required, whatever you enter will be visible to other users.


(Optional, used for "mailto" link)

Your email address is not required, but if you insert it it will be displayed so people can contact you.

Answer this question correctly to demonstrate that you are not a dumb spambot.



The title for your comment.



Your comment goes here. All relevant comments are welcome, except for those that simply promote an irrelevant product or else are used to fraudulently inflate the link count to an irrelevant web page. They appear after moderation. Don't forget to also fill in the captcha below or your text will be rejected automatically!

You must answer this question to prove you are human
which has the least friendly public image: 1:collie, 2:beagle, 3:doberman, 4:dachsund, 5:dalmatian?

Answer this question correctly to demonstrate that you are not a dumb spambot.