Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
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 Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.BinData,
Chilkat.StringBuilder,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imageData: TBinData;
json: TJsonObject;
http: THttp;
sb: TStringBuilder;
url: string;
resp: THttpResponse;
sbResponseBody: TStringBuilder;
i: Integer;
count_i: Integer;
fullTextAnnotationText: string;
j: Integer;
count_j: Integer;
locale: string;
description: string;
k: Integer;
count_k: Integer;
x: Integer;
y: Integer;
width: Integer;
height: Integer;
languageCode: string;
blockType: string;
i1: Integer;
count_i1: Integer;
j1: Integer;
count_j1: Integer;
k1: Integer;
count_k1: Integer;
text: string;
propertyDetectedBreakType: string;
i2: Integer;
count_i2: Integer;
json1: TJsonObject;
json2: TJsonObject;
begin
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 := TBinData.Create;
// This image file contains some text...
success := imageData.LoadFile('qa_data/jpg/text.jpg');
if (success <> True) then
begin
WriteLn('Failed to load image file.');
Exit;
end;
// Create the above JSON.
json := TJsonObject.Create;
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 := THttp.Create;
http.SessionLogFilename := 'c:/aaworkarea/sessionLog.txt';
sb := TStringBuilder.Create;
url := 'https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY';
resp := THttpResponse.Create;
success := http.HttpJson('POST',url,json,'application/json',resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
WriteLn('status = ' + resp.StatusCode);
// A 200 response status indicate success.
if (resp.StatusCode <> 200) then
begin
WriteLn(resp.BodyStr);
WriteLn('Failed.');
Exit;
end;
sbResponseBody := TStringBuilder.Create;
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:
json1 := TJsonObject.Create;
json2 := TJsonObject.Create;
i := 0;
count_i := json.SizeOfArray('responses');
while i < count_i do
begin
json.I := i;
fullTextAnnotationText := json.StringOf('responses[i].fullTextAnnotation.text');
j := 0;
count_j := json.SizeOfArray('responses[i].textAnnotations');
while j < count_j do
begin
json.J := 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');
while k < count_k do
begin
json.K := 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;
end;
j := j + 1;
end;
j := 0;
count_j := json.SizeOfArray('responses[i].fullTextAnnotation.pages');
while j < count_j do
begin
json.J := 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 do
begin
json.K := k;
languageCode := json.StringOf('responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode');
k := k + 1;
end;
k := 0;
count_k := json.SizeOfArray('responses[i].fullTextAnnotation.pages[j].blocks');
while k < count_k do
begin
json.K := 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');
while i1 < count_i1 do
begin
json1.I := i1;
languageCode := json1.StringOf('property.detectedLanguages[i].languageCode');
i1 := i1 + 1;
end;
json.ObjectOf2('responses[i].fullTextAnnotation.pages[j].blocks[k]',json1);
i1 := 0;
count_i1 := json1.SizeOfArray('boundingBox.vertices');
while i1 < count_i1 do
begin
json1.I := i1;
x := json1.IntOf('boundingBox.vertices[i].x');
y := json1.IntOf('boundingBox.vertices[i].y');
i1 := i1 + 1;
end;
json.ObjectOf2('responses[i].fullTextAnnotation.pages[j].blocks[k]',json1);
i1 := 0;
count_i1 := json1.SizeOfArray('paragraphs');
while i1 < count_i1 do
begin
json1.I := i1;
j1 := 0;
count_j1 := json1.SizeOfArray('paragraphs[i].property.detectedLanguages');
while j1 < count_j1 do
begin
json1.J := j1;
languageCode := json1.StringOf('paragraphs[i].property.detectedLanguages[j].languageCode');
j1 := j1 + 1;
end;
j1 := 0;
count_j1 := json1.SizeOfArray('paragraphs[i].boundingBox.vertices');
while j1 < count_j1 do
begin
json1.J := j1;
x := json1.IntOf('paragraphs[i].boundingBox.vertices[j].x');
y := json1.IntOf('paragraphs[i].boundingBox.vertices[j].y');
j1 := j1 + 1;
end;
j1 := 0;
count_j1 := json1.SizeOfArray('paragraphs[i].words');
while j1 < count_j1 do
begin
json1.J := j1;
k1 := 0;
count_k1 := json1.SizeOfArray('paragraphs[i].words[j].property.detectedLanguages');
while k1 < count_k1 do
begin
json1.K := k1;
languageCode := json1.StringOf('paragraphs[i].words[j].property.detectedLanguages[k].languageCode');
k1 := k1 + 1;
end;
k1 := 0;
count_k1 := json1.SizeOfArray('paragraphs[i].words[j].boundingBox.vertices');
while k1 < count_k1 do
begin
json1.K := 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;
end;
k1 := 0;
count_k1 := json1.SizeOfArray('paragraphs[i].words[j].symbols');
while k1 < count_k1 do
begin
json1.K := 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');
while i2 < count_i2 do
begin
json2.I := i2;
languageCode := json2.StringOf('property.detectedLanguages[i].languageCode');
i2 := i2 + 1;
end;
json1.ObjectOf2('paragraphs[i].words[j].symbols[k]',json2);
i2 := 0;
count_i2 := json2.SizeOfArray('boundingBox.vertices');
while i2 < count_i2 do
begin
json2.I := i2;
x := json2.IntOf('boundingBox.vertices[i].x');
y := json2.IntOf('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;
WriteLn('Success.');
imageData.Free;
json.Free;
http.Free;
sb.Free;
resp.Free;
sbResponseBody.Free;
json1.Free;
json2.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.