Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.12 KB | None | 0 0
  1. [Authorize]
  2.         [HttpPost("UploadSingleFile")]
  3.         public async Task<string> Post(IFormFile photo, [FromHeader] string classId)
  4.         {
  5.             //string classId = "c333";
  6.             if (photo == null)
  7.             {
  8.                 return "No File Uploaded";
  9.             }
  10.             try
  11.             {
  12.                 string fname = DoPhotoUpload(photo);
  13.                 string fullpath = Path.Combine(_env.WebRootPath, "photos/" + fname);
  14.  
  15.                 string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  16.                 Face[] faces = await UploadAndDetectFaces(fullpath);
  17.  
  18.                 int faceId = 0;
  19.                 if (faces.Length != 0)
  20.                 {
  21.                     var webPath = hostName + "/photos/" + fname;
  22.  
  23.                     string sql = @"INSERT INTO Image (ImageName, ImagePath,uploadTime, classId) VALUES
  24.                        ('{0}','{1}','{2:yyyy-MM-dd HH:mm:ss}','{3}')";
  25.                     if (DBUtl.ExecSQL(sql, fname, webPath, date, classId) == 1)
  26.                     {
  27.                         //await faceServiceClient.CreatePersonGroupAsync(classId, classId);
  28.  
  29.                         foreach (var face in faces)
  30.                         {
  31.  
  32.                             Guid[] faceID = new Guid[1];
  33.                             faceID[0] = face.FaceId;
  34.                             string debug = face.FaceId.ToString();
  35.  
  36.                             var results = await faceServiceClient.IdentifyAsync(classId, faceID);
  37.                             foreach (var identifyResult in results)
  38.                             {
  39.                                 if (identifyResult.Candidates.Length != 0)
  40.                                 {
  41.                                     var candidateId = identifyResult.Candidates[0].PersonId;
  42.                                     var person = await faceServiceClient.GetPersonAsync(classId, candidateId);
  43.                                     faceId = System.Convert.ToInt32(person.Name);
  44.                                 }
  45.                             }
  46.                             //store all the emotion data into different data
  47.                             var Happiness = face.FaceAttributes.Emotion.Happiness;
  48.                             var Sadness = face.FaceAttributes.Emotion.Sadness;
  49.                             var Surprise = face.FaceAttributes.Emotion.Surprise;
  50.                             var Fear = face.FaceAttributes.Emotion.Fear;
  51.                             var Anger = face.FaceAttributes.Emotion.Anger;
  52.                             var Contempt = face.FaceAttributes.Emotion.Contempt;
  53.                             var Disgust = face.FaceAttributes.Emotion.Disgust;
  54.                             var Neutral = face.FaceAttributes.Emotion.Neutral;
  55.                             var FaceID = face.FaceId;
  56.                             var Age = face.FaceAttributes.Age;
  57.                             var Gender = face.FaceAttributes.Gender;
  58.                             string sql1 = @"INSERT INTO Face(FaceId, studentId, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, ImageName)
  59.                               VALUES('{0}',{1},{2},{3},{4},{5},{6},{7},{8},{9},'{10}')";
  60.                             string sql2 = @"INSERT INTO Face(FaceId, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, ImageName)
  61.                               VALUES('{0}',{1},{2},{3},{4},{5},{6},{7},{8},'{9}')";
  62.                             int result = 0;
  63.                             if (faceId == 0)
  64.                             {
  65.                                 string debug22 = string.Format(sql2, FaceID, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, fname);
  66.                                 result = DBUtl.ExecSQL(sql2, FaceID, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, fname);
  67.  
  68.                             }
  69.                             else
  70.                             {
  71.                                 string debug11 = string.Format(sql1, FaceID, faceId, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, fname);
  72.                                 result = DBUtl.ExecSQL(sql1, FaceID, faceId, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, fname);
  73.                             }
  74.  
  75.  
  76.                             if (result != 1)
  77.  
  78.                             {
  79.                                 return "insert lesson fail" + DBUtl.DB_Message + String.Format(sql1, FaceID, faceId, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, fname);
  80.                             }
  81.                             returnValue rv = new returnValue(fname, faceId);
  82.                             string value = JsonConvert.SerializeObject(rv);
  83.                             if (faceId != 0)
  84.                             {
  85.                                 markAttendance(faceId, classId);
  86.                             }
  87.  
  88.                         }
  89.                         if (faces.Length > 1)
  90.                         {
  91.                             return "Multi Entry";
  92.  
  93.                         }
  94.                         else
  95.                         {
  96.                             return faceId.ToString();
  97.                         }
  98.                     }
  99.                     else
  100.                     {
  101.                         return "insert image fail" + DBUtl.DB_Message + String.Format(sql, fname, fullpath, date, classId);
  102.                     }
  103.  
  104.                 }
  105.                 else
  106.                 {
  107.                     return "No face detected!";
  108.                 }
  109.  
  110.             }
  111.             catch (Exception ex)
  112.             {
  113.                 return ex.Message;
  114.             }
  115.         }
  116.  
  117.         private string DoPhotoUpload(IFormFile photo)
  118.         {
  119.             string fext = ".jpg";
  120.             string uname = Guid.NewGuid().ToString();
  121.             string fname = uname + fext;
  122.             string fullpath = Path.Combine(_env.WebRootPath, "photos/" + fname);
  123.             using (FileStream fs = new FileStream(fullpath, FileMode.Create))
  124.             {
  125.                 photo.CopyTo(fs);
  126.             }
  127.             return fname;
  128.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement