Zig Requires Chilkat v11.0.0+
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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.
const image_data = try chilkat.BinData.init();
defer image_data.deinit();
// This image file contains some text...
image_data.loadFile("qa_data/jpg/text.jpg") catch {
std.debug.print("Failed to load image file.\n", .{});
return;
};
// Create the above JSON.
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateBd("requests[0].image.content", "base64", image_data) catch {};
json.updateString("requests[0].features[0].type", "TEXT_DETECTION") catch {};
// 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
const http = try chilkat.Http.init();
defer http.deinit();
http.setSessionLogFilename("c:/aaworkarea/sessionLog.txt");
const sb = try chilkat.StringBuilder.init();
defer sb.deinit();
const url = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY";
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", url, json, "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
std.debug.print("status = {d}\n", .{resp.getStatusCode()});
// A 200 response status indicate success.
if (resp.getStatusCode() != 200) {
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
resp.getBodySb(sb_response_body) catch {};
sb_response_body.writeFile("qa_output/textDetectResponse.json", "utf-8", false) catch {};
json.loadSb(sb_response_body) catch {};
// 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: i32 = 0;
var full_text_annotation_text: [:0]const u8 = "";
var j: i32 = 0;
var count_j: i32 = 0;
var locale: [:0]const u8 = "";
var description: [:0]const u8 = "";
var k: i32 = 0;
var count_k: i32 = 0;
var x: i32 = 0;
var y: i32 = 0;
var width: i32 = 0;
var height: i32 = 0;
var language_code: [:0]const u8 = "";
var block_type: [:0]const u8 = "";
var i1_: i32 = 0;
var count_i1: i32 = 0;
var j1: i32 = 0;
var count_j1: i32 = 0;
var k1: i32 = 0;
var count_k1: i32 = 0;
var text: [:0]const u8 = "";
var property_detected_break_type: [:0]const u8 = "";
var i2_: i32 = 0;
var count_i2: i32 = 0;
const json1 = try chilkat.JsonObject.init();
defer json1.deinit();
const json2 = try chilkat.JsonObject.init();
defer json2.deinit();
i = 0;
const count_i = json.sizeOfArray("responses");
while (i < count_i) {
json.setI(i);
full_text_annotation_text = try json.stringOf(alloc, "responses[i].fullTextAnnotation.text");
j = 0;
count_j = json.sizeOfArray("responses[i].textAnnotations");
while (j < count_j) {
json.setJ(j);
locale = try json.stringOf(alloc, "responses[i].textAnnotations[j].locale");
description = try json.stringOf(alloc, "responses[i].textAnnotations[j].description");
k = 0;
count_k = json.sizeOfArray("responses[i].textAnnotations[j].boundingPoly.vertices");
while (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");
while (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");
while (k < count_k) {
json.setK(k);
language_code = try json.stringOf(alloc, "responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode");
k = k + 1;
}
k = 0;
count_k = json.sizeOfArray("responses[i].fullTextAnnotation.pages[j].blocks");
while (k < count_k) {
json.setK(k);
block_type = try json.stringOf(alloc, "responses[i].fullTextAnnotation.pages[j].blocks[k].blockType");
json.objectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]", json1) catch {};
i1_ = 0;
count_i1 = json1.sizeOfArray("property.detectedLanguages");
while (i1_ < count_i1) {
json1.setI(i1_);
language_code = try json1.stringOf(alloc, "property.detectedLanguages[i].languageCode");
i1_ = i1_ + 1;
}
json.objectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]", json1) catch {};
i1_ = 0;
count_i1 = json1.sizeOfArray("boundingBox.vertices");
while (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) catch {};
i1_ = 0;
count_i1 = json1.sizeOfArray("paragraphs");
while (i1_ < count_i1) {
json1.setI(i1_);
j1 = 0;
count_j1 = json1.sizeOfArray("paragraphs[i].property.detectedLanguages");
while (j1 < count_j1) {
json1.setJ(j1);
language_code = try json1.stringOf(alloc, "paragraphs[i].property.detectedLanguages[j].languageCode");
j1 = j1 + 1;
}
j1 = 0;
count_j1 = json1.sizeOfArray("paragraphs[i].boundingBox.vertices");
while (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");
while (j1 < count_j1) {
json1.setJ(j1);
k1 = 0;
count_k1 = json1.sizeOfArray("paragraphs[i].words[j].property.detectedLanguages");
while (k1 < count_k1) {
json1.setK(k1);
language_code = try json1.stringOf(alloc, "paragraphs[i].words[j].property.detectedLanguages[k].languageCode");
k1 = k1 + 1;
}
k1 = 0;
count_k1 = json1.sizeOfArray("paragraphs[i].words[j].boundingBox.vertices");
while (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");
while (k1 < count_k1) {
json1.setK(k1);
text = try json1.stringOf(alloc, "paragraphs[i].words[j].symbols[k].text");
property_detected_break_type = try json1.stringOf(alloc, "paragraphs[i].words[j].symbols[k].property.detectedBreak.type");
json1.objectOf2("paragraphs[i].words[j].symbols[k]", json2) catch {};
i2_ = 0;
count_i2 = json2.sizeOfArray("property.detectedLanguages");
while (i2_ < count_i2) {
json2.setI(i2_);
language_code = try json2.stringOf(alloc, "property.detectedLanguages[i].languageCode");
i2_ = i2_ + 1;
}
json1.objectOf2("paragraphs[i].words[j].symbols[k]", json2) catch {};
i2_ = 0;
count_i2 = json2.sizeOfArray("boundingBox.vertices");
while (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;
}
std.debug.print("Success.\n", .{});
}