Category Archives: Camera

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.

Using ffmpeg to create a video from image sequences

This PHP script will take a sequence of archive images stored in time-specific sub folders hh/mm and renumber them sequentially. Then use ffmpeg to convert the sequence into a video file. The operation is looped over an array of cameras and a staging folder is cleared prior to each video creation.

$ftppath = "/root/path/to/images/";
$stagepath = $ftppath . 'staging/';
$date = date('ymd');

foreach ($cams as $cam) {
 exec('rm ' . $stagepath . '*');
 $camfile = $cam['fileroot'];
 $idx = 1;
 for ($j=0; $j<24; $j++)
 for ($i=0; $i<60; $i++)
 $n = sprintf("%02d/%02d", $j, $i);
 $pathfile = $ftppath . $n . '/' . $camfile . '_320x240.jpg';
 if (file_exists($pathfile)) {
 $sidx = sprintf("%04d", $idx);
 copy($pathfile, $stagepath . $camfile . $sidx . '.jpg');
 exec('ffmpeg -r 4 -i ' . $stagepath . $camfile . '%04d.jpg ' . $ftppath . $camfile . '_' . $date . '.mp4');

Variable range scrollback for archive images

For a given image of class=’camview’  this jQuery javascript will scroll back a variable amount depending on the mouse location over the image.

The mouse over right edge of the image represents the most recent image from the archive. The bottom-left corner represents 24 hours ago. But to have finer granularity at reviewing more recent images, scrolling the mouse over the top of the image will scroll back one time-increment every 10 pixels. Moving the mouse from right to left over the bottom of the image will scroll back over a full 24 hours (1440 one-minute images) skipping frames as needed to fit the mouse resolution. Mouse heights between the top and bottom edges will have a linearly scaled proportion of archive history.

$('.camview').mousemove(function(event) {
 var x = event.pageX - $(this).offset().left;
 var y = event.pageY - $(this).offset().top;
 var mindx = 0.1;
 var maxdx = 1440.0 / $(this).width();
 var dy = 1.0 - y / $(this).height();
 var dx = dy * mindx + (1.0-dy) * maxdx;
 var arc = ($(this).width() - x) * dx;
 $(this).find('img').attr('src', 'camjpg.php?cam=' + $(this).attr('id') + '&arc=' + arc.toFixed(0));

Http proxy for accessing local area network images

To access multiple cameras on a local area network from a single web page, you need to proxy the requests from your web server. Since many netcams require BasicAuth to access images,

A PHP script to handle the proxy request can be found here:
To process a set of cameras, store the camera access data in an array such as this

$cams = array(
 "c30a" => array(
 "name" => "c30a",
 "type" => "http",
 "http" => "",
 "http_user" => "<user>",
 "http_pass" => "<password>",
 "title" => "Backyard",
 "live" => "//"
 "c20a" => array (
 "name" => "c20a",
 "type" => "http",
 "http" => "",
 "http_user" => "<user>",
 "http_pass" => "<password>",
 "title" => "Garage",
 "live" => "//"

Include the above config.php in the php script that will handle the proxy request. It will look up the local area network destination from the ‘http’ element of the array, and pass the request along the camera, then copy the image returned from the camera to the caller.

url = $cam['http'];
 $h->postvars = $_POST;
 if (!$h->fetch($h->url, 0, "", $http_user, $http_pass)) {
   header("HTTP/1.0 501 Script Error");
   echo "proxy.php had an error attempting to query the url";

 $http_user = "";
 $http_pass = "";
 if (isset($cam['http_user'])) $http_user = $cam['http_user'];
 if (isset($cam['http_pass'])) $http_pass = $cam['http_pass'];

 $h->url = $cam['http'];
 $h->postvars = $_POST;
 if (!$h->fetch($h->url, 0, "", $http_user, $http_pass)) {
 header("HTTP/1.0 501 Script Error");
 echo "proxy.php had an error attempting to query the url";

 // Forward the headers to the client.
 $ary_headers = explode("\n", $h->header);
 foreach($ary_headers as $hdr) { header($hdr); }

 // Send the response body to the client.
 echo $h->body;

To use this proxy from a web page, the javascript with jQuery would request an image from the same page as the main web site.

 $('#c30a img').attr('src', 'proxy.php?cam=c30a');