Jump to content

google OCR with Autoit


Recommended Posts

Hey there, is anyone here who got experience using google API OCR in Autoit? 

I´m quiet confused how to use it actually. 

How can I send a screenshot saved on my desktop to google to get to know what a text is written on there? 

thanks for answers

Link to comment
Share on other sites

@lordsocke

Im not sure if this code will works since to enable OCR interface you have to pay for it :)

Anyway this code represent:  vision.images.annotate 

; Enter your api key here.
Local $GOOGLE_API_KEY = "XXXXXX-XXXXXXXXXXX_XXXXXXXXXXX"

; Enter path to image. Image like white background and black text example.
Local $IMAGE_PATH = @ScriptDir & "\OCR.png"

; Create Object for out/in comming connections.
Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1")

; Check if some error durinig creation object.
If Not IsObj($oHttp) Then
    ConsoleWrite("+++ Error 1. Failed to create object." & @CRLF)
    Exit
EndIf

; Create POST request to google OCR
Local $sRequest = "https://vision.googleapis.com/v1/images:annotate?key=" & $GOOGLE_API_KEY

; Open request.
$oHttp.Open("POST", $sRequest , False)

; Set request header - we want received packets in json format
$oHTTP.SetRequestHeader("Content-Type", "application/json")

; Load image and read.
Local $sImage = FileOpen(FileRead($IMAGE_PATH))

; Check for errors.
If @error Then
    ConsoleWrite("+++ Error 2. Failed to load image. Make sure about valid path." & @CRLF)
    Exit
EndIf

; Convert image to base64 IMPORTANT! If not works try this way: base64($sImage, True, True)
$sImage = base64($sImage)

; Check for errors.
If @error or $sImage = "" Then
    ConsoleWrite("+++ Error 3. Failed convert image to base 64." & @CRLF)
    Exit
EndIf

; Create structure in JSON format
Local $sJson
$sJson &= "{"
$sJson &=   '"requests": ['
$sJson &=     "{"
$sJson &=       '"image": {'
$sJson &=         '"content": "' & $sImage & '"'
$sJson &=       '},'
$sJson &=       '"features": ['
$sJson &=         "{"
$sJson &=           '"type": "TEXT_DETECTION"'
$sJson &=         "}"
$sJson &=       "]"
$sJson &=     "}"
$sJson &=   "]"
$sJson &= "}"

ConsoleWrite("Sending request please wait..." & @CRLF)

; Send our request
$oHttp.Send($sJson)

;=======================
;   Sending process...
;   AutoIt freeze now.
;   Wait until finish
;=======================

; Get request back status and output text
Local $iStatus = $oHttp.Status
Local $sOutput = $oHttp.ResponseText

; Check respond status
If $iStatus <> 200 Then
    ConsoleWrite("+++ Error 4. Invaild respond status: " & $iStatus & @CRLF)
Else
    ConsoleWrite("++ Successfully send request." & @CRLF)
EndIf

ConsoleWrite("OUTPUT:" & @CRLF & $sOutput & @CRLF)


;==============================================================================================================================
; Function:         base64($vCode [, $bEncode = True [, $bUrl = False]])
;
; Description:      Decode or Encode $vData using Microsoft.XMLDOM to Base64Binary or Base64Url.
;                   IMPORTANT! Encoded base64url is without @LF after 72 lines. Some websites may require this.
;
; Parameter(s):     $vData      - string or integer | Data to encode or decode.
;                   $bEncode    - boolean           | True - encode, False - decode.
;                   $bUrl       - boolean           | True - output is will decoded or encoded using base64url shema.
;
; Return Value(s):  On Success - Returns output data
;                   On Failure - Returns 1 - Failed to create object.
;
; Author (s):       (Ghads on Wordpress.com), Ascer
;===============================================================================================================================
Func base64($vCode, $bEncode = True, $bUrl = False)

    Local $oDM = ObjCreate("Microsoft.XMLDOM")
    If Not IsObj($oDM) Then Return SetError(1, 0, 1)

    Local $oEL = $oDM.createElement("Tmp")
    $oEL.DataType = "bin.base64"

    If $bEncode then
        $oEL.NodeTypedValue = Binary($vCode)
        If Not $bUrl Then Return $oEL.Text
        Return StringReplace(StringReplace(StringReplace($oEL.Text, "+", "-"),"/", "_"), @LF, "")
    Else
        If $bUrl Then $vCode = StringReplace(StringReplace($vCode, "-", "+"), "_", "/")
        $oEL.Text = $vCode
        Return $oEL.NodeTypedValue
    EndIf

EndFunc ;==>base64

 

Link to comment
Share on other sites

1 hour ago, Ascer said:

@lordsocke

Im not sure if this code will works since to enable OCR interface you have to pay for it :)

Anyway this code represent:  vision.images.annotate 

; Enter your api key here.
Local $GOOGLE_API_KEY = "XXXXXX-XXXXXXXXXXX_XXXXXXXXXXX"

; Enter path to image. Image like white background and black text example.
Local $IMAGE_PATH = @ScriptDir & "\OCR.png"

; Create Object for out/in comming connections.
Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1")

; Check if some error durinig creation object.
If Not IsObj($oHttp) Then
    ConsoleWrite("+++ Error 1. Failed to create object." & @CRLF)
    Exit
EndIf

; Create POST request to google OCR
Local $sRequest = "https://vision.googleapis.com/v1/images:annotate?key=" & $GOOGLE_API_KEY

; Open request.
$oHttp.Open("POST", $sRequest , False)

; Set request header - we want received packets in json format
$oHTTP.SetRequestHeader("Content-Type", "application/json")

; Load image and read.
Local $sImage = FileOpen(FileRead($IMAGE_PATH))

; Check for errors.
If @error Then
    ConsoleWrite("+++ Error 2. Failed to load image. Make sure about valid path." & @CRLF)
    Exit
EndIf

; Convert image to base64 IMPORTANT! If not works try this way: base64($sImage, True, True)
$sImage = base64($sImage)

; Check for errors.
If @error or $sImage = "" Then
    ConsoleWrite("+++ Error 3. Failed convert image to base 64." & @CRLF)
    Exit
EndIf

; Create structure in JSON format
Local $sJson
$sJson &= "{"
$sJson &=   '"requests": ['
$sJson &=     "{"
$sJson &=       '"image": {'
$sJson &=         '"content": "' & $sImage & '"'
$sJson &=       '},'
$sJson &=       '"features": ['
$sJson &=         "{"
$sJson &=           '"type": "TEXT_DETECTION"'
$sJson &=         "}"
$sJson &=       "]"
$sJson &=     "}"
$sJson &=   "]"
$sJson &= "}"

ConsoleWrite("Sending request please wait..." & @CRLF)

; Send our request
$oHttp.Send($sJson)

;=======================
;   Sending process...
;   AutoIt freeze now.
;   Wait until finish
;=======================

; Get request back status and output text
Local $iStatus = $oHttp.Status
Local $sOutput = $oHttp.ResponseText

; Check respond status
If $iStatus <> 200 Then
    ConsoleWrite("+++ Error 4. Invaild respond status: " & $iStatus & @CRLF)
Else
    ConsoleWrite("++ Successfully send request." & @CRLF)
EndIf

ConsoleWrite("OUTPUT:" & @CRLF & $sOutput & @CRLF)


;==============================================================================================================================
; Function:         base64($vCode [, $bEncode = True [, $bUrl = False]])
;
; Description:      Decode or Encode $vData using Microsoft.XMLDOM to Base64Binary or Base64Url.
;                   IMPORTANT! Encoded base64url is without @LF after 72 lines. Some websites may require this.
;
; Parameter(s):     $vData      - string or integer | Data to encode or decode.
;                   $bEncode    - boolean           | True - encode, False - decode.
;                   $bUrl       - boolean           | True - output is will decoded or encoded using base64url shema.
;
; Return Value(s):  On Success - Returns output data
;                   On Failure - Returns 1 - Failed to create object.
;
; Author (s):       (Ghads on Wordpress.com), Ascer
;===============================================================================================================================
Func base64($vCode, $bEncode = True, $bUrl = False)

    Local $oDM = ObjCreate("Microsoft.XMLDOM")
    If Not IsObj($oDM) Then Return SetError(1, 0, 1)

    Local $oEL = $oDM.createElement("Tmp")
    $oEL.DataType = "bin.base64"

    If $bEncode then
        $oEL.NodeTypedValue = Binary($vCode)
        If Not $bUrl Then Return $oEL.Text
        Return StringReplace(StringReplace(StringReplace($oEL.Text, "+", "-"),"/", "_"), @LF, "")
    Else
        If $bUrl Then $vCode = StringReplace(StringReplace($vCode, "-", "+"), "_", "/")
        $oEL.Text = $vCode
        Return $oEL.NodeTypedValue
    EndIf

EndFunc ;==>base64

 

looks awesome tank you very much <3

Link to comment
Share on other sites

  • 4 weeks later...

Ok, I've managed to make it work with an url, but I'd like to use a local file.

 

      "image":{
        "source":{
          "imageUri":
            "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
        }
      },

 

Link to comment
Share on other sites

  • 2 weeks later...
On 30.03.2018 at 5:46 AM, Quezako said:

Ok, I've managed to make it work with an url, but I'd like to use a local file.

 

      "image":{
        "source":{
          "imageUri":
            "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
        }
      },

 

You have to change

Local $sImage = FileOpen(FileRead($IMAGE_PATH))

to

Local $sImage = FileRead(FileOpen($IMAGE_PATH))

 

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...