Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- 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.
local imageData = chilkat.BinData.new()
-- This image file contains some text...
local success = imageData:load_file("qa_data/jpg/text.jpg")
if not success then
print("Failed to load image file.")
return
end
-- Create the above JSON.
local json = chilkat.JsonObject.new()
json:update_bd("requests[0].image.content", "base64", imageData)
json:update_string("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
local http = chilkat.Http.new()
http.session_log_filename = "c:/aaworkarea/sessionLog.txt"
local sb = chilkat.StringBuilder.new()
local url = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"
local resp = chilkat.HttpResponse.new()
success = http:http_json("POST", url, json, "application/json", resp)
if not success then
print(http.last_error_text)
return
end
print("status = " .. resp.status_code)
-- A 200 response status indicate success.
if resp.status_code ~= 200 then
print(resp.body_str)
print("Failed.")
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
sbResponseBody:write_file("qa_output/textDetectResponse.json", "utf-8", false)
json:load_sb(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:
local fullTextAnnotationText = ""
local j = 0
local count_j = 0
local locale = ""
local description = ""
local k = 0
local count_k = 0
local x = 0
local y = 0
local width = 0
local height = 0
local languageCode = ""
local blockType = ""
local i1 = 0
local count_i1 = 0
local j1 = 0
local count_j1 = 0
local k1 = 0
local count_k1 = 0
local text = ""
local propertyDetectedBreakType = ""
local i2 = 0
local count_i2 = 0
local json1 = chilkat.JsonObject.new()
local json2 = chilkat.JsonObject.new()
local i = 0
local count_i = json:size_of_array("responses")
while i < count_i do
json.i = i
fullTextAnnotationText = json:string_of("responses[i].fullTextAnnotation.text")
j = 0
count_j = json:size_of_array("responses[i].textAnnotations")
while j < count_j do
json.j = j
locale = json:string_of("responses[i].textAnnotations[j].locale")
description = json:string_of("responses[i].textAnnotations[j].description")
k = 0
count_k = json:size_of_array("responses[i].textAnnotations[j].boundingPoly.vertices")
while k < count_k do
json.k = k
x = json:int_of("responses[i].textAnnotations[j].boundingPoly.vertices[k].x")
y = json:int_of("responses[i].textAnnotations[j].boundingPoly.vertices[k].y")
k = k + 1
end
j = j + 1
end
j = 0
count_j = json:size_of_array("responses[i].fullTextAnnotation.pages")
while j < count_j do
json.j = j
width = json:int_of("responses[i].fullTextAnnotation.pages[j].width")
height = json:int_of("responses[i].fullTextAnnotation.pages[j].height")
k = 0
count_k = json:size_of_array("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages")
while k < count_k do
json.k = k
languageCode = json:string_of("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode")
k = k + 1
end
k = 0
count_k = json:size_of_array("responses[i].fullTextAnnotation.pages[j].blocks")
while k < count_k do
json.k = k
blockType = json:string_of("responses[i].fullTextAnnotation.pages[j].blocks[k].blockType")
json:object_of2("responses[i].fullTextAnnotation.pages[j].blocks[k]", json1)
i1 = 0
count_i1 = json1:size_of_array("property.detectedLanguages")
while i1 < count_i1 do
json1.i = i1
languageCode = json1:string_of("property.detectedLanguages[i].languageCode")
i1 = i1 + 1
end
json:object_of2("responses[i].fullTextAnnotation.pages[j].blocks[k]", json1)
i1 = 0
count_i1 = json1:size_of_array("boundingBox.vertices")
while i1 < count_i1 do
json1.i = i1
x = json1:int_of("boundingBox.vertices[i].x")
y = json1:int_of("boundingBox.vertices[i].y")
i1 = i1 + 1
end
json:object_of2("responses[i].fullTextAnnotation.pages[j].blocks[k]", json1)
i1 = 0
count_i1 = json1:size_of_array("paragraphs")
while i1 < count_i1 do
json1.i = i1
j1 = 0
count_j1 = json1:size_of_array("paragraphs[i].property.detectedLanguages")
while j1 < count_j1 do
json1.j = j1
languageCode = json1:string_of("paragraphs[i].property.detectedLanguages[j].languageCode")
j1 = j1 + 1
end
j1 = 0
count_j1 = json1:size_of_array("paragraphs[i].boundingBox.vertices")
while j1 < count_j1 do
json1.j = j1
x = json1:int_of("paragraphs[i].boundingBox.vertices[j].x")
y = json1:int_of("paragraphs[i].boundingBox.vertices[j].y")
j1 = j1 + 1
end
j1 = 0
count_j1 = json1:size_of_array("paragraphs[i].words")
while j1 < count_j1 do
json1.j = j1
k1 = 0
count_k1 = json1:size_of_array("paragraphs[i].words[j].property.detectedLanguages")
while k1 < count_k1 do
json1.k = k1
languageCode = json1:string_of("paragraphs[i].words[j].property.detectedLanguages[k].languageCode")
k1 = k1 + 1
end
k1 = 0
count_k1 = json1:size_of_array("paragraphs[i].words[j].boundingBox.vertices")
while k1 < count_k1 do
json1.k = k1
x = json1:int_of("paragraphs[i].words[j].boundingBox.vertices[k].x")
y = json1:int_of("paragraphs[i].words[j].boundingBox.vertices[k].y")
k1 = k1 + 1
end
k1 = 0
count_k1 = json1:size_of_array("paragraphs[i].words[j].symbols")
while k1 < count_k1 do
json1.k = k1
text = json1:string_of("paragraphs[i].words[j].symbols[k].text")
propertyDetectedBreakType = json1:string_of("paragraphs[i].words[j].symbols[k].property.detectedBreak.type")
json1:object_of2("paragraphs[i].words[j].symbols[k]", json2)
i2 = 0
count_i2 = json2:size_of_array("property.detectedLanguages")
while i2 < count_i2 do
json2.i = i2
languageCode = json2:string_of("property.detectedLanguages[i].languageCode")
i2 = i2 + 1
end
json1:object_of2("paragraphs[i].words[j].symbols[k]", json2)
i2 = 0
count_i2 = json2:size_of_array("boundingBox.vertices")
while i2 < count_i2 do
json2.i = i2
x = json2:int_of("boundingBox.vertices[i].x")
y = json2:int_of("boundingBox.vertices[i].y")
i2 = i2 + 1
end
k1 = k1 + 1
end
j1 = j1 + 1
end
i1 = i1 + 1
end
k = k + 1
end
j = j + 1
end
i = i + 1
end
print("Success.")