2017-02-01 2 views
0

Я делаю веб-проект в .net для проверки подлинности лица, когда лицо подбирается, оба изображения (захваченные и сопоставленные) должны отображаться в div и вместе с тем должны быть показатель доверия для заполнения.сеанс с моей страницей

Основываясь на уверенном счете, следует сделать аутентификацию.

В настоящее время моя проблема заключается в том, что аутентификация выполнена нормально, но изображения отображаются пустым, а также показатель доверия отображается пустым. Мне нужно вставить изображения и оценку доверия на мою веб-страницу.

Ниже мой CSHTML

@section scripts 
{ 
    <script src="~/Scripts/blockUI.js"></script> 
    <script src="@Url.Content("~/Scripts/jquery.webcam.js")"> 
    </script> 

    <script> 
     function MatchFindFunction() { 
      $.ajax({ 
       type: "POST", 
       url: "/Image/FindSimilarImages", //Call the function FindSimilarImages present in the ImageController 
       contentType: "application/text; charset=utf-8", 
       dataType: "text", 
       success: function (data) { 
        if (data == "MATCHFOUND") { 


         $('#imageDisplay').append('<table border="1" cellspacing="1" cellpadding="4"><tr><th>Captured Image</th><th>Similar Image</th></tr><tr><td><img id="showsec" style="width:150px;height:150px;margin-top:5px;" src="@Session["CapturedImageData"]" /></td><td><img id="show" style="width:150px;height:150px;margin-top:5px;" src="@Session["ImageData"]" /></td></tr></table><table border="1" cellspacing="0" cellpadding="4" id="tblConfidence"><tr><td><label style="color:green;" id="Confidence">Confidence Score:@Session["ConfidenceScore"]</label></td></tr></table>'); 
         setTimeout(function() { 
          setTimeout(function() { 
           $.blockUI({ message: '<h6> You are registered with, Welcome back. You will see the chatbot screen shortly. </h6>' }); 
           window.location.href = '@Url.Action("ChatBot", "Home")'; 
          }, 
           7000); 
         }, 5000); 
        } 
        else if (data == "NOMATCHFOUND") { 
         $.blockUI({ message: '<h6> You are yet to register yourself with us, can you please proceed with register? </h6>' }); 
         setTimeout(function() { 
          window.location.href = '@Url.Action("Register", "Home")'; 
         }, 7000); 
        } 
        else { 
         //$('#ErrorMessage').show(); 
         //$("body").scrollTop(0); 
         $.blockUI({ message: '<h6> You are yet to register yourself with us, can you please proceed with register? </h6>' }); 
         setTimeout(function() { 
          window.location.href = '@Url.Action("Register", "Home")'; 
         }, 7000); 
        } 

       } 
      }); 
     } 

    </script> 
    <script> 

     $("#DivCamera").webcam({ //Display the camera in DivCamera division 
      width: 450, 
      height: 350, 
      mode: "save", 
      swffile: "@Url.Content("~/Scripts/jscam.swf")", 
      onTick: function() { }, 
      onSave: function() { 


      }, 
      onCapture: function() { 
       webcam.save("@Url.Content("~/Image/ImageCapture")/"); 
       MatchFindFunction(); 
      }, 
      debug: function() { }, 
      onLoad: function() { } 
     }); 
    </script> 
} 

<div> 
    <label style="display:none;margin-left:500px; color:red;" id="ErrorMessage">Face API Subscription key Expired</label> 
</div> 
<div style="width:330px;height:420px; margin-top:20px;border: 4px solid #ccc; padding: 5px;float:left; margin-right:10px;"> 
</div> 
<div id="DivCameraCapture" style="width:300px;margin-top:20px;margin-left:0px;float:left;margin-right:10px;"> 
    <table style="border: 4px solid #ccc; padding: 5px; "> 
     <tr> 
      <td> 
       <label for="Livecamera" style="font-size:15px;color:black;margin-left:165px;" id="LblLivCamera">Live Camera</label> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <div id="DivCamera"></div> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <input type="submit" value="Login" class="btn-block" style="margin-left:90px;" onclick="webcam.capture();" /> 
      </td> 
     </tr> 
    </table> 
</div> 
<div id="imageDisplay" style="width:330px;height:420px;margin-top:20px;border: 4px solid #ccc; padding: 5px;float:right;"> 

</div> 

и мой ImageController.cs (ответственный установить значение сеанса), как показано ниже.

using System.IO; 
using System; 
using System.Collections.Generic; 
using System.Web.Mvc; 
using System.Threading.Tasks; 
using Microsoft.ProjectOxford.Face; 
using System.Net.Http; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Web; 
using System.Collections.Specialized; 
using System.Runtime.CompilerServices; 
using System.Linq; 
//using Microsoft.ProjectOxford.Face.Contract; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Collections; 
using System.Web.UI.WebControls; 
using System.Text; 
using System.Web.UI; 
using System.Runtime.InteropServices; 
using System.Security; 
using System.Security.AccessControl; 
using System.Threading; 
using System.Configuration; 
using System.Configuration.Provider; 
using ComplianceBot.Models; 

namespace ComplianceBot.Controllers 
{ 
    public class ImageController : Controller 
    { 
     CommonModels CommonModel = new CommonModels(); 
     UserModels Usermodel = new UserModels(); 
     public ActionResult Index() 
     { 
      return View(); 
     } 
     public void ImageCapture() //Capturing the image and save as test.jpg 
     { 
      CommonModel.stream = Request.InputStream; 
      using (CommonModel.reader = new StreamReader(CommonModel.stream)) 
       CommonModel.dump = CommonModel.reader.ReadToEnd(); 
      CommonModel.path = Server.MapPath("~/test.jpg"); 

      System.IO.File.WriteAllBytes(CommonModel.path, ConvertStringToByte(CommonModel.dump)); 
      //Session["testURL"] = CommonModel.path; 


     } 
     private byte[] ConvertStringToByte(string strInput) //To convert the captured image to byte array 
     { 
      CommonModel.numBytes = (strInput.Length)/2; 
      CommonModel.bytes = new byte[CommonModel.numBytes]; 
      for (CommonModel.count = 0; CommonModel.count < CommonModel.numBytes; ++CommonModel.count) 
      { 
       CommonModel.bytes[CommonModel.count] = Convert.ToByte(strInput.Substring(CommonModel.count * 2, 2), 16); 
      } 
      return CommonModel.bytes; 
     } 
     public async Task<string> FindSimilarImages() //Compare the captured image with already strored images in the Image folder 
     { 
      CommonModel.MatchedImgcount = 0; 
      CommonModel.CapturedImgName = Server.MapPath("~/test.jpg"); 

      byte[] capturedImageByteData = System.IO.File.ReadAllBytes(CommonModel.CapturedImgName); 
      string capturedImageBase64Data = Convert.ToBase64String(capturedImageByteData); 
      string capturedImageDataURL = string.Format("data:image/png;base64,{0}", capturedImageBase64Data); 
      Session["CapturedImageData"] = capturedImageDataURL; 

      CommonModel.DirInfo = new DirectoryInfo(Server.MapPath("~/Image/")); 

      if (!Directory.Exists(Server.MapPath("~/Image"))) 
      { 
       Directory.CreateDirectory(Server.MapPath("~/Image")); 
      } 


      CommonModel.faceListName = Guid.NewGuid().ToString(); // Generating a Unique-ID 
      var faceServiceClients = new FaceServiceClient(CommonModel.FaceAPI_subscriptionKeyValue); //calling the Face API by passing subscription key 
      try 
      { 
       await faceServiceClients.CreateFaceListAsync(CommonModel.faceListName, CommonModel.faceListName, "face_Images"); //Calling the API service'CreateFaceListAsync' to create a facelist with Unique-ID. 
      } 

      catch (FaceAPIException ex) //If Subscription Key Expired 
      { 
       CommonModel.Errormesg = ex.ErrorMessage; 
       CommonModel.ErrorCode = CommonModel.ERRCODE_LIMITEXPIRED; 
       return CommonModel.ErrorCode; 
      } 

      CommonModel.DirInfo = new DirectoryInfo(Server.MapPath("~/Image/")); 
      Dictionary<string, string> DictionaryListofPersistanceIDAndImagePath = new Dictionary<string, string>(); 
      try 
      { 

       foreach (var file in CommonModel.DirInfo.GetFiles("*.jpg")) 
       { 
        string imgPath = Server.MapPath("~/Image/") + file.ToString(); 
        CommonModel.fStream = new FileStream(imgPath, FileMode.Open, FileAccess.Read); 
        var faces = await faceServiceClients.AddFaceToFaceListAsync(CommonModel.faceListName, CommonModel.fStream); //Adding of each jpg image content to the created facelist in the Face API using the service 'AddFaceToFaceListAsync' 
        DictionaryListofPersistanceIDAndImagePath.Add(faces.PersistedFaceId.ToString(), imgPath);//Storing the PersistedFaceId of the image returned by the Face API service and image path in dictionary 

       } 
      } 

      catch (FaceAPIException ex) //If morethan one face found while capturing the image 
      { 
       CommonModel.Errormesg = ex.ErrorMessage; 
       CommonModel.ErrorCode = CommonModel.ERRCODE_MORETHANONEFACEFOUND; 
       return ex.ErrorMessage; 


      } 

      CommonModel.CapturedImgName = Server.MapPath("~/test.jpg"); 
      using (CommonModel.fileStream = System.IO.File.OpenRead(CommonModel.CapturedImgName)) 
      { 
       var faceServiceClient = new FaceServiceClient(CommonModel.FaceAPI_subscriptionKeyValue); 
       var faces = await faceServiceClient.DetectAsync(CommonModel.fileStream); //Calling the Face API 'DetectAsync' to detect the captured image by sending the content of the captured image 

       foreach (var f in faces) //Loop through the returned faceid of the image 
       { 
        CommonModel.faceId = f.FaceId; 
        try 
        { 
         var result = await faceServiceClient.FindSimilarAsync(CommonModel.faceId, CommonModel.faceListName, CommonModel.requestCandidatesCount); // Matching the captured image with images by sending faceId and faceListName to the Face API 'FindSimilarAsync' 
                                           //The variable result contains the matched image's PersistedFaceId 
         CommonModel.MatchedImgpath = new string[CommonModel.requestCandidatesCount]; //Declare an array with size 'requestCandidatesCount' to store the matched images path 

         foreach (var fr in result) //Loop through the PersistedFaceId of matched faces 
         { 
          if (fr.Confidence >= 0.8) //To check whether the confidence value of the matched image is >=0.8 
          { 
           Session["ConfidenceScore"] = fr.Confidence; 
           if (DictionaryListofPersistanceIDAndImagePath.ContainsKey(fr.PersistedFaceId.ToString()))//To check whether the Persistance id is present in the dictionary 
                                 //if present retrive the curresponding image-path of that PersistedFaceId. 
           { 
            CommonModel.MatchedImgpath[CommonModel.MatchedImgcount] = DictionaryListofPersistanceIDAndImagePath[fr.PersistedFaceId.ToString()]; //Store the image-path in an array.This array contains all the matched image path which have confidence-value >=0.8 
            CommonModel.MatchedImgcount = CommonModel.MatchedImgcount + 1; 
            CommonModel.path = DictionaryListofPersistanceIDAndImagePath[fr.PersistedFaceId.ToString()]; 
            byte[] imageByteData = System.IO.File.ReadAllBytes(CommonModel.path); 
            string imageBase64Data = Convert.ToBase64String(imageByteData); 
            string imageDataURL = string.Format("data:image/png;base64,{0}", imageBase64Data); 
            Session["ImageData"] = imageDataURL; 
            Usermodel.LoggedUsername = CommonModel.path.Substring(CommonModel.path.LastIndexOf("\\"), CommonModel.path.Length - CommonModel.path.LastIndexOf("\\")); //retrive username from the matched imagename 
            Usermodel.LoggedUsername = Usermodel.LoggedUsername.Remove(0, 1); 
            Usermodel.LoggedUsername = Path.GetFileNameWithoutExtension(Usermodel.LoggedUsername); 
            Session["loggedUserName"] = Usermodel.LoggedUsername.ToString(); 
           } 
          } 

         } 
        } 
        catch (FaceAPIException ex) 
        { 
         CommonModel.Errormesg = ex.ErrorMessage;//If Image Folder contains zero images 
         CommonModel.ErrorCode = CommonModel.EMPTYIMAGEFOLDER; 
         return ex.ErrorMessage; 

        } 
       } 
      } 
      if (CommonModel.MatchedImgcount != 0) 
      { 
       return CommonModel.MATCHFOUND; 

      } 
      else 
      { 
       return CommonModel.NOMATCHFOUND; 

      } 
     } 
    } 
} 

, пожалуйста, дайте мне знать, где я ошибаюсь, получая данные сеанса, я имею в виду изображения и оценки. Опять же, аутентификация работает нормально, мне просто нужно получить эти данные.

Благодаря

+0

Какова ценность 'src' после отображения пустого изображения? –

+0

@SiamakFerdos, он ничего не показывает, это должно быть идеально захвачено из сеанса. когда у меня есть предупреждение внутри 'success', оно запускается правильно. Кроме того, я обновил свой вопрос abit – user3872094

+0

Если код запускается успешно, вы можете легко отлаживать его с помощью инспектора браузера и проверять данные успеха функции успеха, а также фиксировать значение сеанса, если они правильно установлены в код. –

ответ

1

вызов AJAX для FindSimilarImages будет установить переменные сеанса на стороне сервера, но это не имеет никакого эффекта на стороне клиента AJAX success функции, в которой вы пытаетесь использовать эти значения. Функция success отображается в браузере только один раз, перед вызовом FindSimilarImages, и поэтому значения сеанса, используемые во время рендеринга, будут использоваться до тех пор, пока страница не будет перезагружена.

решения будет иметь FindSimilarImages возвращать JSON структуру данных, которая не только содержит «MATCHFOUND», но и URL изображения и партитуры, а затем использовать оба из data объекта внутри функций success.

+0

Спасибо за предложение. Не могли бы вы дать мне знать, как я могу это сделать? – user3872094

+0

К сожалению, я не могу сделать это прямо сейчас, но если вы выполните поиск 'mvc jsonresult', вы найдете много информации, примеров и руководств. –

+0

Это было бы здорово. Благодаря :) – user3872094