/// <summary> /// Converts the image. /// </summary> /// <param name="pathIn">The path in.</param> /// <param name="pathOut">The path out.</param> public void ConvertImages(string pathIn, string pathOut) { int counter = 0; var fileList = Directory.GetFiles(@pathIn); int count = fileList.Length; var multiplier = 100d / count; Directory.CreateDirectory(Constants.PRE_FOLDER_BACKGROUND); Directory.CreateDirectory(Constants.PRE_FOLDER_HUMAN); foreach (string fileName in fileList) { BitmapSource image = new BitmapImage(new Uri(fileName, UriKind.RelativeOrAbsolute)); var preprocessedArray = PreprocessImage(image, Filter.Sobel); var preprocessedImage = AuxiliaryMethods.BitmapSourceFromArray(preprocessedArray, false); var resizedImage = ResizeImage(preprocessedImage); resizedImage.SaveImage($"{pathOut}\\{counter}.png"); double percentage = counter * multiplier; counter++; ReportProgress?.Invoke(this, new ProgressArgs { Percentage = percentage, Message = string.Empty }); } }
/// <summary> /// Initializes a new instance of the <see cref="HogProcessor"/> class. /// </summary> public HogProcessor() { if (File.Exists(Constants.PATH_SVM)) { _svm = AuxiliaryMethods.DeserializeSVM(Constants.PATH_SVM); } _imageProcessor = new ImageProcessor(); _clusterisationProcessor = new ClusterisationProcessor(); }
/// <summary> /// Adds the training samples to database. /// </summary> /// <param name="pathIn">The path in.</param> /// <param name="pathOut">The path out.</param> public void AddTrainingSamplesToDatabase(string pathIn, string pathOut) { var samplesHuman = new List <ObjectOfRecognition>(); var samplesBackGround = new List <ObjectOfRecognition>(); int isHuman = pathIn.Contains(Constants.HUMAN_TITLE) ? 1 : 0; long counter = 0; var fileList = Directory.GetFiles(pathIn); int count = fileList.Length; foreach (string filename in fileList) { if (!filename.Contains(".png") && !filename.Contains(".jpg")) { continue; } var image = new BitmapImage(new Uri(filename, UriKind.RelativeOrAbsolute)); //var bimapImage = new Bitmap(filename); var byteArray = image.ToByteArray(); var hogVector = ExtractHog(byteArray); //var hogVector = ExtractHogAccord(bimapImage); var objOfRec = new ObjectOfRecognition { ID = counter, IsHuman = isHuman, HOG = hogVector.ToArray() }; if (pathIn.Contains(Constants.HUMAN_TITLE)) { samplesHuman.Add(objOfRec); } else { samplesBackGround.Add(objOfRec); } double percentage = counter * 100d / count; counter++; ReportProgress?.Invoke(this, new ProgressArgs { Percentage = percentage, Message = string.Empty }); } AuxiliaryMethods.Serialize(pathIn.Contains(Constants.HUMAN_TITLE) ? samplesHuman : samplesBackGround, pathOut); }
/// <summary> /// Alls the passes of window. /// </summary> /// <param name="src">The source.</param> /// <param name="step">The step.</param> /// <returns></returns> public Task <List <RecognizedObject>[]> AllPassesOfWindow(BitmapSource src, int step) { var preprocessedArray = _imageProcessor.PreprocessImage(src, Filter.Sobel); var preprocessedImage = AuxiliaryMethods.BitmapSourceFromArray(preprocessedArray, false); int width = Constants.WIDTH; int height = Constants.HEIGHT; var taskList = new List <Task <List <RecognizedObject> > >(); while (width < preprocessedImage.Width && height < preprocessedImage.Height) { src.Freeze(); taskList.Add(OnePassOfWindow(src, width, height, step)); width = (int)Math.Round(width * 1.5); height = (int)Math.Round(height * 1.5); } return(Task.WhenAll(taskList)); }
/// <summary> /// Trains the specified true path. /// </summary> /// <param name="truePath">The true path.</param> /// <param name="falsePath">The false path.</param> public void Train(string truePath, string falsePath) { var samples = new List <ObjectOfRecognition>(); samples.AddRange(AuxiliaryMethods.Deserialize <List <ObjectOfRecognition> >(truePath)); samples.AddRange(AuxiliaryMethods.Deserialize <List <ObjectOfRecognition> >(falsePath)); var trainArray = samples.Select(item => item.HOG).ToArray(); var outputArray = samples.Select(item => item.IsHuman).ToArray(); var teacher = new SequentialMinimalOptimization <Gaussian> { UseComplexityHeuristic = true, UseKernelEstimation = true }; SupportVectorMachine <Gaussian> svm = teacher.Learn(trainArray, outputArray); var resultLine = svm.Weights; AuxiliaryMethods.WriteWeight(resultLine, Constants.PATH_WEIGHT); AuxiliaryMethods.SerializeSVM(svm, Constants.PATH_SVM); }