Official ObjectGraph Blog

Saturday, April 28, 2007

DIY: Building your own live camera with Macmini

OK, I have huge headache today, and this happens to me every year when the season is changing. I took a day-off and I woke up late around noon, then I just decided to have some fun.

What you need:

  1. Macam driver (if your web camera is not supported)
  2. VideoScript - Automate capture images
  3. JDK - Any version to compile applet


Installing Driver:

I got a nice & small Logitech Web Cameralogitech_camera.jpg yesterday, and I just wanted to use this for my Skype which is installed on my macmini in living room. I mounted this on my Sony's LCD, and launched Skype but nothing showed up. According to Logitech's website, the driver for MacOSX is not supported yet. So I found a opensource driver it's called Macam. I downloaded, and the installation was easy (don't forget to install the component in order to use from other applications). Now Skype shows me configuration for the video, and I could see my face. Then I remember that I used to broadcast my living room. OK, let's do it again.


Building Applet:

Since my Hello World experience was in Java, I spend some time with Applet in long time ago. An applet program to stream from jpg file might be nice. This is the program:

import java.awt.*; 
import java.applet.Applet; 

public class WebCamClient extends Applet { 
 private Image mImg;
 private int mIsStarted = 1;
 private int mInterval = 10000; // in millisec
 private String mFile = "webcam.jpg";

 public void start() { 
  mIsStarted = 1; 
 } 

 public void stop() { 
  mIsStarted = 0; 
 } 

 public void paint ( Graphics g ) { 
  mImg = getImage( getDocumentBase(),mFile); 
  MediaTracker mt = new MediaTracker(this); 
  mImg = getImage( getDocumentBase(),mFile); 
  mt.addImage(mImg,0); 
  try { 
   showStatus("loading ..."); 
   mt.waitForAll(); 
  } 
  catch (InterruptedException e){ 
   System.err.println("ERROR: "+e); 
  } 
  g.drawImage( mImg,0,0,this); 
  mImg.flush(); 

  showStatus("Showing every "+mInterval+" milliseconds"); 

  if ( mIsStarted == 1 ) {    
   repaint();
   //repaint(mInterval); // this does not guarantee delay

   // Wait for next cycle
   try {
    Thread.sleep(mInterval);
   }
   catch (InterruptedException e){ 
    System.err.println("ERROR: "+e); 
   }
  } 
 } 

 public void update(Graphics g) { 
  paint(g); 
 } 
}


Good, this program grab webcam.jpg in the same directory and just show it in every 10 seconds. I stored this file in

/Users/kiichi/work/java/webcam/WebCamClient.java

and just compile like:

javac WebCamClient.java



Writting HTML Files:

All you have to do is write am index.html to load this applet in your browser:

index.html

Let's prepare for html refresh version too:

index2.html

Automating Video Capture:

I spend a little bit time for researching automating video capturing and I found VideoScript. This script does a lot of jobs; however, I would like to use the simplest feature to capture the image. When you download the package, it comes with a small editor so that you can run&check your script. This is how it looks like:

set vs to videosource;
repeat 4 times every 1 seconds // min is 4 sec
set f to frame of vs; 
set file "/Users/kiichi/work/java/webcam/webcam.jpg" to f;


In the beginning, I just had a black screen. I tested a sample script to generate QuickTime movie, and it works fine, so what's wrong with the still image? The forum told me the reason because the camera takes a couple of seconds to calibrate the exposure. That's why I inserted the second line to wait 4 seconds. I saved it as update.vs and just run like:

videoscript -f update.vs

This gave me a clear image!

I think, it's ready to go. I had two choices to broadcast my living room. 1. Upload this picture to remote. 2. Open port 80 and let other people look at my Macmini. For today. I decided #1 so that I don't overwhelm my connection speed.



Scheduling FTP upload:

Mac OSX is just another UNIX system, so it might have cron to schedule my jobs. I found crontab under /etc/crontab (or /private/etc/crontab). I did

sudo gvim

to edit crontab. What I added is this line

* * * * * kiichi sh /Users/kiichi/work/java/webcam/update.sh

this means, run update.sh for every minutes.

This is inside update.sh

#!/bin/bash
videoscript -f /Users/kiichi/work/java/webcam/update.vs

FTPSERVER='your ftp url goes here'
FTPUSER='username'
FTPPASS='password'
SOURCE='/Users/kiichi/work/java/webcam/webcam.jpg'
DEST='.'
TARGET='webcam.jpg'

ftp -ni $FTPSERVER <<EOF
user $FTPUSER $FTPPASS
cd $DEST
binary
put $SOURCE $TARGET
quit
EOF
exit 0


It's all set. cron suppose to pickup this script, and this script takes care of generating image and upload on ftp. If you access my streaming web page, the applet automatically refreshes those images. Since this script does not hold the device, when you have Skype call, it will sends a black image that says "Web camera is used by another program" for my ftp.

In the future, this is the todo list:

  • Archiving images
  • Streaming from the local machine - you can write a VideoScript to hold the device, and dump image for every second within the for-loop.
  • Add date and time stamp
  • Motion Detection

Download Source Code:

webcam_sample.zip

Labels:


posted by Kiichi Takeuchi at 1:59 AM

1 Comments:

  • Very interesting!! It would be good if the pictures are archived every now and then

    By Anonymous Anonymous, at 12:55 PM  

Post a Comment

<< Home