public async Task <string[]> RecogniseFaces(Face[] faces)
        {
            await AsyncWaitTrainer();

            List <string> names = new List <string>();

            foreach (Face face in faces)
            {
                Guid[]           faceIds = faces.Select(f => f.FaceId).ToArray();
                IdentifyResult[] results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                foreach (IdentifyResult identifyResult in results)
                {
                    if (identifyResult.Candidates.Length != 0)
                    {
                        Guid   candidateId = identifyResult.Candidates[0].PersonId;
                        Person person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                        names.Add(person.Name);
                    }
                    else
                    {
                        names.Add("Unknown");
                    }
                }
            }
            return(names.ToArray());
        }
예제 #2
0
        public static async Task <IList <string> > GetPeopleAsync(Stream stream)
        {
            var result = new List <string>();
            var faces  = await faceServiceClient.DetectAsync(stream);

            var faceIds = faces.Select(face => face.FaceId).ToArray();

            if (faceIds.Length != 0)
            {
                var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                foreach (var identifyResult in results)
                {
                    if (identifyResult.Candidates.Length != 0 && identifyResult.Candidates[0].Confidence > confidentThresold)
                    {
                        // Get top 1 among all candidates returned
                        var candidateId = identifyResult.Candidates[0].PersonId;
                        var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                        result.Add(person.Name);
                    }
                }
            }
            return(result);
        }
예제 #3
0
        public async Task <int[]> search(Stream path)
        {
            StringBuilder sb = new StringBuilder();

            using (path)
            {
                int[] id    = new int[5];
                int   i     = 0;
                var   faces = await faceServiceClient.DetectAsync(path);

                if (faces.Length == 0)
                {
                    id[i] = -1;
                }
                else
                {
                    var faceIds = faces.Select(face => face.FaceId).ToArray();

                    var results = await faceServiceClient.IdentifyAsync(groupId, faceIds);

                    foreach (var identifyResult in results)
                    {
                        if (identifyResult.Candidates.Length == 0)
                        {
                            id[i] = 0;
                            if (i == 4)
                            {
                                break;
                            }
                            i++;
                        }
                        else
                        {
                            // Get top 1 among all candidates returned
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(groupId, candidateId);

                            try
                            {
                                id[i] = Convert.ToInt32(person.Name);
                                if (i == 4)
                                {
                                    break;
                                }
                                i++;
                            }
                            catch
                            {
                                if (i == 4)
                                {
                                    break;
                                }
                                i++;
                            }
                        }
                    }
                }
                return(id);
            }
        }
예제 #4
0
        private async Task IdentifyPersons(string personGroupId, string testImageFile)
        {
            using (Stream s = File.OpenRead(testImageFile))
            {
                var faces = await faceClient.DetectAsync(s);

                var faceIds = faces.Select(face => face.FaceId).ToArray();

                var results = await faceClient.IdentifyAsync(personGroupId, faceIds);

                foreach (var identifyResult in results)
                {
                    Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                    if (identifyResult.Candidates.Length == 0)
                    {
                        Console.WriteLine("No one identified");
                    }
                    else
                    {
                        // Get top 1 among all candidates returned
                        var candidateId = identifyResult.Candidates[0].PersonId;
                        var person      = await faceClient.GetPersonAsync(personGroupId, candidateId);

                        //var person = await faceClient.PersonGroupPerson.GetAsync(personGroupId, candidateId);
                        Console.WriteLine("Identified as {0}", person.Name);
                    }
                }
            }
        }
예제 #5
0
        public async void Detect(Stream testImageFile)
        {
            //  using (Stream s = File.OpenRead(testImageFile))
            //{
            var faces = await faceServiceClient.DetectAsync(testImageFile);

            var faceIds = faces.Select(face => face.FaceId).ToArray();

            var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

            foreach (var identifyResult in results)
            {
                // Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                // MessageBox.Show(identifyResult.FaceId.ToString());
                if (identifyResult.Candidates.Length == 0)
                {
                    Console.WriteLine("No one identified");
                }
                else
                {
                    // Get top 1 among all candidates returned
                    var candidateId = identifyResult.Candidates[0].PersonId;
                    var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                    // Console.WriteLine(;
                    //  MessageBox.Show(person.Name);
                }
                // }
            }
        }
예제 #6
0
        private async void Identify()
        {
            _(String.Format("Attempting to identify face using {0} group...", groupId));
            try
            {
                Face[] faces = await UploadAndDetectFaces(grabPath);

                var faceIds = faces.Select(face => face.FaceId).ToArray();
                _("Calling service...");
                try
                {
                    var results = await faceServiceClient.IdentifyAsync(groupId, faceIds);

                    foreach (var identifyResult in results)
                    {
                        _("Parsing results...");
                        if (identifyResult.Candidates.Length != 0)
                        {
                            _("Candidate found...");
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            try
                            {
                                _("Trying to identify person...");
                                var person = await faceServiceClient.GetPersonAsync(groupId, candidateId);

                                // user identificated: person.name is the associated name
                                _(String.Format("****************** {0} identified ******************", person.Name));
                            }
                            catch (FaceAPIException eGetPersonAsync)
                            {
                                _(eGetPersonAsync.ErrorMessage);
                            }
                            catch (TaskCanceledException terr)
                            {
                                _(terr.Message);
                            }
                        }
                        else
                        {
                            // user not recognized
                            _("Person not recognized.");
                        }
                    }
                }
                catch (FaceAPIException eIdentifyAsync)
                {
                    _(eIdentifyAsync.ErrorMessage);
                }
            }
            catch (FaceAPIException eUploadAndDetectFaces)
            {
                _(eUploadAndDetectFaces.ErrorMessage);
            }
            catch (System.Net.Http.HttpRequestException httper)
            {
                _(httper.Message);
            }
        }
        public async Task <NamedFace[]> AnalyzeImageUsingHelper(Stream stream)
        {
            Face[] faces = await faceDetector.DetectAsync(stream, false, true, true, false);

            NamedFace[] namedFaces = new NamedFace[faces.Length];

            //Copy to named faces vector.
            for (int i = 0; i < faces.Length; i++)
            {
                namedFaces[i] = new NamedFace(faces[i]);
            }



            // TODO: Is this the right place to get the images from???
            bool identifyFaces;

            bool.TryParse(ConfigurationManager.AppSettings["IdentifySpecificPeople"] ?? "false", out identifyFaces);

            if (identifyFaces && faces.Length > 0)
            {
                var faceIds = faces.Select(face => face.FaceId).ToArray();

                var results = await faceDetector.IdentityAsync("coworkers", faceIds);

                foreach (var identifyResult in results)
                {
                    Console.WriteLine("Result of face: {0}", identifyResult.FaceId);

                    if (identifyResult.Candidates.Length == 0)
                    {
                        Console.WriteLine("No one identified");
                    }
                    else
                    {
                        var candidateId = identifyResult.Candidates[0].PersonId;
                        var person      = await faceDetector.GetPersonAsync("coworkers", candidateId);

                        if (identifyResult.Candidates[0].Confidence > 0.5)
                        {
                            for (int i = 0; i < namedFaces.Length; i++)
                            {
                                if (namedFaces[i].FaceId == identifyResult.FaceId)
                                {
                                    // Set name.
                                    namedFaces[i].Name = person.Name;
                                }
                            }

                            Console.WriteLine("Identified as {0}", person.Name);
                        }
                    }
                }
            }

            return(namedFaces);
        }
예제 #8
0
        //Uploads the image file and calls Detect Faces.
        private async Task <Face[]> UploadAndDetectFaces(string imageFilePath)
        {
            //Call the Face API.
            try
            {
                using (Stream imageFileStream = File.OpenRead(imageFilePath))
                {
                    Face[] faces = await faceServiceClient.DetectAsync(imageFileStream, returnFaceId : true,
                                                                       returnFaceLandmarks : false);

                    System.Diagnostics.Debug.WriteLine("얼굴감지");

                    //Identify face against person group
                    var faceIds = faces.Select(face => face.FaceId).ToArray();
                    System.Diagnostics.Debug.WriteLine("얼굴식별시작");
                    var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                    foreach (var identifyResult in results)
                    {
                        Console.WriteLine("Result of face:{0}", identifyResult.FaceId);
                        System.Diagnostics.Debug.WriteLine("Result of face:{0}", identifyResult.FaceId);
                        if (identifyResult.Candidates.Length == 0)
                        {
                            MessageBox.Show("외부인이 침입했습니다. 시동을 제한합니다.");
                            System.Diagnostics.Debug.WriteLine("외부인이 침입했습니다.");
                            //sendImage();
                        }
                        else
                        {
                            //Get top 1 among all candidates returned
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                            MessageBox.Show("승인된 사람입니다. " + person.Name);
                            System.Diagnostics.Debug.WriteLine("Identified as {0}", person.Name);
                        }
                    }
                    return(faces);
                }
            }
            //Catch and display Face API errors.
            catch (FaceAPIException f)
            {
                MessageBox.Show(f.ErrorMessage, f.ErrorCode);
                System.Diagnostics.Debug.WriteLine("얼굴 식별 & 감지" + f);
                return(new Face[0]);
            }
            //Catch and display all other errors.
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error");
                System.Diagnostics.Debug.WriteLine("얼굴 식별 & 감지" + e);
                return(new Face[0]);
            }
        }
예제 #9
0
        /// <summary>
        /// Use the training data to spot individuals on a provided image
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnIdentify_Click(object sender, EventArgs e)
        {
            try
            {
                Face[] faces = await UploadAndDetectFaces(_imagePath);

                var faceIds = faces.Select(face => face.FaceId).ToArray();

                var faceBitmap = new Bitmap(imgBox.Image);
                idList.Items.Clear();

                using (var g = Graphics.FromImage(faceBitmap))
                {
                    foreach (var identifyResult in await faceServiceClient.IdentifyAsync(_groupId, faceIds))
                    {
                        if (identifyResult.Candidates.Length != 0)
                        {
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(_groupId, candidateId);

                            // Writes name above face rectangle
                            var x = faces.FirstOrDefault(y => y.FaceId == identifyResult.FaceId);
                            if (x != null)
                            {
                                g.DrawString(person.Name, this.Font, Brushes.White, x.FaceRectangle.Left, x.FaceRectangle.Top + x.FaceRectangle.Height + 15);
                            }

                            idList.Items.Add(person.Name);
                        }
                        else
                        {
                            idList.Items.Add("< Unknown person >");
                        }
                    }
                }

                imgBox.Image         = faceBitmap;
                progressBar1.Visible = false;
                if (idList.FindString(txt_reg.Text) >= 0)
                {
                    MessageBox.Show("Welcome, " + txt_reg.Text);
                }
                else
                {
                    MessageBox.Show("Sorry! I Don't know you, please try again!");
                }
            } catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
예제 #10
0
        public static async Task <string> VerifyPerson(Guid[] personID)
        {
            try
            {
                string username = "";
                if (faceGroupID != "")
                {
                    var results = await faceServiceClient.IdentifyAsync(faceGroupID, personID, 1);

                    foreach (var identifyResult in results)
                    {
                        if (identifyResult.Candidates.Length == 0)
                        {
                            //Not Verified
                            App.WriteMessage("User Not Verified");
                        }
                        else
                        {
                            //Get Verified Person Details
                            var candidate = identifyResult.Candidates[0];
                            if (candidate.Confidence > 0.70)
                            {
                                var candidateId = identifyResult.Candidates[0].PersonId;
                                var person      = await faceServiceClient.GetPersonAsync(faceGroupID, candidateId);

                                App.WriteMessage("User Verfied as " + person.Name + " (Confidence: " + candidate.Confidence + ")");
                                username = person.Name;
                            }
                            else
                            {
                                username = "******";
                                App.WriteMessage("User Verification Failed - Confidence: " + candidate.Confidence);
                            }
                        }
                    }
                }
                else
                {
                    //Identification Not Possible - Unknown Owner
                    username = "******";
                }

                return(username);
            }
            catch (Exception)
            {
                return("");
            }
        }
예제 #11
0
        public async void ProcessIdentify(string catchPath)
        {
            Console.WriteLine(catchPath);
            using (Stream s = File.OpenRead(catchPath))
            {
                try
                {
                    var faces = await _faceServiceClient.DetectAsync(s, returnFaceLandmarks : true, returnFaceAttributes : _requiedFaceAttributes);

                    s.Close();
                    Dictionary <Guid, ExFace> queryFaceMap = new Dictionary <Guid, ExFace>();
                    faces.ToList().ForEach(face => queryFaceMap.Add(face.FaceId, new ExFace(face)));
                    IdentityResultForm resultForm = new IdentityResultForm();
                    resultForm.Show();
                    resultForm.ShowDetectFaces(catchPath, queryFaceMap.Values.ToList());
                    var faceIds = faces.Select(face => face.FaceId).ToArray();
                    var results = await _faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                    bool isOpen = false;
                    foreach (var identifyResult in results)
                    {
                        Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                        if (identifyResult.Candidates.Length == 0)
                        {
                            Console.WriteLine("No one identified");
                            queryFaceMap[identifyResult.FaceId].PersonName = ExFace.UNKNOWN;
                        }
                        else
                        {
                            // Get top 1 among all candidates returned
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await _faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                            Console.WriteLine("Identified as {0}", person.Name);
                            queryFaceMap[identifyResult.FaceId].PersonName = person.Name;
                            isOpen = true;
                        }
                    }
                    resultForm.SetDoor(isOpen);
                    resultForm.ShowIdentityFaces(queryFaceMap.Values.ToList());
                }
                catch (FaceAPIException ex)
                {
                    Console.WriteLine("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                }
            }
        }
예제 #12
0
        private async Task <FaceRectangle> UploadAndDetectFaces(String imageFilePath)
        {
            try
            {
                Windows.Storage.StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(imageFilePath));

                Windows.Storage.StorageFile sampleFile = await storageFolder.GetFileAsync(System.IO.Path.GetFileName(imageFilePath));

                var inputStream = await sampleFile.OpenReadAsync();

                Stream stream = inputStream.AsStreamForRead();

                var faces = await faceServiceClient.DetectAsync(stream);

                var faceRects = faces.Select(face => face.FaceRectangle);

                var faceIds = faces.Select(face => face.FaceId).ToArray();
                var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                for (int j = 0; j < results.Length; j++)
                {
                    Debug.WriteLine("Result of face: {0}", results[j].FaceId);
                    if (results[j].Candidates.Length == 0)
                    {
                        Debug.WriteLine("No one identified");
                    }
                    else
                    {
                        var candidateId = results[j].Candidates[0].PersonId;
                        var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                        Debug.WriteLine("Identified as {0}", person.Name);

                        if (person.Name == "person1")
                        {
                            FaceRectangle rectID;
                            return(rectID = faces[j].FaceRectangle);
                        }
                    }
                }
            }
            catch (Microsoft.ProjectOxford.Face.FaceAPIException exc)
            {
                Debug.WriteLine(exc.ErrorCode);
            }
            return(null);
        }
        public async Task <NamedFace[]> AnalyzeImageUsingHelper(Stream fileStream)
        {
            NamedFace[] namedFaces = null;
            try
            {
                // Face[] faces = null;


                Face[] faces = await faceDetector.DetectAsync(fileStream, true, false, new FaceAttributeType[] { FaceAttributeType.Gender, FaceAttributeType.Age, FaceAttributeType.Smile, FaceAttributeType.Glasses }); //, false, true, new FaceAttributeType[] { FaceAttributeType.Gender, FaceAttributeType.Age, FaceAttributeType.Smile, FaceAttributeType.Glasses }

                namedFaces = new NamedFace[faces.Length];
                //Copy to named faces vector.
                //for (int i = 0; i < faces.Length; i++)
                //{
                //    namedFaces[i] = new NamedFace(faces[i]);
                //}
                var faceIds = faces.Select(face => face.FaceId).ToArray();
                var results = await faceDetector.IdentifyAsync(Groupname, faces.Select(ff => ff.FaceId).ToArray());

                for (int i = 0; i < results.Length; i++)
                {
                    namedFaces[i] = new NamedFace(faces[i]);
                    Debug.WriteLine("Result of face: {0}", results[i].FaceId);
                    if (results[i].Candidates.Length == 0)
                    {
                        namedFaces[i].Name = "unknown";
                        Debug.WriteLine("No one identified");
                    }
                    else
                    {
                        App.isAuthenricated = true;
                        // Get top 1 among all candidates returned
                        var candidateId = results[i].Candidates[0].PersonId;
                        var person      = await faceDetector.GetPersonAsync(Groupname, candidateId);

                        namedFaces[i].Name = person.Name;
                        Debug.WriteLine("Identified as {0}", person.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
            }
            return(namedFaces);
        }
예제 #14
0
        private async void getPersonData(System.Guid id)
        {
            try
            {
                Person p = await faceServiceClient.GetPersonAsync(this.personGroupId, id);

                string json_obj = Newtonsoft.Json.JsonConvert.SerializeObject(p.UserData);
                MessageBox.Show(
                    "Person Data is " + p.Name + " User data " + json_obj);
                Console.Out.WriteLine("User data: " + json_obj);
            }
            catch (FaceAPIException f)
            {
                MessageBox.Show(f.ErrorCode, f.ErrorMessage);
                return;
            }
        }
예제 #15
0
        private async Task <Identification> GetIdentification(Face face)
        {
            var groups = await client.ListPersonGroupsAsync();

            var group           = groups.First();
            var identifyResults = await client.IdentifyAsync(group.PersonGroupId, new[] { face.FaceId });

            Person person   = null;
            var    personId = identifyResults.FirstOrDefault()?.Candidates.FirstOrDefault()?.PersonId;

            if (personId != null)
            {
                person = await client.GetPersonAsync(group.PersonGroupId, personId.Value);
            }

            return(new Identification(face, person));
        }
        public async Task <int> search(MediaFile file)
        {
            using (Stream s = file.GetStream())
            {
                int i;
                var faces = await faceServiceClient.DetectAsync(s);

                if (faces.Length == 0)
                {
                    i = -1;
                    return(i);
                }
                else
                {
                    var faceIds = faces.Select(face => face.FaceId).ToArray();

                    var results = await faceServiceClient.IdentifyAsync(groupId, faceIds);

                    foreach (var identifyResult in results)
                    {
                        if (identifyResult.Candidates.Length == 0)
                        {
                            return(0);
                        }
                        else
                        {
                            // Get top 1 among all candidates returned
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(groupId, candidateId);

                            try
                            {
                                return(Convert.ToInt32(person.Name));
                            }
                            catch (Exception)
                            {
                                return(0);
                            }
                        }
                    }
                }
                return(0);
            }
        }
예제 #17
0
        private async Task <PersonMetaData> GetPersonMetaDataFromResultAsync(IdentifyResult[] result, string pictureUrl)
        {
            if (result.Length > 0)
            {
                if (result[0].Candidates.Length > 0)
                {
                    string confidence  = $"{result[0].Candidates[0].Confidence * 100}%";
                    var    candidateId = result[0].Candidates[0].PersonId;
                    Person person      = await _faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                    return(new PersonMetaData {
                        Name = person.Name, ImageUrl = pictureUrl, Confidence = confidence
                    });
                }
            }
            return(new PersonMetaData {
                Name = "Not found"
            });
        }
        private async Task <string> IdentifyUser(string imageName)
        {
            StorageFolder appFolder = ApplicationData.Current.LocalCacheFolder;
            StorageFile   file      = await appFolder.GetFileAsync(imageName);

            using (var randomAccessStream = await file.OpenReadAsync())
                using (Stream s = randomAccessStream.AsStreamForRead())
                {
                    var faces = await faceServiceClient.DetectAsync(s);

                    var faceIds = faces.Select(face => face.FaceId).ToArray();

                    var results = await faceServiceClient.IdentifyAsync(personGroup, faceIds);

                    foreach (var identifyResult in results)
                    {
                        Debug.WriteLine("Result of face: {0}", identifyResult.FaceId);

                        if (identifyResult.Candidates.Length == 0)
                        {
                            Debug.WriteLine("No one identified");
                        }
                        else
                        {
                            // Get top 1 among all candidates returned
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(personGroup, candidateId);

                            userName = person.Name;
                            Debug.WriteLine("Identified as {0}", userName);
                            IdentityTextBlock.Text = "Hey " + userName + "!";
                            await Speak("Hello, " + userName);
                        }
                    }
                }

            return(userName);
        }
예제 #19
0
        // recognise image
        public async void identifyUser(ListBox identifiedUserListBox, String imagePath, String groupId)
        {
            Face[] faces = await UploadAndDetectFaces(imagePath);

            var faceIds = faces.Select(face => face.FaceId).ToArray();

            identifiedUserListBox.Items.Clear();

            foreach (var identifyResult in await faceServiceClient.IdentifyAsync(groupId, faceIds))
            {
                if (identifyResult.Candidates.Length != 0)
                {
                    var candidateId = identifyResult.Candidates[0].PersonId;
                    var person      = await faceServiceClient.GetPersonAsync(groupId, candidateId);

                    identifiedUserListBox.Items.Add(person.Name);
                }
                else
                {
                    identifiedUserListBox.Items.Add("< Unknown person >");
                }
            }
        }
예제 #20
0
        public async void TestMethodAsync()
        {
            try
            {
                string testImageFile = @"D:\test.jpg";

                using (Stream s = File.OpenRead(testImageFile))
                {
                    var faces = await faceServiceClient.DetectAsync(s);

                    var faceIds = faces.Select(face => face.FaceId).ToArray();

                    var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                    foreach (var identifyResult in results)
                    {
                        Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                        if (identifyResult.Candidates.Length == 0)
                        {
                            Console.WriteLine("No one identified");
                        }
                        else
                        {
                            // Get top 1 among all candidates returned
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                            Console.WriteLine("Identified as {0} & Confident Level {1}", person.Name, identifyResult.Candidates[0].Confidence.ToString());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }
예제 #21
0
        public async Task <JsonResult> IdentifyAsync(string imageData)
        {
            byte[] bytes = Convert.FromBase64String(imageData);

            Image image;

            using (MemoryStream ms = new MemoryStream(bytes))
            {
                image = Image.FromStream(ms);
            }

            var fileName = "~/Images/Test/IdentifyThisPerson.png";
            var filePath = Server.MapPath(fileName);

            image.Save(filePath);

            var faces   = await faceServiceClient.DetectAsync(Request.Url.Scheme + "://" + Request.Url.Authority + Url.Content(fileName));;
            var faceIds = faces.Select(face => face.FaceId).ToArray();

            var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

            Person person = new Person();

            foreach (var identifyResult in results)
            {
                Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                if (identifyResult.Candidates.Length != 0)
                {
                    // Get top 1 among all candidates returned
                    var candidateId = identifyResult.Candidates[0].PersonId;
                    person = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);
                }
            }

            return(Json(new { Data = person }, JsonRequestBehavior.AllowGet));
        }
예제 #22
0
        public static async Task CheckAuthorized(Microsoft.ProjectOxford.Face.Contract.Face[] faces, TraceWriter log)
        {
            var faceIds = faces.Select(face => face.FaceId).ToArray();
            var results = await faceServiceClient.IdentifyAsync("whsworkers", faceIds);

            foreach (var identifyResult in results)
            {
                if (identifyResult.Candidates.Length == 0)
                {
                    log.Info("FACE UNRECOGNISED");
                    data.notification = "Unrecognised Worker";
                    throw new UnrecognisedFaceException();
                }
                else
                {
                    // Get top 1 among all candidates returned
                    var candidateId = identifyResult.Candidates[0].PersonId;
                    var person      = await faceServiceClient.GetPersonAsync("whsworkers", candidateId);

                    data.name = person.Name;
                    log.Info("FACE IS " + person.Name);
                }
            }
        }
예제 #23
0
        //20 api calls per minut grej
        private async void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            var openDlg = new Microsoft.Win32.OpenFileDialog();

            openDlg.Filter = "JPEG images (*.jpeg)|*.jpeg|JPG images (*.jpg)|*.jpg|PNG images (*.png)|*.png|BMP images (*.bmp)|*.bmp"
                             + "|All Files (*.*)|*.*";
            bool?result = openDlg.ShowDialog(this);

            if (!(bool)result)
            {
                return;
            }

            string filePath = openDlg.FileName;

            Uri         fileUri      = new Uri(filePath);
            BitmapImage bitmapSource = new BitmapImage();

            bitmapSource.BeginInit();
            bitmapSource.CacheOption = BitmapCacheOption.None;
            bitmapSource.UriSource   = fileUri;
            bitmapSource.EndInit();

            FacePhoto.Source = bitmapSource;
            Title            = "Detecting...";
            FaceRectangle[] faceRects = await UploadAndDetectFaces(filePath);

            Title = String.Format("Detection Finished. {0} face(s) detected", faceRects.Length);


            using (s = File.OpenRead(filePath))
            {
                var faces = await faceServiceClient.DetectAsync(s);

                var faceIds = faces.Select(face => face.FaceId).ToArray();



                var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);


                foreach (var identifyResult in results)
                {
                    //    Console.WriteLine("Result of face: {0}", identifyResult.FaceId);


                    if (identifyResult.Candidates.Length == 0)
                    {
                        Console.WriteLine("No one identified");
                        outputBox.Text     = "No one identified";
                        confidenceBox.Text = "Confidence: " + 0;
                    }
                    else
                    {
                        // Get top 1 among all candidates returned
                        var candidateId = identifyResult.Candidates[0].PersonId;
                        var ress        = identifyResult.Candidates[0].Confidence;
                        var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);


                        Console.WriteLine(ress);
                        confidenceBox.Text = "Confidence: " + ress;
                        Console.WriteLine("Identified as {0}", person.Name);
                        outputBox.Text = "Identified as " + person.Name;
                    }
                    if (faceRects.Length > 0)
                    {
                        DrawingVisual  visual         = new DrawingVisual();
                        DrawingContext drawingContext = visual.RenderOpen();
                        drawingContext.DrawImage(bitmapSource,
                                                 new Rect(0, 0, bitmapSource.Width, bitmapSource.Height));
                        double dpi          = bitmapSource.DpiX;
                        double resizeFactor = 96 / dpi;

                        foreach (var faceRect in faceRects)
                        {
                            drawingContext.DrawRectangle(
                                Brushes.Transparent,
                                new Pen(Brushes.Red, 2),
                                new Rect(
                                    faceRect.Left * resizeFactor,
                                    faceRect.Top * resizeFactor,
                                    faceRect.Width * resizeFactor,
                                    faceRect.Height * resizeFactor
                                    )
                                );
                        }

                        drawingContext.Close();
                        RenderTargetBitmap faceWithRectBitmap = new RenderTargetBitmap(
                            (int)(bitmapSource.PixelWidth * resizeFactor),
                            (int)(bitmapSource.PixelHeight * resizeFactor),
                            96,
                            96,
                            PixelFormats.Pbgra32);

                        faceWithRectBitmap.Render(visual);
                        FacePhoto.Source = faceWithRectBitmap;
                    }
                }
            }
        }
예제 #24
0
 public Task <Person> GetPersonAsync(string personGroupId, Guid personId)
 {
     return(RateLimitAwareCall(() => innerClient.GetPersonAsync(personGroupId, personId)));
 }
예제 #25
0
 public async Task <Microsoft.ProjectOxford.Face.Contract.Person> GetPersonFromGroup(Guid personId)
 {
     return(await _faceServiceClient.GetPersonAsync(_personGroupId, personId));
 }
        /// <summary>
        /// Tab 5 Identify voters with face rectangles and voters'name in section 'Identify Voters'
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void IdentifyButton_Click(object sender, RoutedEventArgs e)
        {
            int itemNum       = Tab5PersonGroupIDComboBox.Items.Count;
            int selectedGroup = Tab5PersonGroupIDComboBox.SelectedIndex;

            if (itemNum == 0)
            {
                MessageBox.Show("Please define the person group ID first in page 2");
            }
            if (selectedGroup == -1)
            {
                MessageBox.Show("Please select a group.");
            }
            if (itemNum != 0 && selectedGroup != -1)
            {
                TestFacePhoto.Source = null;
                var openDlg = new Microsoft.Win32.OpenFileDialog();

                openDlg.Filter = "JPEG Image(*.jpg)|*.jpg";
                bool?result = openDlg.ShowDialog(this);

                if (!(bool)result)
                {
                    return;
                }

                string filePath = openDlg.FileName;

                Uri         fileUri      = new Uri(filePath);
                BitmapImage bitmapSource = new BitmapImage();

                bitmapSource.BeginInit();
                bitmapSource.CacheOption = BitmapCacheOption.None;
                bitmapSource.UriSource   = fileUri;
                bitmapSource.EndInit();


                using (Stream s = File.OpenRead(filePath))
                {
                    var faces = await faceServiceClient.DetectAsync(s);

                    var faceRects = faces.Select(face => face.FaceRectangle).ToArray();
                    var faceIds   = faces.Select(face => face.FaceId).ToArray();

                    string personGroupId = Tab5PersonGroupIDComboBox.Text;

                    var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                    int           i        = 0;
                    FaceRectangle faceRect = null;

                    DrawingVisual  visual         = new DrawingVisual();
                    DrawingContext drawingContext = visual.RenderOpen();
                    drawingContext.DrawImage(bitmapSource,
                                             new Rect(0, 0, bitmapSource.Width, bitmapSource.Height));
                    double dpi          = bitmapSource.DpiX;
                    double resizeFactor = 96 / dpi;

                    foreach (var identifyResult in results)
                    {
                        Console.WriteLine("Identify result of face id: {0}", identifyResult.FaceId);
                        Console.WriteLine("Identify result of length of candidates: {0}", identifyResult.Candidates.Length);

                        faceRect = faceRects[i];


                        ///No candidate indicates that the person has not provided his registered face image to register as a voter.
                        if (identifyResult.Candidates.Length == 0)
                        {
                            ///Draw a face rectangle
                            drawingContext.DrawRectangle(
                                Brushes.Transparent,
                                new Pen(Brushes.Red, 2),
                                new Rect(
                                    faceRect.Left * resizeFactor,
                                    faceRect.Top * resizeFactor,
                                    faceRect.Width * resizeFactor,
                                    faceRect.Height * resizeFactor
                                    )
                                );
                            ///Draw a context with 'Unknow' in 'Ghostwhite' color
                            Point  point = new Point(faceRect.Left * resizeFactor, faceRect.Top * resizeFactor - 10);
                            string text  = "Unknow";
                            System.Windows.Media.FormattedText myText = new System.Windows.Media.FormattedText(
                                text,
                                CultureInfo.GetCultureInfo("en-us"),
                                FlowDirection.LeftToRight,
                                new Typeface("Marlet"),
                                8.0,
                                System.Windows.Media.Brushes.GhostWhite);
                            drawingContext.DrawText(myText, point);
                        }

                        ///The Candidate.Length != 0 means that this person with a bunch of registered face images has been succeed registered into this system
                        if (identifyResult.Candidates.Length != 0)
                        {
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                            Console.WriteLine("Identified as {0}", person.Name);

                            ///Draw a face rectangle
                            drawingContext.DrawRectangle(
                                Brushes.Transparent,
                                new Pen(Brushes.Red, 2),
                                new Rect(
                                    faceRect.Left * resizeFactor,
                                    faceRect.Top * resizeFactor,
                                    faceRect.Width * resizeFactor,
                                    faceRect.Height * resizeFactor
                                    )
                                );

                            ///Draw a context with register person's name in 'GreenYellow' colour
                            Point  point = new Point(faceRect.Left * resizeFactor, faceRect.Top * resizeFactor - 10);
                            string text  = person.Name;
                            System.Windows.Media.FormattedText myText = new System.Windows.Media.FormattedText(
                                text,
                                CultureInfo.GetCultureInfo("en-us"),
                                FlowDirection.LeftToRight,
                                new Typeface("Marlet"),
                                10.0,
                                System.Windows.Media.Brushes.GreenYellow);
                            drawingContext.DrawText(myText, point);
                        }
                        i++;
                    }
                    drawingContext.Close();
                    RenderTargetBitmap faceWithRectBitmap = new RenderTargetBitmap(
                        (int)(bitmapSource.PixelWidth * resizeFactor),
                        (int)(bitmapSource.PixelHeight * resizeFactor),
                        96,
                        96,
                        PixelFormats.Pbgra32);

                    faceWithRectBitmap.Render(visual);
                    TestFacePhoto.Source = faceWithRectBitmap;
                }
            }
        }
예제 #27
0
        public async Task <String> Testpicture(String testImageFile)
        {
            try
            {
                People        people = new People();
                List <string> rslist = new List <string>();
                string[]      HeadRandom;
                StringBuilder Mount_path = new StringBuilder();
                //FaceServiceClient fc = new FaceServiceClient(ApiKey);

                using (Stream s = File.OpenRead(testImageFile))
                {
                    var requiredFaceAttributes = new FaceAttributeType[]
                    {
                        FaceAttributeType.Age,
                        FaceAttributeType.Gender,
                        FaceAttributeType.Smile,
                        FaceAttributeType.FacialHair,
                        FaceAttributeType.HeadPose,
                        FaceAttributeType.Glasses,
                        FaceAttributeType.Emotion
                    };

                    //var faces = faceServiceClient.DetectAsync(s).Result;
                    //Console.WriteLine(faces);
                    var faces = await faceServiceClient.DetectAsync(s, returnFaceLandmarks : true, returnFaceAttributes : requiredFaceAttributes);

                    var faceIds = faces.Select(face => face.FaceId).ToArray();
                    var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                    try
                    {
                        int isM = 0, isF = 0;
                        // string age = "";
                        string sex = "";
                        int    age;
                        String age_s       = "";
                        String emr         = "";
                        String Top_Emotion = "";
                        Dictionary <string, float> Emotion = new Dictionary <string, float>();
                        foreach (var face in faces)
                        {
                            var      faceRect       = face.FaceRectangle;
                            var      attributes     = face.FaceAttributes;
                            float    Happiness      = attributes.Emotion.Happiness;
                            float    Anger          = attributes.Emotion.Anger;
                            float    Neutral        = attributes.Emotion.Neutral;
                            float    Contempt       = attributes.Emotion.Contempt;
                            float    Disgust        = attributes.Emotion.Disgust;
                            float    Fear           = attributes.Emotion.Fear;
                            float    Sadness        = attributes.Emotion.Sadness;
                            float    Surprise       = attributes.Emotion.Surprise;
                            String[] Emotion_string = { "Anger", "Happiness", "Neutral", "Contempt", "Disgust", "Fear", "Sadness", "Surprise" };
                            float[]  Emotion_array  = { Anger, Happiness, Neutral, Contempt, Disgust, Fear, Sadness, Surprise };

                            // g.DrawEllipse(new Pen(Brushes.Blue, 5), new System.Drawing.Rectangle(faceRect.Left-90, faceRect.Top-90,
                            //   faceRect.Width+150, faceRect.Height+150));

                            /* g.DrawRectangle(
                             * new Pen(Brushes.Red, 3),
                             * new System.Drawing.Rectangle(faceRect.Left, faceRect.Top,
                             *   faceRect.Width, faceRect.Height));*/
                            //g.DrawString(new Font(attributes.Gender.ToString(),));
                            for (int i = 0; i < Emotion_string.Length; i++)
                            {
                                Emotion.Add(Emotion_string[i], Emotion_array[i]);
                            }

                            if (attributes.Gender.StartsWith("male"))
                            {
                                isM += 1;
                            }
                            else
                            {
                                isF += 1;
                            }


                            age   = Convert.ToInt32(attributes.Age);
                            age_s = age.ToString();
                            sex   = attributes.Gender.ToString();

                            Top_Emotion = GetEmotion(attributes.Emotion);
                            Console.WriteLine("Emotion: " + Top_Emotion);
                            Console.WriteLine("Age: " + age_s);
                            Console.WriteLine("Female: " + isF);
                            Console.WriteLine("Male: " + isM);
                            //String name = "";
                            //people.Name = name;
                        }
                        String name = "";
                        foreach (var identifyResult in results)
                        {
                            // Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                            if (identifyResult.Candidates.Length == 0)
                            {
                                Console.WriteLine("No one identified");
                                name = "None";
                            }
                            else if (identifyResult.Candidates.Length != 0)
                            {
                                var candidateId = identifyResult.Candidates[0].PersonId;
                                var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                                Console.WriteLine("Identified as {0}", person.Name);
                                name = person.Name;
                            }
                        }
                        people.Name             = name;
                        people.Age              = age_s;
                        people.Gender           = sex;
                        people.Emotion          = Top_Emotion;
                        people.Emotionlistscore = Emotion;

                        SendMessageToCloud sc = new SendMessageToCloud();
                        sc.sendWindTurbineMessageToCloudAsync(people.Name, people.Age, people.Gender, people.Emotion, deviceConnectionString);

                        s.Close();
                        Emotion.Clear();
                        return("OK");
                    }
                    catch (FaceAPIException fs)
                    {
                        Console.WriteLine(fs.ToString());
                        return(null);
                    }
                }
            }
            catch (Exception e)
            {
                String msg = "Oops! Something went wrong. Try again later";
                if (e is ClientException && (e as ClientException).Error.Message.ToLowerInvariant().Contains("access denied"))
                {
                    msg += " (access denied - hint: check your APIKEY ).";
                    Console.Write(msg);
                }
                Console.Write(e.ToString());
                return(null);
            }
        }
        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            // Helper.SaveImageCapture((BitmapSource)captureImage.Source);

            try
            {
                Title = String.Format("Request: Training group \"{0}\"", personGroupId);
                await faceServiceClient.TrainPersonGroupAsync(personGroupId);

                TrainingStatus trainingStatus = null;
                while (true)
                {
                    await Task.Delay(1000);

                    trainingStatus = await faceServiceClient.GetPersonGroupTrainingStatusAsync(personGroupId);

                    Title = String.Format("Response: {0}. Group \"{1}\" training process is {2}", "Success", personGroupId, trainingStatus.Status);
                    if (trainingStatus.Status.ToString() != "running")
                    {
                        break;
                    }
                }
            }
            catch (FaceAPIException ex)
            {
                //MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                Title = String.Format("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
            }

            Title = "Identifing....";
            //string testImageFile = @"D:\Pictures\detection2.jpg";
            string getDirectory  = Directory.GetCurrentDirectory();
            string testImageFile = getDirectory + "\\test1.jpg";

            using (Stream s = File.OpenRead(testImageFile))
            {
                var faces = await faceServiceClient.DetectAsync(s);

                var faceIds = faces.Select(face => face.FaceId).ToArray();
                try
                {
                    var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                    foreach (var identifyResult in results)
                    {
                        //Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                        Title = String.Format("Result of face: {0}", identifyResult.FaceId);

                        if (identifyResult.Candidates.Length == 0)
                        {
                            //Console.WriteLine("No one identified");
                            Title = String.Format("No one identified");
                        }
                        else
                        {
                            // Get top 1 among all candidates returned
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                            //Console.WriteLine("Identified as {0}", person.Name);
                            Title = String.Format("Identified as {0}", person.Name);
                        }
                    }
                }
                catch (FaceAPIException ex)
                {
                    return;
                }
            }
        }
예제 #29
0
        /// <summary>
        /// Use the training data to spot individuals on a provided image
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnIdentify_Click(object sender, EventArgs e)
        {
            try
            {
                Face[] faces = await UploadAndDetectFaces(_imagePath);

                var faceIds = faces.Select(face => face.FaceId).ToArray();

                var faceBitmap = new Bitmap(imgBox.Image);
                idList.Items.Clear();

                using (var g = Graphics.FromImage(faceBitmap))
                {
                    foreach (var identifyResult in await faceServiceClient.IdentifyAsync(_groupId, faceIds))
                    {
                        if (identifyResult.Candidates.Length != 0)
                        {
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(_groupId, candidateId);

                            // Writes name above face rectangle
                            var x = faces.FirstOrDefault(y => y.FaceId == identifyResult.FaceId);
                            if (x != null)
                            {
                                g.DrawString(person.Name, this.Font, Brushes.White, x.FaceRectangle.Left, x.FaceRectangle.Top + x.FaceRectangle.Height + 15);
                            }

                            idList.Items.Add(person.Name);
                            voice.SpeakAsync(string.Format("Hello {0}, how are you doing?", person.Name));
                        }
                        else
                        {
                            idList.Items.Add("< Unknown person >");
                            voice.SpeakAsync("There is an unknown person.");
                        }
                    }
                }

                imgBox.Image = faceBitmap;
                //MessageBox.Show("Identification successfully completed");
            } catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                voice.SpeakAsync(ex.Message);
            }

            //try
            //{

            //    foreach(string name in idList.Items)
            //    {
            //        if (name != "< Unknown person >")
            //        {
            //            voice.SpeakAsync(string.Format("Hello {0}, how are you doing?", name));
            //        }
            //        else
            //        {
            //            voice.SpeakAsync("There is an unknown person.");
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //    voice.SpeakAsync(ex.Message);
            //}
        }
예제 #30
0
        private async Task <IdentifyInfos> identify_person_async(string path, string person_group_id)
        {
            Debug.WriteLine($"Identify photo: {path}"); //TODO

            // Init class
            IdentifyInfos infos = new IdentifyInfos {
                person = new Dictionary <string, string>(),
                info   = new AnalysisResult()
            };

            //Get current photo
            infos.path = path;

            // The list of Face attributes to return.
            IEnumerable <FaceAttributeType> faceAttributes = new FaceAttributeType[] { FaceAttributeType.Gender, FaceAttributeType.Age, FaceAttributeType.Smile, FaceAttributeType.Emotion, FaceAttributeType.Glasses, FaceAttributeType.Hair, FaceAttributeType.Accessories, FaceAttributeType.Blur, FaceAttributeType.Exposure, FaceAttributeType.FacialHair, FaceAttributeType.HeadPose, FaceAttributeType.Makeup, FaceAttributeType.Noise, FaceAttributeType.Occlusion };

            try {
                // Read file
                using (Stream stream = File.OpenRead(path)) {
                    infos.faces = await face_service_client.DetectAsync(stream, returnFaceId : true, returnFaceLandmarks : false, returnFaceAttributes : faceAttributes);

                    // If face identified in the photo
                    if (infos.faces.Length != 0)
                    {
                        Guid[] faceIds = infos.faces.Select(face => face.FaceId).ToArray();

                        IdentifyResult[] results = await face_service_client.IdentifyAsync(person_group_id, faceIds);

                        // Process face detected
                        foreach (var identifyResult in results)
                        {
                            Debug.WriteLine("Result of face: {0}", identifyResult.FaceId);
                            if (identifyResult.Candidates.Length == 0)
                            {
                                Debug.WriteLine("No one identified");
                                infos.person.Add(identifyResult.FaceId.ToString(), "not identified");
                            }
                            else
                            {
                                // Get top 1 among all candidates returned
                                var candidateId = identifyResult.Candidates[0].PersonId;
                                var person      = await face_service_client.GetPersonAsync(person_group_id, candidateId);

                                Debug.WriteLine("Identified as {0}", person.Name);
                                infos.person.Add(identifyResult.FaceId.ToString(), person.Name.ToString());
                            }
                        }
                    }
                    else
                    {
                        // No face identified
                    }
                }

                // If error during person identify
                //if(infos != null) {
                // If error during person identify or no person identified
                if (infos.faces.Length != 0)
                {
                    //Identify things in the photo
                    infos.info = await identify_image_async(path);
                }

                // If error during photo identify
                if (infos.info.ImageType != null)
                {
                    return(infos);
                }
                else
                {
                    return(null);
                }
            } catch (Exception ex) { infos = null; return(null); } // MessageBox.Show(ex.ToString());
        }