Skip to content

Instantly share code, notes, and snippets.

@jc4p
Created October 9, 2014 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jc4p/14c990d3652becfa4b9c to your computer and use it in GitHub Desktop.
Save jc4p/14c990d3652becfa4b9c to your computer and use it in GitHub Desktop.
Detect eyes, mouth, and ears from a camera.
from SimpleCV import Camera, Display, HaarCascade, Image, Color
cam = Camera()
display = Display()
cascadeEye = HaarCascade("eye")
cascadeLeftEar = HaarCascade("left_ear")
cascadeRightEar = HaarCascade("right_ear")
cascadeMouth = HaarCascade("mouth")
def process_eyes(image, eyes):
"""I stole this from https://github.com/sightmachine/SimpleCV/blob/master/SimpleCV/examples/detection/dealwithit.py
I have no idea why it works, but without it I get a lot of false negatives."""
dx, dy = eyes[-1].coordinates() - eyes[-2].coordinates()
if dx > 0:
right_eye = eyes[-2]
left_eye = eyes[-1]
else:
dx = -1*dx
right_eye = eyes[-1]
left_eye = eyes[-2]
if dx > image.width/15: #Reduces amount of wrong matches
return (left_eye, right_eye)
else:
return (None, None)
def find_real_mouth(mouths, red_blobs, white_blobs):
""" Given a list of HaarFeature-s for a mouth, find the candidate
that shares a bounding box with one of the reddest portions of the image (lips)
or one of the whitest portions of the image (teeth, persumably clean)"""
rBlobs = red_blobs[::-1]
if len(rBlobs) > 10:
rBlobs = rBlobs[:10]
wBlobs = white_blobs[::-1]
for mouth in mouths:
for b in rBlobs + wBlobs:
if b.overlaps(mouth):
return mouth
return None
while display.isNotDone():
img = cam.getImage().flipHorizontal().scale(0.5)
eyes = img.findHaarFeatures(cascadeEye) or []
leftEar = img.findHaarFeatures(cascadeLeftEar)
rightEar = img.findHaarFeatures(cascadeRightEar)
mouths = img.findHaarFeatures(cascadeMouth)
red_blobs = img.colorDistance(Color.RED).findBlobs()
white_blobs = img.findBlobs()
left_eye, right_eye = (None, None)
if eyes and len(eyes) >= 2:
left_eye, right_eye = process_eyes(img, eyes)
if left_eye:
# If left_eye is not None, so is right_eye
left_eye.draw(Color.RED)
right_eye.draw(Color.RED)
if mouths:
mouth = find_real_mouth(mouths, red_blobs, white_blobs)
if mouth:
if left_eye and right_eye:
if mouth.below(left_eye) and mouth.below(right_eye):
mouth.draw(Color.YELLOW)
# Only works if camera is looking from the side
if leftEar:
leftEar.draw(Color.GREEN)
if rightEar:
rightEar.draw(Color.GREEN)
img.save(display)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment