• Welcome to OS2World OLD-STATIC-BACKUP Forum.
 

News:

This is an old OS2World backup forum for reference only. IT IS READ ONLY!!!

If you need help with OS/2 - eComStation visit http://www.os2world.com/forum

Main Menu

Script->Scanimage->Tesseract->Script (repeatedly)

Started by jep, 2008.11.15, 20:45:46

Previous topic - Next topic

jep

Hello,

RobertM pointed out that Tesseract 2.03 (compiled for OS/2-eCS by Franz Bakan, just as GOCR) is a the better choice. My tests suggest that too, very impressive, though I can't get all letters in the Swedish alphabet currently as å and Å ( see instruction¹  further down, how to pronunciate ) are replaced by ä and Ä. One can download support for different languages too, but more has to be added, Swedish is one of them.

It doesn't seem to be hard to add language support to Tesseract either, so I've looked at Chris Wohlgemuth's DrDialog Control for DrDialog in combination with GBM and it should be possible to write a tool that can help the users add more languages quite easily.

The output from Tesseract is in UTF-8 format if you have text that contain characters not found within the first 127 characters of UTF-8 ², so you therefore have to use e.g. OpenOffice or similar to see the text as you expect.

Below, you'll find code I've reused from the GOCR script. The code has been modified to allow mutiple page scans, something very useful if you're in a hurry. Please note that I haven't tested nor expect Tesseract to be able to read from STDIN (as GOCR) so the debug mode should be set at all times. The same warning apply here as well.

OBS!! *** VARNING!! *** OBS!!
This code is provided "AS IS" without any warranty of any kind that it may work and/or not cause damage to your hardware (e.g. scanner).

I've tested this script repeatedly on my computer, but the documentation for SANE mention that you should take care not to use the wrong settings that the scanner can't handle as it then may do something illegal and thus cause damage. Please, read the manual for SANE to check that the parameters used in this script is ok with your setup. You may also want to adjust the params, but any usage of the script that result in fault/damage is your own fault.

Pay special attention to the variable for scanner resolution ( cfg.scanarea.1.dpi, cfg.scanarea.2.dpi etc. ) and that your scanner can handle it.
OBS!! *** VARNING!! *** OBS!!


//Jan-Erik

/* Tesseract */

'@echo off'

call RxFuncAdd 'SysGetKey', 'RexxUtil'
call RxFuncAdd 'SysCls', 'RexxUtil'

/* Try to determine the scanner */
'@scanimage -L | rxqueue SESSION /FIFO'
parse value LINEIN( 'QUEUE:' ) with 'device `'cfg.device"' is a" cfg.scanner.manufacturer cfg.scanner.name cfg.scanner.model cfg.scanner.type 'scanner'
'@rxqueue SESSION /CLEAR'

if length( cfg.device ) = 0 then
  if ARG() = 0 then
    return Usage()
  else
    cfg.device = ARG(1) /* e.g. plustek:usbcalls:2 */

cfg.debug = 1

cfg.scanarea.1.l = 10
cfg.scanarea.1.t = 10
cfg.scanarea.1.x = 200
cfg.scanarea.1.y = 277
cfg.scanarea.1.dpi = 600
cfg.scanarea.1.filter = XRANGE(A, Z)||XRANGE(a, z)||XRANGE( 0, 9 )||"åäöÅÄÖ-"
counter = 0
do forever
   retval = GetTextWithOCR(1, counter )
   say 'Scan the next page?!'
   if SysGetKey() <> d2c( 13 ) then
      return 0
   Call SysCls
   counter = counter + 1
end
Return 0

GetTextWithOCR: procedure expose cfg.
  counter = ARG(1) /* variable to point to the right coordinates */
  /* Scan the area of the image with the info we want to read */
  /* pipe the image data to GOCR to interpret it and pipe the output text to the current rexx queue */
  if cfg.debug then
  do
    '@scanimage -d '||cfg.device||' --progress --format=tiff --mode Gray -l '||cfg.scanarea.counter.l||' -t '||cfg.scanarea.counter.t||' -x '||cfg.scanarea.counter.x||' -y '||cfg.scanarea.counter.y||' --resolution '||cfg.scanarea.counter.dpi||' > out.tif'
    '@tesseract "out.tif" "out'||ARG(2)||'" -l deu'
    filesize = STREAM( 'out'||ARG(2)||'.txt', 'C', 'QUERY SIZE' )
    if filesize > 0 then
      retval = CHARIN( 'out'||ARG(2)||'.txt', 1, filesize )
    else retval = ''
    call STREAM 'out'||ARG(2)||'.txt', 'C', 'CLOSE'
  end
  else
  do
    '@scanimage -d '||cfg.device||' --format=tiff --mode Gray -l '||cfg.scanarea.counter.l||' -t '||cfg.scanarea.counter.t||' -x '||cfg.scanarea.counter.x||' -y '||cfg.scanarea.counter.y||' --resolution '||cfg.scanare.counter.dpi||'| tesseract -l deu | rxqueue SESSION /FIFO'
    retval = LINEIN( 'QUEUE:' )
    '@rxqueue SESSION /CLEAR'
  end
Return retval /* Return the text found (if any) */

Usage:
PARSE SOURCE . . fname
say FILESPEC( 'N', fname )||' name_of_device'
say
say 'Example: '||FILESPEC( 'N', fname )||' plustek:usbcalls:2'
Return 0


_______________
¹ A short lesson how to hear/say å, ä and ö ( Å, Ä and Ö ).

Repeat after me (preferably with a British accent):

"Gordon cares for the earth"

below, I've replaced each part with the corresponding letters.

"Gårdon cäres får the örth"

Do you hear the difference between the o's in "Gordon"?
The first one is more like "å" than "o".

Now you know how to read and talk Swedish, sort of...  ;D
____________
² UTF-8 apparently look the same for the first 127 characters as most other western/latin codepages (850, 437, 1004/1252 etc).