Please see my comment to Solution 2.
You can calculate the distance to some object, if you know precisely:
- The natural (real-life) size of the object you recognized.
- Sizes of the working area of your camera sensor (in both sm/mm and pixels).
- Exact optical characteristics of your lens (at least at the given position of the zoom if zoom is available), as well as the location of its back (image) focal plain relative to the sensor; these characteristics change with focusing and zooming. In many cases, you can use technical specs of the lens and EXIF data, to figure out working position.
In many (but not all) cases, you can use simple approximation of "thin lens" to get sufficient accuracy. Then this is a matter of simple optical calculations at the level of high school course of physics. Please see:
http://en.wikipedia.org/wiki/Lens_%28optics%29#Thin_lens_equation[
^].
—SA