Ejemplo n.º 1
0
        public void IrisSupportVectorMachineClassifierTest()
        {
            DataSetLoader dataSetLoader = new DataSetLoader();

            Console.WriteLine(" Reading DataSet.. ");
            var irises = dataSetLoader.SelectIrises();

            for (double i = 0; i < 1; i = i + 1)
            {
                Kernel        kernel = new LinearKernel();
                SVMClassifier animalSVMClassifier = new SVMClassifier(irises, kernel, 0.001, 10.0);
                animalSVMClassifier.Train();
                var irisesTest  = dataSetLoader.SelectIrises();
                var trueCounter = 0;
                var counter     = 0;
                foreach (var item in irisesTest)
                {
                    var outputValue = animalSVMClassifier.Classify(item.Item1);
                    if (outputValue == item.Item2)
                    {
                        trueCounter++;
                    }
                    Debug.WriteLine(string.Format("Value {0} - Predicted {1} = {2}",
                                                  item.Item2, outputValue, (outputValue == item.Item2) ? "true" : "false"));
                    counter++;
                }
                Debug.WriteLine(string.Format(" i = {0} Data {1} - True {2} Verhältnis: {3}", i,
                                              counter.ToString(), trueCounter.ToString(), (Convert.ToDouble(trueCounter) / Convert.ToDouble(counter)).ToString()));
            }
        }
        public void SanFranciscoCrimeSVMClassificationDataSetTest()
        {
            DataSetLoader dataSetLoader = new DataSetLoader();

            Console.WriteLine(" Reading DataSet.. ");
            var           crimes        = dataSetLoader.SelectCrimes();
            Kernel        kernel        = new GaussianKernel(0.9);
            SVMClassifier svmClassifier =
                new SVMClassifier(crimes, kernel);

            svmClassifier.Train();
            var crimeTests  = dataSetLoader.SelectCrimes();
            var trueCounter = 0;
            var counter     = 0;

            foreach (var item in crimeTests)
            {
                var outputValue = svmClassifier.Classify(item.Item1);
                if (outputValue == item.Item2)
                {
                    trueCounter++;
                }
                Debug.WriteLine(string.Format("Value {0} - Predicted {1} = {2}",
                                              item.Item2, outputValue, (outputValue == item.Item2) ? "true" : "false"));
                counter++;
            }
            Debug.WriteLine(string.Format("Data {0} - True {1} Verhältnis: {2}",
                                          counter.ToString(), trueCounter.ToString(), (Convert.ToDouble(trueCounter) / Convert.ToDouble(counter)).ToString()));
        }
        private void detectBtn_Click(object sender, EventArgs e)
        {
            StringValues sv      = new StringValues();
            Bitmap       bmp     = new Bitmap(inputFilePath);
            int          xmax    = bmp.Width;
            int          ymax    = bmp.Height;
            Bitmap       bwimage = processImage(bmp);

            int[] xaxis = new int[xmax];
            int[] yline = new int[ymax];
            int[] tempx = new int[xmax];
            int[] tempy = new int[ymax];
            int   i = 1, j = 1;

            //calculated y axis
            yaxis = getYArray(xmax, ymax, bwimage);

            //todo- calculate yaxis for each character and store it.

            xaxis = getXArray(xmax, ymax, bwimage, yline);

            int k;

            Bitmap[] croppedImg = new Bitmap[30];
            for (i = 1, j = 2, k = 0; k < (xaxis.Length) / 2; k++)
            {
                try
                {
                    Crop filter = new Crop(new Rectangle(xaxis[i], yaxis[1], (xaxis[j] - xaxis[i]), yaxis[2] - yaxis[1]));
                    croppedImg[k] = filter.Apply(bwimage);
                    ResizeBicubic rbc = new ResizeBicubic(60, 90);
                    croppedImg[k] = rbc.Apply(croppedImg[k]);
                    croppedImg[k].Save(sv.browseFileBoundaryDetectionPath + "\\img_" + k + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                catch
                {
                    break;
                }
                i = i + 2;
                j = j + 2;
            }



            string       loc1 = sv.browseFileBoundaryDetectionPath;
            FileStream   fs   = new FileStream(sv.browseFileFeatureExtractionFile, FileMode.Create, FileAccess.Write);
            StreamWriter sr   = new StreamWriter(fs);

            String[]      straimageloc = System.IO.Directory.GetFiles(loc1, "*.jpg*");
            List <Bitmap> imageList    = new List <Bitmap>();

            foreach (var path in straimageloc)
            {
                Bitmap image = new Bitmap(path);
                imageList.Add(image);
            }
            int    label = 0;
            string data  = label + " ";

            for (int n = 0; n < imageList.Count; n++)
            {
                float  norvar    = 0;
                Bitmap grayimage = processImage(new Bitmap(imageList[n]));

                ResizeBicubic filter = new ResizeBicubic(60, 90);
                grayimage     = filter.Apply(grayimage);
                float[,] zone = new float[90, 60];
                int index = 1;
                for (j = 0; j < 90; j += 10)
                {
                    for (i = 0; i < 60; i += 10)
                    {
                        float  x      = 0;
                        Bitmap sector = grayimage.Clone(new System.Drawing.Rectangle(i, j, 10, 10), grayimage.PixelFormat);
                        for (k = 0; k < 10; k++)
                        {
                            for (int l = 0; l < 10; l++)
                            {
                                if (sector.GetPixel(k, l).Name == "ff000000")
                                {
                                    x++;
                                }
                            }
                        }
                        norvar += x;
                    }
                }
                for (j = 0; j < 90; j += 10)
                {
                    for (i = 0; i < 60; i += 10)
                    {
                        float  x      = 0;
                        Bitmap sector = grayimage.Clone(new System.Drawing.Rectangle(i, j, 10, 10), grayimage.PixelFormat);
                        for (k = 0; k < 10; k++)
                        {
                            for (int l = 0; l < 10; l++)
                            {
                                if (sector.GetPixel(k, l).Name == "ff000000")
                                {
                                    x++;
                                }
                            }
                        }
                        zone[j, i] = x / norvar;
                        data      += index++ + ":" + zone[j, i] + " ";
                    }
                }

                index = 0;

                sr.WriteLine(data);


                data = label + " ";
            }
            label++;
            sr.Flush();
            sr.Close();
            fs.Close();

            SVMClassifier svm = new SVMClassifier();

            svm.Classify();


            foreach (string line in File.ReadLines(sv.finalOutputFile, Encoding.UTF8))
            {
                Console.WriteLine(line);
                string c = Char.ConvertFromUtf32(Int32.Parse(line) + 64);
                outputLabel.Text += c;
            }
        }