Code and Hacks

Stuff I've stumbled on or figured out... mostly Perl, Linux, Mac and Cygwin.

My Photo
Location: CA, United States

Perl hacker, investor and entrepreneur.

Wednesday, May 27, 2009

Easy Access to Your Minicpan Repository

I am a big fan of CPANPLUS and minicpan. I like the plugin structure and power of CPANPLUS. ([Warning: shameless plug follows] I have written a simple plugin that allows you to see/install the prereqs for an module with commands like cpanp /prereqs show or cpanp /prereqs install.) And minicpan is great for getting work done on an airplane or when I am away from the net.

One thing I have struggle with in the past is getting cpanp to use either my local minicpan mirror or another mirror other than my default. Editing the config file is not that hard, but it is far to permanent for what I am trying to do. So I wrote two simple scripts (basically tweaked versions of /usr/bin/cpanp) that change the mirror to my local minicpan or a mirror passed on the command line: cpanp-local and cpanp-mirror. Both could be significantly improved, documented and they should probably be combined, but they get the job done.

Labels: , ,

Wednesday, May 20, 2009

Recovering After Vim Terminates

When vim is shutdowns abnormally it leaves a bunch of .swp files around.1 Given the way I program (very iterative and test focused) those backups are rarely more current than the saved file. A great time saver to help with recovery is the vim DiffOrig command. After learning about it on StackOverflow, I put together the following bash script to find any swap files under the current directory, open each in vim, run DiffOrig and prompt to delete after you close vim.


swap_files=`find . -name "*.swp"`

for s in $swap_files ; do
     orig_file=`echo $s | perl -pe 's!/\.([^/]*).swp$!/$1!' `
     echo "Editing $orig_file"
     sleep 1
     vim -r $orig_file -c "DiffOrig"
     echo -n "  Ok to delete swap file? [y/n] "
     read resp
     if [ "$resp" == "y" ] ; then
             echo "  Deleting $s"
             rm $s

I was planning on converting this to a vim function, but for now the extra step of exiting vim and answering a prompt works fine. Also, my vim already loads DiffOrig. ArchLinux includes it in /etc/vimrc. If your vim doesn't, I believe the following command will do it:

if !exists(":DiffOrig")
command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
               \ | wincmd p | diffthis

BTW Iron Men, I realize this post isn't really Perl focused, but it is usually Perl files that I'm recovering!

1. Which is almost never vim's fault. More likely an ssh connection was terminated or something along those lines.


Wednesday, May 6, 2009

Facial Detection and Recongition in Perl

I just recently came across the new facial detection features in Picasa, and I have to say I am very impressed. This is a very useful feature and well implemented; once again Google has set the bar. Unfortunately, it is only available in the web version, and I have way too many photos to upload. Further, there seem to be more privacy concerns with a web version of a tool like this.

So, I was very excited when I saw a brief mention of presentation about OpenCV from a meeting (thanks to Yanick's blog). Maybe this was the tool to implement facial detection/recognition in my photos locally.

Without going into much detail, OpenCV is a library of routines for facial detection, recognition, and similar computer vision tasks. So far, I have only experimented with the facial detection part and read a bit about the facial recognition routines. There is a handy Perl binding (Image::ObjectDetect) for the facial detection part. With it and reference to the slides from, I was able hack together a very simple script to look for and highlight faces in my photo collection.

While there were many false positives and some missed faces, OpenCV and the script show promise. Most of the missed faces were either very small or profiles, and there is another "cascade" (essentially a set of configuration presets to the detection algorithm) that might yield better results for profiles.

Reading about facial recognition from the OpenCV site was a bit underwhelming, though. Apparently OpenCV only supports one method of recognition, Principal Component Analysis. Which sounds like it has some sever limitations. From the site:

However it does have its weaknesses. PCA is translation variant - Even if the images are shifted it wont recognize the face. It is Scale variant - Even if the images are scaled it will be difficult to recognize. PCA is background variant- If you want to recognize face in an image with different background, it will be difficult to recognize. Above all, it is lighting variant- if the light intensity changes, the face wont be recognized that accurate.

Not exactly a great marketing pitch! PCA doesn't sounds like the most suitable method for recognizing faces from arbitrary photos. I haven't played with it yet, but that's on the todo list.

To sum it all up, OpenCV looks like a great tool. I'll update this blog as I continue to experiment with it and my homebrew version of PicasaWeb's facial detection/recognition features. If anyone else has had experience with OpenCV or other facial detection/recognition tools (particularly those with Perl interfaces) I would love to hear about it.

Labels: ,