Exemple #1
0
        public async Task <ScreenshotResult> Screenshot(string URL, string DirectoryPath, bool IncludeLogoWatermark, string WatermarkFilePath, Point CropStartPoint, Size CropSize, bool DoCropImage)
        {
            if (LoadedFine)
            {
                try
                {
                    string ImagePath = DirectoryPath + "Screenshot-" + DateTime.UtcNow.Month.ToString() + "-" + DateTime.UtcNow.Day.ToString() + "-" + DateTime.UtcNow.Year.ToString() + "-" + DateTime.UtcNow.Hour.ToString() + "h" + DateTime.UtcNow.Minute.ToString() + "m" + DateTime.UtcNow.Second.ToString() + "s.png";

                    driver.Navigate()
                    .GoToUrl(URL);

                    Thread.Sleep(3000);

                    var bytearray = driver.GetScreenshot().AsByteArray;

                    //driver.Quit();

                    Bitmap bmp;
                    using (var ms = new MemoryStream(bytearray))
                    {
                        bmp = new Bitmap(ms);
                    }

                    Bitmap CroppedImage;
                    if (DoCropImage)
                    {
                        Rectangle section = new Rectangle(CropStartPoint, CropSize);
                        CroppedImage = CropImage(bmp, section);
                    }
                    else
                    {
                        CroppedImage = bmp;
                    }

                    if (IncludeLogoWatermark)
                    {
                        Bitmap Logo;

                        if (WatermarkFilePath != "")
                        {
                            if (File.Exists(WatermarkFilePath))
                            {
                                Logo = new Bitmap(WatermarkFilePath);
                            }
                            else
                            {
                                Logo = Properties.Resources.Watermark;
                            }
                        }
                        else
                        {
                            Logo = Properties.Resources.Watermark;
                        }


                        Bitmap FinalLogo = ResizeBitmap(Logo, Logo.Width / 3, Logo.Height / 3);


                        Graphics gCroppedImage = Graphics.FromImage(CroppedImage);
                        gCroppedImage.DrawImage(FinalLogo, CroppedImage.Width / 2 - FinalLogo.Width / 2, 70, new Rectangle(0, 0, FinalLogo.Width, FinalLogo.Height), GraphicsUnit.Pixel);

                        CroppedImage.Save(ImagePath, ImageFormat.Png);

                        gCroppedImage.Dispose();
                        FinalLogo.Dispose();
                        Logo.Dispose();
                    }
                    else
                    {
                        CroppedImage.Save(ImagePath, ImageFormat.Png);
                    }

                    CroppedImage.Dispose();
                    bmp.Dispose();

                    return(new ScreenshotResult(true, ImagePath));
                }
                catch (Exception ex)
                {
                    return(new ScreenshotResult(false, ""));
                }
            }
            else
            {
                return(new ScreenshotResult(false, ""));
            }
        }
Exemple #2
0
        /// <summary>
        /// This is testting Face module
        /// </summary>
        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, "https://southeastasia.api.cognitive.microsoft.com/face/v1.0");
                string            personGroupId = "test";
                // await fc.CreatePersonGroupAsync(personGroupId,"My Family");
                //using (Stream s = File.OpenRead(testImageFile))
                //{
                //    if (s != null)
                //        return "yes";
                //    else
                //        return "no";
                //}
                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 = await fc.DetectAsync(s, returnFaceLandmarks : true, returnFaceAttributes : requiredFaceAttributes);

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

                        var fspicture = new FileStream(testImageFile, FileMode.Open);

                        Bitmap bmp = new Bitmap(fspicture);

                        Graphics g = Graphics.FromImage(bmp);

                        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(Top_Emotion);
                            //Font drawFont = new Font("Arial", 60, FontStyle.Bold);
                            //SolidBrush drawBrush = new SolidBrush(Color.Blue);
                            //PointF drawPoint = new PointF(faceRect.Left - 90, faceRect.Top - 50);
                            //PointF drawPoint2 = new PointF(faceRect.Left - 10, faceRect.Top - 50);
                            //g.DrawString(age_s, drawFont, drawBrush, drawPoint);
                            //g.DrawString(sex, drawFont, drawBrush, drawPoint2);



                            Bitmap CroppedImage = null;

                            if (face.FaceAttributes.HeadPose.Roll >= 10 || face.FaceAttributes.HeadPose.Roll <= -10)

                            {
                                System.Drawing.Rectangle rect = new System.Drawing.Rectangle(Convert.ToInt32(face.FaceRectangle.Left - 200), Convert.ToInt32(face.FaceRectangle.Top - 200), face.FaceRectangle.Width + 200, face.FaceRectangle.Height + 200);

                                CroppedImage = new Bitmap(CropRotatedRect(bmp, rect, Convert.ToSingle(face.FaceAttributes.HeadPose.Roll * -1), true));
                            }

                            else

                            {
                                try
                                {
                                    CroppedImage = new Bitmap(bmp.Clone(new System.Drawing.Rectangle(face.FaceRectangle.Left - 150, face.FaceRectangle.Top - 150, face.FaceRectangle.Width + 300, face.FaceRectangle.Height + 300), bmp.PixelFormat));
                                }
                                catch (Exception e)
                                {
                                }
                            }
                            bmp.Dispose();
                            CroppedImage.Save(@"C:\Users\v-altsai\Pictures\allfix.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                            CroppedImage.Dispose();
                            Console.WriteLine("Age " + age);
                        }
                        Console.WriteLine("Female: " + isF);
                        Console.WriteLine("Male: " + isM);
                        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 fc.GetPersonAsync(personGroupId, candidateId);

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

                                Bitmap oribmp = new Bitmap(@"C:\Users\v-altsai\Pictures\allfix.jpg");

                                using (Bitmap tmpBmp = new Bitmap(oribmp))

                                {
                                    Mount_path.Clear();

                                    Mount_path.Append("C:\\Users\\v-altsai\\Pictures\\Family\\");

                                    Mount_path.Append(person.Name);

                                    Mount_path.Append(".jpg");

                                    tmpBmp.Save(Mount_path.ToString(), System.Drawing.Imaging.ImageFormat.Jpeg);

                                    Console.WriteLine("{0}", Mount_path.ToString());
                                }
                            }
                        }



                        // bmp.Save(@"C:\Users\v-altsai\Documents\Visual Studio 2017\Projects\Kinect-FaceRecognition\Images\allfix.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

                        people.Name             = name;
                        people.Age              = age_s;
                        people.Gender           = sex;
                        people.Emotion          = Top_Emotion;
                        people.Emotionlistscore = Emotion;
                        JSONHelper helper     = new JSONHelper();
                        String     jsonResult = "";
                        jsonResult = helper.ConvertObjectToJSon(people);
                        fspicture.Close();
                        s.Close();
                        Emotion.Clear();
                        return(jsonResult);
                    }
                    catch (FaceAPIException fs)
                    {
                        Console.WriteLine(fs.ToString());
                        Console.WriteLine("error results");
                        //if (s != null)
                        //    s.Close();
                        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);
            }
        }