title: python: OpenCV Root Directory
body: <p>I am using OpenCV for various object detectors, and I am finding it difficult to write portable code.</p>
  
  <p>For instance, to load a face detector, on a mac with OpenCV installed via homebrew, I have to write:</p>
  
  <pre><code>haar=cv.Load('/usr/local/Cellar/opencv/2.4.2/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
  </code></pre>
  
  <p>This is not portable; if I wish to change to another machine I'll have to determine another absolute path and change this code.</p>
  
  <p>Is there a variable that holds the OpenCV root for OpenCV?  That way I could write something like:</p>
  
  <pre><code>haar=cv.Load(os.path.join(OpenCVRoot, "haarcascades", 
                            "haarcascade_frontalface_default.xml"))
  </code></pre>
  
  <p><strong>UPDATE</strong>:  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:</p>
  
  <pre><code>&gt;&gt;&gt; import cv
  &gt;&gt;&gt; image = cv.LoadImageM("lena.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)
  &gt;&gt;&gt; cascade = cv.Load("../../data/haarcascades/haarcascade_frontalface_alt.xml")
  &gt;&gt;&gt; print cv.HaarDetectObjects(image, cascade, cv.CreateMemStorage(0), 1.2, 2, 0, (20, 20))
  [((217, 203, 169, 169), 24)]
  </code></pre>
  
  <p>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.</p>
  
  <p>Source: <a href="http://opencv.willowgarage.com/documentation/python/objdetect_cascade_classification.html" rel="noreferrer">http://opencv.willowgarage.com/documentation/python/objdetect_cascade_classification.html</a> (Retrieved 3/5/13)</p>