레이블이 OCR인 게시물을 표시합니다. 모든 게시물 표시
레이블이 OCR인 게시물을 표시합니다. 모든 게시물 표시

2015년 1월 19일 월요일

Open Source OCR on Linux using GUI Frontends for Tesseract

Although I use Linux both at home and at work, for some tasks, like OCR for Korean and Chinese, I have had to rely on proprietary software on Windows (ABBYY Finereader provides excellent recognition results, by the way). This is starting to change, thanks to the tesseract OCR engine currently sponsored by Google.

Tesseract has been around for several years, but it wasn't easily accessible before the advent of GUI frontends that make it easy to select the area of an image to be recognized. The two more popular frontends to tesseract are YAGF (which also works with the Cuneiform OCR engine) and gimagereader both of which now use the QT framework (the latter used to be based on gtk, but in recent versions, QT can also be used).

Screenshot of YAGF



Screenshot of gimagereader


Tesseract's English-language recognition is almost on par with ABBYY Finereader for 300 dpi images, but much worse than Finereader at detecting images less than 300 dpi resolution. When it comes to non-English text, especially Asian text such as CJK (Chinese, Japanese, Korean) and other scripts, however, the performance of the tesseract engine still has a long way to go before matching the performance of Finereader.

YAGF doesn't give the option to use Asian languages, despite the existence of tesseract data files for many Asian languages. For example, here is a listing of the available tesseract-data packages for various languages in Archlinux:

[archjun@lenovoS310 cam1]$ sudo pacman -Ss tesseract-data
[sudo] password for archjun: 
community/tesseract-data-afr 3.02.02-5 (tesseract-data)
    Tesseract OCR data (afr)
...
community/tesseract-data-chi_sim 3.02.02-5 (tesseract-data)
    Tesseract OCR data (chi_sim)
community/tesseract-data-chi_tra 3.02.02-5 (tesseract-data)
    Tesseract OCR data (chi_tra)
...
community/tesseract-data-jpn 3.02.02-5 (tesseract-data)
    Tesseract OCR data (jpn)
...
community/tesseract-data-kor 3.02.02-5 (tesseract-data) [installed]
    Tesseract OCR data (kor)
...
community/tesseract-data-vie 3.02.02-5 (tesseract-data)
    Tesseract OCR data (vie)

Piping the output through wc -l gives a line count of 130, divided by 2 (two lines per entry) gives 65 unique languages supported by Tesseract. As you can see in the sample output above, Asian languages CJK and Vietnamese are supported. According to the YAGF developer, Asian language OCR will be added to the GUI menu after European languages.

Fortunately, gimageview does support OCR for Asian languages as long as the necessary language data for tesseract has been installed. You may notice that the screenshot of gimagereader shows Korean text being recognized. Unfortunately, tesseract does a poor job of recognizing Korean. Although I haven't done a meticulous count, I would say off the top of my head that the results in the second screenshot above represent a recognition accuracy of maybe 70%. This is much worse than ABBYY Finereader. The tesseract-ocr project page offers some tips for improving OCR accuracy, such as upping the scan resolution, deskewing pages, etc., but the scanned image I used to test tesseract for Korean returned 90%+ OCR accuracy in ABBYY Finereader on Windows.

My conclusion: circa Jan. 2015, tesseract is good for English, not so good for Hangul/Korean.

2014년 6월 10일 화요일

Handy Imagemagick tools for cropping book images prior to OCR

It's annoying to edit out page headers and footers that have been automatically OCR'd. One way to avoid this problem is to manually specify the OCR area, but this can become quickly tedious if you have hundreds of pages to process.

Enter Imagemagick's convert command. Today I will talk about the -chop and -shave config flags for convert.

These commands are a lifesaver when we want to unneeded areas (i.e. footers/headers, page numbers, etc.) that appear in a constant location from multiple pages.

A useful resource for the various image-cropping options available in convert can be found here:

http://www.imagemagick.org/Usage/crop/

Consider the following scan from a book:


Every page contains a page number at the bottom and on every other page along the right margin the book's title written vertically. We want to crop all the book's pages s.t. the page number and vertical title won't appear in the final image -- this will make OCR go much faster as we won't have to manually select which area needs to be OCR'ed.
In this particular example, we can use

convert "스님의_주례사 - 0011.png" -gravity SouthEast -chop 250x200 SE_chopped250x200.png

Which will remove 250 pixels from the right (East) and 200 pixels from the bottom (South). In the case of the -crop config flag, the reference point (0, 0) for all pixel calculations is the top-leftmost corner of the canvas. By using the -gravity flag, however (quote from Imagemagick docs):

The direction you choose specifies where to position text or subimages. For example, a gravity of Center forces the text to be centered within the image. By default, the image gravity is NorthWest.

So -gravity SouthEast will make the reference point (0, 0) the bottom-rightmost corner of the canvas. Now the resulting chopped image looks like:



​As you can see in the above image, the extraneous text from the right and bottom margins has been cropped out!

In other books, however, the location of extraneous text might be different. Let's say you want to remove text from both the top and bottom (or left and right) of the following screencap:

Eliminating the viewing frame can be accomplished with the convert flag -shave, which shaves pixels from the edges of an image (top & bottom, left & right). The arguments of -shave are:

... -shave [numPixelsLeftRightEdges]x[numPixelsTopBottomEdges]

Note that the brackets above should not actually be typed out. So if you wanted to remove 100 pixels from the left and right edges, you would pass the following arguments to - shave:

... -shave 100x0

If you want to remove 100 pixels from the top and bottom edges:

... -shave 0x100

To remove 100 pixels from both the top & bottom as well as left & right edges:

... -shave 100x100

The single page with its top and bottom edges removed:

convert escape_from_evil_frame_ex.png -shave 0x50 shaved_0x50.png


Finally, one more example. Say we have the following screencap containing two facing  pages:


Let's use the -shave flag to remove extraneous areas from both the top and bottom, left and right to make the image more amenable to OCR.

convert two_page.png -shave 80x50 two_page_shaved_80x50.png

This command shaves 80 pixels from both the left and right as well as 50 pixels from the top and bottom leaving us with the following image:


To run any of the above commands on all the images in a directory, simply invoke convert with a wildcard. For example:

convert *.png -configFlag outputFilename.png

Imagemagick will automatically increment outputFilename: outputFilename0.png, outputFilename1.png...