Exemplo n.º 1
0
        public ActionResult Upload()
        {
            var imageData = ProcessRequestImage(Request.InputStream);

            var detectResult = _faceService.DetectFace(imageData);

            if (detectResult == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }

            var identityModel = new IdentifyFaceModel {
                PersonGroupId = personGroupId, FaceIds = new string[] { detectResult.FaceId }
            };
            var identityResult = _faceService.IdentifyFace(identityModel);

            if (identityResult == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }

            var personId = identityResult.Candidates.FirstOrDefault()?.PersonId;

            if (string.IsNullOrEmpty(personId))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }

            var face = _faceRepository.Get(Guid.Parse(personId));

            ViewBag.Name = face?.Name;
            return(View("Index"));
        }
Exemplo n.º 2
0
        public IdentifyFaceResult IdentifyFace(IdentifyFaceModel identifyModel)
        {
            var uri = "https://westus.api.cognitive.microsoft.com/face/v1.0/identify";

            HttpResponseMessage response;

            byte[] byteData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(identifyModel));

            using (var content = new ByteArrayContent(byteData))
            {
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                response = _client.PostAsync(uri, content).Result;
            };

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            return(JsonConvert.DeserializeObject <List <IdentifyFaceResult> >(response.Content.ReadAsStringAsync().Result).FirstOrDefault());
        }