Blog Post

Track Your Face With PiStorms PopHeads Program

Track Your Face With PiStorms PopHeads Program


The PopHeads program uses the Pi Camera and OpenCV to track your face. Once the PiCam recognizes you, it will draw a simple cartoon face on the PiStorms screen. When you move the face will move with you!



This program requires the following hardware for basic functionality:

Raspberry Pi

Pi Camera and its cable



It is recommended to use the following hardware to properly mount all devices:

PiStorms Frame

Camera Mount for PiStorms


For proper assembly of all hardware view the following tutorials:

PiStorms Frame Assembly

Vision for your PiStorms Robot using Pi Camera!


Software Setup

The PopHeads program and all dependent software is installed on the current version of the pre-configured PiStorms image. If you do not see the 01-PopHeads program on your PiStorms Browser, please upgrade your PiStorms software using the instructions here.


Running the Program

The PopHeads program can be run directly from the PiStorms with the PiStorms Browser. 


The program is running when the PiCamera light turns on. From a distance of about 2ft, sit or stand in front of the PiCamera so that the device is pointed directly at your face. Once the program recognizes your face, a white circle with two white dots for eyes will appear on the PiStorms screen.


Move your face a little to each side. The face on the PiStorms screen will move with you. Move toward and then farther away from the camera. The size of the face will change. If the face disappears and does not come back, you are out of range of the PiCamera and it no longer can recognize your face.


The Code

Import all necessary libraries, enable use with PiStorms Browser and create the PiStorms instance.

from picamera.array import PiRGBArray
from picamera import PiCamera
import os,sys,inspect,time
import cv2
import imutils
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
from PiStorms import PiStorms
psm = PiStorms()


Create the haar cascade. This is basically a table that provides the information for the facial recognition.

faceCascade = cv2.CascadeClassifier("/home/pi/PiStorms/programs/haarcascade_frontalface_default.xml")


Set camera parameters to improve the speed of the program.

camera = PiCamera()
camera.resolution = (320, 240)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(320, 240))


Wait for the camera to warm up and specify some variables used in the program.

lastTime = time.time()*1000.0
lastfaces = {}


Continuously take pictures and modify the images.

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    image = frame.array
    (imh, imw) = image.shape[:2]
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


Detect faces in the images taken from the PiCam.

    faces = faceCascade.detectMultiScale(
    minSize=(30, 30),
    flags =


Erase old faces on the PiStorms screen.

    for (x, y, w, h) in lastfaces:
        psm.screen.fillCircle(imw-(x+w), y+h, int((w+h)/3), fill = (0,0,0),display = True)


Draw faces on the PiStorms screen.

    for (x, y, w, h) in faces:, (x+w/2, y+h/2), int((w+h)/3), (255, 255, 255), 1)
        psm.screen.fillCircle(imw-(x+w), y+h, int((w+h)/3), fill = (255,255,255),display = False)
        psm.screen.fillCircle(imw-(x+w), y+h, int(-2+(w+h)/3), fill = (0,0,0),display = False)
        psm.screen.fillCircle(imw-(x+w)+w/4, y+h-h/4, 4, fill = (255,255,255),display = False)
        psm.screen.fillCircle(imw-(x+w)-w/4, y+h-h/4, 4, fill = (255,255,255),display = True)


Reset the variables for the next loop run.

    key = cv2.waitKey(1) & 0xFF
    lastfaces = faces


Provide an exit for the loop.

    if(psm.isKeyPressed() == True or key == ord("q")):

PopHeads in Action

Comments (0)

No comments at this moment
Please Login to add your comments

Featured Posts

The mindsensors Grove Sensor Adapter makes it easy to use a variety of unique Grove sensors with the...

Read more

The mindsensors Grove Sensor Adapter makes it easy to use a variety of unique Grove sensors with the...

Read more


  • What do you use to program your FRC Robot?