I am using OpenCV for various object detectors, and I am finding it difficult to write portable code.
For instance, to load a face detector, on a mac with OpenCV installed via homebrew, I have to write:
haar=cv.Load('/usr/local/Cellar/opencv/2.4.2/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
This is not portable; if I wish to change to another machine I'll have to determine another absolute path and change this code.
Is there a variable that holds the OpenCV root for OpenCV? That way I could write something like:
haar=cv.Load(os.path.join(OpenCVRoot, "haarcascades",
"haarcascade_frontalface_default.xml"))
UPDATE: It looks like this is not just a problem for me; it is also a problem for the OpenCV documentation. The documentation contains the following broken example code:
>>> import cv
>>> image = cv.LoadImageM("lena.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)
>>> cascade = cv.Load("../../data/haarcascades/haarcascade_frontalface_alt.xml")
>>> print cv.HaarDetectObjects(image, cascade, cv.CreateMemStorage(0), 1.2, 2, 0, (20, 20))
[((217, 203, 169, 169), 24)]
This would be simple to avoid if there was a way to infer where examples like lena.jpg and the pre-trained classifiers were installed.
Source: http://opencv.willowgarage.com/documentation/python/objdetect_cascade_classification.html (Retrieved 3/5/13)