This article uses computer vision and image processing techniques to demonstrate the process of detecting the status of the oven switch. There are times in life when you forget to turn off a kitchen appliance like the oven, which can be potentially dangerous. So the authors use Python and OpenCV to automatically recognize whether the oven is on or not using images from a home camera, which can trigger an alarm.

“Did I leave the oven on?”

This question often pops into your head at the most inconvenient of times.

Sometimes it’s when you just walk out the door. Sometimes when you’re on your way to work. Sometimes it’s when you’re sitting on a plane for a long vacation…

There are various ways to solve this problem:

The rubber band method

Say or sing it out loud (like Samuel L. Jackson)

When you leave home on vacation, make a list or note of the appliances, including the oven. Or maybe we can do better…

In this tutorial, we’ll try to solve this problem with technology.

The full code is available on Github.

Problem definition

For us, we need to identify a signal to determine the status of the oven on and off. In my kitchen, that signal is the red light marked “Oven on” at the top.

When the red light is on, the oven is on:

When the red light is off, the oven is off:

Preparation conditions

Make sure you have the following apps installed on your computer:

OpenCV 3.0

Python 2.7

Numpy 1.9

Install OpenCV3.0 and Python 2.7

If you haven’t already installed OpenCV, follow Adrian Rosebrock’s excellent tutorial to install OpenCV 3.0 and Python 2.7+ on an OSX system. I added some comments of my own in the installation steps in case you have problems compiling OpenCV 3.0 on OSX.

steps

If you have successfully installed OpenCV in your environment, we can begin to judge the data analysis of the oven switch.

Load the required packages

Argparse — a parameter handling library.

Numpy – a highly optimized numerical operation library. OpenCV uses numpy in array structures.

Cv2 – Image processing library in OpenCV.

importargparse

importnumpyasnp

importcv2

Load the picture

image = cv2.imread(image_path)

Image noise reduction

In order to denoise the image, we need to smooth the input image. This will make it easier to locate the target in the image. Using the medianBlur function, set the aperture size to 3. The higher the number, the more blurry the image.

blur_image = cv2.medianBlur(image, 3)

Change the image color to HSV format

HSV — Chroma, saturation and purity (brightness). HSV allows us to extract a color object because it is easier to represent colors than the BGR format.

Converting an image to HSV format allows us to determine a color in an image by chroma (one value instead of three).

Here’s how:

hsv_image = cv2.cvtColor(blur_image, cv2.COLOR_BGR2HSV)

The execution result is shown as follows:

Detect colors in images

To detect the color we want, we can look at the histogram of the color in the oven lamp picture.

As we can see, red dominates the picture. Red has two peaks — one high and one low. These color values are converted to chromaticity ranges from 0 to 10, and from 160 to 180 (for red).

For each hue range in the HSV image, we can create a mask to remove all irrelevant colors that are not in the selected range.

defcreate_hue_mask(image,lower_color,upper_color):

lower=np.array(lower_color,np.uint8)

upper=np.array(upper_color,np.uint8)

# Create a mask from the colors

mask=cv2.inRange(image,lower,upper)

output_image=cv2.bitwise_and(image,image,mask=mask)

returnoutput_image

# Get lower red hue

Lower_red_hue = create_hue_mask (hsv_image, [0100100], [10255255])

# Get higher red hue

Higher_red_hue = create_hue_mask (hsv_image, [160100100], [179255255])

The results are as follows:

The images are then combined to capture all the red shades.

Full_image = cv2. AddWeighted (lower_red_hue, 1.0, higher_red_hue, 1.0, 0.0)

The results are as follows:

Find the circle in the picture

Now that we only have a red hue on the image, we need to determine if the red light is on (that is, if there is a circle with a red hue). We need to find circles in the new graph, but first we need to convert the image to a grayscale (since the input to the HoughCircles function is a grayscale).

Detecting circles in images requires the following parameters (using the HoughCircles function in OpenCV) :

Grayscale input.

HOUGH_GRADIENT is the method (currently the only one) used to detect circles.

The inverse ratio of the accumulator to the image resolution. In this case, 1.2.

The minimum distance to the center of the circle to be tested, in this case 100.

#Convert image to grayscale

image_gray=cv2.cvtColor(full_image,cv2.COLOR_BGR2GRAY)

#Find circles in the image

Circles = cv2. HoughCircles (image_gray, cv2. HOUGH_GRADIENT, 1.2, 100)

The results of

Now you can check for circles. If there is, that means at least one oven light is on. If you can’t find the circle it means the lights are off and the oven is off.

To prove this, we can circle the original drawing using the following code:

# Draw the circles on the original image

circles=np.round(circles[0,:]).astype(“int”)

for(center_x,center_y,radius)incircles:

Cv2. Circle (image, (center_x, center_y), radius, (0255, 0), (4)

The results are as follows:

The next step

There are many more things you can do, such as:

Check the switch on of specific lights to find out the true state of the oven.

Set up a service to check oven status remotely.

Add that feature to the Raspberry PI, and we have a gadget that warns if the oven is off.

The full sample code can be found on Github.

Making the link: https://github.com/kazuar/opencv_light_detection

End