Did I leave the oven on? (with OpenCV and Python)
“Did I leave the oven on?”
This question will sometimes pop up at the most inconvenient times.
Sometimes when you just left your house.
Sometimes on your way to work.
Sometimes on a plane while you’re on your way for a long vacation…
There are different solutions for this problem:
- The rubber band method
- Saying / singing it out loud (like Samuel L. Jackson)
- Labeling your appliances or even making a checklist that includes the oven before leaving the house to a long vacation.
Or, we can do something better…
In this tutorial we’ll try the technical approach to the problem.
Full code can be found on Github.
Problem definition
In our case, we need to decide on the signal that we’ll use in order to determine if the oven is on / off. In my kitchen, that signal is a red light on top of the text “OVEN ON”.
When the red light is on, the oven is on:
When the red light is off, the oven is off:
Prerequisites
Make sure that on your machine you have the following:
- OpenCV 3.0
- Python 2.7
- Numpy 1.9
Installing OpenCV 3.0 + Python 2.7
If you don’t have OpenCV installed on your machine, start by following Adrian Rosebrock’s excellent tutorial on installing OpenCV 3.0 and Python 2.7+ on OS X. I’ve added my own notes about the installation process, in case you run into some issues in compiling OpenCV 3.0 on OS X.
Process
Based on the fact you were successfull in installing OpenCV on your environment, we will start the process of analyzing our data in order to determine if the oven is ON / OFF.
Load the required packages
- argparse - for argument handling.
- numpy - a highly optimized library for numerical operations. OpenCV uses numpy for its array structures.
- cv2 - OpenCV for processing images.
Loading the image
Reducing noise in the image
We will want to smooth the input image in order to reduce the noise in the image. This will make it easier to detect objects in the image. For medianBlur we will use aperture size of 3. A higher value means that the image will be more blurry.
Convert the image colors to HSV
HSV - Hue, Saturation and Value (brightness). HSV will allow us to extract a colored object since it is easier to represent a color in HSV than in BGR.
Converting the image to HSV will allow us to identify a color in the image using the hue value (a single value instead of three).
This is how it’s done:
The code will result in the following image:
Detecting color in the image
In order to decide on the color that we want to detect, we can look at the histogram of the color values in the image of the oven light.
We can see that the color red is dominant in the image. There are two peaks of red color - one in the low range and one in the high range. These color values are translated to hue in the range of 0 to 10 and 160 to 180 (for the color red).
For each hue range we will create a mask on the HSV image and remove everything that isn’t in the selected range of the required color.
With the following results:
Next step is to merge these images together in order to catch all of the red hues:
Which will have the following result:
Find circles in the image
Now we have a picture with only the red hues in it, and we will want to identify whether the light is on (there is a circle of red hues) or the light is off (there is no circle). We need to find the circles in the new image, but first we will need to convert the image to grayscale (since the input for HoughCircles is a grayscale image).
For detecting the circles in the image we will use the following parameters (from OpenCV HoughCircles):
- Grayscale input image
- HOUGH_GRADIENT is the circle detection method (currently the only one).
- Inverse ratio of the accumulator resolution to the image resolution. In this case, 1.2.
- Minimum distance between the centers of the detected circles. In this case, 100.
Results
At this point it will be enough to check if there are any circles.
If there are, it means that we have a signal that at least one of the oven lights is on.
If we couldn’t find any circles it means that none of the lights are on and the oven is off.
To prove that, we can draw the circles on the original image with the following code:
The result will be:
Next steps
There are many things that we can do from here, some examples are:
- Detect the specific light that is on and by that understanding the exact status of the oven.
- Create a service that will allow us to check the status of the oven remotely.
- Add this functionality to raspberry pi so we can have a small device that will alert us if the oven is off or on.
Full code sample can be found on Github.