Skip to content

Instantly share code, notes, and snippets.

@mike-lawrence
Created January 5, 2012 13:56
Show Gist options
  • Save mike-lawrence/1565358 to your computer and use it in GitHub Desktop.
Save mike-lawrence/1565358 to your computer and use it in GitHub Desktop.
Pygame & SimpleCV crashing/segfaulting
window_res = (800,600)
detection_image_scale = 4 #faster & more reliable to shrink the image before face detection
pupil_threshold = 240
cam_scale = 1
import pygame
import time
import sys
import cv
import SimpleCV
pygame.init() #initialize pygame
pygame.mouse.set_visible(False) #make the mouse invisible
window = pygame.display.set_mode(window_res) #initialize a window
window_x_center = window_res[0]/2 #store the location of the window's x center
window_y_center = window_res[1]/2 #store the location of the window's y center
this_font = pygame.font.Font(pygame.font.get_default_font(),20)
blink_text = this_font.render('Blink!',True,(255,0,0),(0,0,0))
noblink_text = this_font.render('Blink!',True,(0,0,0),(0,0,0))
def blit_to_window(surf,x_offset=0,y_offset=0):
x = window_x_center+x_offset-surf.get_width()/2.0
y = 0
window.blit(surf,(x,y))
face_cascade = cv.Load('haarcascade_frontalface_alt2.xml')
eyeL_cascade = cv.Load('LEye18x12.1.xml')
eyeR_cascade = cv.Load('REye18x12.1.xml')
bm = SimpleCV.BlobMaker()
camera_stream = SimpleCV.Camera(0)
eyes_found = False
while not eyes_found:
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
response = event.unicode
if response=='\x1b':
pygame.quit()
sys.exit()
raw_image = camera_stream.getImage()
start = time.time()
detection_image = raw_image.scale(int(raw_image.width/detection_image_scale), int(raw_image.height/detection_image_scale))
faces = detection_image.findHaarFeatures(face_cascade)
print ['face detection time:',time.time()-start]
if not faces:
print 'no faces'
elif len(faces)>1:
print 'too many faces'
else:
face_x = faces.x()[0]*detection_image_scale
face_y = faces.y()[0]*detection_image_scale
face_width = faces.width()[0]*detection_image_scale
face_height = faces.height()[0]*detection_image_scale
face_x1 = face_x-face_width/3
face_y1 = face_y-face_height/4
face_x2 = face_x+face_width/3
face_y2 = face_y
faceL_image = raw_image[face_x1:face_x,face_y1:face_y2]
start = time.time()
eyeL = faceL_image.findHaarFeatures(eyeL_cascade)
print ['eyeL detection time:',time.time()-start]
if eyeL:
eyeL[0].draw()
faceL_surf = faceL_image.applyLayers().toPygameSurface()
blit_to_window(faceL_surf,x_offset=-100)
faceR_image = raw_image[face_x:face_x2,face_y1:face_y2]
start = time.time()
eyeR = faceR_image.findHaarFeatures(eyeR_cascade)
print ['eyeR detection time:',time.time()-start]
if eyeR:
eyeR[0].draw()
faceR_surf = faceR_image.applyLayers().toPygameSurface()
blit_to_window(faceR_surf,x_offset=100)
eyeL_x = eyeL[0].x+face_x1
eyeL_y = eyeL[0].y+face_y1
eyeL_width = eyeL[0].width()
eyeL_height = eyeL[0].height()
eyeL_x1 = eyeL_x-eyeL_width/2
eyeL_y1 = eyeL_y-eyeL_height/2
eyeL_x2 = eyeL_x1+eyeL_width
eyeL_y2 = eyeL_y1+eyeL_height
eyeR_x = eyeR[0].x+face_x
eyeR_y = eyeR[0].y+face_y1
eyeR_width = eyeR[0].width()
eyeR_height = eyeR[0].height()
eyeR_x1 = eyeR_x-eyeR_width/2
eyeR_y1 = eyeR_y-eyeR_height/2
eyeR_x2 = eyeR_x1+eyeR_width
eyeR_y2 = eyeR_y1+eyeR_height
if (eyeL_x1+eyeL_width)<eyeR_x1:
if (eyeL_y1+eyeL_height)>eyeR_y1:
if (eyeR_y1+eyeR_height)>eyeL_y1:
eyes_found = True
else:
print 'error1'
else:
print 'error2'
else:
print 'error3'
pygame.display.flip()
if eyes_found:
done = False
while not done:
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
response = event.unicode
if response=='\x1b':
done = True
elif response==',':
pupil_threshold = pupil_threshold-1
elif response=='<':
pupil_threshold = pupil_threshold-10
elif response=='.':
pupil_threshold = pupil_threshold+1
elif response=='>':
pupil_threshold = pupil_threshold+10
blit_to_window(noblink_text)
start = time.time()
raw_image = camera_stream.getImage()
print 'cam time: '+str(time.time()-start)
eyeL = raw_image[eyeL_x1:eyeL_x2,eyeL_y1:eyeL_y2]
start = time.time()
eyeL_blobs = bm.extractFromBinary(eyeL.invert().binarize(thresh=pupil_threshold).invert(),eyeL)
print 'eyeL blob time: '+str(time.time()-start)
eyeR = raw_image[eyeR_x1:eyeR_x2,eyeR_y1:eyeR_y2]
start = time.time()
eyeR_blobs = bm.extractFromBinary(eyeR.invert().binarize(thresh=pupil_threshold).invert(),eyeR)
print 'eyeL blob time: '+str(time.time()-start)
if((len(eyeL_blobs)==0)&(len(eyeR_blobs)==0)):
blit_to_window(blink_text)
eyeL_surf = eyeL.applyLayers().toPygameSurface()
blit_to_window(eyeL_surf,x_offset=-eyeL_width)
eyeR_surf = eyeR.applyLayers().toPygameSurface()
blit_to_window(eyeR_surf,x_offset=eyeR_width)
else:
if(len(eyeL_blobs)>0):
eyeL_blobs[0].drawOutline()
eyeL_centroid = eyeL_blobs[0].centroid()
eyeL_surf = eyeL.applyLayers().toPygameSurface()
blit_to_window(eyeL_surf,x_offset=-eyeL_width)
pygame.gfxdraw.aacircle(window, int(window_x_center-eyeL_width*3/2+eyeL_centroid[0]), int(eyeL_centroid[1]), 2, (255,0,0))
if(len(eyeR_blobs)>0):
eyeR_blobs[0].drawOutline()
eyeR_centroid = eyeR_blobs[0].centroid()
eyeR_surf = eyeR.applyLayers().toPygameSurface()
blit_to_window(eyeR_surf,x_offset=eyeR_width)
pygame.gfxdraw.aacircle(window, int(window_x_center+eyeR_width/2+eyeR_centroid[0]), int(eyeR_centroid[1]), 2, (255,0,0))
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment