Esempio n. 1
0
 /// <summary>
 /// Dispose
 /// </summary>
 public void Dispose()
 {
     //if exist dispose
     if (faceClient != null)
     {
         faceClient.Dispose();
     }
 }
        public async Task CropFacesAsync(Label results, Button takingPicBtn = null, Button fileExplorerBtn = null, Button ImageTestingBtn = null)
        {
            results.Text += "\n";

            if (takingPicBtn != null)
            {
                // Some opearations can't be done at the same time; as the system doesn't support asynchronous.
                takingPicBtn.Enabled    = false;
                fileExplorerBtn.Enabled = false;
                ImageTestingBtn.Enabled = false;
            }

            // Get DateTime from the server.
            currentDateTime = GetDateTimeFromServer();

            // In order to prevet using server caching, and not to refresh the URL with the new image for testing.
            string guid = "?cache=" + Guid.NewGuid().ToString();

            // I have to upload the image to Azure in order to have an "http" URL & to be able to get the
            // coordinates of all the faces.
            UploadImageToStorage(imageName);

            faceClient = new FaceClient(
                new ApiKeyServiceClientCredentials(subscriptionKey),
                new DelegatingHandler[] { });
            faceClient.Endpoint = uriBase;

            // Get coordinates of all the faces in the image.
            IList <DetectedFace> faces = await faceClient.Face.DetectWithUrlAsync("https://sarabenshabbatproject.blob.core.windows.net/newcontainer/" + imageName + guid, true, false);

            // Crop all the faces in the image.
            Image img = Image.FromFile(@"C:\Generating Attendance Reports - Files\" + imageName);

            // If, at least - one face detected in the img.
            if (faces.Count != 0)
            {
                // Don't forget - Erase this code. (- It's for cropping the faces have deytected in the img & create new ones.)
                //{
                // Go over all the detected coordinates, and create new image(s) according to these.
                //Image croppedFaceImg;
                //FaceRectangle sample;
                //Rectangle cropArea;
                //Bitmap bmpImage = null;
                //int index = 0;
                //
                //foreach (var face in faces)
                //{
                //    sample = face.FaceRectangle;
                //    cropArea = new Rectangle(sample.Left, sample.Top, sample.Width, sample.Height);
                //    bmpImage = new Bitmap(img);
                //
                //    croppedFaceImg = bmpImage.Clone(cropArea, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                //    croppedFaceImg.Save(@"..\..\..\..\" + imageName.Substring(0, imageName.Length - 4) + "_" + (index++) + ".jpg");
                //
                //    bmpImage.Dispose();
                //}

                // Go over all the detected faces: Try to find similar from the company employees & if ther's need, wrtie to DB.
                foreach (var face in faces)
                {
                    await FindSimilar(face.FaceId.ToString(), results);
                }
                results.Text += "New group";
            }

            //}
            else
            {
                results.Text += "No faces for testing, have been detected\n----------------\n";
            }

            // Dispose the server client
            faceClient.HttpClient.Dispose();
            faceClient.Dispose();

            // Release all resorces used by this image.
            img.Dispose();

            // Delete all the image(s) used in this method, from local PC.
            DeleteImages();

            if (takingPicBtn != null)
            {
                // The user can do whatever he wants.
                takingPicBtn.Enabled    = true;
                fileExplorerBtn.Enabled = true;
            }
        }