Пример #1
0
 /// <summary>
 /// for estimation
 /// </summary>
 /// <param name="svm_path"></param>
 public VehicleSpeedEstimator(System.Drawing.Size hog_training_img_size, string svm_path)
 {
     try {
         anomaly           = new AnomalySpeedDetector();
         AllowErrorRate    = 1.5f;
         SurgingSpeedMin   = 0.45f;
         SurgingCountLimit = 7;
         classifier        = OpenCvSharp.ML.SVM.Load(svm_path);
         hog = new HOGDescriptor(new OpenCvSharp.Size(hog_training_img_size.Width, hog_training_img_size.Height), new OpenCvSharp.Size(16, 16),
                                 new OpenCvSharp.Size(8, 8), new OpenCvSharp.Size(8, 8));
         var support_vectors = classifier.GetSupportVectors();
         var vectors         = new float[support_vectors.Width * support_vectors.Height];
         Marshal.Copy(support_vectors.Data, vectors, 0, support_vectors.Width * support_vectors.Height);
         hog.SetSVMDetector(vectors);
     }
     catch (Exception) {
         classifier = OpenCvSharp.ML.SVM.Create();
     }
 }
Пример #2
0
    /// <summary>
    /// for hog training
    /// </summary>
    /// <param name="positive_path"></param>
    /// <param name="negative_path"></param>
    /// <param name="svm_save_path"></param>
    /// <returns></returns>
    public bool Training(string root_path, string svm_save_path)
    {
        try {
            var computes      = new List <float[]>();
            var response      = new List <int>();
            var color_folders = Directory.GetDirectories(root_path);

            if (color_folders.Length == 0)
            {
                return(false);
            }

            foreach (var color in color_folders)
            {
                var positive_path = string.Format(color + "\\" + "positive");
                var negative_path = string.Format(color + "\\" + "negative");
                var positives     = Directory.GetFiles(positive_path);
                var negatives     = Directory.GetFiles(negative_path);

                foreach (var file in positives)
                {
                    computes.Add(hog.Compute(Cv2.ImRead(file, ImreadModes.GrayScale).Resize(hog.WinSize)));
                    response.Add(1);
                }

                foreach (var file in negatives)
                {
                    computes.Add(hog.Compute(Cv2.ImRead(file, ImreadModes.GrayScale).Resize(hog.WinSize)));
                    response.Add(-1);
                }
            }
            var respon_mat = new Mat(response.Count, 1, MatType.CV_32SC1, response.ToArray());
            var samples    = new float[computes[0].Length * computes.Count];
            var sample_mat = new Mat(computes.Count, computes[0].Length, MatType.CV_32FC1, samples);

            for (int idx = 0; idx < computes[0].Length * computes.Count; idx += computes[0].Length)
            {
                for (int col = 0; col < computes[0].Length; col++)
                {
                    samples[idx + col] = computes[idx / computes[0].Length][col];
                }
            }

            classifier.KernelType = OpenCvSharp.ML.SVM.KernelTypes.Linear;
            classifier.Type       = OpenCvSharp.ML.SVM.Types.EpsSvr;
            classifier.P          = 0.1;
            classifier.Nu         = 0.1;
            classifier.C          = 0.01;
            classifier.Gamma      = 0.1;
            classifier.Train(sample_mat, OpenCvSharp.ML.SampleTypes.RowSample, respon_mat);
            classifier.Save(svm_save_path);

            var support_vectors = classifier.GetSupportVectors();
            var vectors         = new float[support_vectors.Width * support_vectors.Height];
            Marshal.Copy(support_vectors.Data, vectors, 0, support_vectors.Width * support_vectors.Height);
            hog.SetSVMDetector(vectors);
            return(true);
        }
        catch (Exception e) {
            Trace.WriteLine(e.StackTrace + e.Message);
            return(false);
        }
    }