// parsing face elements mode values from .csv file public static Tuple <List <int>, List <CoordinatesContainer <int> > > parseFaceElements(List <List <int> > lines) { List <int> item1 = new List <int>(); List <CoordinatesContainer <int> > item2 = new List <CoordinatesContainer <int> >(); CoordinatesContainer <int> eyesNotVisibleContainer = new CoordinatesContainer <int>(); CoordinatesContainer <int> realCoordinates = new CoordinatesContainer <int>(); CoordinatesContainer <int> lookAngleContainer = new CoordinatesContainer <int>(); CoordinatesContainer <int> faceModeSize = new CoordinatesContainer <int>(); List <int> singleRow = new List <int>(); // interate rough rows of values and parse them appropriately foreach (List <int> line in lines) { singleRow.Add(line[0]); singleRow.Add(line[1]); List <int> temp = new List <int>(singleRow); eyesNotVisibleContainer.addRow(temp); singleRow.Clear(); for (int i = 2; i < 12; i++) { singleRow.Add(line[i]); } temp = new List <int>(singleRow); realCoordinates.addRow(temp); singleRow.Clear(); for (int i = 12; i < 16; i++) { singleRow.Add(line[i]); } temp = new List <int>(singleRow); lookAngleContainer.addRow(temp); singleRow.Clear(); singleRow.Add(line[16]); temp = new List <int>(singleRow); faceModeSize.addRow(temp); singleRow.Clear(); } item2.Add(realCoordinates); item2.Add(lookAngleContainer); item2.Add(faceModeSize); item2.Add(eyesNotVisibleContainer); return(Tuple.Create(item1, item2)); }
// save coordinates of the current image index private void saveCoordinates() { // save all rectangles coordinates List <int> coordinates = rectangles.getAllRectCoordinates(); double resizeFactor = 0; try { resizeFactor = imageResizeFactor[currentImageIndex]; } catch (ArgumentOutOfRangeException) { calculateResizeFactor(currentImageIndex); resizeFactor = imageResizeFactor[currentImageIndex]; } int faceWidth = (mode == Constants.faceMode) ? rectangles.getRectangles()[0].Width : imagePanel.BackgroundImage.Width; // calculate real coordinates if (coordinatesList.getRow(currentImageIndex) == null) { // add new properties associated with this image, some are based on current mode lookAngleContainer.addRow(lookAngle.ToList()); faceModeSize.addRow(new List <int> { faceWidth }); coordinatesList.addRow(coordinates); realCoordinatesList.addRow(calculateRealCoordinates(coordinates)); if (mode == Constants.faceMode) { isFacePresent.Add((noFaceCB.Checked ? 1 : 0)); } if (mode == Constants.faceElementsMode) { eyesNotVisibleContainer.addRow(eyesNotVisible.ToList()); } if (mode == Constants.eyeContourMode) { eyeClosed.Add((eyeClosedCB.Checked ? 1 : 0)); } } else { // replace new properties associated with this image, some are based on current mode lookAngleContainer.replaceRow(lookAngle.ToList(), currentImageIndex); coordinatesList.replaceRow(coordinates, currentImageIndex); realCoordinatesList.replaceRow(calculateRealCoordinates(coordinates), currentImageIndex); faceModeSize.replaceRow(new List <int> { faceWidth }, currentImageIndex); if (mode == Constants.faceMode) { isFacePresent[currentImageIndex] = (noFaceCB.Checked ? 1 : 0); } if (mode == Constants.faceElementsMode) { eyesNotVisibleContainer.replaceRow(eyesNotVisible.ToList(), currentImageIndex); } if (mode == Constants.eyeContourMode) { eyeClosed[currentImageIndex] = (eyeClosedCB.Checked ? 1 : 0); } } // set look angle and checkbox states lookAngle = new int[] { 0, 0, 0, 0 }; setCheckBoxes(new CheckBox[] { leftCB, rightCB, upCB, downCB }); if (mode == Constants.faceElementsMode) { eyesNotVisible = new int[] { 0, 0 }; setEyesCheckBoxes(new CheckBox[] { LEnotVCB, REnotVCB }); } if (mode == Constants.faceMode) { noFaceCB.Checked = false; } if (mode == Constants.eyeContourMode) { eyeClosedCB.Checked = false; } }
// used for parsing face elements mode and eye contour mode .csv files public static Tuple <List <int>, List <CoordinatesContainer <int> > > parseTypeOneCSV(char mode, List <List <int> > lines) { List <int> item1 = new List <int>(); List <CoordinatesContainer <int> > item2 = new List <CoordinatesContainer <int> >(); CoordinatesContainer <int> realCoordinates = new CoordinatesContainer <int>(); CoordinatesContainer <int> lookAngleContainer = new CoordinatesContainer <int>(); CoordinatesContainer <int> faceModeSize = new CoordinatesContainer <int>(); List <int> singleRow = new List <int>(); int iCoordLow = 0; int iCoordHigh = 0; // sets starting reading positions base on the working mode if (mode == Constants.faceMode) { iCoordLow = 7; iCoordHigh = 11; } else if (mode == Constants.eyeContourMode) { iCoordLow = 11; iCoordHigh = 15; } // interate rough rows of values and parse them appropriately foreach (List <int> line in lines) { item1.Add(line[0]); for (int i = 1; i < iCoordLow; i++) { singleRow.Add(line[i]); } List <int> temp = new List <int>(singleRow); realCoordinates.addRow(temp); singleRow.Clear(); for (int i = iCoordLow; i < iCoordHigh; i++) { singleRow.Add(line[i]); } temp = new List <int>(singleRow); lookAngleContainer.addRow(temp); singleRow.Clear(); singleRow.Add(line[iCoordHigh]); temp = new List <int>(singleRow); faceModeSize.addRow(temp); singleRow.Clear(); } item2.Add(realCoordinates); item2.Add(lookAngleContainer); item2.Add(faceModeSize); return(Tuple.Create(item1, item2)); }
// import button logic private void importButton_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; // release everything from memory before new import (or new import) cleanUp(); // initialize selected mode initMode(mode); try { // get list of everything in folder passed to imageFolder imageLocation = new List <string>(Directory.GetFiles(imageFolder)); csvFileName = new DirectoryInfo(imageFolder).Name; var sortedFiles = Directory.GetFiles(@"C:\", "*").OrderByDescending(d => new FileInfo(d).CreationTime); // Parse list of image locations to contain only image locations imageLocation = Utilities.parseImagesToList(imageLocation); // import only if there are images found in folder if (imageLocation.Count > 0) { // set imagePanel to first image imagePanel.BackgroundImage = Image.FromFile(imageLocation[0]); currentImageIndex = 0; // extract image names foreach (string name in imageLocation) { imageNames.Add(Path.GetFileNameWithoutExtension(name)); } // check for existing .csv file if (!String.IsNullOrEmpty(csvPath) && csvPath.Contains(".csv")) { // parse .csv file based on mode selected Tuple <List <int>, List <CoordinatesContainer <int> > > tempCSV = Utilities.parseCSV(csvPath, mode); // set mode relevant fields if (mode == Constants.faceMode) { isFacePresent = tempCSV.Item1; } if (mode == Constants.faceElementsMode) { eyesNotVisibleContainer = new CoordinatesContainer <int>(tempCSV.Item2[3]); } if (mode == Constants.eyeContourMode) { eyeClosed = tempCSV.Item1; } // set relevant fields to ones that are parsed from .csv file realCoordinatesList = new CoordinatesContainer <int>(tempCSV.Item2[0]); lookAngleContainer = new CoordinatesContainer <int>(tempCSV.Item2[1]); faceModeSize = new CoordinatesContainer <int>(tempCSV.Item2[2]); // calculate resize factor between original image and image on the imagePanel for (int i = 0; i < imageNames.Count; i++) { imagePanel.BackgroundImage = Image.FromFile(imageLocation[i]); calculateResizeFactor(i); imagePanel.BackgroundImage.Dispose(); } // correct face coordinates if (mode == Constants.faceMode) { Utilities.correctFaceCoordinates(realCoordinatesList, faceModeSize, imageResizeFactor, Constants.modeFRectScale, true); } List <int> singleRow; // set rectangle coordinates based on real one read from .csv file for (int i = 0; i < realCoordinatesList.getSize(); i++) { singleRow = realCoordinatesList.getRow(i); coordinatesList.addRow(new List <int>(calculateRectangleCoordinates(singleRow, i))); } // load currentImageIndex image from file and load associated coordinates imagePanel.BackgroundImage = Image.FromFile(imageLocation[currentImageIndex]); loadCoordinates(currentImageIndex); } imagePathTB.ReadOnly = true; loaded = true; } } // catch if something went wrong in the section above catch (Exception msg) { imagePathTB.ReadOnly = false; MessageBox.Show(msg.Message, Constants.errorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } imagePanel.Refresh(); Cursor.Current = Cursors.Default; }