Advancing OCR

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
stev3n
Junior Coder
Posts: 22
Joined: Thu Nov 01, 2007 1:13 am

Advancing OCR

Post by stev3n » Wed Nov 07, 2007 4:29 am

I have the OCR script as posted in the sample scripts section. Here's what I'm trying to do:

Take a screenshot of a selected area
The area is seperated in a gridlike form [soduku]
I'd like to be able to use the OCR for every square to determine which square is a number and which is just a space
And for every number I'd like to do an action [but I can do this]

So my question is how can I use the OCR to read one specified area at a time? Can I somehow set values for each square that is contained in the screenshot, and have the OCR read each square individually?

Thanks and if this way too complicated, sorry and forget this existed me.

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Nov 07, 2007 4:58 am

If the grid location and cells sizes are known you could take an individual screen shot for each cell and OCR each image to determine its value.

stev3n
Junior Coder
Posts: 22
Joined: Thu Nov 01, 2007 1:13 am

Post by stev3n » Wed Nov 07, 2007 5:37 pm

That's what I thought I might have to do, but I just thought it would take a very long time.

Anyways, I've gotten the coords (all 81), and I'm having problems with the sample OCR script to take just a screenshot of a certain section.

ScreenCapture>242,311,273,342,C:\sod\1.bmp
VBEval>DoOCR("C:\sod\1.bmp"),TheText

returns

:-959966715

EP_E_DCOM_OCR_FAILRECOG

Line 11, Column 2

Here is the whole script I have. And as a side note, is there anyway to do this better than configuring a manual 81 lines of code?

Code: Select all

VBStart
Function DoOCR(bitmapfile)
  Dim miDoc
  Dim miLayout
  Dim stringOut

  set miDoc=CreateObject("MODI.Document")
  miDoc.Create (bitmapfile)
  ' Perform OCR.
  'You can change the mousepointer here to an hourglass or something.
  miDoc.Images(0).OCR
  'Change the mouse back to normal default.
  set miLayout = miDoc.Images(0).Layout
  stringOut=miLayout.Text
  'MsgBox(stringOut)
  DoOCR = stringOut
  Set miLayout = Nothing
  Set miDoc = Nothing
End Function
VBEND

ScreenCapture>242,311,273,342,C:\sod\1.bmp
VBEval>DoOCR("C:\sod\1.bmp"),TheText

//Display the text
MessageModal>TheText

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Nov 07, 2007 8:14 pm

Here's a sample showing how I would approach this. But I don't really know what you're trying to accomplish so this might or might not be relevant.

I get your exact error message if I select a section of screen that has no imagery. In other words if the screencapture> bitmap has only a solid color, the OCR VBscript crashes. I have no idea how to resolve this.

Edit-1: Added the error line Marcus provided and fixed the array variable assignment.

Code: Select all

VBStart
Function DoOCR(bitmapfile)
  Dim miDoc
  Dim miLayout
  Dim stringOut

  on error resume next
  set miDoc=CreateObject("MODI.Document")
  miDoc.Create (bitmapfile)
  ' Perform OCR.
  'You can change the mousepointer here to an hourglass or something.
  miDoc.Images(0).OCR
  'Change the mouse back to normal default.
  set miLayout = miDoc.Images(0).Layout
  stringOut=miLayout.Text
  'MsgBox(stringOut)
  DoOCR = stringOut
  Set miLayout = Nothing
  Set miDoc = Nothing
End Function
VBEND



SRT>MatrixCapture
Add>FileArrayCounter,1
ScreenCapture>MatrixCapture_var_1,MatrixCapture_var_2,MatrixCapture_var_3,MatrixCapture_var_4,%TEMP_DIR%~temp.bmp
VBEval>DoOCR("%TEMP_DIR%~temp.bmp"),TheText
Let>TheText_%FileArrayCounter%=TheText
END>MatrixCapture

Let>FileArrayCounter=0
//GoSub Format:  MatrixCapture,UpperX,UpperY,LowerX,LowerY
GoSub>MatrixCapture,242,311,273,342
GoSub>MatrixCapture,242,342,273,373
GoSub>MatrixCapture,242,373,273,404
GoSub>MatrixCapture,242,404,273,435
//etc.......

Let>FileArrayCounter=0
Repeat>FileArrayCounter
  Add>FileArrayCounter,1
  Let>value=TheText_%FileArrayCounter%
  MDL>value
Until>FileArrayCounter=81
Last edited by JRL on Wed Nov 07, 2007 9:24 pm, edited 1 time in total.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Nov 07, 2007 8:41 pm

You could add this at the top of the VBScript DoOCR function:

on error resume next
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Nov 07, 2007 9:26 pm

Adding "on error resume next" worked for me. Thanks Marcus.

Perhaps that could be added to the Scripts and Tips OCR sample?

SuitedAces
Pro Scripter
Posts: 50
Joined: Thu Feb 21, 2008 9:11 pm

Post by SuitedAces » Thu Feb 21, 2008 9:18 pm

I thought ocr only works with tif files , am I wrong ?

SuitedAces
Pro Scripter
Posts: 50
Joined: Thu Feb 21, 2008 9:11 pm

Re: Advancing OCR

Post by SuitedAces » Thu Feb 21, 2008 9:46 pm

stev3n wrote:I have the OCR script as posted in the sample scripts section. Here's what I'm trying to do:

Take a screenshot of a selected area
The area is seperated in a gridlike form [soduku]
I'd like to be able to use the OCR for every square to determine which square is a number and which is just a space
And for every number I'd like to do an action [but I can do this]

So my question is how can I use the OCR to read one specified area at a time? Can I somehow set values for each square that is contained in the screenshot, and have the OCR read each square individually?

Thanks and if this way too complicated, sorry and forget this existed me.
If each square contained just one word or one number this would be easy to do.

I don't know much about macro scheduler I'm just looking at it now.
If it can accept an array from vb you could easily pass an array of 'words'
from the vb code.

Read the entire image, there is a property of the layout call 'words'.

In the vb code use the word properties to make an array:
miLayout.Words.count....to size the array and loop through the words
miLayout.Words(0).text, miLayout.Words(1).text etc

If it is not impossible to pass an array modify the vb code to print one word per line to a text file.

But if you have blank squares this will not work for you.


In that case you could you the rectangle property to determine what area each word came from
miSelectRectDoc.Images(0).Layout.Words(2).Rects

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Feb 22, 2008 1:58 pm

SuitedAces wrote:I thought ocr only works with tif files , am I wrong ?
OCR works with whatever the OCR program/engine works with. I should imagine OCR would have a hard time with a lossy format such as JPEG which is why TIFF is popular. But BMP is also lossless, Microsoft's OCR engine can read it, and it's also what you get from a screen capture.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts