//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"); }
/// <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); }
//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); }
/// <summary> /// Create training set /// </summary> /// <param name="imageWithChars"> </param> /// <param name="chars"> </param> /// <param name="scaleToDim"> </param> /// <param name="trainingSetName"> </param> public static DataSet createTrainingSet(string trainingSetName, BufferedImage imageWithChars, string chars, Dimension scaleToDim, List <string> imageLabels) { // convert chars from string to list List <string> charList = Arrays.asList(chars.Split(" ", true)); // izgleda da ovo zeza... // extract individual char images which will be used to create training set CharExtractor charExtractor = new CharExtractor(imageWithChars); Dictionary <string, BufferedImage> charImageMap = charExtractor.extractCharImagesToLearn(imageWithChars, charList, scaleToDim); // prepare image labels (we need them to label output neurons...) // ArrayList<String> imageLabels = new ArrayList<String>(); foreach (string imgName in charImageMap.Keys) { StringTokenizer st = new StringTokenizer(imgName, "._"); string imgLabel = st.nextToken(); if (!imageLabels.Contains(imgLabel)) // check for duplicates ... { imageLabels.Add(imgLabel); } } imageLabels.Sort(); // get RGB image data - map chars and their their rgb data IDictionary <string, FractionRgbData> imageRgbData = ImageUtilities.getFractionRgbDataForImages(charImageMap); // also put junk all black and white image in training set (for black n whit emode) BufferedImage allWhite = new BufferedImage(scaleToDim.Width, scaleToDim.Height, BufferedImage.TYPE_INT_RGB); Graphics g = allWhite.Graphics; g.Color = Color.WHITE; g.fillRect(0, 0, allWhite.Width, allWhite.Height); imageRgbData["allWhite"] = new FractionRgbData(allWhite); // BufferedImage allBlack = new BufferedImage(charDimension.getWidth(), charDimension.getHeight(), BufferedImage.TYPE_INT_RGB); // g = allBlack.getGraphics(); // g.setColor(Color.BLACK); // g.fillRect(0, 0, allBlack.getWidth(), allBlack.getHeight()); // imageRgbData.put("allBlack", new FractionRgbData(allBlack)); // put junk images (all red, blue, green) to avoid errors (used for full color mode) // BufferedImage allRed = new BufferedImage(charDimension.getWidth(), charDimension.getHeight(), BufferedImage.TYPE_INT_RGB); // Graphics g = allRed.getGraphics(); // g.setColor(Color.RED); // g.fillRect(0, 0, allRed.getWidth(), allRed.getHeight()); // imageRgbData.put("allRed", new FractionRgbData(allRed)); // // BufferedImage allBlue = new BufferedImage(charDimension.getWidth(), charDimension.getHeight(), BufferedImage.TYPE_INT_RGB); // g = allBlue.getGraphics(); // g.setColor(Color.BLUE); // g.fillRect(0, 0, allBlue.getWidth(), allBlue.getHeight()); // imageRgbData.put("allBlue", new FractionRgbData(allBlue)); // // BufferedImage allGreen = new BufferedImage(charDimension.getWidth(), charDimension.getHeight(), BufferedImage.TYPE_INT_RGB); // g = allGreen.getGraphics(); // g.setColor(Color.GREEN); // g.fillRect(0, 0, allGreen.getWidth(), allGreen.getHeight()); // imageRgbData.put("allGreen", new FractionRgbData(allGreen)); // create training set using image rgb data DataSet dataSet = ImageRecognitionHelper.createBlackAndWhiteTrainingSet(imageLabels, imageRgbData); //DataSet dataSet = ImageRecognitionHelper.createTrainingSet(this.imageLabels, imageRgbData); dataSet.Label = trainingSetName; return(dataSet); }