Пример #1
0
        public ResponseMessage addEmployee(string rfid, EmployeePic employee)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Max-Age", "1728000");

            try
            {
                createDirectories(rfid);
                byte[] pict     = Convert.FromBase64String(employee.picture);
                string pictName = employee.employee.rfid;
                string path     = ROOT + "\\" + rfid + "\\" + ColorType.GREEN.ToString() + "\\" + pictName + ".bmp";
                File.WriteAllBytes(path, pict);

                Image <Bgr, byte>  receivedImg   = new Image <Bgr, byte>(path);
                Image <Gray, byte> normalizedImg = receivedImg.Convert <Gray, Byte>();

                Rectangle[] rectangles = classifier.DetectMultiScale(normalizedImg, 1.4, 1, new Size(100, 100), new Size(800, 800));

                foreach (Rectangle r in rectangles)
                {
                    receivedImg.Draw(r, new Bgr(Color.Red), 2);
                }

                if (rectangles.Length <= 0)
                {
                    //put into red folder
                    return(new ResponseMessage(201, "Impossible to insert a new Employee.Face Not found."));
                }

                normalizedImg = receivedImg.Copy(rectangles[0]).Convert <Gray, byte>().Resize(64, 64, Emgu.CV.CvEnum.Inter.Cubic);
                normalizedImg._EqualizeHist();

                Classifier_Train eigenRecog = new Classifier_Train(ROOT + "\\" + rfid + "\\" + ColorType.GREEN.ToString());


                eigenRecog.AddTrainingImage(normalizedImg, rfid, rfid + ".bmp");

                /* Image<Gray, byte> dummyImg = new Image<Gray, byte>("dummy.jpg");
                 * File.Copy("dummy.jpg", ROOT + "\\" + rfid + "\\" + ColorType.GREEN.ToString()+"\\"+"dummy.jpg");
                 * eigenRecog.AddTrainingImage(dummyImg, "Unknown","dummy.jpg");*/

                DBConnect       db   = new DBConnect();
                MySqlConnection conn = db.getConnection();
                MySqlCommand    cmd2 = new MySqlCommand();
                cmd2.Connection  = conn;
                cmd2.CommandText = "INSERT INTO `user`(`rfid`, `name`, `surname`) VALUES (?a,?b,?c);";
                cmd2.Parameters.Add("?a", MySqlDbType.VarChar).Value = employee.employee.rfid;
                cmd2.Parameters.Add("?b", MySqlDbType.VarChar).Value = employee.employee.name;
                cmd2.Parameters.Add("?c", MySqlDbType.VarChar).Value = employee.employee.surname;
                cmd2.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception e)
            {
                return(new ResponseMessage(201, "Impossible to insert a new Employee."));
            }
            return(new ResponseMessage(200, employee.employee.name + " " + employee.employee.surname + " added correctly."));
        }
Пример #2
0
        public ResponseMessageColor faceDetectionAndConcludeSession(EmployeeBadge employee, string name, string surname, bool isentering)
        {
            DBConnect       db         = new DBConnect();
            MySqlConnection conn       = db.getConnection();
            string          query      = "SELECT * FROM colors where color='GREEN' and rfid='" + employee.rfid + "'";
            MySqlCommand    cmd        = new MySqlCommand(query, conn);
            MySqlDataReader dataReader = cmd.ExecuteReader();

            int count = 0;

            while (dataReader.Read())
            {
                count++;
            }

            conn.Close();

            if (count < 5)
            {
                if (isentering)
                {
                    return(new ResponseMessageColor(200, "Welcome " + name + " " + surname, "YELLOW"));
                }
                else
                {
                    return(new ResponseMessageColor(200, "You worked " + getSeconds(employee), "YELLOW"));
                }
            }



            string pathpicture = getPictureUri(employee.rfid, employee.session);

            Image <Bgr, byte>  receivedImg   = new Image <Bgr, byte>(pathpicture);
            Image <Gray, byte> normalizedImg = receivedImg.Convert <Gray, Byte>();

            Rectangle[] rectangles = classifier.DetectMultiScale(normalizedImg, 1.4, 1, new Size(100, 100), new Size(800, 800));

            foreach (Rectangle r in rectangles)
            {
                receivedImg.Draw(r, new Bgr(Color.Red), 2);
            }

            receivedImg.Save("photoFaced.jpg");

            if (rectangles.Length <= 0)
            {
                // NEVER REACHED THIS CODE ! ! ! ! !


                //put into red folder

                /* ChangePicture c = new ChangePicture();
                 * c.rfid = employee.rfid;
                 * c.session_id = employee.session;
                 * c.color = ColorType.RED.ToString();
                 * changeColor(c);*/
                // putSessionPhotoIntoRightFolder(employee, ColorType.RED);
                return(new ResponseMessageColor(404, "Never reached this code."));
            }



            //face recognition
            normalizedImg = receivedImg.Copy(rectangles[0]).Convert <Gray, byte>().Resize(64, 64, Emgu.CV.CvEnum.Inter.Cubic);
            normalizedImg._EqualizeHist();

            Classifier_Train eigenRecog = new Classifier_Train(ROOT + "\\" + employee.rfid + "\\" + ColorType.GREEN.ToString());

            string labelName = eigenRecog.Recognize(normalizedImg, greenValue);

            //eigenRecog.AddTrainingImage(normalizedImg, "Unknown");

            if (labelName == null || labelName == "" || labelName == "Unknown")
            {
                labelName = eigenRecog.Recognize(normalizedImg, yellowValue);

                if (labelName == null || labelName == "" || labelName == "Unknown")
                {
                    //put into red folder
                    ChangePicture c = new ChangePicture();
                    c.rfid       = employee.rfid;
                    c.session_id = employee.session;
                    c.color      = ColorType.RED.ToString();
                    changeColor(c);
                    //putSessionPhotoIntoRightFolder(employee, ColorType.RED);
                    if (isentering)
                    {
                        return(new ResponseMessageColor(200, "Welcome " + name + " " + surname));
                    }
                    else
                    {
                        return(new ResponseMessageColor(200, "You worked " + getSeconds(employee).ToString()));
                    }
                }
                else
                {
                    //put into yellow folder
                    ChangePicture c = new ChangePicture();
                    c.rfid       = employee.rfid;
                    c.session_id = employee.session;
                    c.color      = ColorType.YELLOW.ToString();
                    changeColor(c);
                    //putSessionPhotoIntoRightFolder(employee, ColorType.YELLOW);
                    if (isentering)
                    {
                        return(new ResponseMessageColor(200, "Welcome " + name + " " + surname, "YELLOW"));
                    }
                    else
                    {
                        return(new ResponseMessageColor(200, "You worked " + getSeconds(employee), "YELLOW"));
                    }
                }
            }
            else
            {
                //put into green folder
                ChangePicture c = new ChangePicture();
                c.rfid       = employee.rfid;
                c.session_id = employee.session;
                c.color      = ColorType.GREEN.ToString();
                changeColor(c);
                eigenRecog.AddTrainingImage(normalizedImg, employee.rfid, employee.rfid + ".bmp");
                if (isentering)
                {
                    return(new ResponseMessageColor(200, "Welcome " + name + " " + surname, "GREEN"));
                }
                else
                {
                    return(new ResponseMessageColor(200, "You worked " + getSeconds(employee), "GREEN"));
                }
            }
        }