Flash AS3 face detection and recognition library

Today I am showing you a nice AS3 library which allows face detection and recognition in a photo. I already blogged about a similar topic in PHP face detection class post, but this time we’ll work with AS3 and face recognition.

You may say: is there a difference between face detection and face recognition? Or are you just using these words randomly?

There is a slight difference, let me explain:

Face detection looks for some features which distinguish an human face from the rest of stuff in a photograpy: usually when we have a pair of eyes, nose, and a mouth we may say we are in front of a face (ok, I made it a bit simpler than it really is, but this is the concept behind it). Let’s think about the Facebook tag option when you upload a new photo.

Face recognition looks for A SPECIFIC face in a photo, just like cameras in an airport looking for some most wanted terrorist faces. With some tweakings, face recognition can be used as face detection.

Oskar Wicha’s ActionScript 3 library allow us to do both things using Eigenfaces concept, basically some patterns which should match a human face.

The following script wants the path of an image in the input text at the bottom of the stage and once you click anywhere, it loads the image and places a tag on any face it recognizes.

package {
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.events.MouseEvent;
	// classes to import
	import com.oskarwicha.images.FaceDetection.FaceDetector;
	import com.oskarwicha.images.FaceDetection.Events.FaceDetectorEvent;
	public class Main extends Sprite {
		// face detector constructor
		private var detector:FaceDetector;
		private var imageCanvas:Sprite=new Sprite();
		private var tagCanvas:Sprite=new Sprite();
		private var urlText:TextField = new TextField();
		public function Main() {
			addChild(imageCanvas);
			addChild(tagCanvas);
			urlText.type=TextFieldType.INPUT;
			urlText.border=true;
			urlText.background=true;
			urlText.backgroundColor=0xFFFFFF;
			urlText.x=10;
			urlText.y=450;
			urlText.width=620;
			urlText.height=20;
			urlText.text="http://i1.tribune.com.pk/wp-content/uploads/2011/06/twilight-640x480.jpg";
			addChild(urlText);
			stage.addEventListener(MouseEvent.CLICK,loadImage);
			loadImage(null);
		}
		private function loadImage(e:MouseEvent):void {
			while (imageCanvas.numChildren>0) {
				imageCanvas.removeChildAt(0);
			}
			tagCanvas.graphics.clear();
			var imageLoader:Loader = new Loader();
			var loader:Loader = new Loader();
			loader.load(new URLRequest(urlText.text));
			imageCanvas.addChild(loader);
			detector=new FaceDetector();
			// loading face image from an url. use loadFaceImageFromBitmap if you want to load it from a bitmap  
			detector.loadFaceImageFromUrl(urlText.text);
			// listener which will be called each time a face will be detected.;
			// with EACH TIME I mean if two faces are detected, callback function will be called twice
			detector.addEventListener(FaceDetectorEvent.FACE_CROPPED,facesDetected);
			// listener which will be called when no faces are detected;
			detector.addEventListener(FaceDetectorEvent.NO_FACES_DETECTED,noFacesDetected);
		}
		private function facesDetected(e:FaceDetectorEvent):void {
			imageCanvas.alpha=1;
			// faces detected are stored in an array of rectangles
			var facesDetected:Array=detector.objectDetector.detected;
			for (var i:Number=0; i

And you will get something like that:

It's just an image because I cannot load external image due to cross domain policy, but try it by yourself, just enter an url and click with the mouse anywhere we running your Flash IDE

Remember that face detection and recognition could be no that accurate, so in some cases it will miss faces, in some other cases it will detect false positive faces.

Download the source code with the library and a zipped file with Eigenfaces data which must be placed in the same folder of your main file. Also, you'll probably have to customize the path to your FaceRecognitionLib.swc library.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers