コード例 #1
0
        private async void HandleBtnCheckSimilarityAnalysis(object sender, EventArgs e)
        {
            var progressDialog = new ProgressDialog(view.Context);

            progressDialog.SetCancelable(true);
            progressDialog.SetMessage("Please wait!");
            progressDialog.SetProgressStyle(ProgressDialogStyle.Spinner);
            progressDialog.Show();
            try
            {
                var httpClient = new HttpClient();
                connection = new SQLiteConnection(dbPath);
                var    userData       = connection.Get <UserSession>(1);
                string fatherToBase64 = await convertImageToBase64(imgvFather);

                string motherToBase64 = await convertImageToBase64(imgvMother);

                string childrenToBase64 = await convertImageToBase64(imgvChildren);

                var compareFaces = new CompareFaces
                {
                    userId        = userData.userId,
                    fatherPhoto   = fatherToBase64,
                    motherPhoto   = motherToBase64,
                    childrenPhoto = childrenToBase64
                };
                var content = new StringContent(JsonConvert.SerializeObject(compareFaces), Encoding.UTF8, "application/json");
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userData.Token);

                var responseApi = await httpClient.PostAsync("UrlApi" + "api/FaceRecognition/CheckSimilarityResults", content);

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

                var checkSimilarity     = JsonConvert.DeserializeObject <CheckSimilarity>(result);
                var displayResultsModel = new DisplayResultsModel {
                    checkSimilarity = checkSimilarity, father = fatherToBase64, mother = motherToBase64, children = childrenToBase64
                };
                List <string> attributes = new List <string> {
                    "Contempt", "Disgust", "Anger", "Fear", "Happiness", "Neutral", "Sadness", "Smile", "Surprise", "Similarity", "Glasses"
                };

                List <string> fatherResults = new List <string> {
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[0].faceAttributes.emotion.contempt.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[0].faceAttributes.emotion.disgust.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[0].faceAttributes.emotion.anger.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[0].faceAttributes.emotion.fear.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[0].faceAttributes.emotion.happiness.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[0].faceAttributes.emotion.neutral.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[0].faceAttributes.emotion.sadness.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[0].faceAttributes.smile.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[0].faceAttributes.emotion.surprise.ToString(),
                    displayResultsModel.checkSimilarity.similarityTest[0].Confidence.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[0].faceAttributes.glasses.ToString()
                };

                List <string> motherResults = new List <string> {
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[1].faceAttributes.emotion.contempt.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[1].faceAttributes.emotion.disgust.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[1].faceAttributes.emotion.anger.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[1].faceAttributes.emotion.fear.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[1].faceAttributes.emotion.happiness.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[1].faceAttributes.emotion.neutral.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[1].faceAttributes.emotion.sadness.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[1].faceAttributes.smile.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[1].faceAttributes.emotion.surprise.ToString(),
                    displayResultsModel.checkSimilarity.similarityTest[1].Confidence.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[1].faceAttributes.glasses.ToString()
                };
                List <string> childrenResults = new List <string> {
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[2].faceAttributes.emotion.contempt.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[2].faceAttributes.emotion.disgust.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[2].faceAttributes.emotion.anger.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[2].faceAttributes.emotion.fear.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[2].faceAttributes.emotion.happiness.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[2].faceAttributes.emotion.neutral.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[2].faceAttributes.emotion.sadness.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[2].faceAttributes.smile.ToString(),
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[2].faceAttributes.emotion.surprise.ToString(),
                    "-",
                    displayResultsModel.checkSimilarity.cognitiveMicrosoft[2].faceAttributes.glasses.ToString()
                };

                FragmentTransaction fragmentManager           = this.FragmentManager.BeginTransaction();
                DisplayResults3PersonsSimilarity diplaResults = new DisplayResults3PersonsSimilarity(attributes, fatherResults, motherResults, childrenResults);
                fragmentManager.Replace(Resource.Id.parent_fragment, diplaResults);
                fragmentManager.SetTransition(FragmentTransit.FragmentOpen);
                fragmentManager.AddToBackStack(null);
                fragmentManager.Commit();
                progressDialog.Hide();
            }
            catch (HttpRequestException httpExec)
            {
                progressDialog.Hide();
                displaySnackBar("Check your internet connection.");
            }
            catch (Exception ex)
            {
                progressDialog.Hide();
                displaySnackBar(ex.ToString());
            }
        }
コード例 #2
0
        public async Task <ActionResult> DisplayResults3View(Similarity3Persons similarity3Persons)
        {
            string SECRETKEY        = ConfigurationManager.AppSettings["captchaSecret"];
            string userResponse     = similarity3Persons.gRecaptchaResponse;
            var    webClient        = new WebClient();
            string verification     = webClient.DownloadString(ConfigurationManager.AppSettings["captchaLink"] + "secret=" + SECRETKEY + "&response=" + userResponse);
            var    verificationJson = JsonConvert.DeserializeObject <CaptchaResponse>(verification);
            bool   rezultati        = verificationJson.success;
            var    httpClient       = new HttpClient();
            var    jsonCaptchaLogs  = new Logs {
                action = verificationJson.action, challenge_ts = verificationJson.challenge_ts, score = verificationJson.score, time_accessed = DateTime.Now, ip = Request.UserHostAddress, hostname = Request.UserHostName, success = verificationJson.success, url = Request.Url.ToString()
            };
            var captchaContent = new StringContent(JsonConvert.SerializeObject(jsonCaptchaLogs), Encoding.ASCII, "application/json");
            await httpClient.PostAsync(ConfigurationManager.AppSettings["url"] + "api/logs/saveRecaptchaLogs", captchaContent);

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

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

                string motherToBase64 = Convert.ToBase64String(motherByte, 0, motherByte.Length);

                byte[] fatherByte = new byte[similarity3Persons.father.ContentLength];
                await similarity3Persons.father.InputStream.ReadAsync(fatherByte, 0, similarity3Persons.father.ContentLength);

                string fatherToBase64 = Convert.ToBase64String(fatherByte, 0, fatherByte.Length);

                byte[] childrenByte = new byte[similarity3Persons.children.ContentLength];
                await similarity3Persons.children.InputStream.ReadAsync(childrenByte, 0, similarity3Persons.children.ContentLength);

                string childrenToBase64 = Convert.ToBase64String(childrenByte, 0, childrenByte.Length);

                if (Session["checkSimilarity3"] != null)
                {
                    var SessionData = (DisplayResultsModel)Session["checkSimilarity3"];
                    if (SessionData.father == fatherToBase64 && SessionData.mother == motherToBase64 && SessionData.children == childrenToBase64)
                    {
                        return(View(SessionData));
                    }
                }

                var compareFaces = new CompareFaces {
                    userId = user.userId, fatherPhoto = fatherToBase64, motherPhoto = motherToBase64, childrenPhoto = childrenToBase64
                };

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

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

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

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

                if (!responseApi.IsSuccessStatusCode)
                {
                    var mesage = JsonConvert.DeserializeObject <Error>(result);
                    TempData["errorCheckSimilarity"] = mesage.Message;
                    return(RedirectToAction("CheckResultView"));
                }

                var checkSimilarity     = JsonConvert.DeserializeObject <CheckSimilarity>(result);
                var displayResultsModel = new DisplayResultsModel {
                    checkSimilarity = checkSimilarity, father = fatherToBase64, mother = motherToBase64, children = childrenToBase64
                };
                Session["checkSimilarity3"] = displayResultsModel;
                return(View(displayResultsModel));
            }
            catch (Exception ex)
            {
                TempData["errorCheckSimilarity"] = ex.ToString();
                return(RedirectToAction("CheckResultView"));
            }
        }