示例#1
0
                public static async Task <SimilarPersistedFace[]> TestFindSimilars(string imageFilePath, string FaceId, string filePathBase)
                {
                    string[] files = Directory.GetFiles(filePathBase, "*.jpg", SearchOption.AllDirectories);
                    await faceServiceClient.DeleteFaceListAsync(faceList);

                    await faceServiceClient.CreateFaceListAsync(faceList, faceList, faceList);

                    foreach (string s in files)
                    {
                        var fileName = s.Split('\\').Last();

                        await AddFaceToFaceList(s, fileName);
                    }

                    try
                    {
                        using (Stream imageFileStream = File.OpenRead(imageFilePath))
                        {
                            IEnumerable <FaceAttributeType> faceAttributes =
                                new FaceAttributeType[] { FaceAttributeType.Gender, FaceAttributeType.Age, FaceAttributeType.Smile, FaceAttributeType.Glasses };
                            Face[] faces = await faceServiceClient.DetectAsync(imageFileStream, returnFaceId : true, returnFaceLandmarks : false, returnFaceAttributes : faceAttributes);

                            if (faces.Any())
                            {
                                var result = await faceServiceClient.FindSimilarAsync(faces[0].FaceId, faceList, 10);

                                //await faceServiceClient.DeleteFaceFromFaceListAsync(faceList, faces[0]);
                                return(result);
                            }
                        }
                    }
                    // Catch and display Face API errors.
                    catch (FaceAPIException f)
                    {
                        // MessageBox.Show(f.ErrorMessage, f.ErrorCode);
                        return(null);
                    }
                    // Catch and display all other errors.
                    catch (Exception e)
                    {
                        //MessageBox.Show(e.Message, "Error");
                        return(null);
                    }
                    return(null);
                }
示例#2
0
        private async Task <SimilarPersistedFace[]> CheckForSimilarity(MSFace aMSFace, string faceListId)
        {
            try
            {
                var similarFaces = await _faceServiceClient.FindSimilarAsync(aMSFace.Id,
                                                                             faceListId, FindSimilarMatchMode.matchFace);

                if (similarFaces.Length == 0)
                {
                    throw new Exception("There is no similar faces");
                }
                return(similarFaces);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(new SimilarPersistedFace[0]);
            }
        }
        private async void button3_OnClick(object sender, RoutedEventArgs e)
        {
            if (myFaceId.Equals(Guid.Empty))
            {
                await new MessageDialog("No Face to Compare!").ShowAsync();
                return;
            }

            StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFolder assets             = await appInstalledFolder.GetFolderAsync("Images");

            var files = await assets.GetFilesAsync();

            if (files.Count < 1)
            {
                await new MessageDialog("No Files Available to Compare!").ShowAsync();
                return;
            }

            double      maxsimilarity = 0.0;
            StorageFile maxfile       = null;

            new MessageDialog("Comparing... Please Wait").ShowAsync();

            Face[] GFace3 = null;
            foreach (var file2 in files)
            {
                var GFace2 = await UploadAndDetectFaces2(file2);

                Guid[] GFace2Ids = GFace2.Select(face => face.FaceId).ToArray();
                if (GFace2Ids.Length < 1)
                {
                    continue;
                }
                SimilarFace[] temp = await faceServiceClient.FindSimilarAsync(myFaceId, GFace2Ids);

                if (temp.Length > 0)
                {
                    foreach (var smf in temp)
                    {
                        if (smf.Confidence > maxsimilarity)
                        {
                            maxsimilarity = smf.Confidence;
                            maxfile       = file2;
                            GFace3        = GFace2;
                        }
                    }
                }
            }

            if (maxsimilarity == 0.0)
            {
                await new MessageDialog("No Match Found!").ShowAsync();
                return;
            }

            IRandomAccessStream astream = await maxfile.OpenReadAsync();

            System.IO.Stream ostream = await maxfile.OpenStreamForReadAsync();

            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(astream);

            WriteableBitmap writeableBmp = await BitmapFactory.New(1, 1).FromStream(ostream);

            facephoto2.Source = writeableBmp;

            // draw rectangles
            FaceRectangle[] faceRects = GFace3.Select(face => face.FaceRectangle).ToArray();

            await new MessageDialog(String.Format("Detection Finished. {0} face(s) detected", faceRects.Length)).ShowAsync();

            if (faceRects.Length > 0)
            {
                double dpi          = decoder.DpiX;
                double resizeFactor = 1;


                foreach (var faceRect in faceRects)
                {
                    writeableBmp.DrawRectangle(
                        (int)(faceRect.Left * resizeFactor),
                        (int)(faceRect.Top * resizeFactor),
                        (int)((faceRect.Left + faceRect.Width) * resizeFactor),
                        (int)((faceRect.Top + faceRect.Height) * resizeFactor),
                        Colors.Red);
                }

                /*drawingContext.Close();
                 * RenderTargetBitmap faceWithRectBitmap = new RenderTargetBitmap(
                 *  (int)(bitmapSource.PixelWidth * resizeFactor),
                 *  (int)(bitmapSource.PixelHeight * resizeFactor),
                 *  96,
                 *  96,
                 *  PixelFormats.Pbgra32);
                 *
                 * faceWithRectBitmap.Render(visual);*/
                facephoto2.Source = writeableBmp;
            }

            await new MessageDialog("Similarity: " + (maxsimilarity * 100).ToString() + "%").ShowAsync();
        }
示例#4
0
 public Task <SimilarFace[]> FindSimilarAsync(Guid faceId, Guid[] faceIds, int maxNumOfCandidatesReturned = 20)
 {
     return(innerClient.FindSimilarAsync(faceId, faceIds, maxNumOfCandidatesReturned));
 }