public ResponseMessage changeColorOptions(ChangePicture emp) { 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"); return(new ResponseMessage(200, "OK")); }
public ResponseMessage changeColor(ChangePicture emp) { 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"); DBConnect db = new DBConnect(); MySqlConnection conn = db.getConnection(); string query = "SELECT * FROM colors where rfid='" + emp.rfid + "' and session_id='" + emp.session_id + "';"; MySqlCommand cmd = new MySqlCommand(query, conn); MySqlDataReader dataReader = cmd.ExecuteReader(); string oldcolor = ""; while (dataReader.Read()) { oldcolor = dataReader["color"].ToString(); } conn.Close(); if (oldcolor != "" && oldcolor != emp.color) { db = new DBConnect(); conn = db.getConnection(); query = "UPDATE `colors` SET `color`='" + emp.color + "' WHERE `session_id`='" + emp.session_id + "' and `rfid`='" + emp.rfid + "';"; cmd = new MySqlCommand(query, conn); int row = cmd.ExecuteNonQuery(); if (row == 1) { File.Move(ROOT + "\\" + emp.rfid + "\\" + oldcolor + "\\" + emp.session_id + ".bmp", ROOT + "\\" + emp.rfid + "\\" + emp.color + "\\" + emp.session_id + ".bmp"); conn.Close(); if (emp.color.Equals(ColorType.GREEN.ToString())) { string trainedFacesFolder = ROOT + "\\" + emp.rfid + "\\" + ColorType.GREEN.ToString(); string fileName = emp.session_id + ".bmp"; if (File.Exists(trainedFacesFolder + "/TrainedLabels.xml")) { XmlDocument docu = new XmlDocument(); bool loading = true; while (loading) { try { docu.Load(trainedFacesFolder + "/TrainedLabels.xml"); loading = false; } catch { docu = null; docu = new XmlDocument(); Thread.Sleep(10); } } //Get the root element XmlElement root = docu.DocumentElement; XmlElement employee_D = docu.CreateElement("EMPLOYEE"); XmlElement rfid_D = docu.CreateElement("RFID"); XmlElement file_D = docu.CreateElement("FILE"); //Add the values for each nodes rfid_D.InnerText = emp.rfid; file_D.InnerText = fileName; //Construct the employee element employee_D.AppendChild(rfid_D); employee_D.AppendChild(file_D); //Add the New employee element to the end of the root element root.AppendChild(employee_D); //Save the document docu.Save(trainedFacesFolder + "/TrainedLabels.xml"); } } else if (oldcolor.Equals(ColorType.GREEN.ToString())) { //loadfile string trainedFacesFolder = ROOT + "\\" + emp.rfid + "\\" + ColorType.GREEN.ToString(); string fileName = emp.session_id + ".bmp"; if (File.Exists(trainedFacesFolder + "/TrainedLabels.xml")) { XmlDocument docu = new XmlDocument(); bool loading = true; while (loading) { try { docu.Load(trainedFacesFolder + "/TrainedLabels.xml"); loading = false; } catch { docu = null; docu = new XmlDocument(); Thread.Sleep(10); } } XmlNodeList nodeList = docu.SelectNodes("//EMPLOYEE[contains(FILE,'" + fileName + "')]"); foreach (XmlNode n in nodeList) { n.ParentNode.RemoveChild(n); } //Save the document docu.Save(trainedFacesFolder + "/TrainedLabels.xml"); } } return(new ResponseMessage(200, "Color updated for " + emp.session_id)); } else { conn.Close(); return(new ResponseMessage(201, "Unable to change color.")); } } return(new ResponseMessage(201, "Unable to change color.")); }
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")); } } }