Пример #1
0
        public static void TrainApertureDetection()
        {
            HOGDescriptor hog = new HOGDescriptor(winSizeAperture, blockSize, blockStride, cellSize, nbins, derivAperture, winSigma, L2HysThreshold, gammaCorrection);

            //Postive samples
            List <float> pos_targets = new List <float>(); List <float[]> pos_features = new List <float[]>();
            {
                string[] files = Directory.EnumerateFiles(@"\users\jie\projects\Intel\data\PSM4-Tx\20160722\Aperture")
                                 .Where(file => file.ToLower().EndsWith(".bmp") || file.ToLower().EndsWith(".jpg"))
                                 .ToArray();

                for (int k = 0; k < files.Length; k++)
                {
                    Emgu.CV.Image <Gray, byte> image = new Emgu.CV.Image <Gray, byte>(files[k]);
                    float[] feature = hog.Compute(image);
                    pos_features.Add(feature);
                    pos_targets.Add(1.0f);
                }
            }

            //Negative samples
            List <float> neg_targets = new List <float>(); List <float[]> neg_features = new List <float[]>();
            {
                string[] files = Directory.EnumerateFiles(@"\users\jie\projects\Intel\data\PSM4-Tx\20160722\ApertureNegative")
                                 .Where(file => file.ToLower().EndsWith(".bmp") || file.ToLower().EndsWith(".jpg"))
                                 .ToArray();

                for (int k = 0; k < files.Length; k++)
                {
                    Emgu.CV.Image <Gray, byte> image = new Emgu.CV.Image <Gray, byte>(files[k]);
                    float[] feature = hog.Compute(image);
                    neg_features.Add(feature);
                    neg_targets.Add(-1.0f);
                }
            }
            List <float> targets = new List <float>(); List <float[]> features = new List <float[]>();

            targets.AddRange(pos_targets); targets.AddRange(neg_targets);
            features.AddRange(pos_features); features.AddRange(neg_features);
            LibSVM.SaveInLibSVMFormat("trainAperture.txt", targets.ToArray(), features.ToArray());
        }
Пример #2
0
        public static void ToSingleVector()
        {
            {   //Aperture
                string svm_file = @"\users\jie\projects\Intel\data\PSM4-Tx\20160722\models\svm_model_aperture";
                string single_vector_svm_model_file = "single_vector_aperture";
                string array_name = "PSM4Tx_APERTURE_SVM";

                LibSVM  svm           = new LibSVM(); svm.LoadModel(svm_file);
                float[] single_vector = svm.ToSingleVector();

                using (StreamWriter sw = new StreamWriter(single_vector_svm_model_file))
                {
                    sw.Write("const float " + array_name + "[] = ");
                    sw.Write("{");
                    for (int i = 0; i < single_vector.Length; i++)
                    {
                        sw.Write("{0}f,", single_vector[i]);
                    }
                    sw.WriteLine("};");
                }
            }

            {   //Isolator
                string svm_file = @"\users\jie\projects\Intel\data\PSM4-Tx\20160722\models\svm_model_isolator";
                string single_vector_svm_model_file = "single_vector_isolator";
                string array_name = "PSM4Tx_ISOLATOR_SVM";

                LibSVM  svm           = new LibSVM(); svm.LoadModel(svm_file);
                float[] single_vector = svm.ToSingleVector();

                using (StreamWriter sw = new StreamWriter(single_vector_svm_model_file))
                {
                    sw.Write("const float " + array_name + "[] = ");
                    sw.Write("{");
                    for (int i = 0; i < single_vector.Length; i++)
                    {
                        sw.Write("{0}f,", single_vector[i]);
                    }
                    sw.WriteLine("};");
                }
            }

            {   //Arrayblock
                string svm_file = @"\users\jie\projects\Intel\data\PSM4-Tx\20160722\models\svm_model_arrayblock";
                string single_vector_svm_model_file = "single_vector_arrayblock";
                string array_name = "PSM4Tx_ARRAYBLOCK_SVM";

                LibSVM  svm           = new LibSVM(); svm.LoadModel(svm_file);
                float[] single_vector = svm.ToSingleVector();

                using (StreamWriter sw = new StreamWriter(single_vector_svm_model_file))
                {
                    sw.Write("const float " + array_name + "[] = ");
                    sw.Write("{");
                    for (int i = 0; i < single_vector.Length; i++)
                    {
                        sw.Write("{0}f,", single_vector[i]);
                    }
                    sw.WriteLine("};");
                }
            }
        }