Skip to content

Instantly share code, notes, and snippets.

@Michael0x2a
Created January 12, 2014 03:40
Show Gist options
  • Save Michael0x2a/8380492 to your computer and use it in GitHub Desktop.
Save Michael0x2a/8380492 to your computer and use it in GitHub Desktop.
import SimpleCV as scv
import pygame
import sys
BLACK = (0, 0, 0)
RED = (255, 0, 0)
SCALE = 0.25
cam = scv.Camera()
pygame.init()
screen = pygame.display.set_mode([800, 600])
screen.fill(BLACK)
def get_faces(img, scale, feature='face'):
features = img.scale(scale).findHaarFeatures(feature + '.xml')
output = []
if features is not None:
for feature in features:
scale = round(1 / scale)
x, y = feature.topLeftCorner()
output.append({
'height': feature.height() * scale,
'width': feature.width() * scale,
'top_left_x': x * scale,
'top_right_x': y * scale,
'center_x': feature.x * scale,
'center_y': feature.y * scale,
'full_feature': feature
})
return output
while True:
img = cam.getImage()
screen.fill(BLACK)
screen.blit(img.getPGSurface(), (0,0))
faces = get_faces(img, SCALE)
for face in faces:
pygame.draw.rect(
screen,
RED,
pygame.Rect(
face['top_left_x'],
face['top_right_x'],
face['width'],
face['height']),
5)
pygame.display.flip()
event = pygame.event.poll()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment