Exemplo n.º 1
0
        /// <summary></summary>
        private async void processImage(Bitmap image)
        {
            await
            Task.Run(
                async() =>
                //This code is intended to run on a new thread, control is returned to the caller on the UI thread.
            {
                Guid photoID = Guid.NewGuid();

                string photoName = photoID.ToString() + ".png";         //file name
                string imagePath = System.IO.Path.Combine(App.imageSavePath, photoName);

                using (var fileStream = new FileStream(imagePath, FileMode.Create))
                {
                    image.Save(fileStream, ImageFormat.Png);
                    fileStream.Close();
                }

                App.WriteMessage("Photo Saved To File");

                //Check for Face & Get Attributes
                bool intruder   = false;
                string username = "";

                //API Call to Microsoft Cognitive
                List <Face> faces = await App.UploadAndDetectFaces(imagePath);

                if (faces != null)
                {
                    if (faces.Count > 0)
                    {
                        //Face Detected
                        App.WriteMessage("Faces Detected From API");

                        //Compare to Owner
                        username = await App.VerifyPerson(faces.Select(f => f.FaceId).ToArray());

                        if (username == "")
                        {
                            //Face detected, not owner
                            intruder = true;
                        }
                    }
                    else
                    {
                        App.WriteMessage("No Faces Detected");
                    }
                }

                //Test to see if we have an intruder
                if (intruder)
                {
                    //Check we have not sent a notification in the past 2 minutes
                    if (DateTime.Now.Subtract(lastNotification).TotalMinutes >= 2)
                    {
                        //Upload Photo to Google Drive
                        byte[] byteArray = File.ReadAllBytes(System.IO.Path.Combine(App.imageSavePath, photoName));

                        //Make Sure I have Google Drive configured
                        if (App.AllowGoogleDrive)
                        {
                            await GoogleAppAuthorisation.AuthorizeAndUpload(byteArray, photoName);
                            App.WriteMessage("Photo Uploaded to Drive");
                        }

                        //Send Notification
                        if (App.smsClient != null)
                        {
                            App.smsClient.sendSMS();
                            App.WriteMessage("SMS Notification Sent");
                        }

                        lastNotification = DateTime.Now;
                    }

                    //Display Verification Failed Message
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        this.cameraWindow.Visibility = Visibility.Collapsed;

                        MessageWindow msg = new MessageWindow {
                            Owner = this, state = 0
                        };
                        msg.ShowDialog();
                    }));
                }
                else if (username != "")
                {
                    //Delete Image - No Need to keep saved image of verified
                    File.Delete(imagePath);

                    //Check if we could verify, but not confident enough
                    if (username != "N/C")
                    {
                        //Unlock Screensaver
                        Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            this.cameraWindow.Visibility = Visibility.Collapsed;

                            MessageWindow msg = new MessageWindow {
                                Owner = this, state = 1, username = username
                            };
                            msg.ShowDialog();

                            App.CloseApplication = true;
                            this.Close();
                        }));
                    }
                    else
                    {
                        //Verified, just not confidence
                        //Display Verification Failed Message
                        Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            this.cameraWindow.Visibility = Visibility.Collapsed;

                            MessageWindow msg = new MessageWindow {
                                Owner = this, state = 2
                            };
                            msg.ShowDialog();

                            //Keep Window Open for re-verification
                            this.cameraWindow.Visibility = Visibility.Visible;
                        }));
                    }
                }
                else
                {
                    //Delete Image - No Need to keep saved image of nothing
                    File.Delete(imagePath);

                    //No Faces Detected
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        cameraLabel.Content = "Verifying User";

                        this.cameraWindow.Visibility = Visibility.Collapsed;
                    }));
                }

                working = false;
            });
        }
Exemplo n.º 2
0
        private async void Application_Startup(object sender, StartupEventArgs e)
        {
            bool displayScreensaver = false;

            if (e.Args.Length == 0 || e.Args[0].ToLower().StartsWith("/s"))
            {
                //Set Image Storage Path & Create Directory if required
                imageSavePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Captures");
                if (!Directory.Exists(imageSavePath))
                {
                    Directory.CreateDirectory(imageSavePath);
                }

                //Load Settings From Configuration File
                if (File.Exists("Settings.xml"))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load("Settings.xml");

                    bool   SMSEnabled = false;
                    string SMSCarrier = "";
                    string SMSNumber  = "";
                    string SMSMessage = "";

                    XmlNode node = doc.SelectSingleNode("Settings");
                    foreach (var ele in node)
                    {
                        if (ele.GetType() == typeof(XmlElement))
                        {
                            //We only want to collect data from XmlElements
                            XmlElement xele = (XmlElement)ele;
                            switch (xele.Name.ToLower())
                            {
                            case "faceid":
                                faceGroupID = xele.InnerText;
                                break;

                            case "sms":
                                if (xele.HasChildNodes)
                                {
                                    //Get SMS Details from Child Node
                                    foreach (var eSMS in xele.ChildNodes)
                                    {
                                        if (eSMS.GetType() == typeof(XmlElement))
                                        {
                                            //Again we only want XmlElements
                                            XmlElement xeSMS = (XmlElement)eSMS;

                                            switch (xeSMS.Name.ToLower())
                                            {
                                            case "enabled":
                                                SMSEnabled = Boolean.Parse(xeSMS.InnerText);
                                                break;

                                            case "carrier":
                                                SMSCarrier = xeSMS.InnerText;
                                                break;

                                            case "mobilenumber":
                                                SMSNumber = xeSMS.InnerText;
                                                break;

                                            case "message":
                                                SMSMessage = xeSMS.InnerText;
                                                break;
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }

                    //Configure Software for Settings
                    if (faceGroupID != "")
                    {
                        //Check Face Group
                        var result = await faceServiceClient.GetPersonGroupAsync(faceGroupID);

                        if (result.PersonGroupId == "")
                        {
                            facialCheckDisabled = true;
                        }
                    }
                    else
                    {
                        //No Face Group
                        //Facial Recognition will be disabled - this is now a general screensaver
                        facialCheckDisabled = true;
                    }

                    //SMS Settings
                    if (SMSEnabled)
                    {
                        switch (SMSCarrier.ToLower())
                        {
                        case "telstra":
                            smsClient = new TelstraSMSService(SMSNumber);
                            break;

                        case "optus":
                            smsClient = new OptusSMSService(SMSNumber);
                            break;

                        case "Vodafone":
                            smsClient = new VodafoneSMSService(SMSNumber);
                            break;
                        }

                        if (smsClient != null && SMSMessage != "")
                        {
                            smsClient.setMessage(SMSMessage);
                        }

                        App.WriteMessage("SMS Service Configured");
                    }

                    App.WriteMessage("Settings File Loaded");
                }
                else
                {
                    //No settings file found
                    //Disable Facial Recognition
                    facialCheckDisabled = true;
                }

                //Google Drive Authorisation
                string storageDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Credentials");
                if (Directory.Exists(storageDirectory))
                {
                    if (Directory.EnumerateFiles(storageDirectory).Count() > 0)
                    {
                        AllowGoogleDrive = true;
                    }
                }

                if (AllowGoogleDrive)
                {
                    GoogleAppAuthorisation.BuildCredentails(storageDirectory);
                }

                //Get IP Address of System
                fetchIP();

                //Display the Screensaver
                displayScreensaver = true;
            }
            else if (e.Args[0].ToLower().StartsWith("/p"))
            {
                facialCheckDisabled = true;

                //No Preview Available

                //Close this application
                App.CloseApplication = true;
                Environment.Exit(0);
            }
            else if (e.Args[0].ToLower().StartsWith("/c"))
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Horus");

                if (key != null)
                {
                    string appPath = (string)key.GetValue("AppPath");

                    if (appPath != null)
                    {
                        //Start configuration Tool
                        Process process = new Process();
                        process.StartInfo.FileName         = "horus-config.exe";
                        process.StartInfo.UseShellExecute  = true;
                        process.StartInfo.WorkingDirectory = appPath;
                        process.Start();
                    }
                }

                //Close this application
                App.CloseApplication = true;
                Environment.Exit(0);
            }

            if (displayScreensaver)
            {
                //Load Screensaver Windows
                foreach (Screen s in Screen.AllScreens)
                {
                    if (s != Screen.PrimaryScreen)
                    {
                        Blank window = new Blank();
                        window.Left   = s.WorkingArea.Left;
                        window.Top    = s.WorkingArea.Top;
                        window.Width  = s.WorkingArea.Width;
                        window.Height = s.WorkingArea.Height;
                        window.Show();
                    }
                    else
                    {
                        MainWindow window = new MainWindow();
                        window.Left   = s.WorkingArea.Left;
                        window.Top    = s.WorkingArea.Top;
                        window.Width  = s.WorkingArea.Width;
                        window.Height = s.WorkingArea.Height;
                        window.Show();
                    }
                }
            }
        }