Пример #1
0
        public bool Decide()
        {
            // Get list of files in folders
            String[] files = Testing.GetFilesFromDirectory(decidePath).ToArray();
            string csvPath = userPath + "\\Decide.txt";
            string resultPath = userPath + "\\Result.txt";

            for (int i = 0; i < files.Length; i++)
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(@csvPath, false);
                ImageVector vec = new ImageVector(files[i], null);
                file.WriteLine(vec.getAllParameters(false));
                file.Close();

                MLApp.MLApp matlab = new MLApp.MLApp();

                string cd = "cd C:\\Users\\anoach\\Desktop\\papa_matlab";
                //string exe = "albumSvm('" + userPath + "');";
                string exe = "albumKnn('" + userPath + "', " + 1 + ");"; //K = 1

                matlab.Execute(cd);
                matlab.Execute(exe);

                string[] readText = File.ReadAllLines(resultPath);
                if (readText[0] == "1")
                {
                    Console.WriteLine("Good Image");
                }
                else
                {
                    Console.WriteLine("Bad Image. Delete it");
                    File.Delete(files[i]);
                }

            }

               return true;
        }
Пример #2
0
        /// <summary>
        /// predicts for all images in folder if true or false
        /// </summary>
        /// <param name="folder">path to folder with images to predict</param>
        /// <param name="res">keeps for each image if true or falde</param>
        /// <returns>true if decision was successful. else false.</returns>
        public bool Decide(string folder, out double [] res)
        {
            res = null;
            // Get list of files in folders
            String[] files;
            try
            {
                files = Testing.GetFilesFromDirectory(folder).ToArray();//Directory.GetFiles(folder);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
                return false;
            }

            DecisionListcount = 0;
            String[] cVectors= new string[files.Length];
            List<ImageVector> vectors = new List<ImageVector>();
            for(int i = 0; i < files.Length; i++)
            {
                string path = "C:\\Users\\anoach\\Desktop\\PAPA\\csv"+i+".txt";
                System.IO.StreamWriter file = new System.IO.StreamWriter(@path, true);
                //get imageVectors
                ImageVector vec = new ImageVector(files[i], ParameterList);

                vectors.Add(new ImageVector(vec));

                //Convert to clasiffied vector
                DecisionListcount++;
                file.WriteLine(vec.getAllParameters(false));
                file.Close();
            }

            double[] results;
            //load algorithm data and predict
            //if (!_algorithm.LoadData())
            //{
            //    return false;
            //}

            if (!Algorithm.Predict(vectors, out results))
                return false;

            res = results;
            return true;
        }
Пример #3
0
        public bool Learn(string folderAll, string folderTrue)
        {
            // Get list of files in folders
            string[] filesAll = Testing.GetFilesFromDirectory(folderAll).ToArray();//Directory.GetFiles(folderAll);
            string[] filesTrue = Testing.GetFilesFromDirectory(folderTrue).ToArray(); //Directory.GetFiles(folderTrue);
            if (filesAll.Length <= 0)
                return false;

            // Make False vector list
            int index=0;
            string[] filesFalse = new string[filesAll.Length - filesTrue.Length];
            bool exist;
            string fn_all, fn_true;
            for (int i = 0; i < filesAll.Length; i++)
            {
                fn_all = filesAll[i].Substring(filesAll[i].LastIndexOf("\\")+1);
                exist = false;
                for (int j = 0; j < filesTrue.Length; j++)
                {
                    fn_true = filesTrue[j].Substring(filesTrue[j].LastIndexOf("\\") + 1);
                    if (fn_true.Equals(fn_all))
                    {
                        exist = true;
                        break;
                    }
                }
                if (exist)
                    continue;

                filesFalse[index++] = filesAll[i];
            }

            // Convert to vector List
            List<ImageVector> vectorsTrue = new List<ImageVector>();
            List<ImageVector> vectorsFalse = new List<ImageVector>();
            List<ImageVector> newTrue = new List<ImageVector>();
            List<ImageVector> newFalse = new List<ImageVector>();
            Listcount = 0;
            _repository.loadList();
            System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\anoach\Desktop\PAPA\csv.txt", true);
            foreach (string path in filesTrue)
            {

                ImageVector existing = _repository.getVectorByPath(path);
                if (existing != null)
                {
                    vectorsTrue.Add(new ImageVector(existing));
                    file.WriteLine(existing.getAllParameters(true));
                }
                else
                {
                    ImageVector vec = new ImageVector(path, _parameterList);
                    vectorsTrue.Add(new ImageVector(vec));
                    newTrue.Add(vec);
                    file.WriteLine(vec.getAllParameters(true));

                }
                Listcount++;
            }
            foreach (string path in filesFalse)
            {
                ImageVector existing = _repository.getVectorByPath(path);
                if (existing != null)
                {
                    vectorsFalse.Add(new ImageVector(existing));
                    file.WriteLine(existing.getAllParameters(false));
                }
                else
                {
                    ImageVector vec = new ImageVector(path, _parameterList);
                    vectorsFalse.Add(new ImageVector(vec));
                    newFalse.Add(vec);
                    file.WriteLine(vec.getAllParameters(false));
                }
                Listcount++;
            }

            file.Close();

            // Push new vectors to repository (Only vectors that was not on the repository)
            _repository.AddToList(newTrue, newFalse);

            // Push vectors to algorithm (Learn by given pictures only)
            bool success = _algorithm.Train(vectorsTrue, vectorsFalse);

            return success;
        }
Пример #4
0
        private bool Internal_DecideImage(MLApp.MLApp matlab, ImageVector image, int algo)
        {
            string csvPathDecide    = userPath + "\\Decide.txt";
            string resultPath       = userPath + "\\Result.txt";

            System.IO.StreamWriter file = new System.IO.StreamWriter(@csvPathDecide, false);
            file.WriteLine(image.getAllParameters(false));
            file.Close();

            string command = Internal_GetCommand(algo);

            matlab.Execute(command);

            string[] readText = File.ReadAllLines(resultPath);
            if (readText[0] == "0")
                return false;
            return true;
        }
Пример #5
0
        public bool Learn()
        {
            // Get list of files in folders
            string[] filesAll = Testing.GetFilesFromDirectory(allPath).ToArray();
            string[] filesTrue = Testing.GetFilesFromDirectory(truePath).ToArray();
            if (filesAll.Length <= 0 || filesTrue.Length <= 0)
                return false;

            // Make False vector list - All the files in all that are not in true.
            int index = 0;
            string[] filesFalse = new string[filesAll.Length - filesTrue.Length];
            bool exist;
            string fn_all, fn_true;
            for (int i = 0; i < filesAll.Length; i++)
            {
                fn_all = filesAll[i].Substring(filesAll[i].LastIndexOf("\\") + 1);
                exist = false;
                for (int j = 0; j < filesTrue.Length; j++)
                {
                    fn_true = filesTrue[j].Substring(filesTrue[j].LastIndexOf("\\") + 1);
                    if (fn_true.Equals(fn_all))
                    {
                        exist = true;
                        break;
                    }
                }
                if (exist)
                    continue;

                filesFalse[index++] = filesAll[i];
            }

            string csvPath = userPath + "\\Learn.txt";
            System.IO.StreamWriter file = new System.IO.StreamWriter(@csvPath, true);

            // Convert to vector List
            foreach (string path in filesTrue)
            {
                ImageVector vec = new ImageVector(path, null);
                file.WriteLine(vec.getAllParameters(true));
            }
            foreach (string path in filesFalse)
            {
                ImageVector vec = new ImageVector(path, null);
                file.WriteLine(vec.getAllParameters(false));
            }

            file.Close();

            MLApp.MLApp matlab = new MLApp.MLApp();

            string cd = "cd C:\\Users\\anoach\\Desktop\\papa_matlab";
            string exe = "albumLearn('" + userPath + "');";

            matlab.Execute(cd);
            matlab.Execute(exe);

            return true;
        }
Пример #6
0
        private bool Internal_DecideImage(MLApp.MLApp matlab, ImageVector image, int algo)
        {
            string csvPathDecide    = userPath + "\\Decide.txt";
            int result = 0;

            System.IO.StreamWriter file = new System.IO.StreamWriter(@csvPathDecide, false);
            file.WriteLine(image.getAllParameters(false));
            file.Close();

            string commandBestKnn = Internal_GetCommand(2);
            string commandBestSvm = Internal_GetCommand(10);

            if (algo != 1)
            {
                matlab.Execute(commandBestKnn);
                result += Internal_GetResult();
            }

            if (algo != 0)
            {
                matlab.Execute(commandBestSvm);
                result += Internal_GetResult();
            }

            if (result == 2)
                return true;
            if ((algo != 2) && (result == 1))
                return true;

            return false;
        }
Пример #7
0
        /*
         * copy imageVector into a file
         */
        public static void convertImageVectorToFile(string path, ImageVector image)
        {
            string algoPath = path;
            string csvPathDecide = algoPath + "\\Decide.txt";

            System.IO.StreamWriter file = new System.IO.StreamWriter(@csvPathDecide, false);
            file.WriteLine(image.getAllParameters(false));
            file.Close();
        }