Jump to content

Tesseract (Screen OCR) UDF


seangriffin
 Share

Recommended Posts

  • 5 months later...

Idk if this post is still alive, but i can`t get it to work.

$Text = _TesseractWinCapture($win_title,"",0,"",1,2,0,0,0,0,1)

"D:\OneDrive\Documents\AutoIT\OCR\Tesseract.au3" (349) : ==> Variable must be of type "Object".:
$Obj1.ShowFile ($capture_filename, 1)
$Obj1^ ERROR

 

any ideas?

Link to comment
Share on other sites

This is the latest place to get Tesseract: https://github.com/tesseract-ocr/tesseract

I believe as of me posting this the latest "stable" version is 3.05 but they also have a 4.00 alpha.

I'm not sure if anyone is still having the issue with Preview.Preview.1 but I managed to fix that a while ago when we had updated all of the PCs in the office.

On 5/17/2017 at 2:29 PM, KemyKo said:

Idk if this post is still alive, but i can`t get it to work.

$Text = _TesseractWinCapture($win_title,"",0,"",1,2,0,0,0,0,1)

"D:\OneDrive\Documents\AutoIT\OCR\Tesseract.au3" (349) : ==> Variable must be of type "Object".:
$Obj1.ShowFile ($capture_filename, 1)
$Obj1^ ERROR

 

any ideas?

You most likely need to change the tesseract path in the UDF. Someone explained how to do it on page 6 of this thread.

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

  • 1 month later...
On 22.5.2017 at 3:57 PM, anthonyjr2 said:

This is the latest place to get Tesseract: https://github.com/tesseract-ocr/tesseract

I believe as of me posting this the latest "stable" version is 3.05 but they also have a 4.00 alpha.

I'm not sure if anyone is still having the issue with Preview.Preview.1 but I managed to fix that a while ago when we had updated all of the PCs in the office.

You most likely need to change the tesseract path in the UDF. Someone explained how to do it on page 6 of this thread.

Hi, 

i have the same issue. that is what i did:

I changed

;ShellExecuteWait(@ProgramFilesDir & "\Tesseract-OCR\tesseract.exe", $capture_filename & " " & $ocr_filename)

To
 

    ShellExecuteWait("C:Program Files (x86)\Tesseract-OCR\tesseract.exe", $capture_filename & " " & $ocr_filename, "", "", @SW_HIDE)

Example Code:

#include <Tesseract.au3>

$omg = _TesseractScreenCapture(0,"",1,1,377,630,493,670,1)
MsgBox(0, "Test:", $omg)


Global $Chrome = WinWait("[TITLE:Google - Google Chrome")
If $Chrome = 0 Then Exit 2

WinActivate($Chrome)

ConsoleWrite('"' & _TesseractScreenCapture(0, "", 1, 5, 781, 113, 832, 136, 1) & '"' & @LF)

 

Error:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Users\admin\Desktop\TesseractExample\Example1.au3"    
"C:\Program Files (x86)\AutoIt3\Include\Tesseract.au3" (187) : ==> Variable must be of type "Object".:
$Obj1.ShowFile ($capture_filename, 1)
$Obj1^ ERROR
>Exit code: 1    Time: 5.532

 

Any idea how to fix it?

 

=>> Tesseract download: tesseract download for windows

=>>Tesseract.au3 file (is located in C:\Program Files (x86)\AutoIt3\Include)

=>> Im using Windows 10 64Bit

Link to comment
Share on other sites

  • 2 months later...
On 2/13/2009 at 9:03 AM, seangriffin said:

This UDF provides text capturing support for applications and controls using Tesseract - an OCR engine currently developed by Google.

 

Tesseract was originally developed as proprietary software at Hewlett-Packard between 1985 until 1995. After ten years without any development taking place, Hewlett Packard and UNLV released it as open source in 2005. Tesseract is currently developed by Google and released under the Apache License, Version 2.0.

 

Tesseract is considered one of the most accurate free software OCR engines currently available. It was one of the top 3 engines in the 1995 UNLV Accuracy test.

 

My main goal in developing this UDF is to provide AutoIT users with a free Screen OCR solution that competes with other commercial (payed) technologies like Microsoft Office Document Imaging (MODI) and Textract.

 

REQUIREMENTS:

 

  • AutoIt3 3.2 or higher
  • Tesseract 2.01 or above

INSTALLATION:

To install Tesseract:

 

LIST OF FUNCTIONS:

 

DEMONSTRATION:

<Under Construction>

 

EXAMPLES:

_TesseractControlCapture.au3_TesseractControlFind.au3

 

DOWNLOAD:

 

Latest Version - v0.6 (17/03/09)

Tesseract.au3

Hi Seangriffin,

you have done awesome work. but while using the above sample scripts i am getting an error.

Could you please help me out. Thanks in advance.

I am highlighting the error message. 

 

Error message : "D:\AutoIt3\OCR_AutoIt\Tesseract\Tesseract.au3" (968) : ==> Subscript used on non-accessible variable.:
$hBMP = _WinAPI_CreateCompatibleBitmap($hDC, ($pos[2] * $scale) - ($right_indent * $scale), ($pos[3] * $scale) - ($bottom_indent * $scale))
$hBMP = _WinAPI_CreateCompatibleBitmap($hDC, ($pos^ ERROR

Link to comment
Share on other sites

  • 1 month later...
On 8/11/2016 at 7:28 AM, MaximusCZ said:

Hello,

I am trying to run tesseract, but I cant find any exe installator anywhere.

All the links seems to be broken/moved and on official repo I can only download source / binaries that doesnt install.

 

Can anyone upload theirs copy of working installator of tesseract for all the people that come looking for it? 

 

Thanks!

Ver 3.02 in: https://sourceforge.net/projects/tesseract-ocr-alt/files/latest/download?source=files

And more dowloads in: https://sourceforge.net/projects/tesseract-ocr-alt/files/

 

Link to comment
Share on other sites

  • 6 months later...

Some example code to test if concept still works used version 3.05.1 output.txt will contain the result (on my system about 30 seconds to analyze full screen which you probably normally should not do)

Tweaking with scaling somewhere above 4.0 scaling gives some nice results certainly on black/white areas.

However 8.0 is much to high.

#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>

local $TIF_FILENAME="c:\temp\test.tif"
Local $TESS_PARAMS= $TIF_FILENAME & " output"
Local $TESS_EXE=@ProgramFilesDir & "\Tesseract-OCR\tesseract.exe"
local $TWorkDir="C:\temp\"
Local $iScale = 8.0 ;1.0 is without any scaling

SaveTiffImage()
sleep(1000) ; Little time for saving the file
AnalyzeImage()

Func SaveTiffImage()
    _GDIPlus_Startup()
    Local Const $iW = @DesktopWidth , $iH = @DesktopHeight 

    Local $hHBmp = _ScreenCapture_Capture("", 0, 0, $iW, $iH) ;create a GDI bitmap by capturing 1/16 of desktop
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI bitmap to GDI+ bitmap
    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore

    Local $hBitmap_Scaled = _GDIPlus_ImageScale($hBitmap, $iScale, $iScale, $GDIP_INTERPOLATIONMODE_NEARESTNEIGHBOR) ;scale image by 275% (magnify)

    ; Save resultant image
    _GDIPlus_ImageSaveToFile($hBitmap_Scaled, $TIF_FILENAME)

    ;cleanup resources
     _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_BitmapDispose($hBitmap_Scaled)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

func AnalyzeImage()
    Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
    shellexecutewait($TESS_EXE, $TESS_PARAMS, $TWorkDir)
    Local $fDiff = TimerDiff($hTimer) ; Find the difference in time from the previous call of TimerInit. The variable we stored the TimerInit handlem is passed as the "handle" to TimerDiff.
    consolewrite( ($fDiff/1000) &  "seconds passed")
EndFunc

 

Link to comment
Share on other sites

  • 1 year later...

Hey guys! Kudos on all the good work! I am need of a little help. I am currently using imagesearch to find "pictures" of word and if the search is true I have a popup followed by a screencapture. My issue is that the pdf reports I am running this on change in size depending on how large the report is. I figured OCR would be my best bet. I know this thread is a wee bit old, but would it be possible to get assistance on this? I am just wanting to find specific words or numbers on the screen. Thanks in advance

Link to comment
Share on other sites

  • 5 months later...
On 5/2/2018 at 1:48 AM, junkew said:

Some example code to test if concept still works used version 3.05.1 output.txt will contain the result (on my system about 30 seconds to analyze full screen which you probably normally should not do)

Tweaking with scaling somewhere above 4.0 scaling gives some nice results certainly on black/white areas.

However 8.0 is much to high.

#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>

local $TIF_FILENAME="c:\temp\test.tif"
Local $TESS_PARAMS= $TIF_FILENAME & " output"
Local $TESS_EXE=@ProgramFilesDir & "\Tesseract-OCR\tesseract.exe"
local $TWorkDir="C:\temp\"
Local $iScale = 8.0 ;1.0 is without any scaling

SaveTiffImage()
sleep(1000) ; Little time for saving the file
AnalyzeImage()

Func SaveTiffImage()
    _GDIPlus_Startup()
    Local Const $iW = @DesktopWidth , $iH = @DesktopHeight 

    Local $hHBmp = _ScreenCapture_Capture("", 0, 0, $iW, $iH) ;create a GDI bitmap by capturing 1/16 of desktop
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI bitmap to GDI+ bitmap
    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore

    Local $hBitmap_Scaled = _GDIPlus_ImageScale($hBitmap, $iScale, $iScale, $GDIP_INTERPOLATIONMODE_NEARESTNEIGHBOR) ;scale image by 275% (magnify)

    ; Save resultant image
    _GDIPlus_ImageSaveToFile($hBitmap_Scaled, $TIF_FILENAME)

    ;cleanup resources
     _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_BitmapDispose($hBitmap_Scaled)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

func AnalyzeImage()
    Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
    shellexecutewait($TESS_EXE, $TESS_PARAMS, $TWorkDir)
    Local $fDiff = TimerDiff($hTimer) ; Find the difference in time from the previous call of TimerInit. The variable we stored the TimerInit handlem is passed as the "handle" to TimerDiff.
    consolewrite( ($fDiff/1000) &  "seconds passed")
EndFunc

 

  • Hi!

Thaks you works great.

Do you know how i can OCR with other lang, not eng?

Link to comment
Share on other sites

  • 2 months later...

Hi all,

I'm currently working with V0.6 of this library and the padding logic isn't bahaving like I'd expect.
(The captured area doesn't match the coordinates I entered and the image is stretched)

Therefore I looked at the code and found what I think is a mistake in the padding logic.
The diff for my fix is attached.

Cheers,
siiikooo0743

 

paddingLogicFix.patch

Link to comment
Share on other sites

  • 4 weeks later...

I rewrite this code, because it didn't work for me. I had the problem, that the functions didn't capture the correct area.

I added some changes from airday, don134.

The changes are marked with ";Start code" and ";End code", the original code is comment out.

I rewrite the code to use coordinates like Pixelsearch, idea from hendrikhe.

Add _GDIPlus_ImageScale to CaptureToTiff function, I didn't find or understand the original scale function.

Tesseract.au3

Link to comment
Share on other sites

  • 1 year later...

Hi there, i cannot get this to work, it will recognize the upper Letters (example pictures are different) but not the lowers.

The correct part of the screen is being captured (checked the image file)

but it does not recocnize the button texts...

Also tried scaling from 1-8...

 

 

#include <Tesseract.au3>
$g_szVersion = "IchBinDa"
If WinExists($g_szVersion) Then Exit ; It's already running
AutoItWinSetTitle($g_szVersion)


   $win_title="App Window Name"
   $left_indent     = 30
   $top_indent      = 130
   $right_indent    = 300
   $bottom_indent   = 390

   $pad_left        = 150
   $pad_top         = 230
   $pad_right       = 220
   $pad_bottom      = 240

   $padA_scale      = 2; try1
   $padA_left       = 180
   $padA_top        = 250
   $padA_right      = 350
   $padA_bottom     = 400

   $padA_scale      = 4; try2
   $padA_left       = 160; einfach doppelte Zahl?
   $padA_top        = 220
   $padA_right      = 350
   $padA_bottom     = 405

   $padA_scale      = 8; try3
   $padA_left       = 147; einfach doppelte Zahl?
   $padA_top        = 202
   $padA_right      = 344
   $padA_bottom     = 400

   $padA_scale      = 1; try4
   $padA_left       = 240
   $padA_top        = 350
   $padA_right      = 345
   $padA_bottom     = 390


$temp_path= 'C:\tmp'
_TesseractTempPathSet($temp_path)
$win_text = ""
$get_last_capture = 0
$delimiter = ""
$cleanup = 0
$scale = 2
$show_capture   = 0

WinActivate($win_title)
$txt=""
$PIN=_TesseractWinCapture($win_title, $win_text, $get_last_capture, $delimiter, $cleanup, $scale, $left_indent, $top_indent, $right_indent, $bottom_indent, $show_capture)
;MsgBoxx($PIN)
$txt="Pin: '" & $PIN & "'(mit Zeilenumbruch)" &@CRLF
$PAD=_TesseractWinCapture($win_title, $win_text, $get_last_capture, $delimiter, $cleanup, $scale, $pad_left, $pad_top, $pad_right, $pad_bottom, $show_capture)
;MsgBoxx($PAD)
$PAD_A=_TesseractWinCapture($win_title, $win_text, $get_last_capture, $delimiter, $cleanup, $padA_scale, $padA_left, $padA_top, $padA_right, $padA_bottom, $show_capture)
$txt=$txt & " $PAD_A: '" & $PAD_A & "'" &@CRLF &"Pad: '" &$PAD &"'"

WinActivate($g_szVersion)
MsgBoxx($txt);

func MsgBoxx($msgboxx_msg)
      MsgBox(0, $g_szVersion, $msgboxx_msg)
EndFunc

 

 

 

unrecognized_part.tif

 

tesseract ver: https://jztkft.dl.sourceforge.net/project/tesseract-ocr-alt/tesseract-ocr-setup-3.02.02.exe

MsgBox.png

example - Kopie.PNG

example.PNG

Edited by yasuo
Link to comment
Share on other sites

  • 5 months later...

I love this utility - thank you for figuring this all out.

My needs are not as complicated and I do not need all the functionality provided here.  All I really ever want to do is to read the text displayed on the active window contained in some rectangle.  I actually find if you limit the OCR to an area of the screen with text (and not with images (icons/dropbox controls/etc.) then the OCR works a lot better.

You still need to #include <Tesseract.au3> and use _TesseractTempPathSet()

Please Note: Unlike PixelSearch when specifying an area to OCR you must specify the pixel area to search from upper left to lower right.

Some examples:

; convert the entire active window screen image to text

$resultText = _SimpleTesseractScreenCapture() 

; convert the text in the pixel area (200,200) to (400,600)

$resultText = _SimpleTesseractScreenCapture(200,200,400,600)

; when the text is small you may find accuracy is better if you raise the scale factor.  Here the pixel area is blown up 5 times before running it into the OCR.

$resultText = _SimpleTesseractScreenCapture(200, 200, 400, 600, 5)

#include <Tesseract.au3>
_TesseractTempPathSet("c:\Users\Odlid Tubpu\Tesseract\Temp")

ConsoleWrite("------------------------"&@CRLF)

; OCR all the text on the active window
$result = _SimpleTesseractScreenCapture()
ConsoleWrite($result&@CRLF)
ConsoleWrite("------------------------"&@CRLF)

; OCR the text on the active window in the area 200 pixels to the left of the window left edge, 200 pixels down from the window top edge to 400 pixels from the left and 600 pixels down
$result = _SimpleTesseractScreenCapture(200,200,400,600)
ConsoleWrite($result&@CRLF)
ConsoleWrite("------------------------"&@CRLF)

; if OCR accuracy is low you can sacrifice performance for more accuracy by magnifying the screen before OCRing (although experiment...sometimes lower magnification works better)
$result = _SimpleTesseractScreenCapture(200,200,400,600,5)
ConsoleWrite($result&@CRLF)
ConsoleWrite("------------------------"&@CRLF)

Func _SimpleTesseractScreenCapture($left=0,$top=0,$right=@DesktopWidth,$bottom=@DesktopHeight,$scale = 2)
    Local $hGUI
    Local $giTIFColorDepth = 24
    Local $giTIFCompression = $GDIP_EVTCOMPRESSIONNONE
    Local $capture_filename, $ocr_filename, $ocr_filename_and_ext, $hBitmap2, $hImage1, $hImage2, $hWnd, $hDC, $hBMP, $hGraphic, $CLSID, $tParams, $tData, $pParams, $final_ocr

    ; create a temporary file for the screen capture and the OCR text results file
    $capture_filename = _TempFile($tesseract_temp_path, "~", ".tif")
    $ocr_filename = StringLeft($capture_filename, StringLen($capture_filename) - 4)
    $ocr_filename_and_ext = $ocr_filename & ".txt"

    ; Capture window
    $hBitmap2 =_ScreenCapture_Capture("",$left,$top,$right,$bottom,False)

    _GDIPlus_Startup ()

    ; Convert the image to a bitmap
    $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap2)

    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, ($right-$left) * $scale, ($bottom-$top) * $scale)

    _WinAPI_ReleaseDC($hWnd, $hDC)
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
    _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, ($right-$left) * $scale, ($bottom-$top)*$scale)
    $CLSID = _GDIPlus_EncodersGetCLSID("TIF")

    ; Set TIFF parameters
    $tParams = _GDIPlus_ParamInit(2)
    $tData = DllStructCreate("int ColorDepth;int Compression")
    DllStructSetData($tData, "ColorDepth", $giTIFColorDepth)
    DllStructSetData($tData, "Compression", $giTIFCompression)
    _GDIPlus_ParamAdd($tParams, $GDIP_EPGCOLORDEPTH, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "ColorDepth"))
    _GDIPlus_ParamAdd($tParams, $GDIP_EPGCOMPRESSION, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "Compression"))
    If IsDllStruct($tParams) Then $pParams = DllStructGetPtr($tParams)

    ; Save TIFF and cleanup
    _GDIPlus_ImageSaveToFileEx($hImage1, $capture_filename, $CLSID, $pParams)
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _WinAPI_DeleteObject($hBMP)
    _GDIPlus_Shutdown()

    ; use an OS shell to command line run tesseract.exe and do the actual OCR
    ShellExecuteWait(@ProgramFilesDir & "\tesseract\tesseract.exe", $capture_filename & " " & $ocr_filename)

    $final_ocr = FileRead($ocr_filename_and_ext)

    ; clean up the files
    FileDelete($ocr_filename & ".*")

    Return $final_ocr

EndFunc

 

Edited by OdlidTubpu
typo in a comment
Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...