How to Implement Face Detection in Web application in asp.net c#


Hello,
    Here is the web application to detect the face on the image .Face Detection is does by open CV (open source Computer vision).

    It is a library programming unit aimed at facial Recognition, motion detection.


The code that Modified from window application to web application.

To execute the application

Create Images Folder in the path of your website , place sample image as test.jpg .

Here is the code for aspx page
Add the following controls : file upload , Button,Image , HiddenField

Here is the code for aspx.cs page
public partial class _Default : System.Web.UI.Page
{
private string fileName;
private Guid imageId;
private Bitmap lastPic;
private clsFaceDetectionWrapper objFaceDetector = null;
private Pen myPen = new Pen(Color.Red, 2.0f);
Graphics picGraphics;
Graphics g;
private string filePath;
protected void Page_Load(object sender, EventArgs e)
{
objFaceDetector = clsFaceDetectionWrapper.GetSingletonInstance();
detectedImage.ImageUrl = Server.MapPath("./images/output.jpg");
}

void DrawRectangle(int lx, int ly, int rx, int ry)
{
g.DrawRectangle(myPen, lx, ly, rx - lx, ry - ly);
}
void DrawCross(int x, int y)
{
}

protected void detectButton_Click(object sender, EventArgs e)
{
imageId = new Guid();
imageId = Guid.NewGuid();
if (imageUpload.HasFile)
{
fileName = imageUpload.FileName.ToString().
Substring(imageUpload.FileName.ToString().LastIndexOf("."));
}
if (fileName.ToLower() == ".jpg" || fileName == ".gif" || fileName == ".jpeg" ||fileName == ".bmp" || fileName == ".png" || fileName == ".tif")
{
if (imageUpload.FileName != "")
{
pathHiddenField.Value = Server.MapPath("./images/" + imageId + "_" + imageUpload.FileName);
imageUpload.PostedFile.SaveAs(Server.MapPath("./images/" + imageId + "_" + imageUpload.FileName));
}
}
lastPic = new Bitmap(pathHiddenField.Value);
g = Graphics.FromImage(lastPic);
// here is the path to plave the cascade folder in the specified path.If Possible place the path cascade file in the execution path else modiy the face locatormethod and its coressponding dependabale method to get the path as arguments
String path = Server.MapPath("./XML/");
if (lastPic != null)
{
int faces = objFaceDetector.WrapDetectFaces(lastPic,path);

unsafe
{
int lx, ly, rx, ry, res;
for (int f = 0; f < faces; f++)
{
objFaceDetector.WrapGetFaceCordinates(f, &lx, &ly, &rx, &ry); g.DrawRectangle(myPen, lx, ly, rx, ry);
}
Response.ContentType = "image/jpeg";
lastPic.Save(Server.MapPath("./Images/Test.jpg"), ImageFormat.Jpeg); } detectedImage.ImageUrl = Server.MapPath("./Images/Test.jpg"); detectedImage.DataBind();
g.Save();
lastPic.Dispose();
g.Dispose();
}
}
Previous
Next Post »
Thanks for your comment