element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi Projects
  • Products
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Pi In The Face Pumpkin: Managing Face Images, Sounds, and Structures
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: screamingtiger
  • Date Created: 12 Oct 2015 5:07 PM Date Created
  • Views 604 views
  • Likes 2 likes
  • Comments 1 comment
  • rpiintermediate
  • pieintheface
  • raspberrypi
  • raspberry_pi_projects
  • pumpkinpi2015
Related
Recommended

Pi In The Face Pumpkin: Managing Face Images, Sounds, and Structures

screamingtiger
screamingtiger
12 Oct 2015

Previous posts for this project can be found here:

http://www.element14.com/community/community/raspberry-pi/raspberrypi_projects/blog/tags#/?tags=pumpkinpi2015

 

I want to blog a little about the pumpkin mainly becuase I need to get the design nailed down while I wait for my hardware to arrive.

 

The pumpkin will play at random, pre-recorded sounds.  When a sound plays, the eyes and mouth move around.  When a

sound ends, the eyes will still move but the mouth wont.

 

Lets start with the eyes.

 

The eyes will blink randomly and if a user "presses" the eye it will squint.  This means I need a set of images

that can be "flipped" to imitate the motions.

 

As an example, a blink is a series of images that are played in order.  This means I need a method of loadig up images and storing them, as well as keeping them in order.

So what I will do is create a file structure that keeps everything nice and tidy.  For now I will only have two

actions for the eye which will be blink and squint.

 

I propose the following directories:

 

faces/

faces/<name>/eye/
faces/<name>/left/
faces/<name>/right/
faces/<name>/mouth/
faces/<name>/sounds/

The <name> will be the name of the face, as I want to be able to load multiple faces.  The default face must exists

and be called 'default'.

Within each eye folder, a set of files is needed.  I propose the following:

 

faces/<name>/eye/left/eye.jpg
faces/<name>/eye/left/blink1.jpg
faces/<name>/eye/left/blink2.jpg
...etc

faces/<name>/eye/left/squint1.jpg
faces/<name>/eye/left/squint2.jpg
...etc

 

The same structure exists for the right eye and the other structures.  When animating, the images are flipped in

order when the action is needed.

 

Having these common directories allows searching and loading of faces to be easier.  For now I will use only the

default face for my development.

 

With the directory in place, now we need to load all the face components which includes the eyes, mouth and sounds.

 

This will require the use of a class for each face to keep everything together.

 

Lets start with an easy example on how to load up all the squint images for the default face.

 

 

import os
import pygame,sys
leftEyeSquint = list()
for dirname, dirnames, filenames in os.walk('./eye/default/left/squint/'):
     for filename in sorted(filenames):
  try:
   leftEyeSquint.append( pygame.image.load(os.path.join(dirname, filename)))
  except:
   print(filename + ": invalid file")
print str(len(leftEyeSquint)) + " Squint images loaded\n"
   


This gives us a list with an ordered set of squint images.  It should be known that any fiels in the squint

directory will be attempted to be loaded as images, this is why an exception clause exists.

 

This function is a generic function for loading images for the left eye squint.  It does not need to be hard coded for the left eye as it can be used for all the face images.

So I will make it part of a Face Class.  This class will have this generic method of loading images given a path.

 

Here is the complete code for loading up all the components for the eyes and storing the images in a list that is part of a class.

 

import os
import pygame,sys
#define the face class, which is just used to keep all the images together that go together
class Face:
 def __init__(self,path):
  #load each component of the eyes using the generic image loading function
  self.leftEyeSquint = self.LoadImages(os.path.join(path, 'eye/left/squint/'))
  self.leftEyeBlink  = self.LoadImages(os.path.join(path , 'eye/left/blink/'))
  self.rightEyeSquint  = self.LoadImages(os.path.join(path , 'eye/right/squint/'))
  self.rightEyeBlink  = self.LoadImages(os.path.join(path , 'eye/right/blink/'))
  
 
 #a generic function for loading a set of images from a given directory
 def LoadImages(self,imagesPath):
  imageList = list()
  for dirname, dirnames, filenames in os.walk(imagesPath):
       for filename in sorted(filenames):
    try:
     imageList.append( pygame.image.load(os.path.join(dirname, filename)))
    except:
     pass
  return imageList

#Create a list of faces classes, this example only has 1 face, but multiple faces can be used
faces = list()
#load the default face 
faces.append(Face('./faces/default/'))

#test the class
print str(len(faces[0].leftEyeSquint)) + " Left squint images in class\n"
print str(len(faces[0].rightEyeSquint)) + " Right squint images in class\n"
  

 

 

The same pattern will be used for the mouth and sounds.  The sound function will call a different method of the pygame but the logic remains the same.  The class will be fully filled out and the a list of classes will be used in the main animation loop.

  • Sign in to reply

Top Comments

  • DAB
    DAB over 10 years ago +1
    Interesting design, I look forward to seeing your implementation. DAB
  • DAB
    DAB over 10 years ago

    Interesting design, I look forward to seeing your implementation.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube