Tuesday, April 12, 2011

OpenCV in Python, in PHP, in Flash and everywhere else...

A good while ago we were doing a website for one of our social aware clients and the visitors could upload pictures of them and their good friends. But oh my, those pictures needed to be cropped to thumbnails and we couldn't upset the clients by asking them not to upload images in portrait format.
So I looked at my Cake and wondered how could I crop and keep their faces. Ok, that sounds like I have something against our clients and I was about to go Lecter-ish on them which is simply not true. What I mean to say is that I wanted to crop the uploaded images while maintaining the areas containing the graphical representations of their faces in the output.
Now there is a name for that and the name is "Computer Vision". And there is an Open Source project for that and it's name is OpenCV.
And there is a very cool dude who goes buy the name of Robert Eisele who created the PHP Facedetect Extension so all PHP devs can find faces in images. Indeed the extension worked very nice for that project and all the faces were there. I must say that I was amazed by the accuracy. It's mind bogging what computers can do if you tell them how to do it!
But it doesn't come for OpenCV2  and that's what I got on MacPorts.

Good thing I have Python running. Because although OpenCV is C it has a full Python interface!
So now you understand my recent interest in snake taming ;-)

But the coolest thing of the whole stack is, you guessed, Flash!
More precisely the the way that Eugene Zatepyakin, Flash wiz extraordinaire used the Alchemy Project to achieve Viola-Jones object detection in the Flash Player. And that really works. But do check all the other uber cool tricks Eugene has up his sleeve. All are quite impressive and def worth your time.
Alchemy seems to work ok on my OSX 10.6. I managed to do the install steps and compile the basic sample so now the sky would be the limit :-)

Friday, April 08, 2011

to MacPorts or not to MacPorts

About this whole Python thing.
It turns out that the more power you get the deeper you have to go.
In my particular case I had to go deep enough that the Snow Leopard 32 bit / 64 bit started to really matter.
The problem arise when I tried to install opencv for Python. Opencv has ffmpeg as a dependency. And ffmpeg is 64 bit only on Snow Leoaprd. And I was running Python in 32 bit mode (VERSIONER_PYTHON_PREFER_32_BIT=yes) because mod_wsgi was compiled agains my XAMPP Apache which in turn was 32 bit. So I had to cut the roots of the problem and ditch XAMPP altogether.
Good thing MacPorts is around. Kudos to those guys who maintain the whole thing. In around 2-3 hrs I was able to have a fully working LAMP stack and all my local dev projects including vhosts worked without major hassles. I even have the XAMPP around and I can fire it up anytime I need.
I had to uninstall all the ports first because while trying to hack opencv into submission I installed a bunch of universal variants of the ports and php5 refused to build with those around. It was a mess. But after uninstalling all ports and taking it step by step I had no troubles whatsoever. I imagine that this is not something everyone can do - ditching all the ports - but we all know that "square one" is a place you can always go back to ;-)
Some of the minor hassles that I had to work may way trough were:
  • manually creating and tweaking php.ini and my.cnf
  • manually editing httpd.conf to get perl and php to work
  • having to symlink /tmp/mysql.sock -> /opt/local/var/run/mysql5/mysqld.sock because CakePHP kept looking for mysql in the tmp folder. Not sure if it was Cake or PHP but now it works and MySQLWorkbench doesn't complain either
  • dumping all my databases from the XAMPP MySQL and importing them into the new MySQL with mysqldump --all-databases > backup_file.sql and mysql < backup_file.sql
  • figuring all the extra php extensions I needed using a rock solid older CentOS setup I did a while ago
Now I do realize that these are always on the todo list for any manual LAMP installation (even Windows) but it's just a heads up for those who didn't do this kind of job yet.

I recommend doing the MacPorts thing just for fun. You'll get a better idea of what's under the hood. And if you dont set anything to start at boot you don't risk breaking your current setup. When you see the added compile times you really understand that the whole LAMP thing is a huge effort and the fact that it seems so simple to use with great tools like XAMPP just adds up to it's coolness.
Now I have Apache2 in 64 bit mode and a brand new MySQL to go with it. Also PHP is at 5.3.6 and MacPorts has about all the PHP extensions you could need.
All I can say is that the same old lesson proved true again. In technology there's no solid ground. If you're not moving forward you're going backwards!

Now as you can imagine I've got no Python with mod_wsgi yet on the new setup :-)
Funny how the snake tricked me into going trough all this pain just to end up with less. But the foundation is solid and I have no doubt that all will fall nicely in place. More posts to follow on the topic as I expect to get further with this new approach.

Update: the MacPorts mod_wsgi works just fine. It is much simpler to install than the XAMPP bag of tricks. I got "Hello World" again. Next is Django but I'll really put that off for a later time.

Wednesday, April 06, 2011

riches for rattles

You tried Perl, you liked the power but dreaded the lingo?
Try Python!