Jump to content

OCR and MODI MiRect property


Go to solution Solved by Nine,

Recommended Posts

I saw a forum question that asked how to OCR non selectable text from a screen and get its coordinates.

This intrigued me, as I would like to have this ability in my scripts.

I found the following VB code and I am having difficulty "Converting" it to Autoit code. It provides a pixel count to a rectangle around a specified word relative to the window it is in.

Sub TestRects()

  Dim miSelectRectDoc As MODI.Document
  Dim miSelectRectWord As MODI.Word
  Dim miSelectRectRects As MODI.MiRects
  Dim miSelectRectRect As MODI.MiRect
  Dim strRectInfo As String
  
  ' Load an existing TIFF file.
  Set miSelectRectDoc = New MODI.Document
  miSelectRectDoc.Create "C:\document1.tif"
  
  ' Perform OCR.
  miSelectRectDoc.Images(0).OCR
  
  ' Retrieve and display bounding rectangle information.
  Set miSelectRectRects = miSelectRectDoc.Images(0).Layout.Words(2).Rects
  strRectInfo = "Word falls within " & miSelectRectRects.Count & _
   " bounding rectangle(s)." & vbCrLf
    For Each miSelectRectRect In miSelectRectRects
    strRectInfo = strRectInfo & _
      " Rectangle coordinates: " & vbCrLf & _
      "  - Left: " & miSelectRectRect.Left & vbCrLf & _
      "  - Right: " & miSelectRectRect.Right & vbCrLf & _
      "  - Top: " & miSelectRectRect.Top & vbCrLf & _
      "  - Bottom: " & miSelectRectRect.Bottom
    Next
  MsgBox strRectInfo, vbInformation + vbOKOnly, _
    "Rectangle Information"
    
  Set miSelectRectRect = Nothing
  Set miSelectRectRects = Nothing
  Set miSelectRectWord = Nothing
  Set miSelectRectDoc = Nothing

End Sub

 

I already have an OCR function that closely resembles this code. I would like to insert just the MiRect function into it.

I get an error when attempting to create the MiRect and MiRects objects:

Func OCR_Region($x,$y,$width,$height)  ;note $height is NOT the height, it is the Y coordinate bottom right corner, width is bottom right corner X.
    
    Dim $miDoc, $Doc
    Dim $str
    Dim $oWord
    Dim $sArray[200]
    Dim $oMyError
    Dim $HexNumber
    Dim $msg
    Dim $i
        ;Const $miLANG_CZECH = 5
        ;Const $miLANG_DANISH = 6
        ;Const $miLANG_DUTCH = 19
        Const $miLANG_ENGLISH = 9
        ;Const $miLANG_FINNISH = 11
        ;Const $miLANG_FRENCH = 12
        ;Const $miLANG_GERMAN = 7
        ;Const $miLANG_GREEK = 8
        ;Const $miLANG_HUNGARIAN = 14
        ;Const $miLANG_ITALIAN = 16
        ;Const $miLANG_JAPANESE = 17
        ;Const $miLANG_KOREAN = 18
        ;Const $miLANG_NORWEGIAN = 20
        ;Const $miLANG_POLISH = 21
        ;Const $miLANG_PORTUGUESE = 22
        ;Const $miLANG_RUSSIAN = 25
        ;Const $miLANG_SPANISH = 10
        ;Const $miLANG_SWEDISH = 29
        ;Const $miLANG_TURKISH = 31
        ;Const $miLANG_SYSDEFAULT = 2048
        ;Const $miLANG_CHINESE_SIMPLIFIED = 2052
        ;Const $miLANG_CHINESE_TRADITIONAL = 1028

        Local $hBitmap1,$hImage1,$temp
        $temp=@ScriptDir & "\temp.bmp"
        _GDIPlus_Startup ()
        ; Capture screen region
        $hBitmap1 = _ScreenCapture_Capture ("",$x, $y, $width, $height, False)
        $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap1)
        _GDIPlus_ImageSaveToFile ($hImage1, $temp)
        _GDIPlus_ImageDispose ($hImage1)
        _WinAPI_DeleteObject ($hBitmap1)
        $miDoc = ObjCreate("MODI.Document")
        
        ;===============================================================================
        ;These two statements added for the MiRect function, they both produce an error
        $miSelectRectRects = ObjCreate("MODI.MiRects")
        $miSelectRectRect = ObjCreate("MODI.MiRect")
        ;==============================================================================


        $miDoc.Create($temp)
        ;MsgBox(0,"",$temp)         ;this is the bitmap image which is "temp.bmp"
        $miDoc.Ocr($miLANG_ENGLISH, True, False)

        ;===============================================================================
        ;These two statements added for the MiRect function, neither produce an error
         $miSelectRectRects = $miDoc.Images(0).Layout.Words(1).Rects
         $Left=$miSelectRectRects.count     ;I don need ".Count", but it does something
        ;==============================================================================

        $i = 0
            For $oWord in $miDoc.Images(0).Layout.Words
                $str = $str & $oWord.text & @CrLf
                ;ConsoleWrite($oWord.text & @CRLF)
                $sArray [$i] = $oWord.text
                $i += 1
            Next

            FileDelete($temp)
            Return $sArray
        EndFunc

Thanks for your help

Edited by AutoitMike
Link to comment
Share on other sites

  • Solution

You may mean something like this :

Local $miRects = $miDoc.Images(0).Layout.Words(1).Rects
Local $strRectInfo = "Word falls within " & $miRects.Count & _
    " bounding rectangle(s)." & @CRLF
For $miRect In $miRects
  $strRectInfo &= _
      " Rectangle coordinates: " & @CRLF & _
      "  - Left: " & $miRect.Left & @CRLF & _
      "  - Right: " & $miRect.Right & @CRLF & _
      "  - Top: " & $miRect.Top & @CRLF & _
      "  - Bottom: " & $miRect.Bottom
Next
MsgBox($MB_SYSTEMMODAL, "", $strRectInfo)

 

Link to comment
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
 Share

×
×
  • Create New...