Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set 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.
set imageData [new_CkBinData]
# This image file contains some text...
set success [CkBinData_LoadFile $imageData "qa_data/jpg/text.jpg"]
if {$success != 1} then {
puts "Failed to load image file."
delete_CkBinData $imageData
exit
}
# Create the above JSON.
set json [new_CkJsonObject]
CkJsonObject_UpdateBd $json "requests[0].image.content" "base64" $imageData
CkJsonObject_UpdateString $json "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
set http [new_CkHttp]
CkHttp_put_SessionLogFilename $http "c:/aaworkarea/sessionLog.txt"
set sb [new_CkStringBuilder]
set url "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"
set resp [new_CkHttpResponse]
set success [CkHttp_HttpJson $http "POST" $url $json "application/json" $resp]
if {$success == 0} then {
puts [CkHttp_lastErrorText $http]
delete_CkBinData $imageData
delete_CkJsonObject $json
delete_CkHttp $http
delete_CkStringBuilder $sb
delete_CkHttpResponse $resp
exit
}
puts "status = [CkHttpResponse_get_StatusCode $resp]"
# A 200 response status indicate success.
if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
puts [CkHttpResponse_bodyStr $resp]
puts "Failed."
delete_CkBinData $imageData
delete_CkJsonObject $json
delete_CkHttp $http
delete_CkStringBuilder $sb
delete_CkHttpResponse $resp
exit
}
set sbResponseBody [new_CkStringBuilder]
CkHttpResponse_GetBodySb $resp $sbResponseBody
CkStringBuilder_WriteFile $sbResponseBody "qa_output/textDetectResponse.json" "utf-8" 0
CkJsonObject_LoadSb $json $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:
set json1 [new_CkJsonObject]
set json2 [new_CkJsonObject]
set i 0
set count_i [CkJsonObject_SizeOfArray $json "responses"]
while {$i < $count_i} {
CkJsonObject_put_I $json $i
set fullTextAnnotationText [CkJsonObject_stringOf $json "responses[i].fullTextAnnotation.text"]
set j 0
set count_j [CkJsonObject_SizeOfArray $json "responses[i].textAnnotations"]
while {$j < $count_j} {
CkJsonObject_put_J $json $j
set locale [CkJsonObject_stringOf $json "responses[i].textAnnotations[j].locale"]
set description [CkJsonObject_stringOf $json "responses[i].textAnnotations[j].description"]
set k 0
set count_k [CkJsonObject_SizeOfArray $json "responses[i].textAnnotations[j].boundingPoly.vertices"]
while {$k < $count_k} {
CkJsonObject_put_K $json $k
set x [CkJsonObject_IntOf $json "responses[i].textAnnotations[j].boundingPoly.vertices[k].x"]
set y [CkJsonObject_IntOf $json "responses[i].textAnnotations[j].boundingPoly.vertices[k].y"]
set k [expr $k + 1]
}
set j [expr $j + 1]
}
set j 0
set count_j [CkJsonObject_SizeOfArray $json "responses[i].fullTextAnnotation.pages"]
while {$j < $count_j} {
CkJsonObject_put_J $json $j
set width [CkJsonObject_IntOf $json "responses[i].fullTextAnnotation.pages[j].width"]
set height [CkJsonObject_IntOf $json "responses[i].fullTextAnnotation.pages[j].height"]
set k 0
set count_k [CkJsonObject_SizeOfArray $json "responses[i].fullTextAnnotation.pages[j].property.detectedLanguages"]
while {$k < $count_k} {
CkJsonObject_put_K $json $k
set languageCode [CkJsonObject_stringOf $json "responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode"]
set k [expr $k + 1]
}
set k 0
set count_k [CkJsonObject_SizeOfArray $json "responses[i].fullTextAnnotation.pages[j].blocks"]
while {$k < $count_k} {
CkJsonObject_put_K $json $k
set blockType [CkJsonObject_stringOf $json "responses[i].fullTextAnnotation.pages[j].blocks[k].blockType"]
CkJsonObject_ObjectOf2 $json "responses[i].fullTextAnnotation.pages[j].blocks[k]" $json1
set i1 0
set count_i1 [CkJsonObject_SizeOfArray $json1 "property.detectedLanguages"]
while {$i1 < $count_i1} {
CkJsonObject_put_I $json1 $i1
set languageCode [CkJsonObject_stringOf $json1 "property.detectedLanguages[i].languageCode"]
set i1 [expr $i1 + 1]
}
CkJsonObject_ObjectOf2 $json "responses[i].fullTextAnnotation.pages[j].blocks[k]" $json1
set i1 0
set count_i1 [CkJsonObject_SizeOfArray $json1 "boundingBox.vertices"]
while {$i1 < $count_i1} {
CkJsonObject_put_I $json1 $i1
set x [CkJsonObject_IntOf $json1 "boundingBox.vertices[i].x"]
set y [CkJsonObject_IntOf $json1 "boundingBox.vertices[i].y"]
set i1 [expr $i1 + 1]
}
CkJsonObject_ObjectOf2 $json "responses[i].fullTextAnnotation.pages[j].blocks[k]" $json1
set i1 0
set count_i1 [CkJsonObject_SizeOfArray $json1 "paragraphs"]
while {$i1 < $count_i1} {
CkJsonObject_put_I $json1 $i1
set j1 0
set count_j1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].property.detectedLanguages"]
while {$j1 < $count_j1} {
CkJsonObject_put_J $json1 $j1
set languageCode [CkJsonObject_stringOf $json1 "paragraphs[i].property.detectedLanguages[j].languageCode"]
set j1 [expr $j1 + 1]
}
set j1 0
set count_j1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].boundingBox.vertices"]
while {$j1 < $count_j1} {
CkJsonObject_put_J $json1 $j1
set x [CkJsonObject_IntOf $json1 "paragraphs[i].boundingBox.vertices[j].x"]
set y [CkJsonObject_IntOf $json1 "paragraphs[i].boundingBox.vertices[j].y"]
set j1 [expr $j1 + 1]
}
set j1 0
set count_j1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words"]
while {$j1 < $count_j1} {
CkJsonObject_put_J $json1 $j1
set k1 0
set count_k1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words[j].property.detectedLanguages"]
while {$k1 < $count_k1} {
CkJsonObject_put_K $json1 $k1
set languageCode [CkJsonObject_stringOf $json1 "paragraphs[i].words[j].property.detectedLanguages[k].languageCode"]
set k1 [expr $k1 + 1]
}
set k1 0
set count_k1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words[j].boundingBox.vertices"]
while {$k1 < $count_k1} {
CkJsonObject_put_K $json1 $k1
set x [CkJsonObject_IntOf $json1 "paragraphs[i].words[j].boundingBox.vertices[k].x"]
set y [CkJsonObject_IntOf $json1 "paragraphs[i].words[j].boundingBox.vertices[k].y"]
set k1 [expr $k1 + 1]
}
set k1 0
set count_k1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words[j].symbols"]
while {$k1 < $count_k1} {
CkJsonObject_put_K $json1 $k1
set text [CkJsonObject_stringOf $json1 "paragraphs[i].words[j].symbols[k].text"]
set propertyDetectedBreakType [CkJsonObject_stringOf $json1 "paragraphs[i].words[j].symbols[k].property.detectedBreak.type"]
CkJsonObject_ObjectOf2 $json1 "paragraphs[i].words[j].symbols[k]" $json2
set i2 0
set count_i2 [CkJsonObject_SizeOfArray $json2 "property.detectedLanguages"]
while {$i2 < $count_i2} {
CkJsonObject_put_I $json2 $i2
set languageCode [CkJsonObject_stringOf $json2 "property.detectedLanguages[i].languageCode"]
set i2 [expr $i2 + 1]
}
CkJsonObject_ObjectOf2 $json1 "paragraphs[i].words[j].symbols[k]" $json2
set i2 0
set count_i2 [CkJsonObject_SizeOfArray $json2 "boundingBox.vertices"]
while {$i2 < $count_i2} {
CkJsonObject_put_I $json2 $i2
set x [CkJsonObject_IntOf $json2 "boundingBox.vertices[i].x"]
set y [CkJsonObject_IntOf $json2 "boundingBox.vertices[i].y"]
set i2 [expr $i2 + 1]
}
set k1 [expr $k1 + 1]
}
set j1 [expr $j1 + 1]
}
set i1 [expr $i1 + 1]
}
set k [expr $k + 1]
}
set j [expr $j + 1]
}
set i [expr $i + 1]
}
puts "Success."
delete_CkBinData $imageData
delete_CkJsonObject $json
delete_CkHttp $http
delete_CkStringBuilder $sb
delete_CkHttpResponse $resp
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $json1
delete_CkJsonObject $json2