Complete implementation of motion detection