Jump to content

Recommended Posts

Posted

hi, I am using the _SimpleTesseract udf found here in the forums.

any suggestions to improve accuracy in the attached sample image?  I have it opened in "Snip" tool in windows. The 43 is found. The 44 is NOT. Is it recommended to use an alternative OCR for numbers?

 

#include <ScreenCapture.au3>
#include <Tesseract.au3>

Global $aOCR_IMG_ORI                                                    ;Global Var that will store the location of the image that was taken to perform OCR on
Global $tesseract_temp_path = @TempDir & "\"                            ;Location of the image that is taken, and the .tsv file that is generated from that
Global $tesseract_path = @ScriptDir & "\tesseract\tesseract.exe" ;Need to have the Tesseract-OCR folder in the sub-folder of the script thats running
Global $sPath, $aFileName
Local $x_buy, $y_buy, $strike_center_c_scan, $price, $bp
_TesseractTempPathSet(@TempDir & "\")



WinActivate ("Snip")
$number43 = _SimpleTesseractScreenCapture(940, 510,992, 532 , 2)
$number44 = _SimpleTesseractScreenCapture(940, 540,992, 562 , 2)

ConsoleWrite ("number43=" &$number43 &@CRLF)
ConsoleWrite ("number44=" &$number44 &@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(@ScriptDir & "\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

 

tesseract_problem.png

Posted (edited)

@drrehak

i have tested your image in Snip tool using same script(it's working fine)

WinActivate ("Snipping Tool")

Output was:

number43=43


number44=44
 

Desktop Resolution:1280x768

info from Autoit v3 Window Info:

Title = Snipping Tool

Position = 837,320

Size 339,395

 

Edited by ad777

none

Posted

it doesnt work for me 😌. I have tested on 2 different computers.  Thats my problem. I think Tesseract can be persnickety with OCR numbers. 

Is it advisable to change it to black and white, or reverse the image (since I have white lettering on a dark grey background)?  I am not sure how I would do that.

Posted (edited)

Hello I used this command line parameters time ago for numbers.

 

--psm 10 --oem 3 -c tessedit_char_whitelist=.0123456789

 

Edited:

If You're over Windows 10 you can use this alternative.

 

Saludos

Edited by Danyfirex
Posted (edited)
Global $tesseract_path = @ScriptDir & "\tesseract\tesseract.exe --psm 10 --oem 3 -c tessedit_char_whitelist=.0123456789"

i added this. My script still only finds the 43.  not the 44.. 

Edited by drrehak
Posted

writing "google it" does not help anyone... 🙄

update:

ShellExecuteWait(@ScriptDir & "\tesseract\tesseract.exe", $capture_filename & " " & $ocr_filename & " -c tessedit_char_whitelist=0123456789 --psm 7")

this is working for me! 

and anyone using tesseract.... I found this explanation of use cases extremely helpful. I think choosing the right psm for me was the important part

https://www.pyimagesearch.com/2021/11/15/tesseract-page-segmentation-modes-psms-explained-how-to-improve-your-ocr-accuracy/

Posted

@drrehak It has helped me a bunch in the past. 🧐 I showed you how I did before, You can't expect people solve your whole problems all time. I enjoy helping people as I did in this thread, I'm sure I helped you but You can't admit it. 😛 great you solved it without "google it" 😉

 

Saludos

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
×
×
  • Create New...