Go
Go
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 Go Downloads
success := 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.
imageData := chilkat.NewBinData()
// This image file contains some text...
success = imageData.LoadFile("qa_data/jpg/text.jpg")
if success != true {
fmt.Println("Failed to load image file.")
imageData.DisposeBinData()
return
}
// Create the above JSON.
json := chilkat.NewJsonObject()
json.UpdateBd("requests[0].image.content","base64",imageData)
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
http := chilkat.NewHttp()
http.SetSessionLogFilename("c:/aaworkarea/sessionLog.txt")
sb := chilkat.NewStringBuilder()
url := "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"
resp := chilkat.NewHttpResponse()
success = http.HttpJson("POST",url,json,"application/json",resp)
if success == false {
fmt.Println(http.LastErrorText())
imageData.DisposeBinData()
json.DisposeJsonObject()
http.DisposeHttp()
sb.DisposeStringBuilder()
resp.DisposeHttpResponse()
return
}
fmt.Println("status = ", resp.StatusCode())
// A 200 response status indicate success.
if resp.StatusCode() != 200 {
fmt.Println(resp.BodyStr())
fmt.Println("Failed.")
imageData.DisposeBinData()
json.DisposeJsonObject()
http.DisposeHttp()
sb.DisposeStringBuilder()
resp.DisposeHttpResponse()
return
}
sbResponseBody := chilkat.NewStringBuilder()
resp.GetBodySb(sbResponseBody)
sbResponseBody.WriteFile("qa_output/textDetectResponse.json","utf-8",false)
json.LoadSb(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:
var i int
var count_i int
var fullTextAnnotationText *string = new(string)
var j int
var count_j int
var locale *string = new(string)
var description *string = new(string)
var k int
var count_k int
var x int
var y int
var width int
var height int
var languageCode *string = new(string)
var blockType *string = new(string)
var i1 int
var count_i1 int
var j1 int
var count_j1 int
var k1 int
var count_k1 int
var text *string = new(string)
var propertyDetectedBreakType *string = new(string)
var i2 int
var count_i2 int
json1 := chilkat.NewJsonObject()
json2 := chilkat.NewJsonObject()
i = 0
count_i = json.SizeOfArray("responses")
for i < count_i {
json.SetI(i)
fullTextAnnotationText = json.StringOf("responses[i].fullTextAnnotation.text")
j = 0
count_j = json.SizeOfArray("responses[i].textAnnotations")
for j < count_j {
json.SetJ(j)
locale = json.StringOf("responses[i].textAnnotations[j].locale")
description = json.StringOf("responses[i].textAnnotations[j].description")
k = 0
count_k = json.SizeOfArray("responses[i].textAnnotations[j].boundingPoly.vertices")
for k < count_k {
json.SetK(k)
x = json.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].x")
y = json.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].y")
k = k + 1
}
j = j + 1
}
j = 0
count_j = json.SizeOfArray("responses[i].fullTextAnnotation.pages")
for j < count_j {
json.SetJ(j)
width = json.IntOf("responses[i].fullTextAnnotation.pages[j].width")
height = json.IntOf("responses[i].fullTextAnnotation.pages[j].height")
k = 0
count_k = json.SizeOfArray("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages")
for k < count_k {
json.SetK(k)
languageCode = json.StringOf("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode")
k = k + 1
}
k = 0
count_k = json.SizeOfArray("responses[i].fullTextAnnotation.pages[j].blocks")
for k < count_k {
json.SetK(k)
blockType = json.StringOf("responses[i].fullTextAnnotation.pages[j].blocks[k].blockType")
json.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",json1)
i1 = 0
count_i1 = json1.SizeOfArray("property.detectedLanguages")
for i1 < count_i1 {
json1.SetI(i1)
languageCode = json1.StringOf("property.detectedLanguages[i].languageCode")
i1 = i1 + 1
}
json.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",json1)
i1 = 0
count_i1 = json1.SizeOfArray("boundingBox.vertices")
for i1 < count_i1 {
json1.SetI(i1)
x = json1.IntOf("boundingBox.vertices[i].x")
y = json1.IntOf("boundingBox.vertices[i].y")
i1 = i1 + 1
}
json.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",json1)
i1 = 0
count_i1 = json1.SizeOfArray("paragraphs")
for i1 < count_i1 {
json1.SetI(i1)
j1 = 0
count_j1 = json1.SizeOfArray("paragraphs[i].property.detectedLanguages")
for j1 < count_j1 {
json1.SetJ(j1)
languageCode = json1.StringOf("paragraphs[i].property.detectedLanguages[j].languageCode")
j1 = j1 + 1
}
j1 = 0
count_j1 = json1.SizeOfArray("paragraphs[i].boundingBox.vertices")
for j1 < count_j1 {
json1.SetJ(j1)
x = json1.IntOf("paragraphs[i].boundingBox.vertices[j].x")
y = json1.IntOf("paragraphs[i].boundingBox.vertices[j].y")
j1 = j1 + 1
}
j1 = 0
count_j1 = json1.SizeOfArray("paragraphs[i].words")
for j1 < count_j1 {
json1.SetJ(j1)
k1 = 0
count_k1 = json1.SizeOfArray("paragraphs[i].words[j].property.detectedLanguages")
for k1 < count_k1 {
json1.SetK(k1)
languageCode = json1.StringOf("paragraphs[i].words[j].property.detectedLanguages[k].languageCode")
k1 = k1 + 1
}
k1 = 0
count_k1 = json1.SizeOfArray("paragraphs[i].words[j].boundingBox.vertices")
for k1 < count_k1 {
json1.SetK(k1)
x = json1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].x")
y = json1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].y")
k1 = k1 + 1
}
k1 = 0
count_k1 = json1.SizeOfArray("paragraphs[i].words[j].symbols")
for k1 < count_k1 {
json1.SetK(k1)
text = json1.StringOf("paragraphs[i].words[j].symbols[k].text")
propertyDetectedBreakType = json1.StringOf("paragraphs[i].words[j].symbols[k].property.detectedBreak.type")
json1.ObjectOf2("paragraphs[i].words[j].symbols[k]",json2)
i2 = 0
count_i2 = json2.SizeOfArray("property.detectedLanguages")
for i2 < count_i2 {
json2.SetI(i2)
languageCode = json2.StringOf("property.detectedLanguages[i].languageCode")
i2 = i2 + 1
}
json1.ObjectOf2("paragraphs[i].words[j].symbols[k]",json2)
i2 = 0
count_i2 = json2.SizeOfArray("boundingBox.vertices")
for i2 < count_i2 {
json2.SetI(i2)
x = json2.IntOf("boundingBox.vertices[i].x")
y = json2.IntOf("boundingBox.vertices[i].y")
i2 = i2 + 1
}
k1 = k1 + 1
}
j1 = j1 + 1
}
i1 = i1 + 1
}
k = k + 1
}
j = j + 1
}
i = i + 1
}
fmt.Println("Success.")
imageData.DisposeBinData()
json.DisposeJsonObject()
http.DisposeHttp()
sb.DisposeStringBuilder()
resp.DisposeHttpResponse()
sbResponseBody.DisposeStringBuilder()
json1.DisposeJsonObject()
json2.DisposeJsonObject()