Skip navigation

Raspberry Pi Projects

9 Posts authored by: m.ratcliffe

OpenCV is an amazing piece of software and the newer Pi's are getting the power to run real time image processing tasks, like face detection.






However, OpenCv is not that user friendly, Lets make an attempt to simplify it a little. Most detection scripts need accompanying files that contain the information to identify what we are searching for in the image we just captured. Luckily OpenCv comes pre packed with some of these Haar-Cascaded [The files that tell open cv what to look for], they can be found in:


/usr/share/opencv/haarcascades/          [for Pi]



[for information on what a HaarCascade is or want to build your own to identify a fish or banana? see:


>Robotics@Cyborg: How to make your own haar trained ".xml" files


OpenCv comes with default Haar-Cascades to find:

>Faces [frontal and side]

>Eyes [Right or left]

>Number Plates

>Upper and lower bodies of people

You can find many more by a quick google search and paste them into the above folder.


So how do we use these to detect faces, run in the terminal:


$sudo python Scriptname <Path to Haar-Cascade you like to use]


To run the one in the video,

>paste the code below into a empty text file saved as, right click and select any one can execute, then:

>Plug in a usb webcam into the pi [the cheap square ones from ebay work well $3]

>Type into terminal:


sudo python /usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml


if you want to exit, click on the video window and press esc on keyboard

## ********** Importing Some Libraries **************##

import cv2

import sys

##Dont know what this bit does, but it works



cascPath = sys.argv[1]

faceCascade = cv2.CascadeClassifier(cascPath)

#Telling script to capture from usb camera



video_capture = cv2.VideoCapture(0)


#************************Main LOOP *****************##



while True:


##********** We burn a few frames to make sure we have the newest one  [stops lag]

    # Capture frame-by-frame

    ret, frame =

    ret, frame =

    ret, frame =

    ret, frame =

    ret, frame =


##****** Resizing Incoming Image

    res = cv2.resize(frame,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_AREA)


##********Converting to Grey Scale



    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

##******** Finding Faces, changing the values for size of faces in camera view



    faces = faceCascade.detectMultiScale(




        minSize=(30, 30),




    # Draw a rectangle around the faces

    for (x, y, w, h) in faces:

        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)



    # Display the resulting frame

    cv2.imshow('Video', frame)



    if cv2.waitKey(33)== 27:




# When everything is done, release the capture




Checkout the main home page for updates:


Animated_Grim Blog: Home Page

The skull is controlled by three servo's giving it three degree's of freedom, it also has Led's in the eyes to add a dramatic effect [see the later blogs]


Degree's of freedom:


>Pan [side to side]

>Head Tilt



I'm not going to be putting a full wiring diagram because Charles Gantt did it so well in his blog:

Trick or Trivia Halloween Candy Dispenser #004 - Building The Candy Dispenser & Servo Coding



Wire it up to these GPIO pins on a pi model 2:

SERVOVertical = 26

SERVOHorizontal= 19

SERVOShake= 13



To keep people upto date:


The Animated grim has just been setup outside and it is epic!

I have been mad busy making the thing after breaking a few parts. I will be blogging about the build early next week.


Goodluck with the projects guys,


It looks like I am one of the first to receive the Kit, So I thought I would do a quick video of what was in the box:



Thanks Element14 and Dave, the kit looks great and was shipped fast as always!

Keep an eye on the main page for future blogs:


Animated_Grim Blog: Home Page [Last Updated 6/10/2015 < British Format]

We will be using quite a lot of image processing on this project, lets make life simple.



It it crazy Just How much miss information there is about opencv and the Raspberry pi, many tutorials will guide you thru a installation process that takes 12 hours+ and has library's linked complexly.


Opencv 2.4.1 [CV2] is available in the default Rasbian Reposttory.

To Install on Rasbian, type the following into the command terminal:


Type Into Terminal

Sudo apt-get update

sudo apt-get upgrade



sudo apt-get install libcv-dev

sudo apt-get install python-opencv

sudo apt-get install libopencv-dev

sudo apt-get install libcv2.3  #[May have updated to libcv2.4]

sudo apt-get install opencv-doc


How To use:

>Open the File manager and go to your documents

>Right click>CreatNew>Empty File

>Add the line [#!/usr/bin/python] To the top of the file

>Save it as <FileName>.py

>Right Click on the File >Properties>Permissions and select everyone> OK

>Double click on the file and execute in terminal



>Remember it is a python based install of openCV 2

>Execute the python script as root if you want access to the GPIO pins

>If the script doesnt work, Cd / into the directory and run python <>


Useful Links:


Adaptive Threshold:

Other Threshold

Why enter the competition

>I have checked out a few of the other Halloween projects and they are great!

>Many fun Halloween pi ideas just wont leave the back of my mind

>The Element14 Challenges are Fun



Project Scope

Right, well I’m guessing you have watched the above video. Lets talk about the plan in a little more detail.



Pi based vision based project that is scary, fun and would make a great project for people interested in the Rpi and vision. I get maybe as little as 20 trick or treaters a year all on the old side for trick or treating. So my plan is to try and scare them, I have a particular creepy corner at the front of my house that would fit a large figure well.



Flow Chart




Death will be a scale model with a real scythe [secured into the ground and blunted] with a mister permanently on flowing fog from under his cloak, beside him will be a chest full of candy.




The screen will be in “Deaths” outstretched left hand and the children will get to select how many children there are. I am hoping most children will be greedy and select a number greater than the number there.


If the selected number is greater than the number seen in the camera the head controlled by two servo’s will turn to look at the child the red led’s behind the eyes will come on and with the mister and a speaker will say “ Liers do not get candy“  the head will then pan around  and “there are only X souls before me” the head will turn back to its defauly position and the children will get a second chance “ select again my honest minions”. And we will loop back to the options again.


Child selects a number equal to or less than the number seen in camera view


As for the candy delivery portion, we will skip automating this bit due to costs. we will just have a chest full of candy and mist.



ISR will be based on a switch on the candy box Lid, if the box is opened without first playing the game.


Voice Clips

The interaction with the player will be via a computer generated voice complemented by head movement and other special effects [Eye LED's, Fog].


[1] " Liar" [Red Led Behing eyes comes on, mist from mouth hole, followed by a pan motion of the head] " I see Y souls before me"

[2] [ Makes eye contact with primary player] "Try your luck again my minion"

[3][Head pans to count number of faces] " I too see X Souls here"

[4]"take your candy my minions and go fourth" [Led's turn green, looks at the candy chest] Led's in chest will dimly light.

[5][ ISR based, red led's on mist on] "Thief's Be Warned! , Play the game or perish"



Potential problems

>The Facedetection is relatively easy, but we will have to take care to make sure it can detect a mask too.

>Theft, where I want to place the unit is on the edge of the property in a particular spooky spot, Easy for a theif to take the whole thing, but it is relatively remote and we usually see around 20 trick or treaters. so we will just take our chances.

>The Kids might rinse the box containing the treats, we will have to hope they are too scared to do this and check on it every so often to top up if needed.


Parts I already Have:

> Red LED’s

>Ultrasonic foger [Already have a diy version of this and it would make a great DIY BLOG]


> Opto Isolators for actuators

>Webcam/Pi Camera

>Small Speaker


Parts I will need to source:

If you can help out with any of these I would be very grateful.

>Supplied touch screen

>Pi 2b [Element14Dave said he has sent one in the post, but a second one would make life much easier]

>2x  Servo’s [for the mask pan/tilt]

> “Screem” Mask and Cloak, [will get these from the dollar store]



I hope I managed to put across what I envision, the first prototype will explain it much better than words alone could do,


With running this project on a non existent budget, a lot of the stuff is going to have be  $1 or borrowed temp from another project.


Here is the poor mans "Fog" build. I built this unit as a pesticide doser, by just adding water it will create safe cool mist and by adding a few delivery hoses we should be able to route this wherever it is needed.


Take a look at the Vertical Growing Blog for more info on this mister:


Automated Green House Blog's Home Page [Updated: 6/10/2015 < British Format]






What you will need:

[The Prices are estimates]


>24v Ultrasonic Mister [£2]

[You may want multiple units for large rooms/ large amounts of mist]




>24v Power supply

Any source of 24V DC, make sure it can supply enough current for your planned number of misters and Fan.

Misters have a female 5.5mm Power jack [common size for wall mounted power supplies]


>24v PC Fan £1

24v Fan


> A Container Of your Choice with removable LID [Wide and shallow is better]


>Fan Speed Controller [Optional] £2

The basic ones just work as a voltage divider, so you can make your own from a potentiometer if you like.



>5.5mm 12v splitter cable [Optional] £2



How to build it:

As Always this is just a set of instructions, follow them at your won risk.

>I'm not including a wiring diagram with this blog, everything operates at the same voltage and it is as simple as wiring all parts in parallel.

>Cut 4 holes in the top of the container you plan to use, two the size of the fan you selected and two smaller ones for the waterproof bungs on the mister wires.

>Mount the fan so it is blowing air into the container [stops the motor getting wet] and alter the mister lead length in the bungs to place them in the middle of the container away from your inlet and exhaust holes.

>Add a exhaust pipe if you want to direct the mist to somewhere else.


How To use:

As a general mister or humidifier to keep cool in summer or for a Halloween project, just add water power it on and let it do its thing, use the fan controller knob to control the amount of mist you want. We will be using a PWM controller to do this later int he project.



Keep an eye on the main Animated_Grim Project:


PumpkinPi2015 Competition Animated_Grim Blog: Home Page

I thought I would share my simple design for a fog generating machine, I made this a few years ago as a pesticide doser. It might be of interest to those of you making a PI based Halloween special, just dont add the pesticide!


I could not find any cheap units when I made this a few years ago, maybe there is a cheap off the shelf solution now? but I still prefer using things built than bought.

This unit just requires water and 24v to operate, no chemicals needed.





For the build tutorial check out:

Automated Green House Blog:12 - Pesticide Doser, Cheap DIY Mister



I look forward to watching the Pi Halloween projects,


Filter Blog

By date: By tag: