Introduction: The Begging Dog

In this project, we will be using a raspberry pi to control a servo that turns a cardboard dog's head away whenever a person looks at the dog, to mimic a begging dog that doesn't want to be seen begging.

Step 1: Gather Your Materials

For this project, we will be using a Raspberry Pi, a small computer you can buy online for about 35$. To effectively utilize this computer, you're also going to need a 5V micro-usb power cable to plug into the Pi, an HDMI cable to see the display, and USB keyboard and mouse. To store the OS on the Pi, a microSD card with at least 8GB of data is needed, and a computer is needed to edit the files on this microSD. In order to achieve facial recognition, we need the Raspberry Pi official camera as well, and a micro servo.

Step 2: Setting Up the Hardware

Once you've gathered the materials, setting up the hardware is relatively easy. The Pi has golden pins called GPIO pins on it that we're going to use to control the servo. Hook in the 3 cables from the servo as shown in the picture. Once those are in, the Pi's camera needs to be plugged in. This ribbon cable simply attaches into the port labeled "camera" on the board of the Pi. Next, plug the HDMI cable into its port on the outside of the pi and connect it to an HDMI-compatible TV or monitor. We're not going to plug in the power cord just yet- we need to do some software work first.

Step 3: Setting Up the MicroSD Software

To set up the microSD software, first install the programs 7-zip and Win32DiskImager. Install the latest version of Rasbian (with Pixel) from the raspberry pi official website, then unzip the image using 7-zip. Finally, use Win32DiskImager to put the unzipped image onto your microSD card (warning: this will delete all previous files on the card, so make sure there's nothing left you want!)

Step 4: Setting Up the Pi Software

Now that the SD card has the Rasbian OS on it, insert the card into the microSD slot on the bottom of the Pi and plug in the micro-USB power cord. On your monitor or TV, you should see a black screen running some code with a picture in the center of the screen. Let it run for a little bit, then you should see a desktop show up. Right click anywhere and select "Create New File", and name it Dog_Turn.py. Right-click the new file and select "open with Python 2 IDLE". Once the IDLE is open, paste the following code into it:

import cv2, wiringpi, picamera
from time import sleep import numpy as np import RPi.GPIO as GPIO import time, random

#servo stuff GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT) p = GPIO.PWM(7,50) p.start(7.5)

#camera stuff camera = picamera.PiCamera() camera.resolution = (640,480) camera.start_preview() sleep(5) camera.stop_preview()

#recognition stuff face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml')

p.start(7.5)

while True: camera.capture('image.jpg')

image = cv2.imread('image.jpg',cv2.CV_LOAD_IMAGE_COLOR) gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) print('{0} faces.'.format(len(faces)))

if len(faces) == 0: p.ChangeDutyCycle(7.5)

if len(faces) > 0: p.ChangeDutyCycle(2.5)

However, for this code to work, certain modules for Python need to be installed. Open the terminal on the Pi and type "sudo apt-get install libopencv-dev python-opencv python-dev" (without the quotes.) and let this run. Then the python code will run.

Step 5: Building the Dog

To build the cardboard dog for this project, we cut out 4 9x6" cardboard rectangles and 2 6x6 squares to form the main dog's body. We also cut a hole in the top of the box for the servo to go through (all the hardware is contained in the main body). This servo attached to a 5" cube that served as the dog's head. To access hardware, we made the dog's back square a flap. For a little extra detail, add some cardboard legs to the bottom like we did!

Step 6: Finishing

Now that both the hardware and the software are set up, simply run the python code and watch it do its thing! Once started, the HDMI, keyboard, and mouse can be unplugged and the code will keep running until you either stop the code or unplug the dog.

Comments

author
Swansong (author)2017-06-16

That's a cute project idea :)