Dim_Face

This is just a description of something I thought was cool; while it isn't likely to horribly break anything, this is JUST a description, not a prescription.
As always with things you find on the internet:
ATTEMPT AT YOUR OWN RISK

This is a piecemeal solution, using components from various sources. The components are the property of their respective creators. Be sure to respect any licensing associated with the software components.

Concept

Picture yourself sitting in front of your computer (crazy, right?).
You have a webpage open with a table for reference. You don't need to navigate away from the page, so you haven't touched the keyboard or mouse in awhile.
Suddenly, your screen dims!

OK, so it isn't the end of the world, but we should be able to do better than this. Isn't there some way to tell if you're in front of the computer, but not typing?

Well, here's one way: if the mouse and keyboard aren't being used, use the camera to briefly look for faces facing the screen. If there's a face, stay awake; otherwise, dim the screen to save power.

Requirements

To do this, three things are needed:

  • Retrieve Idle Time
  • Detect Faces
  • Control Screen Brightness

Implementation; OS X 10.5, "Leopard" & MBP

Here's what I used:

Idle Time
ioreg
Detect Faces
OpenCV
Control Screen Brightness
IOGraphicsLib

ioreg

ioreg is a utility which displays the values of lots of system information, including the idle time of the system. The only hard part is finding and extracting the right key.

This worked for me:
ioreg -n IOHIDSystem -d 4 -k HIDIdleTime | grep HIDIdleTime | cut -d = -f 2

What you see when that runs is the idle time in NANO-seconds (that's a 10^9 multiplier). Convert it to a number and divide by 1,000,000,000 to get the actual idle time in seconds.

OpenCV

This is the most complicated part, but also the coolest component of all this.
I suggest using Darwin Ports to install OpenCV since it's easy as spit, gives you everything you need, and introduces you to Darwin Ports if you've never used it before.

If you're like me and accidentally installed it in /opt/local/ then you'll have to dig around in there to find what you need. Specifically, I found the associated files in
/opt/local/var/macports/software/opencv/1.0.0_0/
for some reason, but you may need to use spotlight or find to see it (search for "opencv").
Anyway, the two things you'll need from the install are the Python package and the examples.

The Python package will be in a folder called "site-packages/opencv" and contains some compiled stuff and some python stuff. This is a complete Python module, and should be copied to the native Python install folder /Library/Python/2.5/site-packages
For me, this was all it took (as all one line in the terminal):
sudo cp -R /opt/local/var/macports/software/opencv/1.0.0_0/Library/Python/2.5/site-packages/opencv /Library/Python/2.5/site-packages/

For me, the examples were in /opt/local/var/macports/software/opencv/1.0.0_0/opt/local/share/opencv
Move that folder wherever (in your Documents folder, perhaps?).
Now test the example programs.
cd samples/python
Specifically, try running facedetect.py with the built-in camera (numeric argument):
python facedetect.py 1
If that works, you should see your bewildered face bordered by a red box, and you should continue. If not, you may need to verify that the python you're using recognizes the opencv package.

IOGraphicsLib

Dealing with low-level OS stuff, like brightness, has a somewhat steep learning curve. Thankfully, Nicholas Riley posted a program to do exactly what we need - detect & modify display brightness.

Download the file at the bottom of this page , then run:
gcc -std=c99 -arch i386 -arch ppc -o brightness brightness.c -framework IOKit -framework ApplicationServices

Putting it Together