Jump to content
Sign in to follow this  
hendrikhe

[LITTLE PROB] OCR , tesseract, Need Help Oo!

Recommended Posts

hendrikhe

I am trying to read numbers on my screen and I can do it only with a pixel-read function.

The numbers are betwen 0.01 and 1000000.99 (always on the same postion in the screen) and looks like the attachment but with changing background color.

I searched on forum and found OCR as best solution solution:

http://www.autoitscript.com/forum/index.ph...mp;hl=tesseract

I downloaded tesseract.au3 to C:\Programme\AutoIt3\Include and installed Terreract.

I have read tesseract.au3 and thought that _TesseractScreenCapture() function would be my solution.

Well I did a mini test script,but I failed hard:

CODE
#include <Tesseract.au3>

sleep(5000)

$test = _TesseractScreenCapture($get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2,

$left_indent = 1, $top_indent = 2, $right_indent = 3,

$bottom_indent = 4, $show_capture = 1)

MsgBox(0, "Result:", $test)

But that results in:

C:\Dokumente und Einstellungen\UserName\Desktop\alter.au3 (5) : ==> Error parsing function call.:

$omg = _TesseractScreenCapture($get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2,

$omg = _TesseractScreenCapture($get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2^ ERROR

I couldnt find any script or guide to help me with tesseract.au3. I would be gratefulfor any help.

Greetz

Hendrik

post-33507-1239167851_thumb.jpg

Edited by hendrikhe

Share this post


Link to post
Share on other sites
Authenticity

lol I'm surprised that AU3Check find this spot of syntax error while the entire call seems to me to be wrong. ;]

Anyway, if you need a command to span more than one line you need to put underscore "_" at the end of the line.

You need to call it as:

$test = _TesseractScreenCapture(0, "", 1, 2, 1, 2, 3, 4, 1)
MsgBox(0, "Result:", $test)

; But because everything is optional except for the first parameter you can call it like this:
;$test = _TesseractScreenCapture(0)
MsgBox(0, "Result:", $test)

...Edit: Please ignore my last comment. Optional parameters are there for you to call it with default values. If you need other values just ignore me.

Edited by Authenticity

Share this post


Link to post
Share on other sites
hendrikhe

I got it :D

here the small code:

CODE
#include <Tesseract.au3>

sleep(5000)

$omg = _TesseractScreenCapture(0,"",1,2,377,630,493,670,1)

MsgBox(0, "Mouse x,y:", $omg)

EDIT:

How can I "scan" only a certain part of my screen like PixelChecksum with the coordinates

PixelChecksum(377,630,493,670)??

The script above scans a lot more than the desired region :/

Edited by hendrikhe

Share this post


Link to post
Share on other sites
hendrikhe

--delete pls--

Edited by hendrikhe

Share this post


Link to post
Share on other sites
hendrikhe

*bump*

Hmm can nobody help me with my problem above? :D

Share this post


Link to post
Share on other sites
Authenticity

Read the function parameters explanation. You can specify rectangular dimensions of the screen. The dimensions are x-left, y-top, x-right, y-bottom. Don't get confused with x, y, width and height or you'll reference unpredictable rectangular dimensions.

Share this post


Link to post
Share on other sites
Angoth

Tesseract has a zoom function, which it defaults to 2. This means that the X,Y coords that you input aren't reliably the screen coords, no matter how carefully you map them out.

Here's what I did. I made a quick side script, turned on the display of the capture and found the correct X1, Y1, X2 and Y2 that I needed through trial and error. It was a bit time consuming, yes, but, the results have been rock solid ever since.

Share this post


Link to post
Share on other sites
hendrikhe

Hmmm, I am pretty sure that _TesseractScreenCapture(0,"",1,2,377,630,493,670,1) is already in x-left, y-top, x-right, y-bottom (377,630 x-left/y-top and 493,670 x-right/y-bottom).

Well if I am wrong pls correct my parameters in the function pls.

@Angoth

Wow thats a completly new aspect for me. But I fear that I am not able to create such a quick side script *desperation*

Share this post


Link to post
Share on other sites
Angoth

Hmmm, I am pretty sure that _TesseractScreenCapture(0,"",1,2,377,630,493,670,1) is already in x-left, y-top, x-right, y-bottom (377,630 x-left/y-top and 493,670 x-right/y-bottom).

Well if I am wrong pls correct my parameters in the function pls.

@Angoth

Wow thats a completly new aspect for me. But I fear that I am not able to create such a quick side script *desperation*

You've got the script there with the parameters you've set out. What I mean is: 377, 630 to 493, 670 isn't the screen X's and Y's. You've got the zoom at the default of 2.

From the docs on the UDF:

_TesseractScreenCapture($get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)

You're setting the $scale to 2, so your coords will not be the absolute screen X,Y. You are also displaying the capture, so you've got everything you need right there to map it out. It takes some time, but, as I said, it's worth it.

Run that portion of the script with the $show_capture = 1 (you are doing it). See if it's what you are looking for, exactly. If not, adjust 377,630,493,670. Rinse, repeat until happy. Season, to taste.

As far as correcting your script parameters, I can't. I don't know what you are looking for and what you're looking at when it caps. You'll have to do it by trial and error. I don't recommend $scale = 1 just to solve this problem. The OCR got buggy on my when I did that. I'd rather spend the time up front getting the cap right and reliably getting screen OCR text than trying to massage the OCR at scale = 1 to shave some time.

Angoth

Edited by Angoth

Share this post


Link to post
Share on other sites
hendrikhe

I understand the problem. I tried _TesseractScreenCapture(0,"",1,1,377,630,493,670,1) with $scale = 1 just to try, but it keeps scanning a wrong area near middle of the screen (I got a 1024x768 resolution).

#include <Tesseract.au3>

sleep(5000)

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

MsgBox(0, "Test:", $omg)

I only need to scan a very small area, not like the script above but more like the picture on my first post.

post-33507-1239399451_thumb.gif

Edited by hendrikhe

Share this post


Link to post
Share on other sites
JRowe

The problem is the bass ackwards method of specifying the screenshot parameters. Indents from top, bottom, left, and right, instead of simple x,y coords.

No offense to seangriffin of course, I'm sure it made sense at the time, but it is very counterintuitive to move from specifying points on the screen to indents.

So,

;                   $left_indent        - A number of pixels to indent the capture from the
;                                           left of the screen.
;                   $top_indent         - A number of pixels to indent the capture from the
;                                           top of the screen.
;                   $right_indent       - A number of pixels to indent the capture from the
;                                           right of the screen.
;                   $bottom_indent      - A number of pixels to indent the capture from the
;                                           bottom of the screen.

1024x768 resolution means =

left top corner = $left_indent, 0+ $top_indent.

right bottom corner = 768 - $right_indent, 1024 - $bottom_indent

I'll revise the screenshot method to take two parameters: top left corner, bottom right corner.

Reverse the math and plug in the values to get your correct box:

377, 614

564, 679

$left_indent = 377

$top_indent = 614

$right_indent = 204 (768-564)

$top_indent = 255 (1024-679)

So your revised, fixed function that scans the area you want:

#include <Tesseract.au3>

sleep(5000)

$omg = _TesseractScreenCapture(0,"",1,1,377,614,204,255,1)
MsgBox(0, "Test:", $omg)

Cool udf, seangriffin!

Share this post


Link to post
Share on other sites
JRowe

Upon further thought, the zoom.au3 example has some good screencapture and zoom functionality. I'm trying to think of what other scripts I've seen using sectional screenshots, but the zoom script seems the best place to start.

A real time (polling) OCR demo would be cool, I just can't recall where I saw the realtime zoom/magnifying script. Anyone else recall where it is?

Share this post


Link to post
Share on other sites
hendrikhe

So your revised, fixed function that scans the area you want:

#include <Tesseract.au3>

sleep(5000)

$omg = _TesseractScreenCapture(0,"",1,1,377,614,204,255,1)
MsgBox(0, "Test:", $omg)

Cool udf, seangriffin!

Hmm I tried but it didnt worked like we wanted (take a look on Attachment). But thanks with the explication with the resolution, I will take a look and try to fix my problem with that.

I tested a bit and modified Tesseract.au3 (ofc I undo my modifications before testing ur suggestions) getting my area but the screenshot got a too bad quality to recognize any letters for OCR.

CODE
Func CaptureToTIFF($win_title = "", $win_text = "", $ctrl_id = "", $sOutImage = "", $scale = 1, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0)

Local $hWnd, $hwnd2, $hDC, $hBMP, $hImage1, $hGraphic, $CLSID, $tParams, $pParams, $tData, $i = 0, $hImage2, $pos[4]

Local $Ext = StringUpper(StringMid($sOutImage, StringInStr($sOutImage, ".", 0, -1) + 1))

Local $giTIFColorDepth = 24

Local $giTIFCompression = $GDIP_EVTCOMPRESSIONNONE

; If capturing a control

if StringCompare($ctrl_id, "") <> 0 Then

$hwnd2 = ControlGetHandle($win_title, $win_text, $ctrl_id)

$pos = ControlGetPos($win_title, $win_text, $ctrl_id)

Else

; If capturing a window

if StringCompare($win_title, "") <> 0 Then

$hwnd2 = WinGetHandle($win_title, $win_text)

$pos = WinGetPos($win_title, $win_text)

Else

; If capturing the desktop

$hwnd2 = ""

$pos[0] = 0

$pos[1] = 0

$pos[2] = @DesktopWidth

$pos[3] = @DesktopHeight

EndIf

EndIf

; Capture an image of the window / control

if IsHWnd($hwnd2) Then

WinActivate($win_title, $win_text)

$hBitmap2 = _ScreenCapture_CaptureWnd("", $hwnd2, 0, 0, -1, -1, False)

Else

$hBitmap2 = _ScreenCapture_Capture("", 0, 0, -1, -1, False)

EndIf

$hBitmap2 = _ScreenCapture_Capture("", 377, 630, 493, 670, False) ;---!!!!!!!! Here I modified !!!!!!!!!---

_GDIPlus_Startup ()

; Convert the image to a bitmap

$hImage2 = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap2)

$hWnd = _WinAPI_GetDesktopWindow()

$hDC = _WinAPI_GetDC($hWnd)

$hBMP = _WinAPI_CreateCompatibleBitmap($hDC, ($pos[2] * $scale) - ($right_indent * $scale), ($pos[3] * $scale) - ($bottom_indent * $scale))

_WinAPI_ReleaseDC($hWnd, $hDC)

$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)

$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)

_GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0 - ($left_indent * $scale), 0 - ($top_indent * $scale), ($pos[2] * $scale) + $left_indent, ($pos[3] * $scale) + $top_indent)

$CLSID = _GDIPlus_EncodersGetCLSID($Ext)

; 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, $sOutImage, $CLSID, $pParams)

_GDIPlus_ImageDispose($hImage1)

_GDIPlus_ImageDispose($hImage2)

_GDIPlus_GraphicsDispose ($hGraphic)

_WinAPI_DeleteObject($hBMP)

_GDIPlus_Shutdown()

EndFunc

post-33507-1239404936_thumb.gif

Edited by hendrikhe

Share this post


Link to post
Share on other sites
hendrikhe

I solved my problem modificating tesseract.au3 a bit, especially the function CaptureToTIFF().

Adding a few parameters, I am able now to use _TesseractScreenCapture like other AutoIt standard pixel-functions like

PixelChecksum or Pixelsearch.

#

CODE
include <SimpleTesseract.au3>

sleep(5000)

$omg = _TesseractScreenCapture(0,"",1,3,377,630,493,670,1)

MsgBox(0, "Test:", $omg)

_TesseractScreenCapture($get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2, $iLeft = 0, $iTop = 0, $iRight = 1, $iBottom = 1, $show_capture = 0)

I added 4 new parameters for left, top, right, bottom (e.g. above 377,630,493,670).

On the attachment you can dl the modified script. You need the programm Tesseract to run it http://www.autoitscript.com/forum/index.ph...mp;hl=tesseract.

And thnx for the help guys.

Hendrik

SimpleTesseract.au3

Edited by hendrikhe

Share this post


Link to post
Share on other sites
Capel

Hendrik,

I like what you have done here, but I have a problem. When I specify a window title I get a solid black image, if I let it default to the desktop, I get the correct image. Here is my script:

#include <SimpleTesseract.au3>

sleep(1000)

$tempfile = "test.tif"

if FileExists($tempfile) Then

FileDelete($tempfile)

EndIf

CaptureToTIFF("ocr_test_text.txt - Notepad","","",$tempfile,1,0,0,800,600)

What am I missing?

Share this post


Link to post
Share on other sites
Authenticity

If you've changed CaptureToTIFF to the one hendrikhe posted you'll need to change this:

if IsHWnd($hwnd2) Then
    
    WinActivate($win_title, $win_text)
    $hBitmap2 = _ScreenCapture_CaptureWnd("", $hwnd2, $iTop, $iRight, $iBottom, False) ; Missing $iLeft
ElseoÝ÷ ÚÚ-+ºÚ"µÍYÒÛ
    ÌÍÚÛH[BUÚ[XÝ]]J   ÌÍÝÚ[Ý]K   ÌÍÝÚ[Ý^
BIÌÍÚ]XHÔØÜY[ØWÐØUÛ
    ][ÝÉ][ÝË    ÌÍÚÛ    ÌÍÚSY    ÌÍÚUÜ   ÌÍÚTYÚ  ÌÍÚPÝÛK[ÙJB[Ù

Share this post


Link to post
Share on other sites
Capel

That did it! Thanks Authenticity.

Share this post


Link to post
Share on other sites
Capel

Next question.

How do I specify a particular window with _TesseractScreenCapture? I won't always know where the window is because I'm working in a dual screen environment. The app I'm trying to OCR could be on the left or right. I got the screenshot using CaptureToTIFF, but it's not clear how to provide this to TesseractScreenCapture. Using the $get_last_capture did not seem to work.

Share this post


Link to post
Share on other sites
hendrikhe

If you've changed CaptureToTIFF to the one hendrikhe posted you'll need to change this:

if IsHWnd($hwnd2) Then
    
    WinActivate($win_title, $win_text)
    $hBitmap2 = _ScreenCapture_CaptureWnd("", $hwnd2, $iTop, $iRight, $iBottom, False) ; Missing $iLeft
ElseoÝ÷ ÚÚ-+ºÚ"µÍYÒÛ
    ÌÍÚÛH[BUÚ[XÝ]]J   ÌÍÝÚ[Ý]K   ÌÍÝÚ[Ý^
BIÌÍÚ]XHÔØÜY[ØWÐØUÛ
    ][ÝÉ][ÝË    ÌÍÚÛ    ÌÍÚSY    ÌÍÚUÜ   ÌÍÚTYÚ  ÌÍÚPÝÛK[ÙJB[Ù
Yeah true, changed and updated, thnx for the hint.

Would like to help you Capel but I dont know how ^_^ Maybe try to WinMove() the window first in a certain position before using _TesseractScreenCapture ?

SimpleTesseract.au3

Edited by hendrikhe

Share this post


Link to post
Share on other sites
hendrikhe

No idea what happened, I had Windows XP and the this script was fine... Since I am using Vista I got this error msg:

C:\Program Files (x86)\AutoIt3\Include\SimpleTesseract2.au3 (140) : ==> Variable must be of type "Object".:

$Obj1.ShowFile ($capture_filename, 1)

$Obj1^ ERROR

>Exit code: 1 Time: 0.912

No idea if it matter but my folder is c:\Programe (x86)\ and not C:\Program Files (x86)\

Any ideas?

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×