Skip to content

My Lunch Cam Money Jar Hack

May 24, 2011

Allow me to explain.
My company, Triptrotting is an Idealab backed company.
At the Idealab office, we enjoy many cool perks: refreshments, video games, fruits, and food…. sort of. Every Monday, Wednesday, and Friday, the office orders a bunch of food and places it on the banana shaped table at the other side of the office. However, it’s not free. There is always a money jar at the end of the table and you MUST pay $6 in order to help yourself to a serving. We at Idealab have come to realize that the money jar gets removed after a while and any food that’s left on the table is free to take!
This is where it gets tricky with timing. The people conveniently sitting on that side of the office have an advantage because they can see when the money jar gets removed and can grab the food right away…. leaving us with nothing. 😦 What makes it even more interesting is that there is an office cam that points directly at the table.
So technically, one can stare at that lunch room cam for an hour or two and wait for that money jar to disappear, then RUN to that side of the office and grab food.

So I tell Shana: “I should make a script that detects whether or not the money jar is there and alerts us as soon as it’s gone”
Shana: “You wouldn’t be able to make that”
Me: “Wanna bet your life on it?”
Shana: “Sure!”
Me: “Deal!”

So I start with a little probing. Here’s the idealab intranet site that shows the feed from the webcam:

I dig in a little more and get the uri of the image source. Weird…. since it’s constantly refreshing, the browser save image fails. I try wget, and it hangs. Hmmm. I check out the manual for the panasonic webcam and find a way to get stills. Bingo. Let’s make it slightly higher res:

Now I need to prep this image using GD so I can detect edges. Let’s tamper with the image a bit.

imagefilter($image, IMG_FILTER_CONTRAST, -255);
should do the trick:

Great. Now it’s ready to run the GD edge detect filter and remove the details we aren’t interested in. So I run a
imagefilter($image, IMG_FILTER_EDGEDETECT);
and I get:

Perfect. These are the edges I’m looking for. I can definitely work with this. Now you notice something? The money jar is always near the edge of the table, so from this camera angle, it sticks out off the “horizon” of the table. This may be easier than I thought! I can do this without any TRUE pattern detection…. after all, it’s a hack… right? So using GD, I grab a 110 x 20px sample 105 px from the left and 195 px from the top. Here’s what I get:

All I’m interested in is this little segment of the photo so I can detect whether or not the money jar is there!

When it’s NOT there, it will look like this:

SWEET. Time to code this up:


<?php

function detectjar(&$image1){
    $blackline = 0;
    for($x=0;$x<110;$x++){
        for($y=0;$y<20;$y++){
            if(imagecolorat($image1,$x,$y)==0) $blackline++;
        }   
    }   
    return $blackline;
}
$movecameraurl = "http://10.1.1.104/nphControlCamera?Direction=Preset&PresetOperation=Move&Data=6";
$ch = curl_init($movecameraurl);
curl_setopt($ch, CURLOPT_USERAGENT, "Internet Explorer");
ob_start();
curl_exec($ch);
curl_close($ch);
ob_end_clean();
sleep(2);

$image = imagecreatefromjpeg("http://10.1.1.104/SnapShotJPEG?Resolution=640x480&Quality=Standard");
imagefilter($image, IMG_FILTER_CONTRAST, -255);
imagefilter($image, IMG_FILTER_EDGEDETECT);
$newwidth = 110;
$newheight = 20; 
$sourcex = 105;
$sourcey = 195;
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $image, 0, 0, $sourcex, $sourcey, $newwidth, $newheight, $newwidth, $newheight);

?>
<html>
    <head>
        <title>Eddie's Money Jar Script!</title>
<?
if(detectjar($thumb) < 8){
    // money jar is gone!
    ?>
</head>
<body>
    <h1>Money jar is GONE!</h1>
    <div style="display:block; overflow:hidden; width:1px; height:1px;">
        <? embedded a wave file here but wordpress strips it ?>
    </div>
    <br />
    <img src="http://10.1.1.104/SnapShotJPEG?Resolution=640x480&Quality=Standard" />
    <?
}
else{
    // money jar is still there
    ?>
    <meta http-equiv="refresh" content="15;url=<?=$_SERVER['PHP_SELF']?>">
</head>
<body>
    <h1>Money jar is still there</h1>
    <?
}
imagedestroy($image);
imagedestroy($thumb);

?>
    </body>
</html>

And it works GREAT! Run the script on the local network anytime after lunch is served…. and as SOON as the money jar is gone, a woman’s voice will kindly alert you “The money jar is gone”.

You can view the code here:
Money Jar source code

….and Idealab employees in the west wing can download it and use it here:
Money Jar script
(needs to be run within the idealab network. Requires PHP and GD)

So in the end…. what did I gain out of this? Shana’s life. I considered auctioning it off on ebay, but I decided to give it back to her. Besides, it was fun!

From → Hacks

10 Comments
  1. Tod permalink

    While not willing to place my life on the line, I hereby challenge you to add two more notifications to the script:
    1. The food is here (since sometimes it shows up really early or somewhat late)
    2. The food is ready (might be as easy as detecting the placement of the money jar)

    (I also want the pleasant voice notification)

  2. Superb post however , I was wanting to know if you could
    write a litte more on this topic? I’d be very grateful if you could elaborate a little bit more. Kudos!

    • Thank you! I’d be happy to write more on this. What would like me to elaborate more on?

  3. Daniel permalink

    Hey! Someone in my Facebook group shared this site
    with us so I came to take a look. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers!
    Excellent blog and outstanding style and design.

  4. Andy permalink

    Just a brief note to say thanks for the great update, that was exactly what I was looking for!
    !

  5. You actually make it seem so easy with your presentation but
    I find this topic to be actually something which I think I would
    never understand. It seems too complex and extremely broad for
    me. I am looking forward for your next post, I will try to get the
    hang of it!

  6. Whats Going down i’m new to this, I stumbled upon this I’ve discovered It absolutely helpful and it has aided me out loads.
    I am hoping to contribute & help different customers like its helped me.
    Good job.

  7. Quality content is the important to attract the viewers
    to pay a visit the website, that’s what this web site is providing.

  8. One more important part is that if you are a mature person, travel insurance regarding pensioners is something you should make sure you really consider.
    The old you are, greater at risk you happen to be for getting
    something bad happen to you while overseas.

    If you are certainly not covered by quite a few comprehensive insurance policies,
    you could have quite a few serious challenges. Thanks for expressing
    your good tips on this weblog.

Trackbacks & Pingbacks

  1. Hacking Into That Security Camera! « cranklin.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: