Category Archives: Imaging

Raspberry Pi Oil Meter Reader

This project uses a Raspberry Pi with a NOIR camera and IR lighting to take a picture of an oil tank analog meter and convert to a percentage using OpenCV for use with a home automation system. OilMeterSetup The camera resolution is high enough that the Pi can be place back far enough to get a reasonable focus. The image will be cropped show the meter on the output.

This uses a single python script running every minute from cron. The script depends on openCV and picamera.

The basic algorithm is:

  1. Rotate the image as needed
  2. Find the meter background contour
  3. Find the needle contour within the background
  4. Find the pivot point within the needle (used for adjusting the angle orientation)
  5. Fit a line to the needle contour
  6. Adjust the line vector +/-180 degrees based on which side of the pivot point the needle is on. Ex. adjusts a horizontal line to be either pointing left or pointing right.
  7. Convert the angle to a percentage based on the empty/full angles if the meter
  8. Crop the image based on the bounds of the meter background for display
  9. Upload the percentage value to openHAB for use by the home automation system

Output of the processing script

openHAB charting

openHAB charting

The sources can be found on Github:

Accessing JPG from IP Cameras

I use off-the-shelf IP cameras but monitor them from a central server instead of looking at each camera’s own interface. In this setup it can be better to PULL images off the camera from the server instead of being PUSHED from the the camera to the server. Pushing images requires setting up each camera with the server information and a schedule to upload pictures, which can be sparse in options depending on the camera.

By pulling images on demand, the server can control the time interval and no special setup is required for each camera other than possibly setting up a user name and password for access. Most will require Basic Auth to access in image.

Here’s the JPG access URLs for cameras I’ve tested:

Panasonic cameras (C30A, C131A, C20A, C1A)

Foscam (FI8918W, FI8910W, FI8904W, FI8905W, etc)

Trendnet (TV-IP110W)

These cameras are not good for this purpose. Only an ActiveX viewer is available for the web or FTP for single images to a server.

Windows PCs with webcams running Yawcam.

Enabling the web access port 8888 for example.

Archiving thumnails of FTP camera images

This php script will iterate over an array $cams that has an element file that is the filename.

It uses the PHP Imagick package to resize the image and overlay a timestamp. The file timestamp is used so the time of the last ftp image is used, which may differ from the current time if the ftp has stalled. The image is then copied to an Hour/Minute specific folder for a 24 hour rolling archive.

$H = date('H');
$i = date('i');
$arcpath = $ftppath . $H . '/';
$arcpath = $ftppath . $H . '/' . $i . '/';

foreach ($cams as $cam)
  $pathfile = $ftppath . $cam['file'];
  if (file_exists($pathfile)) {
    $thumbname = $cam['file'] . '_320x240';

    $thumb = new Imagick($pathfile);

    $draw = new ImagickDraw();
    $draw->setFillColor(new ImagickPixel("#ffffff"));

    $draw->annotation(5, 12,  date ("F d Y H:i:s", filemtime($pathfile)));

    $thumb->writeImage($ftppath . $thumbname);
    copy($ftppath . $thumbname, $arcpath . $thumbname);

AspireOne Petcam

Have a spare AspireOne sitting around not being actively used? With built-in wireless and webcam it can easily substitute for a dedicated wireless webcam. As the prices of these netbooks continue to drop they may even be better deals while providing the additional features of a display and full operating system.

I’ll be looking at using an AspireOne netbook with Ubuntu 9.04 netbook remix installed, other linux distributions should also work. For Windows netbooks I like the program Yawcam, that provides lots of functionality along with a GUI.

A quick search for ‘webcam’ in the Synaptic Package Manager will show a variety of available programs that support webcam capture with ftp uploads. I tried these but had little success.

  • camstream – program hung when run
  • camgrab – the AspireOne camera wasn’t found
  • camorama – could not connect with the webcam
  • camE – came the closest of the ‘webcam’ apps. captured images to disk, connected to the ftp server, but failed to complete uploads. I was not able to resolve the upload failure.
  • vgrabbj – fails to connect to the webcam
  • webcamd – sort-of worked. captured images, uploads worked. however it seemed to have upload troubles and be low on options and/or documentation.

The best program for webcam monitoring on linux is Motion. Install it using the Synaptic package manager. This will place sample configuration files in /etc/motion/motion.conf

Since I plan to occasionally use the netbook for casual surfing and want to keep an eye on the camera operations to easily stop it for privacy, I’ll run it as a user rather than in daemon mode. Copy the sample configuration to your user directory /home/<user>/.motion/motion.conf

I changed just a few settings to support uploading a snapshot every 60 seconds. I also disabled the motion images and videos for now. I’ll include these in a future setup, but for now the captures would quickly fill the small drive of the netbook if left running.

Edit the motion.conf file in your user ~/.motion directory and modify the following settings:

# turn of daemon mode, I'll run in a shell
daemon off
# optionally use a larger image size
width 640
height 480
# turn off motion capture images
output_normal off
# turn off motion video
ffmpeg_cap_new off
# take a picture every 60 seconds
snapshot_interval 60
# reuse the jpg image file name lastsnap
snapshot_filename lastsnap
# run an FTP upload script after taking a picture
on_picture_save /home/<user>/ftppicture %f

Motion does not include any FTP functionality, but it does provide a set of events that can be used to run external scripts. on_picture_save allows you to specify a script that will be run after each image is taken and stored to the local drive. Here we will run the script ftppicture, the contents of which are listed below:



quote USER $USER
cd cams
put $filename
delete aspireone.jpg
rename $filename aspireone.jpg
exit 0

Fill in the constants at the top with your ftp server login information. This script will connect to the server, upload the file lastsnap.jpg , delete the image aspireone.jpg from the ftp site, then finally rename the new lastsnap.jpg to aspireone.jpg on the server.

From a terminal run the program with ‘motion’. Motion includes a web server, so you can view the live camera image for adjusting its postion by opening a web browser to http://localhost:8081

It will now take and upload images every 60 seconds. Stop the program with CTRL-C in the terminal and run motion to restart.