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. 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:
- Rotate the image as needed
- Find the meter background contour
- Find the needle contour within the background
- Find the pivot point within the needle (used for adjusting the angle orientation)
- Fit a line to the needle contour
- 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.
- Convert the angle to a percentage based on the empty/full angles if the meter
- Crop the image based on the bounds of the meter background for display
- Upload the percentage value to openHAB for use by the home automation system
The sources can be found on Github: https://github.com/techsavi/OilLevelReader