コード例 #1
0
        private void PerformClassification(List <Bitmap>[] circles, List <Bitmap>[] triangles, List <Bitmap>[] rectangles, ImageViewModel viewModel)
        {
            var signData = new Dictionary <Bitmap[], Sign>();

            for (int i = 0; i < circles[1].Count; i++)
            {
                var circle    = circles[1][i];
                var colorInfo = ColorInfo.Extract(circle);
                signData.Add(new[] { circles[0][i], circles[1][i] }, new Sign(100, colorInfo[0], colorInfo[1], colorInfo[2], colorInfo[3]));
            }
            for (int i = 0; i < triangles[1].Count; i++)
            {
                var triangle  = triangles[1][i];
                var colorInfo = ColorInfo.Extract(triangle);
                signData.Add(new[] { triangles[0][i], triangles[1][i] }, new Sign(50, colorInfo[0], colorInfo[1], colorInfo[2], colorInfo[3]));
            }
            for (int i = 0; i < rectangles[1].Count; i++)
            {
                var rectangle = rectangles[1][i];
                var colorInfo = ColorInfo.Extract(rectangle);
                signData.Add(new[] { rectangles[0][i], rectangles[1][i] }, new Sign(150, colorInfo[0], colorInfo[1], colorInfo[2], colorInfo[3]));
            }

            var containers = new[]
            {
                viewModel.WarningSigns, viewModel.ProhibitingSigns, viewModel.RegulatorySigns, viewModel.InformationSigns,
                viewModel.TemporarySigns
            };

            var    formatter   = new BinaryFormatter();
            Stream stream      = new FileStream("..\\..\\..\\perceptrons.bin", FileMode.Open, FileAccess.Read, FileShare.None);
            var    perceptrons = (List <Perceptron>)formatter.Deserialize(stream);

            stream.Close();

            var tc = new PrebuildSignsClassifier();

            tc.Teach();
            var signsViewModel = (SignsGrid.DataContext) as SignViewModel;

            signsViewModel.Signs.Clear();

            foreach (var sign in signData)
            {
                var groupIndex       = tc.FindClass(sign.Value) - 1;
                var signsImageSource = BitmapConverter.GetBitmapSource(sign.Key[1]);
                containers[groupIndex].Add(new ImageModel(signsImageSource));
                var perceptron = perceptrons[groupIndex];
                var histogram  =
                    HistogramExtracter.Process(BilinearInterpolation.Resize(sign.Key[0], ResizeWidth, ResizeHeight));
                var classificationResult = perceptron.Classify(histogram).ToList();
                var classIndex           = classificationResult.IndexOf(classificationResult.Max());
                var description          = SignDescription.Get(groupIndex, classIndex);
                signsViewModel.Signs.Add(new SignModel(signsImageSource, description));
            }
        }
コード例 #2
0
        private void Teach_Click(object sender, RoutedEventArgs e)
        {
            var teachingData = new List <List <List <double[]> > >();

            for (var i = 0; i < GroupsCount; i++)
            {
                var groupsContainer = new List <List <double[]> >();
                for (var j = 0; j < ClassesCount; j++)
                {
                    groupsContainer.Add(new List <double[]>());
                }
                teachingData.Add(groupsContainer);
            }

            var dialog = new FolderBrowserDialog
            {
                SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
            };
            var result = dialog.ShowDialog(this.GetIWin32Window());

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                foreach (var path in Directory.EnumerateFiles(dialog.SelectedPath, "*.*", SearchOption.AllDirectories)
                         .Where(file => file.EndsWith(".processed.bmp", StringComparison.OrdinalIgnoreCase)))
                {
                    var bitmap    = BitmapConverter.GetBitmap(new BitmapImage(new Uri(path)));
                    var histogram = HistogramExtracter.Process(bitmap);
                    var tmp       = path.Split('\\').ToList();
                    tmp.Reverse();
                    var currentGroup = int.Parse(tmp[2]) - 1;
                    var currentClass = int.Parse(tmp[1]) - 1;
                    teachingData[currentGroup][currentClass].Add(histogram);
                }
            }

            var perceptrons = new List <Perceptron>();

            perceptrons.AddRange((Enumerable.Repeat(new Perceptron(), GroupsCount)));
            Parallel.ForEach(teachingData, groupData =>
            {
                var teachingSet = new Dictionary <double[], double[]>();
                for (var i = 0; i < groupData.Count; i++)
                {
                    var classData      = groupData[i];
                    var expectedOutput = new double[groupData.Count];
                    expectedOutput[i]  = 1;
                    foreach (var classInput in classData)
                    {
                        teachingSet.Add(classInput, expectedOutput);
                    }
                }
                var perceptron = new Perceptron();
                perceptron.Reset();
                perceptron.Teach(teachingSet);
                var index          = teachingData.IndexOf(groupData);
                perceptrons[index] = perceptron;
            });
            var    formatter = new BinaryFormatter();
            Stream stream    = new FileStream(dialog.SelectedPath + "\\perceptrons.bin", FileMode.Create, FileAccess.Write, FileShare.None);

            formatter.Serialize(stream, perceptrons);
            stream.Close();
        }