3

I am currently using tesseract to OCR some jpeg files to txt files (Ubuntu 16.04). Typically this is ~500 files in one directory.

I know I can do this by making a text file with all the file names (savedlist.txt), and then do:

tesseract savedlist.txt output.txt

however output.txt is a single file with all the ocr results.

What I need is to be able to save the ocr results to individual txt files with the same file name as the original image file. For example:

input file: image456.jpeg
output file: image456.txt

I realize that ABBYY FineReader more recent versions can do this, but this is for a gratis project so I am looking for a free or inexpensive (less than $20) solution.

What I am looking for a software front-end or GUI using tesseract that can batch process ocr like this, all in one batch operation.

A command line solution to do this would also be OK. If off-topic here, I can ask this on another site but I didn't want to post on two sites at the same time.

  • @Izzy In case there is a programming solution rather than a software one, would there be any problem to also post this question on another site? Any recommendation (SuperUser or another one)? – user3169 Nov 19 '18 at 19:00
  • If by programming you mean scripting, SU might fit. Programming normaly is located on SO. If you make sure the question is focused differently (so it's indeed a different question – e.g. here asking for existing software, there asking for scripting solutions), there shouldn't be a problem. – Izzy Nov 20 '18 at 0:05
2

In Bash:

for file in FILES ; do tesseract "$file" "${file%%.*}" ; done

where in place of FILES, you should write a series of file names like image456.jpeg image457.jpeg image458.jpeg, or a glob pattern like *.jpeg, or any such combination.

  • I see, but how to handle the extension change? Output files needs to have the .txt extension. FILES will be a list of file names. – user3169 Nov 23 '18 at 0:31
  • @user3169 Tesseract adds the extension ".txt" automatically, the last time I checked. If you say tesseract img.jpg foo.txt, then you actually get foo.txt.txt. Usually it's annoying, but here it's convenient. – Kodiologist Nov 23 '18 at 3:19
  • I tried this and it did the tesseract processing OK, but the results were still all in one text file (FILES.txt). Your example doesn't seem to change the fact that Tesseract only supports a single file output, unless I'm missing something. – user3169 Nov 23 '18 at 19:17
  • @user3169 I think you may have misunderstood my example. The literal characters FILES shouldn't appear in your command. What command did you use, exactly? – Kodiologist Nov 23 '18 at 20:42
  • No, I just named my list "FILES" to follow you example. My command: for file in FILES ; do tesseract "$file" "$file" ; done. I presume "$file" is the image file name pulled from FILE, since the ocr function was performed. But then where is the individual output text file? All I got was one FILE.txt with all the results. – user3169 Nov 23 '18 at 22:50
0

There is another solution, which require a bit more parsing.

I needed to handle a fair amount of files, and to grab for each one the confidence level of each word found; handling file one by one was very slow.

Your post gave me the right path : use a file containing your list, and output result to another file, but do not use TSV or TXt file; they're not splitted nor sorted by file.

Use HOCR instead; it's plain HTML in which you can navigate programatically :

list.txt :

SAMPLE-000.png
SAMPLE-001.png
SAMPLE-002.png

Command line :

tesseract list.txt list hocr

Sample output ( part of, for readability ); list.hocr :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  <meta name='ocr-system' content='tesseract 4.1.0' />
  <meta name='ocr-capabilities' content='ocr_page ocr_carea ocr_par ocr_line ocrx_word ocrp_wconf'/>
 </head>
 <body>
  <div class='ocr_page' id='page_1' title='image "SAMPLE-000.png"; bbox 0 0 3508 2592; ppageno 0'>
   <div class='ocr_carea' id='block_1_1' title="bbox 3283 1170 3313 1477">
    <p class='ocr_par' id='par_1_1' lang='eng' title="bbox 3283 1170 3313 1477">
     <span class='ocr_caption' id='line_1_1' title="bbox 3283 1170 3313 1477; baseline -307 0; x_size 39.714287; x_descenders 9.9285717; x_ascenders 9.9285717">
      <span class='ocrx_word' id='word_1_1' title='bbox 3284 1170 3313 1220; x_wconf 94'>EP</span>
      ( ... )
     </span>
    </p>
    ( ... ) 
   </div>
  </div>
  <div class='ocr_page' id='page_2' title='image "SAMPLE-001.png"; bbox 0 0 2592 3508; ppageno 1'>
   <div class='ocr_carea' id='block_2_1' title="bbox 792 194 1861 225">
    <p class='ocr_par' id='par_2_1' lang='eng' title="bbox 792 194 1861 225">
     <span class='ocr_header' id='line_2_1' title="bbox 792 194 1861 225; baseline 0.006 -4; x_size 39.866665; x_descenders 9.9666662; x_ascenders 9.9666662">
      <span class='ocrx_word' id='word_2_1' title='bbox 792 194 828 221; x_wconf 96'>17</span>
      ( ... ) 
     </span>
    </p>
   </div>
   ( ... )    
  </div>
 </body>
</html>

You can notice some interesting attributes in markup :

  • title='image "SAMPLE-000.png"; bbox 0 0 3508 2592; ppageno 0'
  • x_wconf which seems to stand for confidence

etc. You "just" have to parse it with your favorite tool.

-1

It is more easy than described here. Think of a more difficult situation where you only have the files in a folder, and you want to extract the texts and save it in a csv file output.

Just visit my github repo at https://github.com/aneendo/Image_to_Text and run the main.py file

New contributor
Anindya Sundar Sinha is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.