Created
March 25, 2014 20:42
-
-
Save geoffreysmith/9770887 to your computer and use it in GitHub Desktop.
sitecore facial recognition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MediaRequestOverride : MediaProvider | |
{ | |
public override Media GetMedia(MediaUri mediaUri) | |
{ | |
var mediaData = GetMediaData(mediaUri); | |
var media = GetMedia(mediaData); | |
return GetMediaFace(media); | |
} | |
public override Media GetMedia(MediaItem item) | |
{ | |
var mediaData = GetMediaData(item); | |
var media = GetMedia(mediaData); | |
return GetMediaFace(media); | |
} | |
public Media GetMediaFace(Media media) | |
{ | |
using (new SecurityDisabler()) | |
{ | |
var stream = media.GetStream(); | |
var resultingBitmap = DetectFact(Image.FromStream(stream.Stream)); | |
if (resultingBitmap == null) | |
return media; | |
var memoryStream = new MemoryStream(); | |
resultingBitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg); | |
media.SetStream(memoryStream, "jpeg"); | |
} | |
return media; | |
} | |
private Bitmap DetectFact(Image imageFromBitmap) | |
{ | |
var imageBitmap = new Bitmap(imageFromBitmap); | |
var image = new Image<Bgr, byte>(imageBitmap); | |
var gray = image.Convert<Gray, Byte>(); | |
var face = new HaarCascade("haarcascade_frontalface_default.xml"); | |
var facesDetected = gray.DetectHaarCascade(face, 1.1, 4, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20)); | |
Graphics g= null; | |
foreach (var f in facesDetected[0]) | |
{ | |
using (g = Graphics.FromImage(imageBitmap)) | |
{ | |
g.DrawRectangle(new Pen(Color.Red, 5), f.rect); | |
g.Save(); | |
} | |
break; | |
} | |
if (g != null) | |
return imageBitmap; | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment