Setting up Python, OpenCV and ffmpeg in OSX

by .

This seems to work  on both OS X Mountain Lion 10.8.x and Mavericks 10.9.x

  • Download and install Xcode 5 (for Mavericks 10.9.x)
  • Download and install MacPorts

Check that Xcode command line is installed

xcode-select -p
#if you see the following then command line is installed
/Applications/Xcode.app/Contents/Developer
#if not, just invoke gcc and os x will prompt you to install
gcc --version

#update feb 9, 2014
#this will install xocde command line tools on Maveriks
xcode-select --install

Use MacPorts to install/upgrade Python, NumPy, SciPy, and OpenCV

This takes a really long time, maxes out your CPU and on my macbook the cpu temp was 200 deg farenheight.

sudo port selfupdate
sudo port install python27 py27-numpy py27-scipy
sudo port install opencv +python27
sudo port select --set python python27

#install ffmpeg
sudo port install ffmpeg

Check your installation from within Python

From a command prompt, start Python. Inside python, check the versions

cudmore$ python
Python 2.7.6 (default, Nov 12 2013, 13:26:39) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import numpy
>>> import scipy
>>> import cv2
>>> print sys.version
2.7.6 (default, Nov 12 2013, 13:26:39) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))]
>>> numpy.__version__
'1.8.0'
>>> scipy.__version__
'0.13.1'
>>> cv2.__version__
'2.4.7'

Now This is getting complicated

I want to just make a simple python hello world with an interface. Pillow is a branch of PIL

sudo easy_install Pillow

This now works

Problem here is .show() is saving file and then opening it in preview. What I wanted to do was have a native Python window with a single slice of the tif stack, scroll the mouse-wheel and update the displayed slice. Until I figure this out … back to Fiji.

import Tkinter
from PIL import Image

top = Tkinter.Tk()

Tkinter.Button(top, text='Hello World').grid()
Tkinter.Button(top, text='Hello World 2').grid()

im = Image.open('/Users/cudmore/Desktop/X20130927_a141_009_ch1.tif')
im.show()

top.mainloop()

[Update Aug 2014] I finally sat down to use Python with matplotlib, mysql etc.

#update and upgrade macports to see what I get.
sudo port selfupdate
sudo port upgrade outdated #took about 20min at 100% cpu

#now I have python 2.7.8
sudo easy_install pip #other commands did not work for me

#install matplot lib following: http://matplotlib.org/1.4.0/faq/installing_faq.html
git clone git://github.com/matplotlib/matplotlib.git
cd matplotlib
sudo python setup.py install #i am starting to get worried I have mistakenly installed something with sudo. I seem to need it more and more?

#now python ‘import matplotlib’ works

#trying to install mySQLdb is not easy, may be easy but hard to figure out how
#did the following and have suspicion it may cause problems
#sudo port install py-mysql

#this gave an error at end but i ignored it and now can do python ‘import mySQLdb’
sudo pip install MySQL-python
#i can connect to remote sql database (on blue host) and print some data!!!

#now pytz is complaining
sudo easy_install pytz

#can now do ‘import putz’
#now matplotlib function calls are complaining about ‘ImportError: numpy.core.multiarray failed to import’

#still need to look at:
http://matplotlib.org/faq/installing_faq.html

#plotting in python is now working with matplotlib.

#my conclusion — it sucks — back to Igor Pro

Links

 

Leave a Reply

Your email address will not be published. Required fields are marked *