Vis2 - Image to Text OCR()

Post your working scripts, libraries and tools for AHK v1.1 and older
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Vis2 - OCR() - Update: Additional Language Support

28 Mar 2018, 13:23

Test One.PNG
Test One.PNG (23.66 KiB) Viewed 6951 times
Test Two.PNG
Test Two.PNG (30.08 KiB) Viewed 6951 times
Edit:
iseahound wrote:
@gameba Redownload latest version from GitHub. Vis2 is now compatable with AHK_H
Running DetectBugs.ahk, I get the following error messages using AHK_H 64-bit, 1.1.28.00:

Code: Select all

>"C:\Program Files\AutoHotkey\AutoHotkey.exe" /ErrorStdOut "C:\Downloads\Automation\AHK related\Vis2\DetectBugs.ahk"    
C:\Program Files\AutoHotkey\Lib\Gdip_All.ahk (66) : ==> Duplicate function definition.
     Specifically: UpdateLayeredWindow(hwnd, hdc, x="", y="", w="", h="", Alpha=255)
>Exit code: 2    Time: 0.4351
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Vis2 - OCR() - Update: Additional Language Support

28 Mar 2018, 23:21

UPDATE Added error handling code on my GitHub. Also fixed a few bugs. Given that I am unable to reproduce the "blank box" myself, I cannot tell if it has been fixed.

Thanks to all the testers, and people who are willing to respond. As always posting the error message is very helpful.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Vis2 - OCR() - Update: Additional Language Support

28 Mar 2018, 23:29

@burque505 Is it possible that you have two copies of Gdip_All.ahk somewhere? I did not define a second version of UpdateLayeredWindow() I wonder if @gameba encountered this error.
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Vis2 - OCR() - Update: Additional Language Support

29 Mar 2018, 07:37

Hi iseahound, gdip_all is in my Lib folder (i.e. C:\Program Files\Autohotkey\Lib). Removing Gdip_all.ahk from the github lib folder fixed that part! AHK_H now works, so does AHK_L, at least in theory, because I still have the "blank box."
Now I'll try the new code - oops :(
Win7 SP 64-bit, AHK_L 1.1.28.00 64-bit
Error.PNG
Error.PNG (61.88 KiB) Viewed 6904 times
Then the blank box.
That does give me a clue for spelunking in the code, thanks. Now I'm thinking that somewhere in this leptonica code segment (lines 2025-2027):

Code: Select all

            _cmd .= this.leptonica " " in " " out
            _cmd .= " " negateArg " 0.5 " performScaleArg " " scaleFactor " " ocrPreProcessing " 5 2.5 " ocrPreProcessing  " 2000 2000 0 0 0.0"
            RunWait % ComSpec " /C " _cmd,, Hide
things are going south, on my system at least. I have DPI scaled up to 125%, but the December 2017 version worked fine, so maybe that has nothing to do with it.

Thanks for staying on top of this, iseahound.

Regards,
burque505
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Vis2 - OCR() - Update: Additional Language Support

29 Mar 2018, 12:59

@Burque505 Do you have a non-English username? As in C:/Users/%username%
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Vis2 - OCR() - Update: Additional Language Support

29 Mar 2018, 14:36

No, it's pretty generic English :)
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Vis2 - OCR() - Update: Additional Language Support

29 Mar 2018, 15:07

Does replacing

Code: Select all

RunWait % ComSpec " /C " _cmd,, Hide
with

Code: Select all

RunWait % _cmd,, Hide
fix it?

EDIT: what about this?

Code: Select all

RunWait % "cmd /C " _cmd,, Hide
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Vis2 - OCR() - Update: Additional Language Support

29 Mar 2018, 15:30

How about commenting out this line?

Code: Select all

         __New(language:=""){
            this.language := language
            ;this.uuid := Vis2.stdlib.CreateUUID()          ; <-- This one
            this.file := A_Temp "\Vis2_screenshot" this.uuid ".bmp"
            this.fileProcessedImage := A_Temp "\Vis2_preprocess" this.uuid ".tif"
            this.fileConvertedText := A_Temp "\Vis2_text" this.uuid ".txt"
         }

EDIT: Just to give you an idea of what I'm doing, and why this bug is so odd.
This is the preprocess function in December 2017

Code: Select all

      preprocess(f_in, f_out){
         static ocrPreProcessing := 1
         static negateArg := 2
         static performScaleArg := 1
         static scaleFactor := 3.5

         RunWait, % Vis2.Tesseract.leptonica " " f_in " " f_out " " negateArg " 0.5 " performScaleArg " " scaleFactor " " ocrPreProcessing " 5 2.5 " ocrPreProcessing  " 2000 2000 0 0 0.0", , Hide
      }
this is it today:

Code: Select all

         preprocess(in:="", out:=""){
            static ocrPreProcessing := 1
            static negateArg := 2
            static performScaleArg := 1
            static scaleFactor := 3.5

            in := (in != "") ? in : this.file
            out := (out != "") ? out : this.fileProcessedImage

            if !(FileExist(in))
               throw Exception("Input image for preprocessing not found.",, in)

            _cmd .= this.leptonica " " in " " out
            _cmd .= " " negateArg " 0.5 " performScaleArg " " scaleFactor " " ocrPreProcessing " 5 2.5 " ocrPreProcessing  " 2000 2000 0 0 0.0"
            RunWait % ComSpec " /C " _cmd,, Hide

            if !(FileExist(out))
               throw Exception("Preprocessing failed.")

            return out
         }
there has been no real changes to the function or to the underlying leptonica.exe.

EDIT #2: https://github.com/iseahound/Vis2/archive/master.zip Added even better error handling code.
Last edited by iseahound on 29 Mar 2018, 16:00, edited 1 time in total.
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: Vis2 - OCR() - Update: Additional Language Support

29 Mar 2018, 16:00

Iseahound. How does the accuracy of this compare to Capture2Text? Have you ever tested Capture2Text?

I'm interested in this project...

Thank you for working on this.

Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Vis2 - OCR() - Update: Additional Language Support

29 Mar 2018, 16:02

@robodesign It is the same accuracy since we both use the same open source projects, tesseract and leptonica.
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: Vis2 - OCR() - Update: Additional Language Support

29 Mar 2018, 16:22

Thanks for the quick response.

Capture2Text can automatically select regions to capture text from. For example, a text line underneath the mouse cursor.... Can this script do this as well?

Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Vis2 - OCR() - Update: Additional Language Support

30 Mar 2018, 10:49

Getting closer, but no luck yet. First I captured the actual _cmd string like this, and stopped execution:

Code: Select all

            fileappend, %_cmd%, Command.txt
            msgbox execute command.txt in leptonica directory
Checking in %temp%, these files were created:
Capture1.PNG
Capture1.PNG (3.81 KiB) Viewed 6831 times
Then I executed the contents of '_cmd' from the command prompt. These files were created:
Capture2.PNG
Capture2.PNG (6.18 KiB) Viewed 6831 times
So the .tif CAN be created.
I put in a 5-second delay, and processing still failed, but this time at the conversion to txt.

EDIT: Honing in on it. Changed the leptonica command to:

Code: Select all

RunWait % _cmd, ,Hide
Now the .tif file gets created in %temp%, and error is now thrown by tesseract, not leptonica.
EDIT: Also changed tesseract runwait to:

Code: Select all

RunWait % _cmd, ,Hide
Still getting tesseract error, but command runs.
EDIT: I used the same "command.txt" strategy to run tesseract in the middle of the process. Here's the result:
CaptureTess.PNG
CaptureTess.PNG (7.1 KiB) Viewed 6826 times
EDIT: The above command will run if I enclose all the parameters in quotes. Now all I need to do is figure out how to get the variables in

Code: Select all

         convert_best(in:="", out:="", fast:=0){
            in := (in) ? in : this.fileProcessedImage
            out := (out) ? out :  this.fileConvertedText
            fast := (fast) ? this.tessdata_fast : this.tessdata_best
enclosed in quotes.

Workaround: No spaces in folder names :)


Regards,
burque505
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Vis2 - OCR() - Update: Additional Language Support

30 Mar 2018, 13:50

Can't put more than 3 images in a post, so here's a screenshot with the folder names stripped of spaces.
NoSpaces.PNG
NoSpaces.PNG (14.62 KiB) Viewed 6819 times
Win7, 64-bit no changes to script. Just made sure containing folder was named correctly.
Still trying to modify the code so it will accept spaces.
But :dance: it works for now.

Regards,
burque505
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Vis2 - OCR() - Update: Additional Language Support

30 Mar 2018, 14:26

burque505 wrote:enclosed in quotes.
That is a very silly mistake I made. I updated a new file, it works with spaces now. Thanks for all the hard work!
Himmilayah

Re: Vis2 - OCR() - Update: Additional Language Support

30 Mar 2018, 15:00

iseahound wrote:
burque505 wrote:enclosed in quotes.
That is a very silly mistake I made. I updated a new file, it works with spaces now. Thanks for all the hard work!
I hope you can fix it for windows 10 64 bit as well

I am getting a blank box
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Vis2 - OCR() - Update: Additional Language Support

30 Mar 2018, 17:21

Thank you, iseahound! For me, lines 2002 through 2037 are very instructive on the proper use of 'q' for quoting. Thanks for teaching me.
Spoiler
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Vis2 - OCR() - Update: Additional Language Support

30 Mar 2018, 18:54

burque505 wrote:Thank you, iseahound! For me, lines 2002 through 2037 are very instructive on the proper use of 'q' for quoting. Thanks for teaching me.
Spoiler
Does the latest update fix the blank box bug? If so, we can finally lay this bug to rest :D [AHK has its own escape character, but it makes the code impossible to read. Using q := Chr(34) or Chr(0x22) has the benefit of being v2 compatible as well.]
GeneBene

Re: Vis2 - OCR() - Update: Additional Language Support

30 Mar 2018, 22:56

iseahound wrote:
burque505 wrote:Thank you, iseahound! For me, lines 2002 through 2037 are very instructive on the proper use of 'q' for quoting. Thanks for teaching me.
Spoiler
Does the latest update fix the blank box bug? If so, we can finally lay this bug to rest :D [AHK has its own escape character, but it makes the code impossible to read. Using q := Chr(34) or Chr(0x22) has the benefit of being v2 compatible as well.]
Win 10 64bit blank box, tested on three systems
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Vis2 - OCR() - Update: Additional Language Support

31 Mar 2018, 00:27

@GeneBene Did you download the latest version from https://github.com/iseahound/Vis2/archive/master.zip ? Is there an error message? I just tested Vis2 again on Windows 10 64 bit, new install.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 126 guests