Exemple #1
0
        public void TrainRecognizer()
        {
            var allFaces = new FRService().All();

            if (allFaces.Count > 0)
            {
                var faceImages = new Image <Gray, byte> [allFaces.Count];
                var faceLabels = new int[allFaces.Count];
                for (int i = 0; i < allFaces.Count; i++)
                {
                    Stream stream = new MemoryStream();
                    stream.Write(allFaces[i].Face, 0, allFaces[i].Face.Length);
                    var faceImage = new Image <Gray, byte>(new Bitmap(stream));
                    faceImages[i] = faceImage.Resize(100, 100, Inter.Cubic);
                    faceLabels[i] = (int)(allFaces[i].Id);
                }

                // can also try :LBPHFaceRecognizer
                var fr = new EigenFaceRecognizer();
                fr.Train(faceImages, faceLabels);

                var retPath   = ConfigurationManager.AppSettings["trainedPath"];
                var savedFile = retPath + $"{DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss")}_frModel";
                fr.Save(savedFile);

                MessageBox.Show($"Model trained successfully. saved into {savedFile}");
            }
            else
            {
                MessageBox.Show("No face found in db");
            }
        }
Exemple #2
0
        private void btnEnroll_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtName.Text))
            {
                MessageBox.Show("Name can not be empty");
                return;
            }

            var name      = txtName.Text;
            var file      = lblFile.Tag.ToString();
            var fileData  = File.ReadAllBytes(file);
            var frService = new FRService();
            var error     = "";

            frService.Enroll(new UserFace()
            {
                UserName = name,
                Face     = fileData
            }, out error);
            if (!string.IsNullOrWhiteSpace(error))
            {
                MessageBox.Show(error, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                lblFile.Text = "";
                txtName.Text = "";
                MessageBox.Show("enrolled successfully.");
            }
        }
Exemple #3
0
        private void btnRecognize_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(PathPhoto) || string.IsNullOrWhiteSpace(
                    PathModel))
            {
                MessageBox.Show("need to select photo and model");
            }
            else
            {
                try
                {
                    var userBmp   = new Bitmap(PathPhoto);
                    var userImage = new Image <Gray, byte>(userBmp);

                    _faceRecognizer.Load(PathModel);
                    var result = _faceRecognizer.Predict(userImage.Resize(100, 100, Inter.Cubic));
                    var userId = result.Label;

                    var userRecord = new FRService().GetById(userId);
                    if (userRecord != null)
                    {
                        lblResult.Text = userRecord.UserName;
                    }
                    else
                    {
                        MessageBox.Show("User not enrolled in db");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Exemple #4
0
        public static IForm <EnqueueForm> BuildForm()
        {
            OnCompletionAsyncDelegate <EnqueueForm> processOrder = async(context, state) =>
            {
                //Get the actual state of the FaceRecognitionModel , for then get the object id, photoId, name and file name
                FaceRecognitionModel faceRecognitionState = new FaceRecognitionModel();
                AppoinmentService    appointmentService   = new AppoinmentService();
                try
                {
                    if (!context.UserData.TryGetValue <FaceRecognitionModel>("FaceRecognitionModel", out faceRecognitionState))
                    {
                        faceRecognitionState = new FaceRecognitionModel();
                    }

                    FRService frService = new FRService();
                    int       caseId    = await frService.enqueueCustomer(faceRecognitionState.ObjectId, faceRecognitionState.PhotoId, faceRecognitionState.Name, faceRecognitionState.FileName);

                    //Get the actual user state of the customer
                    ACFCustomer customerState = new ACFCustomer();
                    try
                    {
                        if (!context.UserData.TryGetValue <ACFCustomer>("customerState", out customerState))
                        {
                            customerState = new ACFCustomer();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Not exists a user session");
                    }
                    //In this case I have the case, that has been enqueue, but the middleware don´thave a method for get by case id,
                    //but have yes a method for get a list of cases of customer id
                    if (caseId > 0)
                    {
                        Case _case = AppoinmentService.GetCaseById(customerState.CustomerId);
                        await context.PostAsync($"Your ticket was created, your number is: " + _case.QCode + _case.QNumber);
                    }
                    else
                    {
                        throw new Exception("Error: The case Id = 0");
                    }
                }
                catch (Exception ex)
                {
                    await context.PostAsync($"Failed with message: {ex.Message}");
                }
            };
            CultureInfo ci = new CultureInfo("en");

            Thread.CurrentThread.CurrentCulture   = ci;
            Thread.CurrentThread.CurrentUICulture = ci;
            FormBuilder <EnqueueForm> form = new FormBuilder <EnqueueForm>();

            return(form   //Message("Wait a moment please")
                   .Field(new FieldReflector <EnqueueForm>(nameof(UserId)).SetActive(InactiveField))
                   .AddRemainingFields()
                   .OnCompletion(processOrder)
                   .Build());
        }
        /// <summary>
        /// This method create IForm for save the customer information
        /// </summary>
        /// <returns></returns>
        public static IForm <SaveCustomerForm> BuildForm()
        {
            OnCompletionAsyncDelegate <SaveCustomerForm> processOrder = async(context, state) =>
            {
                ACFCustomer customer = new ACFCustomer();
                try
                {
                    customer.CustomerId  = 0;//It is setted to 0, beacuse we will create new user, in other hand we can pass the id for update the record
                    customer.FirstName   = state.FirstName;
                    customer.LastName    = state.LastName;
                    customer.Email       = state.Email;
                    customer.PhoneNumber = state.PhoneNumber;
                    customer.Sex         = GetIntSex(state.Sex.ToString());
                    int customerId = AppoinmentService.SaveCustomer(customer.PhoneNumber, customer.Email, customer.FirstName,
                                                                    customer.LastName, customer.Sex, customer.PhoneNumber, customer.CustomerId);
                    state.customerId = customerId.ToString();
                    if (customerId > 0)
                    {
                        FRService frService = new FRService();
                        //Get the idImage Saved
                        int idImageSaved = 0;
                        if (!context.UserData.TryGetValue <int>("idImageSaveId", out idImageSaved))
                        {
                            idImageSaved = 0;
                        }

                        //Set the FaceRecognition Model,
                        FaceRecognitionModel faceRecognitionModel = new FaceRecognitionModel();
                        faceRecognitionModel.ObjectId = Utilities.Util.generateObjectId(customer.FirstName, customer.LastName, customer.PhoneNumber);
                        faceRecognitionModel.PhotoId  = savedImageId.ToString();
                        faceRecognitionModel.Name     = imageName;
                        faceRecognitionModel.FileName = imageName;
                        context.UserData.SetValue <FaceRecognitionModel>("FaceRecognitionModel", faceRecognitionModel);

                        bool uploadImage = await frService.uploadImage(customer.FirstName, customer.LastName, customer.PhoneNumber
                                                                       , imageName, savedImageId);

                        await context.PostAsync("Your user has been saved succesfully with the id: " + customerId);

                        /* Create the userstate */
                        //Instance of the object ACFCustomer for keept the customer
                        ACFCustomer customerState = new ACFCustomer();
                        customerState.CustomerId  = customerId;
                        customerState.FirstName   = customer.FirstName;
                        customerState.PhoneNumber = customer.PhoneNumber;
                        customerState.Sex         = Convert.ToInt32(customer.Sex);
                        customerState.PersonaId   = customer.PhoneNumber;
                        context.UserData.SetValue <ACFCustomer>("customerState", customerState);
                        context.UserData.SetValue <bool>("SignIn", true);
                    }
                }
                catch (Exception ex)
                {
                    await context.PostAsync("We have an error: " + ex.Message);
                }
            };
            CultureInfo ci = new CultureInfo("en");

            Thread.CurrentThread.CurrentCulture   = ci;
            Thread.CurrentThread.CurrentUICulture = ci;
            var culture  = Thread.CurrentThread.CurrentUICulture;
            var form     = new FormBuilder <SaveCustomerForm>();
            var yesTerms = form.Configuration.Yes.ToList();
            var noTerms  = form.Configuration.No.ToList();

            yesTerms.Add("Yes");
            noTerms.Add("No");
            form.Configuration.Yes = yesTerms.ToArray();
            return(form.Message("Fill the next information, please")
                   .Field(nameof(FirstName))
                   .Field(nameof(LastName))
                   .Field(nameof(Email))
                   .Field(nameof(PhoneNumber))
                   .Field(nameof(Sex))
                   .Field(new FieldReflector <SaveCustomerForm>(nameof(customerId)).SetActive(inActiveField))
                   .Confirm("Are you selected the information: " +
                            "\n* Name: {FirstName} " +
                            "\n* Last name:  {LastName} " +
                            "\n* Email:  {Email} " +
                            "\n* Phone Number: {PhoneNumber} " +
                            "\n* Sex:  {Sex}? \n" +
                            "(yes/no)")
                   .AddRemainingFields()
                   .Message("The process for save your user has been started!")
                   .OnCompletion(processOrder)
                   .Build());
        }
        /// <summary>
        /// This method create IForm for save the customer information
        /// </summary>
        /// <returns></returns>
        public static IForm <SaveCustomerFormApp> BuildForm()
        {
            OnCompletionAsyncDelegate <SaveCustomerFormApp> processOrder = async(context, state) =>
            {
                ACFCustomer customer = new ACFCustomer();
                try
                {
                    customer.CustomerId = 0;//It is setted to 0, beacuse we will create new user, in other hand we can pass the id for update the record

                    if (!string.IsNullOrEmpty(state.name) && !string.IsNullOrEmpty(state.LastName))
                    {
                        try
                        {
                            customer.FirstName = state.name;
                            customer.LastName  = state.LastName;
                        }
                        catch (Exception)
                        {
                        }
                    }
                    customer.PhoneNumber = state.PhoneNumber;
                    int customerId = AppoinmentService.SaveCustomer(customer.PhoneNumber, customer.FirstName,
                                                                    customer.LastName, customer.PhoneNumber, customer.CustomerId);
                    state.customerId = customerId.ToString();
                    if (customerId > 0)
                    {
                        FRService frService = new FRService();
                        //Get the idImage Saved
                        int idImageSaved = 0;
                        if (!context.UserData.TryGetValue <int>("idImageSaveId", out idImageSaved))
                        {
                            idImageSaved = 0;
                        }
                        //Set the FaceRecognition Model,
                        FaceRecognitionModel faceRecognitionModel = new FaceRecognitionModel();
                        faceRecognitionModel.ObjectId = Utilities.Util.generateObjectId(customer.FirstName, customer.LastName, customer.PhoneNumber);
                        faceRecognitionModel.PhotoId  = idImageSaved.ToString();
                        faceRecognitionModel.Name     = imageName;
                        faceRecognitionModel.FileName = imageName;
                        context.UserData.SetValue <FaceRecognitionModel>("FaceRecognitionModel", faceRecognitionModel);
                        bool uploadImage = await frService.uploadImage(customer.FirstName, customer.LastName, customer.PhoneNumber
                                                                       , imageName, idImageSaved);

                        await context.PostAsync("Congratulations, your account was created!");

                        /* Create the userstate */
                        //Instance of the object ACFCustomer for keept the customer
                        ACFCustomer customerState = new ACFCustomer();
                        customerState.CustomerId  = customerId;
                        customerState.FirstName   = customer.FirstName;
                        customerState.PhoneNumber = customer.PhoneNumber;
                        customerState.PersonaId   = customer.PhoneNumber;
                        context.UserData.SetValue <ACFCustomer>("customerState", customerState);
                        context.UserData.SetValue <bool>("SignIn", true);
                    }
                }
                catch (Exception ex)
                {
                    await context.PostAsync("We have an error: " + ex.Message);
                }
            };
            CultureInfo ci = new CultureInfo("en");

            Thread.CurrentThread.CurrentCulture   = ci;
            Thread.CurrentThread.CurrentUICulture = ci;
            var culture  = Thread.CurrentThread.CurrentUICulture;
            var form     = new FormBuilder <SaveCustomerFormApp>();
            var yesTerms = form.Configuration.Yes.ToList();
            var noTerms  = form.Configuration.No.ToList();

            yesTerms.Add("Yes");
            noTerms.Add("No");
            form.Configuration.Yes = yesTerms.ToArray();
            return(form//.Message("Fill the next information, please")
                   .Field(nameof(name))
                   .Field(nameof(LastName))
                   .Field(nameof(PhoneNumber), validate: async(state, response) =>
            {
                //This source code appear in warning beacuse not have a method async implemented, but works good without these method
                Regex rgx = new Regex(@"^[1-9][0-9]{7,14}$");
                //Regex rgx = new Regex(@"^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$");
                var result = new ValidateResult {
                    IsValid = true, Value = response
                };
                if (!string.IsNullOrEmpty(result.Value.ToString()))
                {
                    if (rgx.IsMatch(result.Value.ToString()))
                    {
                        result.IsValid = true;
                    }
                    else
                    {
                        result.Feedback = "Invalid phone number format";
                        result.IsValid = false;
                    }
                }
                return result;
            })
                   .Field(new FieldReflector <SaveCustomerFormApp>(nameof(customerId)).SetActive(inActiveField))
                   .AddRemainingFields()
                   .OnCompletion(processOrder)
                   .Build());
        }