5

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.

2
  • @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, 2018 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, 2018 at 0:05

4 Answers 4

5

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.

14
  • 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, 2018 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. Nov 23, 2018 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, 2018 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? Nov 23, 2018 at 20:42
  • 1
    @sup My guess is that you're using a different shell. In Bash, x=foo.jpg; echo "${x%%.*}" prints foo, as intended. Aug 24, 2020 at 18:35
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.

0

It's a bit late for you but I have the same issue and noticed that while tesseract only outputs one text file, each output is separated by a form feed character (0x0C).

2
  • You can add the argument -c page_separator='' that should take care of that.
    – user3169
    Mar 3, 2021 at 6:07
  • thanks but that was not an issue, i was answering to your question. You can split the file into multiples by looking for the form feed separator. Mar 3, 2021 at 13:50
-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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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