Did you know that optical character recognition (OCR) can bring forth your artistry? Have you seen the newspaper blackout poems? Using OCR I was able to create a simple program that allowed me to create just that. This whitepaper shows how I used OCR Xpress for Java to OCR a scanned newspaper, redact the key words, and accomplished my goal. Redaction is a very popular means of removing sensitive material from any document. However, it can also be entertaining.
Introduction
There were a few functionalities that I required before I channeled my inner Walt Whitman. The first was OCR, the second was word/phrase search, the third was redaction or, in my case, negated redaction, and the final thing was saving it to a file. In this article, I will briefly go over how I was able to create a small application using OCR Xpress.
OCR’n
Getting OCR up and running was my first step to becoming a modern-day poet. OCR is the conversion of an image (or images) that has typed / printed text to an electronic output. OCR has many use cases for many different companies and also has a practical need for the blind and visually impaired. My intent, however, was much more whimsical.
package com.accusoft.ocrxpress.samples;
import com.accusoft.ocrxpress.*;
import java.awt.Color;
import java.awt.image.*:
import javax.imageio.*;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Flowlayout:
I took the Memory sample that OCR Xpress was packaged with and added a NewspaperBlackout
class. Above are the imports and the package that I needed within my class.
There are were a few different ways to code this application, but I decided to create a couple helper functions. Below I used InputString
, DisplayImage
, ConvertToBlack
, and ConvertRect
.
public class NewspaperBlackout
{
public static String InputString() {
Scanner scanner = new scanner (System.in);
System.out.prints "Type your poem. <Press Enter for default>");
return scanner.nextLine();
}
This helper function allows you to either hit "Enter" for the default poem, or type your own poem within the Console window.
public static void DisplayImage(BufferedImage bi) {
ImageIcon icon = new ImageIcon(bi);
JFrame frame = new JFrame();
frame.setLayouts new FlowLayouts());
frame.setSize (bi.getWidth(), bi.getHeight());
JLabel lbl = new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setWisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_0N_close);
}
The helper function displays the final output in a JFrame.
public static void convertToBlack(BufferedImage bi) {
Color c = new Color (0,0,0);
for (int x = 0; x < bi.getWidth(); x++) {
for (int y = 0; y < bi.getHeight(); y ++) {
bi.setRGB(x, y, c.getRGB());
}
}
}
This helper function converts the image to completely black. There are many different ways to accomplish this, and I recommend using the method you feel most comfortable with.
public static java.awt.Rectangle ConvertRect
(com.accusoft.ocrxpress.Rectangle accusoftRect) {
java.awt.Rectangle javaRect = new java.awt.Rectangle();
javaRect.x = accusoftRect.getLeft();
javaRect.y = accusoftRect.getTop ();
javaRect.width = accusoftRect.getRight() - accusoftRect.getLeft();
javaRect.height = accusoftRect.getBottom() - accusoftRect.getTop();
return javaRect;
}
This helper function takes in an Accusoft rectangle, which has Top, Bottom, Right, and Left, and converts it to the standard Java rectangle, x, y, width, and height.
public static void main (String[] args) throws OcrxException {
String inputImagePath = "images/NP01.bmp";
BufferedImage originalImg = null, outputImg = null;
try {
originalImg = ImageIO.read(new File (inputImagePath));
}
catch (IOException e) {
e.addSuppressed(e);
return;
}
Once I had the newspaper scanned and saved as a BMP image, I was ready to load my image into a bufferedImage
by calling ImageIO.read
.
0crXpress ocrx = new 0crXpress();
initializeLicensing(ocrx);
RecognitionParameters parameters = new RecognitionParameters();
parameters.setLanguage(Language.ENGLISH);
Document document = ocrx.recognizeToMemory(parameters, original Img);
Image scanned to BMP? Check. BMP loaded as a bufferedimage
? Check. Now onto the next step of my project. OCR Xpress provided a few options: output to file, output to PDF, and output to memory. For my needs, I wanted to use recognizeToMemory
. I needed to initialize the OCR Xpress engine, and RecognitionParameters
. Then I simply called recognizeToMemory
to OCR the image to memory.
Word Search
Now that I had my newspaper in an electronic form, I could search for words/phrases. This also allowed me to find the rectangle area of each word/phrase that I searched for. I was able to add these to a list of RECTs, which I then merged with my "blackout" page.
String searchString = InputString();
if (searchString.isEmpty()) {
searchString = "You give something possible by psychic sign passed forth on networks?";
}
String[] searchWords = searchString.split("\\s+");
Using the helper function InputString
, all I needed to do was pass in a default value and populate the string array with Java’s split function using either the users input or the default value.
This part was the most difficult, I actually had to stop and think of a poem to write, using the words given in the article.
http://www.pressreader.com/
"You give something possible by psychic sign passed forth on networks?" OK, so not the best, but writing poetry is a lot harder than writing code.
Redaction Action
The next step (which seemed to be most difficult) was also made easy. First, let me explain redaction and what it’s used for. Redaction is the process of censoring or obscuring part of a text for legal or security purposes. When someone says redaction, you might think top secret "your eyes only" documents. However, every company has sensitive material that they may wish to redact, such as customer data, company data, or employee data to name a few. However, my redaction needs are more of the "I love you" variety. Once the text was redacted, I was then able to merge the redacted areas with the original image that was converted to black.
try {
outputImg = ImageIO.read(new File (inputImagePath));
ConvertsToBlack(outputImg);
} catch (IOException e) {
e.addSuppressed(e);
return;
}
There are a few different ways to copy the original image, but for simplicity I created a new bufferedImage
outputImg
. For this image, I needed to convert all pixels to black. This is where the ConvertToBlack
helper function comes into play.
int curWord = 0;
for (Word word : document.getWords()) {
if (curWord >= searchWords.length)
break;
if (word.getText().equals(searchWords[curWord]) == false)
continue;
Raster img = originalImg.getData(ConvertRect(word.getArea()));
outputImg.setsData(img);
curWord++;
}
DisplayImage(outputImg);
}
}
I needed to search each word and extract the area corresponding to the image’s coordinates. OCR Xpress provided a getArea()
function that did just that. I had the area of the word I was searching. I needed to convert the RECTs, get the pixel data, and then set the data to the outputImg
file before moving to the next word.
Then I could display my newspaper blackout poem.
Conclusion
Before you run off and create your own newspaper blackout poems, let’s quickly summarize what was mentioned above. I described what OCR was, and how many companies find it very useful in everyday practical means. I also went over redaction and how companies and individuals benefit from its use. Lastly, I showed you how I used OCR and redaction for an interesting and fun project. I would encourage you to try this project for yourself, and share your "artsy" side with the world.
Links
You can download OCR Xpress and this example from the following links:
OCR Xpress: https://www.accusoft.com/products/ocr-xpress/overview/.
To obtain an evaluation license, you will need to contact Accusoft’s support. They will also be able to answer any questions that you might have.
OCR Xpress & NewspaperBlackout Application: https://github.com/Accusoft/Programmable-Poetry-Using-OCR