Home Automation With a Raspberry Pi (Part 2)
With our Raspberry Pi ready, it's time to connect your your infrared device and your app to the same network, then teach the app how to recognize hand gestures.
Join the DZone community and get the full member experience.Join For Free
in our most recent blog , we talked about software installation to set up and begin automating home appliances using human hand gestures to control them.
we are now in the second part of this series, where we intend to develop an application that is built on a raspberry pi using java and computer vision apis. it will detect hand gestures and consequently pass the signals to an ir device, which will then perform the appropriate home automation.
this blog guides you through the complete procedure for detecting human hand gestures and interacting with hardware/home appliances.
let us first have a look at the role of the application in implementing hand gesture recognition for home automation.
here is our process:
- the application and ir device must be part of the same network.
- the application captures camera input continuously.
- from captured image frames, the hand gesture is detected using opencv data structures/algorithms.
- appropriate signals are found and consequently passed onto the ir device (http call) with the proper command (on/off) to the external device.
core functionalities and modules
- gesture detection: the application contains open computer vision apis. opencv and javacv process the camera input and apply algorithms to process their frames.
- smartconfig: this is the heart of the application, which can talk with the ir device to control an external device. this module has two modes: learning and execution. in learning mode, it configures the ir device using the ti smart config protocol. it monitors and records data sequences for different infrared sequences for home appliance operations. during execution mode, it can send the recorded sequences to the ir device, which are converted by the ir device into an infrared sequence to control different devices.
- signal processing: this is the remote processing part of the application, in which the application stores the signals and passes them to the ir device for further operations.
let's have a look at the technologies used in implementing hand gesture recognition.
in our project, we use opencv contours for recognizing the hand image.
contours can be explained simply as a curve joining all the continuous points (along the boundary), having the same color or intensity. the contours are a useful tool for shape analysis and object detection and recognition. for better accuracy, use binary images. so before finding contours, apply a threshold or canny edge detection. the findcontours function modifies the source image. so if you want the source image even after finding contours, store it to some other variables.
in opencv, finding contours is like finding a white object from a black background. so remember, an object to be found should be white and the background should be black.
method/algorithms to detect hands from video
briefly, let's look at the several ways to process a frame to detect the hand and their gestures:
- background subtraction : a simple method to start with is subtracting the pixel values. however, this will result in negative values and values greater than 255, which is the maximum value used to store an integer. and what if we have a black background? nothing gets subtracted in that case. instead, we use an inbuilt background subtractor based on a gaussian mixture-based background/foreground segmentation algorithm. background subtraction involves calculating a reference image, subtracting each new frame from this image and the threshold. the result is a binary segmentation of the image, which highlights regions of non-stationary objects. we then use erosion and dilation to enhance the changes to make it more prominent.
color detection : skin color detection is one of the most popular methods. this method is simple and depends on skin color, which can be white, black, or other colors. it also considers environment light conditions as well as the background.
: an edge detection operator uses a multi-stage algorithm to detect a wide range of edges in images. its aim is to satisfy these criteria:
- lower error rate
- good localization
- minimal response
for our solution, we used the color detection method to detect hands from the frame via user skin color. the application itself has a gui to detect user hand skin color and to persist it for further application uses.
after analyzing the application side requirements, let’s move on to midway device for further communication, referred to here as our ir device.
the ir device works as an ir transceiver. it is used to record ir commands for different appliances, and that recorded sequence is used to control appliances based on different gestures detected in the previous phase. the ir device can be connected to a tv, set-top boxes, stereos, air conditioners, and other appliances, and then can pass an infrared remote control signal and start the home appliance.
after understanding the all the devices, let's now understand the procedure to develop the application.
guide to develop the application
home automation is the residential extension of building automation and involves the control and automation of lighting, air conditioning, and security, as well as home appliances such as washers/dryers, television, ovens, or refrigerators/freezers using wi-fi for remote monitoring.
modern systems generally consist of switches and sensors connected to a central hub, sometimes called a “gateway,” from which the system is controlled with a user interface that is interacted either with a wall-mounted terminal, mobile phone software, tablet computer, or a web interface, often, but not always, via internet cloud services.
the application we have developed has a user interface that is able to manage devices and communication paths. it has a robust feature to make devices habituate via their learning mode and execution mode.
it’s a wi-fi setup process that allows multiple in-home devices to connect to wi-fi networks quickly and efficiently. it is specifically for applications that typically do not have a display or keyboard to enter network name and password.
the smartconfig interface gives end users the ability to easily connect ir devices to an access point. through a simple smartconfig method/interface, consumers can use any programming concept to create their own application using the smartconfig interface.
to connect the ir device/hardware with the application, send broadcast datagram packets with network (wi-fi) details (ssid and password).
this process is able to send and receive broadcast datagram packets and make a connection with the application and ir hardware device. the application has a gui that allow the user to connect to the device.
the application first checks whether any ir device is already connected. if not, then it will prompt a window to connect the device with some validations:
the application creates a socket connection with the host (router) and sends broadcast datagram packets with the ssid and password to connect with the ir device and waits for an acknowledgment from the ir smart device.
device learning : after the ir device is connected to the application, the user is able to set up commands/signals for home appliances based on their hand gestures, and it starts with a learning step.
in the learning phase, a user is able to set up one or multiple instructions or commands for multiple devices using the gui of application.
our current implementation has basic gestures like a 0-5 count, up-down, and left-right. based on user configuration, the application will operate their attached devices.
for example, let's increase our tv volume with an up and down motion of the hand.
after the learning phase, you are ready to operate your home appliances with your hand gestures. now, users can turn a tv on and off and also manage its volume in our application implementation.
we have configured six or seven commands to operate tv, like on, off, volume up-down, mute, open menu bar, exit menu bar, and their operations.
signal processing (remote service) : our application is able to detect hand gestures and pass the ir signal to configured home appliances depending on human hand gestures.
the application is capable of managing user interactions, ir device communication, signals, and skin color persistence.
the application gateway is incorporated to connect smart home appliances, and an iot connection management platform is required to enable various applications.
these applicable solutions include: home automation, which, ideally, promotes a happier, more comfortable, and convenient lifestyle.
the solution will help operators transform from traditional home broadband services to provide intelligent home services.
ir signal execution (ir signal transmission)
home automation is a significant undertaking in the iot domain. with improved convenience, comfort, energy efficiency, and security under the iot domain, it has proved to be incredibly beneficial for both individuals and society.
in our next blog, we are going to look at the real-time working application demo and step-by-step code snippets of the application. so let’s grab the opportunity and see what the next blog brings.
Published at DZone with permission of Santosh Prajapat, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.