Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

OCR.ahk - Library for recognizing text in images


  • Please log in to reply
88 replies to this topic
camerb
  • Moderators
  • 573 posts
  • Last active: Sep 14 2015 03:32 PM
  • Joined: 19 Mar 2009
It seems to me that there are a bunch of forum posts on OCR (and ODR), where a lot of people say "I used OCR with AHK", and others say "How do you use OCR with AHK?" But nobody has actually made a lib for it to make it easy to use. I went ahead and did it, cause I've been kinda wanting to do it for a while... so here you go!

OCR.zip v0.96 [AHK_basic] [AHK_L ANSI] [AHK_L Unicode] [32-bit or 64-bit]

Starting with 0.96, this lib should work fine with 64-bit. Let me know if there are any issues.

To start out, extract the zip and verify that the function works by running OCR-preview.ahk, and you should be able to see that the OCR program evaluates the area under the pink rectangle that is displayed. Try looking at larger text first, to make sure that it works. Then, feel free to use OCR-example.ahk to see what your code should look like. As with any AHK lib, you'll need the rest of the files to stay in a common lib directory, but you should only need to #include OCR.ahk to use the RunOCR() function.

Simply put, the OCR function will make a JPG screenshot of the region specified (using GDIp), convert it to a PNM (using djpeg.exe), then run gocr.exe to get a good guess of what that region contained. Note that smaller screen fonts are less reliable, and GOCR frequently confuses letters that look similar to one another (like lowercase T/F, L/I, etc...). There is also a parameter called "options" that allows you enter "debug" mode or "numeric"-only mode (just pass it a string with that text in it). Note that I'm planning a future version that should be capable of getting rid of some of the misinterpretations that GOCR makes, and thinking it will also be able to determine if a given string is displayed in a given region of the screen. Only time will tell.

Please feel free to give feedback, info on bugs, etc...
keywords: camerb OCR lib, Optical Character Recognition, Optical Digit Recognition, text in images,
Aren't you glad that I didn't put an annoying gif here?

sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010
Nice if it would work, but it didn't read a single letter unfortunately. I just unzipped it all and ran both the example and preview. OCR seems to be #included "as it is". I run Win7 x64 and AHK_L Unicode x32.

feedbacker
  • Guests
  • Last active:
  • Joined: --
Same here. Tried it, but it doesn't work. :( WinXP AHK_L Unicode x32

camerb
  • Moderators
  • 573 posts
  • Last active: Sep 14 2015 03:32 PM
  • Joined: 19 Mar 2009
I believe it will only work for AHK_basic at the moment. I'll see if I can get it working on AHK_L this week. Thanks for letting me know!
Aren't you glad that I didn't put an annoying gif here?

guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
worked ok for me on AHK Basic using the test script with the pink box. some letters were wrong though, but not your fault

you might also want to check out this older thread, its not exactly a library, but its a complete solution using gocr or tesseract. look towards the end of the thread where people had fixed some of the issues. i believe the technique used was to convert the image to black&white before doing the ocr to get more consistent results:

<!-- m -->http://www.autohotke...pic.php?t=11186<!-- m -->

camerb
  • Moderators
  • 573 posts
  • Last active: Sep 14 2015 03:32 PM
  • Joined: 19 Mar 2009
Uploaded version 0.91 - reduced the number of library dependencies and added support for AHK_L.
Aren't you glad that I didn't put an annoying gif here?

Grendahl
  • Members
  • 416 posts
  • Last active: Jul 07 2014 08:01 PM
  • Joined: 10 Aug 2009
Still doesn't work for me at all. (Win7 x64)

The box "reads" the 200x50 area, but returns garbage. Not a single character is correct, and the font it returns them to me in looks like chinese.

camerb
  • Moderators
  • 573 posts
  • Last active: Sep 14 2015 03:32 PM
  • Joined: 19 Mar 2009
Grendahl: I sent you a PM, but I'll post my request for more info here as well... Can you send me a screenshot of the preview script as it is running? Also, have you tried running it over larger text to see if it recognizes that? What AHK version are you running? Is it AHK_L, AHK_basic? Is it 32 bit or 64 bit? Unicode or ANSI?

Thanks for your help.
Aren't you glad that I didn't put an annoying gif here?

StarBlue
  • Members
  • 17 posts
  • Last active: May 14 2013 10:17 PM
  • Joined: 12 Aug 2011
Could you possibly make it clearer as to where should the files for the OCR function be put (I mean the files from the zipped OCR folder that has been made available for download). I presently put them in the same directory where the Autohotkey exe is located, but all I see when I fire the autohotkey exe is a pink rectangle with no text captured. Thanks.

bmoore45
  • Members
  • 273 posts
  • Last active: Mar 14 2012 01:00 PM
  • Joined: 10 Jul 2011
this works great for me thanks op!

camerb
  • Moderators
  • 573 posts
  • Last active: Sep 14 2015 03:32 PM
  • Joined: 19 Mar 2009
bmoore45: Awesome! Glad you're liking it.

Also, I'm currently working with StarBlue to track down the issues he is having with the lib.
Aren't you glad that I didn't put an annoying gif here?

yash.dhiman
  • Members
  • 210 posts
  • Last active: May 12 2016 05:49 AM
  • Joined: 22 Aug 2011
Hello Camerb,

I have one problem.....i want to read a text from web application but wingettext command is not working properly.


IfWinNotActive, AWD/Contact – Windows Internet Explorer, Favorites Command Ba,
WinActivate, AWD/Contact
WinWait, AWD/Contact
WinGetText, text ; The window found above will be used.
MsgBox, The text is:`n%text%

in this script varable text returns some other values which doesn't exist on that window like...navigationbar...etc. so could you please help me to sort out this issue........

camerb
  • Moderators
  • 573 posts
  • Last active: Sep 14 2015 03:32 PM
  • Joined: 19 Mar 2009
yash.dhiman: Please post a new thread in the "Ask for Help" section with your question. This thread is not related to the question you have asked...

Ask for Help section: <!-- m -->http://www.autohotke...ewforum.php?f=1<!-- m -->
Aren't you glad that I didn't put an annoying gif here?

bmoore45
  • Members
  • 273 posts
  • Last active: Mar 14 2012 01:00 PM
  • Joined: 10 Jul 2011
hey camerb, is there a way to limit the OCR recognition to numbers only and not letters?? thanks!

camerb
  • Moderators
  • 573 posts
  • Last active: Sep 14 2015 03:32 PM
  • Joined: 19 Mar 2009
Not at the moment, but I am thinking about investigating that someday. It isn't high on my priority list, though.
Aren't you glad that I didn't put an annoying gif here?