/// <summary> /// Recognize characters in given text images and returns character list </summary> /// <param name="neuralNet"> </param> /// <param name="image"> </param> /// <param name="charDimension"> </param> /// <returns> </returns> public static List <char?> recognizeText(NeuralNetwork neuralNet, BufferedImage image, Dimension charDimension) { CharExtractor charExtractor = new CharExtractor(image); List <BufferedImage> charImages = charExtractor.extractCharImagesToRecognize(); List <char?> characters = recognize(neuralNet, charImages, charDimension); return(characters); }
/// <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); }