//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static void main(String[] args) throws java.io.IOException
        public static void Main(string[] args)
        {
            // path to image directory
            string imageDir = "/home/zoran/Downloads/MihailoHSLTest/trening";

            // image names - used for output neuron labels
            List <string> imageLabels = new ArrayList();

            imageLabels.Add("bird");
            imageLabels.Add("cat");
            imageLabels.Add("dog");


            // create dataset
            IDictionary <string, FractionRgbData> map = ImageRecognitionHelper.getFractionRgbDataForDirectory(new File(imageDir), new Dimension(20, 20));
            DataSet dataSet = ImageRecognitionHelper.createRGBTrainingSet(imageLabels, map);

            // create neural network
            List <int?> hiddenLayers = new List <int?>();

            hiddenLayers.Add(12);
            NeuralNetwork nnet = ImageRecognitionHelper.createNewNeuralNetwork("someNetworkName", new Dimension(20, 20), ColorMode.COLOR_RGB, imageLabels, hiddenLayers, TransferFunctionType.SIGMOID);

            // set learning rule parameters
            MomentumBackpropagation mb = (MomentumBackpropagation)nnet.LearningRule;

            mb.LearningRate = 0.2;
            mb.MaxError     = 0.9;
            mb.Momentum     = 1;

            // traiin network
            Console.WriteLine("NNet start learning...");
            nnet.learn(dataSet);
            Console.WriteLine("NNet learned");
        }
示例#2
0
        /// <summary>
        /// Creates neural network for OCR, which contains OCR plugin. OCR plugin provides interface for character recognition. </summary>
        /// <param name="label"> neural network label </param>
        /// <param name="samplingResolution"> character size in pixels (all characters will be scaled to this dimensions during recognition) </param>
        /// <param name="colorMode"> color mode used fr recognition </param>
        /// <param name="characterLabels"> character labels for output neurons </param>
        /// <param name="layersNeuronsCount"> number of neurons ih hidden layers </param>
        /// <param name="transferFunctionType"> neurons transfer function type </param>
        /// <returns> returns NeuralNetwork with the OCR plugin </returns>
        public static NeuralNetwork createNewNeuralNetwork(string label, Dimension samplingResolution, ColorMode colorMode, List <string> characterLabels, List <int?> layersNeuronsCount, TransferFunctionType transferFunctionType)
        {
            NeuralNetwork neuralNetwork = ImageRecognitionHelper.createNewNeuralNetwork(label, samplingResolution, colorMode, characterLabels, layersNeuronsCount, transferFunctionType);

            neuralNetwork.addPlugin(new OcrPlugin(samplingResolution, colorMode));

            return(neuralNetwork);
        }
示例#3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static void main(String[] args) throws java.io.IOException
        public static void Main(string[] args)
        {
            //     User input parameteres
            //*******************************************************************************************************************************
            string imagePath   = "C:/Users/Mihailo/Desktop/OCR/slova.png";    //path to the image with letters                        *
            string folderPath  = "C:/Users/Mihailo/Desktop/OCR/ImagesDir/";   // loaction folder for storing segmented letters           *
            string textPath    = "C:/Users/Mihailo/Desktop/OCR/slova.txt";    // path to the .txt file with text on the image          *
            string networkPath = "C:/Users/Mihailo/Desktop/OCR/network.nnet"; // location where the network will be stored     *
            int    fontSize    = 12;                                          // fontSize, predicted by height of the letters, minimum font size is 12 pt                          *
            int    scanQuality = 300;                                         // scan quality, minimum quality is 300 dpi                                                      *
            //*******************************************************************************************************************************

            BufferedImage    image = ImageIO.read(new File(imagePath));
            ImageFilterChain chain = new ImageFilterChain();

            chain.addFilter(new GrayscaleFilter());
            chain.addFilter(new OtsuBinarizeFilter());
            BufferedImage binarizedImage = chain.processImage(image);



            Letter letterInfo = new Letter(scanQuality, binarizedImage);
            //        letterInfo.recognizeDots(); // call this method only if you want to recognize dots and other litle characters, TODO

            Text texTInfo = new Text(binarizedImage, letterInfo);

            OCRTraining ocrTraining = new OCRTraining(letterInfo, texTInfo);

            ocrTraining.FolderPath       = folderPath;
            ocrTraining.TrainingTextPath = textPath;
            ocrTraining.prepareTrainingSet();



            List <string> characterLabels = ocrTraining.CharacterLabels;

            IDictionary <string, FractionRgbData> map = ImageRecognitionHelper.getFractionRgbDataForDirectory(new File(folderPath), new Dimension(20, 20));
            DataSet dataSet = ImageRecognitionHelper.createBlackAndWhiteTrainingSet(characterLabels, map);


            dataSet.FilePath = "C:/Users/Mihailo/Desktop/OCR/DataSet1.tset";
            dataSet.save();


            List <int?> hiddenLayers = new List <int?>();

            hiddenLayers.Add(12);

            NeuralNetwork   nnet = ImageRecognitionHelper.createNewNeuralNetwork("someNetworkName", new Dimension(20, 20), ColorMode.BLACK_AND_WHITE, characterLabels, hiddenLayers, TransferFunctionType.SIGMOID);
            BackPropagation bp   = (BackPropagation)nnet.LearningRule;

            bp.LearningRate = 0.3;
            bp.MaxError     = 0.1;


            //        MultiLayerPerceptron mlp = new MultiLayerPerceptron(12,13);
            //        mlp.setOutputNeurons(null);

            Console.WriteLine("Start learning...");
            nnet.learn(dataSet);
            Console.WriteLine("NNet learned");

            nnet.save(networkPath);
        }