AutoIt
AutoIt
Google Cloud Vision Text Detection
See more HTTP Misc Examples
Demonstrates calling the Google Cloud Vision for text detection (performs Optical Character Recognition). "Detects and extracts text within an image with support for a broad range of languages. It also features automatic language identification." See https://cloud.google.com/vision/docs/detecting-textChilkat AutoIt Downloads
Local $bSuccess = False
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
; Build the following request:
; {
; "requests": [
; {
; "image": {
; "content": "/9j/7QBEUGhvdG9zaG9...base64-encoded-image-content...fXNWzvDEeYxxxzj/Coa6Bax//Z"
; },
; "features": [
; {
; "type": "TEXT_DETECTION"
; }
; ]
; }
; ]
; }
; Use this online tool to generate the code from sample JSON:
; Generate Code to Create JSON
; Load an image file.
$oImageData = ObjCreate("Chilkat.BinData")
; This image file contains some text...
$bSuccess = $oImageData.LoadFile("qa_data/jpg/text.jpg")
If ($bSuccess <> True) Then
ConsoleWrite("Failed to load image file." & @CRLF)
Exit
EndIf
; Create the above JSON.
$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.UpdateBd("requests[0].image.content","base64",$oImageData)
$oJson.UpdateString("requests[0].features[0].type","TEXT_DETECTION")
; Send the following POST with the HTTP request body containing the above JSON.
; POST https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.SessionLogFilename = "c:/aaworkarea/sessionLog.txt"
$oSb = ObjCreate("Chilkat.StringBuilder")
Local $sUrl = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"
$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpJson("POST",$sUrl,$oJson,"application/json",$oResp)
If ($bSuccess = False) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("status = " & $oResp.StatusCode & @CRLF)
; A 200 response status indicate success.
If ($oResp.StatusCode <> 200) Then
ConsoleWrite($oResp.BodyStr & @CRLF)
ConsoleWrite("Failed." & @CRLF)
Exit
EndIf
$oSbResponseBody = ObjCreate("Chilkat.StringBuilder")
$oResp.GetBodySb($oSbResponseBody)
$oSbResponseBody.WriteFile("qa_output/textDetectResponse.json","utf-8",False)
$oJson.LoadSb($oSbResponseBody)
; The response is a JSON document like this:
; Use this online tool to generate parsing code from sample JSON:
; Generate Parsing Code from JSON
; {
; "responses": [
; {
; "textAnnotations": [
; {
; "locale": "en",
; "description": "Chilkat is a cross-language, cross-platform\nAPl providing 90+ classes for many Internet\nprotocols, formats, and algorithms.\n",
; "boundingPoly": {
; "vertices": [
; {
; "x": 17,
; "y": 14
; },
; ...
; ]
; }
; ],
; "text": "Chilkat is a cross-language, cross-platform\nAPl providing 90+ classes for many Internet\nprotocols, formats, and algorithms.\n"
; }
; }
; ]
; }
; The parsing code generated from the online tool:
Local $i
Local $iCount_i
Local $sFullTextAnnotationText
Local $iJ
Local $iCount_j
Local $sLocale
Local $sDescription
Local $iK
Local $iCount_k
Local $iX
Local $iY
Local $iWidth
Local $iHeight
Local $sLanguageCode
Local $sBlockType
Local $i1
Local $iCount_i1
Local $iJ1
Local $iCount_j1
Local $iK1
Local $iCount_k1
Local $sText
Local $sPropertyDetectedBreakType
Local $i2
Local $iCount_i2
$oJson1 = ObjCreate("Chilkat.JsonObject")
$oJson2 = ObjCreate("Chilkat.JsonObject")
$i = 0
$iCount_i = $oJson.SizeOfArray("responses")
While $i < $iCount_i
$oJson.I = $i
$sFullTextAnnotationText = $oJson.StringOf("responses[i].fullTextAnnotation.text")
$iJ = 0
$iCount_j = $oJson.SizeOfArray("responses[i].textAnnotations")
While $iJ < $iCount_j
$oJson.J = $iJ
$sLocale = $oJson.StringOf("responses[i].textAnnotations[j].locale")
$sDescription = $oJson.StringOf("responses[i].textAnnotations[j].description")
$iK = 0
$iCount_k = $oJson.SizeOfArray("responses[i].textAnnotations[j].boundingPoly.vertices")
While $iK < $iCount_k
$oJson.K = $iK
$iX = $oJson.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].x")
$iY = $oJson.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].y")
$iK = $iK + 1
Wend
$iJ = $iJ + 1
Wend
$iJ = 0
$iCount_j = $oJson.SizeOfArray("responses[i].fullTextAnnotation.pages")
While $iJ < $iCount_j
$oJson.J = $iJ
$iWidth = $oJson.IntOf("responses[i].fullTextAnnotation.pages[j].width")
$iHeight = $oJson.IntOf("responses[i].fullTextAnnotation.pages[j].height")
$iK = 0
$iCount_k = $oJson.SizeOfArray("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages")
While $iK < $iCount_k
$oJson.K = $iK
$sLanguageCode = $oJson.StringOf("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode")
$iK = $iK + 1
Wend
$iK = 0
$iCount_k = $oJson.SizeOfArray("responses[i].fullTextAnnotation.pages[j].blocks")
While $iK < $iCount_k
$oJson.K = $iK
$sBlockType = $oJson.StringOf("responses[i].fullTextAnnotation.pages[j].blocks[k].blockType")
$oJson.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",$oJson1)
$i1 = 0
$iCount_i1 = $oJson1.SizeOfArray("property.detectedLanguages")
While $i1 < $iCount_i1
$oJson1.I = $i1
$sLanguageCode = $oJson1.StringOf("property.detectedLanguages[i].languageCode")
$i1 = $i1 + 1
Wend
$oJson.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",$oJson1)
$i1 = 0
$iCount_i1 = $oJson1.SizeOfArray("boundingBox.vertices")
While $i1 < $iCount_i1
$oJson1.I = $i1
$iX = $oJson1.IntOf("boundingBox.vertices[i].x")
$iY = $oJson1.IntOf("boundingBox.vertices[i].y")
$i1 = $i1 + 1
Wend
$oJson.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",$oJson1)
$i1 = 0
$iCount_i1 = $oJson1.SizeOfArray("paragraphs")
While $i1 < $iCount_i1
$oJson1.I = $i1
$iJ1 = 0
$iCount_j1 = $oJson1.SizeOfArray("paragraphs[i].property.detectedLanguages")
While $iJ1 < $iCount_j1
$oJson1.J = $iJ1
$sLanguageCode = $oJson1.StringOf("paragraphs[i].property.detectedLanguages[j].languageCode")
$iJ1 = $iJ1 + 1
Wend
$iJ1 = 0
$iCount_j1 = $oJson1.SizeOfArray("paragraphs[i].boundingBox.vertices")
While $iJ1 < $iCount_j1
$oJson1.J = $iJ1
$iX = $oJson1.IntOf("paragraphs[i].boundingBox.vertices[j].x")
$iY = $oJson1.IntOf("paragraphs[i].boundingBox.vertices[j].y")
$iJ1 = $iJ1 + 1
Wend
$iJ1 = 0
$iCount_j1 = $oJson1.SizeOfArray("paragraphs[i].words")
While $iJ1 < $iCount_j1
$oJson1.J = $iJ1
$iK1 = 0
$iCount_k1 = $oJson1.SizeOfArray("paragraphs[i].words[j].property.detectedLanguages")
While $iK1 < $iCount_k1
$oJson1.K = $iK1
$sLanguageCode = $oJson1.StringOf("paragraphs[i].words[j].property.detectedLanguages[k].languageCode")
$iK1 = $iK1 + 1
Wend
$iK1 = 0
$iCount_k1 = $oJson1.SizeOfArray("paragraphs[i].words[j].boundingBox.vertices")
While $iK1 < $iCount_k1
$oJson1.K = $iK1
$iX = $oJson1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].x")
$iY = $oJson1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].y")
$iK1 = $iK1 + 1
Wend
$iK1 = 0
$iCount_k1 = $oJson1.SizeOfArray("paragraphs[i].words[j].symbols")
While $iK1 < $iCount_k1
$oJson1.K = $iK1
$sText = $oJson1.StringOf("paragraphs[i].words[j].symbols[k].text")
$sPropertyDetectedBreakType = $oJson1.StringOf("paragraphs[i].words[j].symbols[k].property.detectedBreak.type")
$oJson1.ObjectOf2("paragraphs[i].words[j].symbols[k]",$oJson2)
$i2 = 0
$iCount_i2 = $oJson2.SizeOfArray("property.detectedLanguages")
While $i2 < $iCount_i2
$oJson2.I = $i2
$sLanguageCode = $oJson2.StringOf("property.detectedLanguages[i].languageCode")
$i2 = $i2 + 1
Wend
$oJson1.ObjectOf2("paragraphs[i].words[j].symbols[k]",$oJson2)
$i2 = 0
$iCount_i2 = $oJson2.SizeOfArray("boundingBox.vertices")
While $i2 < $iCount_i2
$oJson2.I = $i2
$iX = $oJson2.IntOf("boundingBox.vertices[i].x")
$iY = $oJson2.IntOf("boundingBox.vertices[i].y")
$i2 = $i2 + 1
Wend
$iK1 = $iK1 + 1
Wend
$iJ1 = $iJ1 + 1
Wend
$i1 = $i1 + 1
Wend
$iK = $iK + 1
Wend
$iJ = $iJ + 1
Wend
$i = $i + 1
Wend
ConsoleWrite("Success." & @CRLF)