PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_ImageData
oleobject loo_Json
oleobject loo_Http
oleobject loo_Sb
string ls_Url
oleobject loo_Resp
oleobject loo_SbResponseBody
integer i
integer li_Count_i
string ls_FullTextAnnotationText
integer j
integer li_Count_j
string ls_Locale
string ls_Description
integer k
integer li_Count_k
integer x
integer y
integer li_Width
integer li_Height
string ls_LanguageCode
string ls_BlockType
integer li_I1
integer li_Count_i1
integer li_J1
integer li_Count_j1
integer li_K1
integer li_Count_k1
string ls_Text
string ls_PropertyDetectedBreakType
integer li_I2
integer li_Count_i2
oleobject loo_Json1
oleobject loo_Json2
li_Success = 0
// 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.
loo_ImageData = create oleobject
li_rc = loo_ImageData.ConnectToNewObject("Chilkat.BinData")
if li_rc < 0 then
destroy loo_ImageData
MessageBox("Error","Connecting to COM object failed")
return
end if
// This image file contains some text...
li_Success = loo_ImageData.LoadFile("qa_data/jpg/text.jpg")
if li_Success <> 1 then
Write-Debug "Failed to load image file."
destroy loo_ImageData
return
end if
// Create the above JSON.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.UpdateBd("requests[0].image.content","base64",loo_ImageData)
loo_Json.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
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.SessionLogFilename = "c:/aaworkarea/sessionLog.txt"
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
ls_Url = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpJson("POST",ls_Url,loo_Json,"application/json",loo_Resp)
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_ImageData
destroy loo_Json
destroy loo_Http
destroy loo_Sb
destroy loo_Resp
return
end if
Write-Debug "status = " + string(loo_Resp.StatusCode)
// A 200 response status indicate success.
if loo_Resp.StatusCode <> 200 then
Write-Debug loo_Resp.BodyStr
Write-Debug "Failed."
destroy loo_ImageData
destroy loo_Json
destroy loo_Http
destroy loo_Sb
destroy loo_Resp
return
end if
loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")
loo_Resp.GetBodySb(loo_SbResponseBody)
loo_SbResponseBody.WriteFile("qa_output/textDetectResponse.json","utf-8",0)
loo_Json.LoadSb(loo_SbResponseBody)
// 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:
loo_Json1 = create oleobject
li_rc = loo_Json1.ConnectToNewObject("Chilkat.JsonObject")
loo_Json2 = create oleobject
li_rc = loo_Json2.ConnectToNewObject("Chilkat.JsonObject")
i = 0
li_Count_i = loo_Json.SizeOfArray("responses")
do while i < li_Count_i
loo_Json.I = i
ls_FullTextAnnotationText = loo_Json.StringOf("responses[i].fullTextAnnotation.text")
j = 0
li_Count_j = loo_Json.SizeOfArray("responses[i].textAnnotations")
do while j < li_Count_j
loo_Json.J = j
ls_Locale = loo_Json.StringOf("responses[i].textAnnotations[j].locale")
ls_Description = loo_Json.StringOf("responses[i].textAnnotations[j].description")
k = 0
li_Count_k = loo_Json.SizeOfArray("responses[i].textAnnotations[j].boundingPoly.vertices")
do while k < li_Count_k
loo_Json.K = k
x = loo_Json.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].x")
y = loo_Json.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].y")
k = k + 1
loop
j = j + 1
loop
j = 0
li_Count_j = loo_Json.SizeOfArray("responses[i].fullTextAnnotation.pages")
do while j < li_Count_j
loo_Json.J = j
li_Width = loo_Json.IntOf("responses[i].fullTextAnnotation.pages[j].width")
li_Height = loo_Json.IntOf("responses[i].fullTextAnnotation.pages[j].height")
k = 0
li_Count_k = loo_Json.SizeOfArray("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages")
do while k < li_Count_k
loo_Json.K = k
ls_LanguageCode = loo_Json.StringOf("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode")
k = k + 1
loop
k = 0
li_Count_k = loo_Json.SizeOfArray("responses[i].fullTextAnnotation.pages[j].blocks")
do while k < li_Count_k
loo_Json.K = k
ls_BlockType = loo_Json.StringOf("responses[i].fullTextAnnotation.pages[j].blocks[k].blockType")
loo_Json.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",loo_Json1)
li_I1 = 0
li_Count_i1 = loo_Json1.SizeOfArray("property.detectedLanguages")
do while li_I1 < li_Count_i1
loo_Json1.I = li_I1
ls_LanguageCode = loo_Json1.StringOf("property.detectedLanguages[i].languageCode")
li_I1 = li_I1 + 1
loop
loo_Json.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",loo_Json1)
li_I1 = 0
li_Count_i1 = loo_Json1.SizeOfArray("boundingBox.vertices")
do while li_I1 < li_Count_i1
loo_Json1.I = li_I1
x = loo_Json1.IntOf("boundingBox.vertices[i].x")
y = loo_Json1.IntOf("boundingBox.vertices[i].y")
li_I1 = li_I1 + 1
loop
loo_Json.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",loo_Json1)
li_I1 = 0
li_Count_i1 = loo_Json1.SizeOfArray("paragraphs")
do while li_I1 < li_Count_i1
loo_Json1.I = li_I1
li_J1 = 0
li_Count_j1 = loo_Json1.SizeOfArray("paragraphs[i].property.detectedLanguages")
do while li_J1 < li_Count_j1
loo_Json1.J = li_J1
ls_LanguageCode = loo_Json1.StringOf("paragraphs[i].property.detectedLanguages[j].languageCode")
li_J1 = li_J1 + 1
loop
li_J1 = 0
li_Count_j1 = loo_Json1.SizeOfArray("paragraphs[i].boundingBox.vertices")
do while li_J1 < li_Count_j1
loo_Json1.J = li_J1
x = loo_Json1.IntOf("paragraphs[i].boundingBox.vertices[j].x")
y = loo_Json1.IntOf("paragraphs[i].boundingBox.vertices[j].y")
li_J1 = li_J1 + 1
loop
li_J1 = 0
li_Count_j1 = loo_Json1.SizeOfArray("paragraphs[i].words")
do while li_J1 < li_Count_j1
loo_Json1.J = li_J1
li_K1 = 0
li_Count_k1 = loo_Json1.SizeOfArray("paragraphs[i].words[j].property.detectedLanguages")
do while li_K1 < li_Count_k1
loo_Json1.K = li_K1
ls_LanguageCode = loo_Json1.StringOf("paragraphs[i].words[j].property.detectedLanguages[k].languageCode")
li_K1 = li_K1 + 1
loop
li_K1 = 0
li_Count_k1 = loo_Json1.SizeOfArray("paragraphs[i].words[j].boundingBox.vertices")
do while li_K1 < li_Count_k1
loo_Json1.K = li_K1
x = loo_Json1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].x")
y = loo_Json1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].y")
li_K1 = li_K1 + 1
loop
li_K1 = 0
li_Count_k1 = loo_Json1.SizeOfArray("paragraphs[i].words[j].symbols")
do while li_K1 < li_Count_k1
loo_Json1.K = li_K1
ls_Text = loo_Json1.StringOf("paragraphs[i].words[j].symbols[k].text")
ls_PropertyDetectedBreakType = loo_Json1.StringOf("paragraphs[i].words[j].symbols[k].property.detectedBreak.type")
loo_Json1.ObjectOf2("paragraphs[i].words[j].symbols[k]",loo_Json2)
li_I2 = 0
li_Count_i2 = loo_Json2.SizeOfArray("property.detectedLanguages")
do while li_I2 < li_Count_i2
loo_Json2.I = li_I2
ls_LanguageCode = loo_Json2.StringOf("property.detectedLanguages[i].languageCode")
li_I2 = li_I2 + 1
loop
loo_Json1.ObjectOf2("paragraphs[i].words[j].symbols[k]",loo_Json2)
li_I2 = 0
li_Count_i2 = loo_Json2.SizeOfArray("boundingBox.vertices")
do while li_I2 < li_Count_i2
loo_Json2.I = li_I2
x = loo_Json2.IntOf("boundingBox.vertices[i].x")
y = loo_Json2.IntOf("boundingBox.vertices[i].y")
li_I2 = li_I2 + 1
loop
li_K1 = li_K1 + 1
loop
li_J1 = li_J1 + 1
loop
li_I1 = li_I1 + 1
loop
k = k + 1
loop
j = j + 1
loop
i = i + 1
loop
Write-Debug "Success."
destroy loo_ImageData
destroy loo_Json
destroy loo_Http
destroy loo_Sb
destroy loo_Resp
destroy loo_SbResponseBody
destroy loo_Json1
destroy loo_Json2