Skip to main content
aparajitas0001
October 28, 2021
Question

Extract Text from Image using Falcon Java Module

  • October 28, 2021
  • 0 replies
  • 0 views

We are trying to extract text from an image and used the example provided in suggested answer on this question: https://community.appian.com/discussions/f/rpa/19189/how-to-use-ocr-s-in-appian-rpa-using-falcon/75140#75140

The image is being recognized correctly on screen using Falcon module but the bot is unable to extract the text from it.

Could anyone please help with the type of image that we can use (Paragraph of text as image, single line text as image) and the different configuration that can be added which would help in successfully extracting text.

Code that we are using:

private String drawRectangle(IFalconImage imageFound) throws AWTException, InterruptedException {

			try {
				
				Path screenshot = Paths.get(server.getCurrentDir(), "screenshot.png");
				ImageIO.write(server.getScreen(), "png", screenshot.toFile());
				
				BufferedImage img = ImageIO.read(screenshot.toFile());
				Point p = imageFound.getPointsWhereFound().get(0);
				Rectangle rectanguloCif = new Rectangle(p.x, p.y);
				String text = falcon.extractText(convertToARGB(img), rectanguloCif, ETextFormInImage.ASSUME_A_SINGLE_UNIFORM_BLOCK_OF_TEXT,
						ELanguageInImage.ENGLISH, null, 1.9f, 0f);
				server.info("Result:" + text);

				String output = addSuffix(screenshot.toFile().getAbsolutePath(), "_mod");

				File f = new File(output);

				ImageIO.write(img, "png", f);
				server.info("Save image modified: " + output);
				
				falcon.sendImage(ImageIO.read(new File(output)), output);

				return output;
			} catch (IOException e) {
				server.info("Error Occured!");
				return null;
			}

		}
		
		public static BufferedImage convertToARGB(BufferedImage image)
			{
			    BufferedImage newImage = new BufferedImage(
			        image.getWidth(), image.getHeight(),
			        BufferedImage.TYPE_INT_ARGB);
			    
			    
			    Graphics2D g = newImage.createGraphics();
			    RenderingHints rh = new RenderingHints(
			             RenderingHints.KEY_TEXT_ANTIALIASING,
			             RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
			    g.setRenderingHints(rh);
			    g.drawImage(image, 36, 15, null);
			    g.dispose();
			    return newImage;
				
				}