Exemple #1
0
        private void btnTestImage_Click(object sender, EventArgs e)
        {
            if (Classifier == null || !Classifier.IsTrained)
            {
                MessageBox.Show("Classifier not trained. Please press Calculate EFs first!");
            }
            else
            {
                OpenFileDialog Dialog = new OpenFileDialog();
                Dialog.Filter = "jpeg files (*.jpg)|*.jpg|(*.gif)|gif||";
                if (DialogResult.OK == Dialog.ShowDialog())
                {
                    ATTFace face = DataUnit.GetATTFace(Dialog.FileName);
                    this.TestBitmap = face.getImagesAsBmp();
                    ShowImage(this.TestBitmap, picTestImage);
                    FileInfo fileInfo = new FileInfo(Dialog.FileName);
                    lblTestImage.Text = fileInfo.Name;
                    ///Showing mean adjusted
                    Bitmap meanAdjusted = DataUnit.ArrayToBitmap(Classifier.SubstractArrays(face.ImageVector, Classifier.MeanFaceAsArray));
                    ShowImage(meanAdjusted, picMeanAdjusted);
                    ////Showing the reconstruction from eigen faces
                    double[] projected     = Classifier.ProjectAndReconstruct(face.ImageVector);
                    Bitmap   bmp_projected = DataUnit.ArrayToBitmap(projected);
                    ShowImage(bmp_projected, picReconstructed);

                    ////Trying to find best matches
                    List <ATTFace> bestFaces = new List <ATTFace>();
                    int            guessID   = 0;
                    Classifier.FindBestMatch(face.ImageVector, DecisionType.ClossestNeighbor, ClossenessMeasure.Euclidean, ref bestFaces, ref guessID, onlyID: false);
                    ShowBestFaces(bestFaces);
                }
            }
        }
        public Object Clone()
        {
            ATTFace Copy = new ATTFace();

            Copy.Closeness              = this.Closeness;
            Copy.ImageVector            = this.ImageVector;
            Copy.ImageVectorTransformed = this.ImageVectorTransformed;
            Copy.personID = this.personID;
            Copy.FileName = this.FileName;
            return(Copy);
        }
Exemple #3
0
        /// <summary>
        /// Read files and generates a training dataset
        /// </summary>
        /// <param name="trainingPath"> Directory Path of where Training data is located</param>
        /// <returns> </returns>
        public static List <ATTFace> GetTrainingData(String trainingPath)
        {
            List <ATTFace> Faces = new List <ATTFace>();
            DirectoryInfo  di    = new DirectoryInfo(trainingPath);

            FileInfo[] files = di.GetFiles("*.jpg");
            foreach (FileInfo fileInfo in files)
            {
                String   fileName     = fileInfo.Name;
                String[] parts        = fileName.Split('_');
                int      personID     = int.Parse(parts[0].Substring(1, parts[0].Length - 1));
                Bitmap   faceBitmap   = new Bitmap(trainingPath + "\\" + fileName);
                double[] ImageAsArray = BitmapToArray(faceBitmap);
                ATTFace  face         = new ATTFace(fileName, personID, ImageAsArray);
                Faces.Add(face);
            }
            return(Faces);
        }
        public int CompareTo(Object obj)
        {
            if (obj == null)
            {
                return(1);
            }

            ATTFace OtherFace = obj as ATTFace;

            if (OtherFace != null)
            {
                return(this.CompareTo(OtherFace.Closeness));
            }
            else
            {
                throw new ArgumentException("Object is not an ATTFace");
            }
        }
        /// <summary>
        /// Finds the Best Match(es) as a list, and provides a guessed Person ID.
        /// </summary>
        /// <param name="imageAsVector"> Image of the person to be classified.</param>
        /// <param name="decisionType"> Either ClossestNeighbor or KKN</param>
        /// <param name="measureType"> Either Euclidean or Projection. Determines which measurement is to be used.</param>
        /// <param name="BestFaces"> List of ATTFaces passed by reference. Must be empty. BestMatches added to the list if onlyID = false</param>
        /// <param name="guessID">int passed by reference. The class of the person images guessed by PCA.</param>
        /// <param name="neighborNumber"> How many neighbors to use for KNN. Default is 3</param>
        /// <param name="numberFaces">How many Best Matches to add to BestFaces. Default is 6</param>
        /// <param name="onlyID"> Whether ONLY the class of the person is of interest. If the BestFaces are also of interest, set to false/</param>
        public void FindBestMatch(double[] imageAsVector, DecisionType decisionType, ClossenessMeasure measureType, ref List <ATTFace> BestFaces, ref int guessID, int neighborNumber = 3, int numberFaces = 6, bool onlyID = true)
        {
            double[] meanAdjusted   = SubstractArrays(imageAsVector, MeanFaceAsArray);
            double[] projectedImage = ComputeProjection(meanAdjusted);
            foreach (ATTFace Person in DataSet)
            {
                double similarity;
                if (measureType == ClossenessMeasure.Euclidean)
                {
                    similarity       = EuclideanDistance(Person.ImageVectorTransformed, projectedImage);
                    Person.Closeness = similarity;
                }
                else if (measureType == ClossenessMeasure.Projection)
                {
                    similarity       = DotProduct(Person.ImageVectorTransformed, projectedImage);
                    Person.Closeness = -similarity; //A maximization is transformed to a minimization by putting a -
                }
                else
                {
                    throw new ArgumentException("ClosenessMeasure is not supported!");
                }
            }

            DataSet.Sort();

            // ------------------------------------------ Retrieving best guess ------------------------------//
            if (decisionType == DecisionType.ClossestNeighbor)
            {
                guessID = DataSet[0].personID;
            }
            else if (decisionType == DecisionType.KNN)
            {
                IDictionary <int, int> Tally = new Dictionary <int, int>();
                int maxVotes = 0;
                guessID = -1;
                for (int i = 0; i < neighborNumber; i++)
                {
                    ATTFace Person = DataSet[i];
                    if (Tally.ContainsKey(Person.personID))
                    {
                        Tally[Person.personID]++;
                        if (Tally[Person.personID] > maxVotes)
                        {
                            //To be here, a personID needs at leats two votes
                            maxVotes = Tally[Person.personID];
                            guessID  = Person.personID;
                        }
                    }
                    else
                    {
                        Tally.Add(Person.personID, 1);
                    }
                }
            }
            else
            {
                throw new ArgumentException("DecisionType not valid!");
            }

            // ---------------------------- Populating the list onlyID = False ----------------//
            if (!onlyID)
            {
                for (int i = 0; i < numberFaces; i++)
                {
                    BestFaces.Add((ATTFace)DataSet[i].Clone());
                }
            }
        }