Simple Face Detection for Windows Phone (5/5)

Simple Face Detection for Windows Phone (5/5)

Simple Face Detection for Windows Phone (5/5)

To explain simple face detection, I wrote 5 posts with each another technique in the face detection process. When you combine all 5 steps, you will be able to detect faces in pictures or video streams.

The steps described in these posts are:

In this final stage we still have a little too much items. Therefore we can filter out some more items..

First we can filter out the items that are too small. You can let this append on the size of the total picture, or just a hard value as we can’t do anything with a face that is 10 pixels high.

Then we can filter out the parts that are a lot smaller than others.When we have some clear faces on the foreground, we don’t need much smaller ones far in the background.

We can filter out wrong proportions as well. When a face is twice as wide as its height, it probably isn’t a face or something went wrong.. However, this can give issues with overlapping faces.

After this we should be done! We’ve got bounding boxes that should cover the faces in the picture. The last thing is just to draw it! Prefer to draw it over your original picture of course! :)

public static void FilterOutSmallBoxes(Collection<BoundingBox> boundingBoxes, int minWidth, int minHeight)
{
    for (int i = boundingBoxes.Count - 1; i >= 0; i--)
		if (boundingBoxes[i].Width < minWidth || boundingBoxes[i].Height < minHeight)
			boundingBoxes.Remove(boundingBoxes[i]);
}

public static void FilterOutSmallerBoxes(Collection<BoundingBox> boundingBoxes, double minCompare = 0.5)
{
	int largest = 0;
	for (int l = boundingBoxes.Count - 1; l >= 0; l--)
		if (largest < boundingBoxes[l].Width * boundingBoxes[l].Height)
			largest = boundingBoxes[l].Width * boundingBoxes[l].Height;

	for (int i = boundingBoxes.Count - 1; i >= 0; i--)
		if (boundingBoxes[i].Width * boundingBoxes[i].Height / minCompare < largest)
			boundingBoxes.Remove(boundingBoxes[i]);
}

public static void FilterOutWrongProportions(Collection<BoundingBox> boundingBoxes, double minWidthProportion = 1, double minHeightProportion = 2, double maxWidthProportion = 1, double maxHeightProportion = 0.5)
{
    for (int i = boundingBoxes.Count - 1; i >= 0; i--)
    	if (!((double)(boundingBoxes[i].Width) / (double)(boundingBoxes[i].Height) > (double)(minWidthProportion) / (double)(minHeightProportion) &&
			(double)(boundingBoxes[i].Width) / (double)(boundingBoxes[i].Height) < (double)(maxWidthProportion) / (double)(maxHeightProportion)))
			boundingBoxes.Remove(boundingBoxes[i]);
}

public static void DrawRectangles(int[] pixels, int width, int height, Collection<BoundingBox> boundingBoxes, int color = 0xFF << 24 | 0xFF << 16)
{
	foreach (BoundingBox box in boundingBoxes)
	{
		if (box.Width > 1 && box.Height > 1)
		{
			for (int x = 0; x < box.Width; x++)
			{
				//Horizontal lines
				pixels[box.Y * width + box.X + x] = color;
				pixels[(box.Y + box.Height) * width + box.X + x] = color;
			}
			for (int y = 0; y < box.Height; y++)
			{
				//Vertical lines
				pixels[(box.Y + y) * width + box.X] = color;
				pixels[(box.Y + y) * width + box.X + box.Width] = color;
			}
		}
	}
}
Bounding boxes drawn in the modified picture

Bounding boxes drawn in the modified picture

Bounding boxes drawn on the original picture

Bounding boxes drawn on the original picture

Download Download the whole demo project

4 thoughts on “Simple Face Detection for Windows Phone (5/5)”

Leave a Reply

Your email address will not be published. Required fields are marked *