public async Task <IHttpActionResult> FindFacesFamily(CompareFacesFamily compareFacesFamily)
        {
            try
            {
                var faceDescription = new FaceDescription();
                var user            = await UserManager.FindByIdAsync(compareFacesFamily.userID);

                var resultsOfFacesDetected = await faceDescription.MakeAnalysisRequestAsync(Convert.FromBase64String(compareFacesFamily.familyPhoto));

                if (resultsOfFacesDetected.Count < 3)
                {
                    return(BadRequest(resultsOfFacesDetected.Count.ToString()));
                }

                return(Ok(resultsOfFacesDetected));
            }
            catch (Exception ex) {
                return(BadRequest("Error"));
            }
        }
示例#2
0
        public async Task <ActionResult> findFatherMotherChildren(HttpPostedFileBase familyTogether)
        {
            await saveLog();

            if (Session.Count == 0)
            {
                TempData["LogInError"] = "Session has ended. Please log in again.";
                return(RedirectToAction("Index", "LogIn"));
            }
            var httpClient = new HttpClient();

            try
            {
                User   user = (User)Session["userSession"];
                byte[] familyTogetherByte = new byte[familyTogether.ContentLength];
                await familyTogether.InputStream.ReadAsync(familyTogetherByte, 0, familyTogether.ContentLength);

                string familyToBase64 = Convert.ToBase64String(familyTogetherByte, 0, familyTogetherByte.Length);
                if (Session["FamilyCheckResult"] != null)
                {
                    var SessionData = (DisplayResultModelFamily)Session["FamilyCheckResult"];
                    if (SessionData.familyPhoto == familyToBase64)
                    {
                        return(View(SessionData));
                    }
                }
                var compareFamily = new CompareFacesFamily {
                    userId = user.userId, familyPhoto = familyToBase64
                };
                var content = new StringContent(JsonConvert.SerializeObject(compareFamily), Encoding.UTF8, "application/json");
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Session["Access_Token"].ToString());
                var responseApi = await httpClient.PostAsync(ConfigurationManager.AppSettings["url"] + "api/FaceRecognition/FindFacesFamily", content);

                var result = await responseApi.Content.ReadAsStringAsync();

                try
                {
                    var errorMessage = JsonConvert.DeserializeObject <Error>(result);
                    if (errorMessage.Message != null)
                    {
                        TempData["ChekcSimilarityError"] = errorMessage.Message;
                        return(RedirectToAction("CheckResultView"));
                    }
                }catch (Exception ex) {}

                if (responseApi.StatusCode == (HttpStatusCode)429)
                {
                    TempData["ChekcSimilarityError"] = "429";
                    return(RedirectToAction("CheckResultView"));
                }

                var cognitiveMicrosofts = JsonConvert.DeserializeObject <List <CognitiveMicrosoft> >(result);

                var checkSimilarityTogether = new CheckSimilarityTogether();
                checkSimilarityTogether.cognitiveMicrosoft = cognitiveMicrosofts;
                try
                {
                    var father   = cognitiveMicrosofts.Where(face => face.faceAttributes.gender.Equals("male")).OrderByDescending(face => face.faceAttributes.age).ToList()[0];
                    var mother   = cognitiveMicrosofts.Where(face => face.faceAttributes.gender.Equals("female")).OrderByDescending(face => face.faceAttributes.age).ToList()[0];
                    var children = cognitiveMicrosofts.OrderBy(face => face.faceAttributes.age).ToList()[0];

                    checkSimilarityTogether.fatherPositon    = cognitiveMicrosofts.ToList().IndexOf(father);
                    checkSimilarityTogether.motherPosition   = cognitiveMicrosofts.ToList().IndexOf(mother);
                    checkSimilarityTogether.childrenPosition = cognitiveMicrosofts.ToList().IndexOf(children);
                }catch (Exception ex) { }
                Session["FamilyData"] = new DisplayResultModelFamily {
                    checkSimilarityTogether = checkSimilarityTogether, familyPhoto = familyToBase64
                };
                return(View(new DisplayResultModelFamily {
                    checkSimilarityTogether = checkSimilarityTogether, familyPhoto = familyToBase64
                }));
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
示例#3
0
        public async Task <ActionResult> DisplayResultsTogetherView(HttpPostedFileBase familyTogether)
        {
            await saveLog();

            if (Session.Count == 0)
            {
                TempData["LogInError"] = "Session has ended. Please log in again.";
                return(RedirectToAction("Index", "LogIn"));
            }
            var httpClient = new HttpClient();

            try
            {
                User   user = (User)Session["userSession"];
                byte[] familyTogetherByte = new byte[familyTogether.ContentLength];
                await familyTogether.InputStream.ReadAsync(familyTogetherByte, 0, familyTogether.ContentLength);

                string familyToBase64 = Convert.ToBase64String(familyTogetherByte, 0, familyTogetherByte.Length);

                if (Session["FamilyCheckResult"] != null)
                {
                    var SessionData = (DisplayResultModelFamily)Session["FamilyCheckResult"];
                    if (SessionData.familyPhoto == familyToBase64)
                    {
                        return(View(SessionData));
                    }
                }

                var compareFamily = new CompareFacesFamily {
                    userId = user.userId, familyPhoto = familyToBase64
                };

                var content = new StringContent(JsonConvert.SerializeObject(compareFamily), Encoding.UTF8, "application/json");

                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Session["Access_Token"].ToString());

                var responseApi = await httpClient.PostAsync(ConfigurationManager.AppSettings["url"] + "api/FaceRecognition/FamilyCheck", content);

                var result = await responseApi.Content.ReadAsStringAsync();

                var errorMessage = JsonConvert.DeserializeObject <Error>(result);
                if (errorMessage.Message != null)
                {
                    TempData["ChekcSimilarityError"] = errorMessage.Message;
                    return(RedirectToAction("CheckResultView"));
                }


                if (responseApi.StatusCode == (HttpStatusCode)429)
                {
                    TempData["ChekcSimilarityError"] = "429";
                    return(RedirectToAction("CheckResultView"));
                }


                var checkSimilarityTogether = JsonConvert.DeserializeObject <CheckSimilarityTogether>(result);

                Session["FamilyCheckResult"] = new DisplayResultModelFamily {
                    checkSimilarityTogether = checkSimilarityTogether, familyPhoto = familyToBase64
                };

                return(View(new DisplayResultModelFamily {
                    checkSimilarityTogether = checkSimilarityTogether, familyPhoto = familyToBase64
                }));
            }
            catch (Exception ex)
            {
                return(RedirectToAction("CheckResultView"));
            }
        }