// correct face rectangle coordinates because stored coordinates are coordinates
        // of top left corner of the rectangle and we need central point for neural network input
        public static void correctFaceCoordinates(CoordinatesContainer <int> realCoordinatesList, CoordinatesContainer <int> faceModeSize, List <double> imageResizeFactor, double scale, bool reverse = false)
        {
            int i = 0;

            // scale faceModeSize (face width) to get real coordinates of face right
            if (!reverse)
            {
                foreach (List <int> l in faceModeSize.getCoordinates())
                {
                    l[0] = (int)(Math.Round(l[0] / imageResizeFactor[i], MidpointRounding.AwayFromZero));
                    i++;
                }
            }

            i = 0;
            // correction factor for the first if face mode
            foreach (List <int> l in realCoordinatesList.getCoordinates())
            {
                int halfWidth = (reverse == false ? faceModeSize.getRow(i)[0] / 2 : -faceModeSize.getRow(i)[0] / 2);
                l[0] = l[0] + halfWidth;
                l[1] = l[1] + (int)(Math.Round(halfWidth * scale, MidpointRounding.AwayFromZero));
                i++;
            }

            i = 0;
            // scale faceModeSize (face width) to get relative coordinates (rectangleCoordinates) of face right
            if (reverse)
            {
                foreach (List <int> l in faceModeSize.getCoordinates())
                {
                    l[0] = (int)(Math.Round(l[0] * imageResizeFactor[i], MidpointRounding.AwayFromZero));
                    i++;
                }
            }
        }
        // write minimal and maximal values of coordinates used for normalization and denormalization of data
        public static void writeMinMax <T, U>(char mode, CoordinatesContainer <T> minMax, CoordinatesContainer <U> faceModeMinMax = null)
        {
            bool          boolMode   = (mode == Constants.faceMode ? true : false);
            TextWriter    writer     = new StreamWriter(CaptureLabel.exportMinMaxDirectory, false, Encoding.UTF8);
            CsvSerializer serializer = new CsvSerializer(writer, System.Globalization.CultureInfo.CurrentCulture);
            CsvWriter     csv        = new CsvWriter(serializer);

            List <List <T> > minMaxT = new List <List <T> >(minMax.getCoordinates());

            minMaxT = Enumerable.Range(0, minMaxT[0].Count)
                      .Select(i => minMaxT.Select(lst => lst[i]).ToList()).ToList();

            csv.WriteField("");

            // check which mode is selected to construct appropriate header
            string[] rectNames = (boolMode) ? Constants.rectangleNameF : (mode == Constants.faceElementsMode ? Constants.rectangleNameE : Constants.rectangleNameG);

            // construct header
            foreach (string s in rectNames)
            {
                csv.WriteField(s);
                csv.WriteField("");
            }

            if (boolMode)
            {
                csv.WriteField("Face width:");
            }

            csv.NextRecord();
            csv.WriteField("");

            for (int i = 0; i < rectNames.Length; i++)
            {
                csv.WriteField("(x,");
                csv.WriteField("y)");
            }

            csv.NextRecord();
            csv.WriteField("Min:");

            // write minimal and maximal values to .csv file
            for (int i = 0; i < minMaxT.Count; i++)
            {
                for (int j = 0; j < minMaxT[i].Count; j++)
                {
                    csv.WriteField(minMaxT[i][j]);
                }

                if (mode == Constants.faceMode && faceModeMinMax != null)
                {
                    csv.WriteField(faceModeMinMax.getRow(0)[i]);
                }

                csv.NextRecord();
                csv.WriteField("Max:");
            }

            writer.Close();
        }
        // normalize output values for face mode
        public static Tuple <List <List <T> >, List <List <int> > > normalizeOutputFaceMode <T, U>(CoordinatesContainer <U> realCoordinatesList)
        {
            List <List <U> >   coordinates  = new List <List <U> >(realCoordinatesList.getCoordinates());
            List <List <int> > minMaxValues = new List <List <int> >();
            List <List <T> >   result       = new List <List <T> >();

            // transpose elements
            coordinates = Enumerable.Range(0, coordinates[0].Count)
                          .Select(i => coordinates.Select(lst => lst[i]).ToList()).ToList();

            // normalize elements
            foreach (List <U> l in coordinates)
            {
                int min = Convert.ToInt32(l.Min());
                int max = Convert.ToInt32(l.Max());

                minMaxValues.Add(new List <int>()
                {
                    min, max
                });

                List <T> temp = new List <T>();

                // normalize output with formula:
                // Xnorm = (X - Xmin) / (Xmax - Xmin)
                for (int i = 0; i < l.Count; i++)
                {
                    double val = (double)(Convert.ToInt32(l[i]) - min) / (max - min);

                    // if value is not a number set it to zero
                    if (Double.IsNaN(val))
                    {
                        val = 0;
                    }

                    temp.Add((T)(object)val);
                }

                result.Add(temp);
            }

            // transpose elements
            result = Enumerable.Range(0, result[0].Count)
                     .Select(i => result.Select(lst => lst[i]).ToList()).ToList();


            return(Tuple.Create(result, minMaxValues));
        }
        // normalize output values for face elements and eye countour mode
        public static Tuple <List <List <T> >, List <List <int> > > normalizeOutputFaceElements <T, U>(CoordinatesContainer <U> realCoordinatesList, CoordinatesContainer <int> faceModeSize)
        {
            List <List <U> >   coordinates  = new List <List <U> >(realCoordinatesList.getCoordinates());
            List <List <int> > minMaxValues = new List <List <int> >();
            List <List <T> >   result       = new List <List <T> >();

            // normalize elements
            int i = 0;

            foreach (List <U> l in coordinates)
            {
                int faceWidth = faceModeSize.getRow(i)[0];

                List <T> temp = new List <T>();

                // normalize output with formula:
                // Xnorm = (X - Xmin) / (Xmax - Xmin)
                for (int j = 0; j < l.Count; j += 2)
                {
                    double valX = (double)(Convert.ToDouble(l[j]) / faceWidth);
                    double valY = (double)(Convert.ToDouble(l[j + 1]) / (faceWidth * Constants.modeFRectScale));

                    // if value is not a number set it to zero
                    if (Double.IsNaN(valX) || Double.IsNaN(valY))
                    {
                        valX = 0;
                        valY = 0;
                    }
                    temp.Add((T)(object)valX);
                    temp.Add((T)(object)valY);
                }

                result.Add(temp);
                i++;
            }


            return(Tuple.Create(result, minMaxValues));
        }
Exemple #5
0
 // copy constructor
 public CoordinatesContainer(CoordinatesContainer <T> container)
 {
     rowCoordinates = new List <List <T> >(container.getCoordinates());
 }