Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Authorize]
- [HttpPost("UploadSingleFile")]
- public async Task<string> Post(IFormFile photo, [FromHeader] string classId)
- {
- //string classId = "c333";
- if (photo == null)
- {
- return "No File Uploaded";
- }
- try
- {
- string fname = DoPhotoUpload(photo);
- string fullpath = Path.Combine(_env.WebRootPath, "photos/" + fname);
- string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- Face[] faces = await UploadAndDetectFaces(fullpath);
- int faceId = 0;
- if (faces.Length != 0)
- {
- var webPath = hostName + "/photos/" + fname;
- string sql = @"INSERT INTO Image (ImageName, ImagePath,uploadTime, classId) VALUES
- ('{0}','{1}','{2:yyyy-MM-dd HH:mm:ss}','{3}')";
- if (DBUtl.ExecSQL(sql, fname, webPath, date, classId) == 1)
- {
- //await faceServiceClient.CreatePersonGroupAsync(classId, classId);
- foreach (var face in faces)
- {
- Guid[] faceID = new Guid[1];
- faceID[0] = face.FaceId;
- string debug = face.FaceId.ToString();
- var results = await faceServiceClient.IdentifyAsync(classId, faceID);
- foreach (var identifyResult in results)
- {
- if (identifyResult.Candidates.Length != 0)
- {
- var candidateId = identifyResult.Candidates[0].PersonId;
- var person = await faceServiceClient.GetPersonAsync(classId, candidateId);
- faceId = System.Convert.ToInt32(person.Name);
- }
- }
- //store all the emotion data into different data
- var Happiness = face.FaceAttributes.Emotion.Happiness;
- var Sadness = face.FaceAttributes.Emotion.Sadness;
- var Surprise = face.FaceAttributes.Emotion.Surprise;
- var Fear = face.FaceAttributes.Emotion.Fear;
- var Anger = face.FaceAttributes.Emotion.Anger;
- var Contempt = face.FaceAttributes.Emotion.Contempt;
- var Disgust = face.FaceAttributes.Emotion.Disgust;
- var Neutral = face.FaceAttributes.Emotion.Neutral;
- var FaceID = face.FaceId;
- var Age = face.FaceAttributes.Age;
- var Gender = face.FaceAttributes.Gender;
- string sql1 = @"INSERT INTO Face(FaceId, studentId, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, ImageName)
- VALUES('{0}',{1},{2},{3},{4},{5},{6},{7},{8},{9},'{10}')";
- string sql2 = @"INSERT INTO Face(FaceId, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, ImageName)
- VALUES('{0}',{1},{2},{3},{4},{5},{6},{7},{8},'{9}')";
- int result = 0;
- if (faceId == 0)
- {
- string debug22 = string.Format(sql2, FaceID, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, fname);
- result = DBUtl.ExecSQL(sql2, FaceID, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, fname);
- }
- else
- {
- string debug11 = string.Format(sql1, FaceID, faceId, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, fname);
- result = DBUtl.ExecSQL(sql1, FaceID, faceId, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, fname);
- }
- if (result != 1)
- {
- return "insert lesson fail" + DBUtl.DB_Message + String.Format(sql1, FaceID, faceId, Anger, Contempt, Disgust, Fear, Happiness, Neutral, Sadness, Surprise, fname);
- }
- returnValue rv = new returnValue(fname, faceId);
- string value = JsonConvert.SerializeObject(rv);
- if (faceId != 0)
- {
- markAttendance(faceId, classId);
- }
- }
- if (faces.Length > 1)
- {
- return "Multi Entry";
- }
- else
- {
- return faceId.ToString();
- }
- }
- else
- {
- return "insert image fail" + DBUtl.DB_Message + String.Format(sql, fname, fullpath, date, classId);
- }
- }
- else
- {
- return "No face detected!";
- }
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- private string DoPhotoUpload(IFormFile photo)
- {
- string fext = ".jpg";
- string uname = Guid.NewGuid().ToString();
- string fname = uname + fext;
- string fullpath = Path.Combine(_env.WebRootPath, "photos/" + fname);
- using (FileStream fs = new FileStream(fullpath, FileMode.Create))
- {
- photo.CopyTo(fs);
- }
- return fname;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement