Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi DLL) Google Cloud Vision Text DetectionDemonstrates 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-text
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, BinData, StringBuilder, HttpResponse, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var imageData: HCkBinData; success: Boolean; json: HCkJsonObject; http: HCkHttp; sb: HCkStringBuilder; url: PWideChar; resp: HCkHttpResponse; sbResponseBody: HCkStringBuilder; i: Integer; count_i: Integer; fullTextAnnotationText: PWideChar; j: Integer; count_j: Integer; locale: PWideChar; description: PWideChar; k: Integer; count_k: Integer; x: Integer; y: Integer; width: Integer; height: Integer; languageCode: PWideChar; blockType: PWideChar; i1: Integer; count_i1: Integer; json1: HCkJsonObject; j1: Integer; count_j1: Integer; k1: Integer; count_k1: Integer; text: PWideChar; propertyDetectedBreakType: PWideChar; i2: Integer; count_i2: Integer; json2: HCkJsonObject; begin // 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 := CkBinData_Create(); // This image file contains some text... success := CkBinData_LoadFile(imageData,'qa_data/jpg/text.jpg'); if (success <> True) then begin Memo1.Lines.Add('Failed to load image file.'); Exit; end; // Create the above JSON. json := CkJsonObject_Create(); 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 http := CkHttp_Create(); sb := CkStringBuilder_Create(); url := 'https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY'; resp := CkHttp_PostJson3(http,url,'application/json',json); if (CkHttp_getLastMethodSuccess(http) <> True) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); Exit; end; Memo1.Lines.Add('status = ' + IntToStr(CkHttpResponse_getStatusCode(resp))); // A 200 response status indicate success. if (CkHttpResponse_getStatusCode(resp) <> 200) then begin Memo1.Lines.Add(CkHttpResponse__bodyStr(resp)); Memo1.Lines.Add('Failed.'); CkHttpResponse_Dispose(resp); Exit; end; sbResponseBody := CkStringBuilder_Create(); CkHttpResponse_GetBodySb(resp,sbResponseBody); CkStringBuilder_WriteFile(sbResponseBody,'qa_output/textDetectResponse.json','utf-8',False); 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: i := 0; count_i := CkJsonObject_SizeOfArray(json,'responses'); while i < count_i do begin CkJsonObject_putI(json,i); fullTextAnnotationText := CkJsonObject__stringOf(json,'responses[i].fullTextAnnotation.text'); j := 0; count_j := CkJsonObject_SizeOfArray(json,'responses[i].textAnnotations'); while j < count_j do begin CkJsonObject_putJ(json,j); locale := CkJsonObject__stringOf(json,'responses[i].textAnnotations[j].locale'); description := CkJsonObject__stringOf(json,'responses[i].textAnnotations[j].description'); k := 0; count_k := CkJsonObject_SizeOfArray(json,'responses[i].textAnnotations[j].boundingPoly.vertices'); while k < count_k do begin CkJsonObject_putK(json,k); x := CkJsonObject_IntOf(json,'responses[i].textAnnotations[j].boundingPoly.vertices[k].x'); y := CkJsonObject_IntOf(json,'responses[i].textAnnotations[j].boundingPoly.vertices[k].y'); k := k + 1; end; j := j + 1; end; j := 0; count_j := CkJsonObject_SizeOfArray(json,'responses[i].fullTextAnnotation.pages'); while j < count_j do begin CkJsonObject_putJ(json,j); width := CkJsonObject_IntOf(json,'responses[i].fullTextAnnotation.pages[j].width'); height := CkJsonObject_IntOf(json,'responses[i].fullTextAnnotation.pages[j].height'); k := 0; count_k := CkJsonObject_SizeOfArray(json,'responses[i].fullTextAnnotation.pages[j].property.detectedLanguages'); while k < count_k do begin CkJsonObject_putK(json,k); languageCode := CkJsonObject__stringOf(json,'responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode'); k := k + 1; end; k := 0; count_k := CkJsonObject_SizeOfArray(json,'responses[i].fullTextAnnotation.pages[j].blocks'); while k < count_k do begin CkJsonObject_putK(json,k); blockType := CkJsonObject__stringOf(json,'responses[i].fullTextAnnotation.pages[j].blocks[k].blockType'); json1 := CkJsonObject_ObjectOf(json,'responses[i].fullTextAnnotation.pages[j].blocks[k]'); i1 := 0; count_i1 := CkJsonObject_SizeOfArray(json1,'property.detectedLanguages'); while i1 < count_i1 do begin CkJsonObject_putI(json1,i1); languageCode := CkJsonObject__stringOf(json1,'property.detectedLanguages[i].languageCode'); i1 := i1 + 1; end; CkJsonObject_Dispose(json1); json1 := CkJsonObject_ObjectOf(json,'responses[i].fullTextAnnotation.pages[j].blocks[k]'); i1 := 0; count_i1 := CkJsonObject_SizeOfArray(json1,'boundingBox.vertices'); while i1 < count_i1 do begin CkJsonObject_putI(json1,i1); x := CkJsonObject_IntOf(json1,'boundingBox.vertices[i].x'); y := CkJsonObject_IntOf(json1,'boundingBox.vertices[i].y'); i1 := i1 + 1; end; CkJsonObject_Dispose(json1); json1 := CkJsonObject_ObjectOf(json,'responses[i].fullTextAnnotation.pages[j].blocks[k]'); i1 := 0; count_i1 := CkJsonObject_SizeOfArray(json1,'paragraphs'); while i1 < count_i1 do begin CkJsonObject_putI(json1,i1); j1 := 0; count_j1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].property.detectedLanguages'); while j1 < count_j1 do begin CkJsonObject_putJ(json1,j1); languageCode := CkJsonObject__stringOf(json1,'paragraphs[i].property.detectedLanguages[j].languageCode'); j1 := j1 + 1; end; j1 := 0; count_j1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].boundingBox.vertices'); while j1 < count_j1 do begin CkJsonObject_putJ(json1,j1); x := CkJsonObject_IntOf(json1,'paragraphs[i].boundingBox.vertices[j].x'); y := CkJsonObject_IntOf(json1,'paragraphs[i].boundingBox.vertices[j].y'); j1 := j1 + 1; end; j1 := 0; count_j1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].words'); while j1 < count_j1 do begin CkJsonObject_putJ(json1,j1); k1 := 0; count_k1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].words[j].property.detectedLanguages'); while k1 < count_k1 do begin CkJsonObject_putK(json1,k1); languageCode := CkJsonObject__stringOf(json1,'paragraphs[i].words[j].property.detectedLanguages[k].languageCode'); k1 := k1 + 1; end; k1 := 0; count_k1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].words[j].boundingBox.vertices'); while k1 < count_k1 do begin CkJsonObject_putK(json1,k1); x := CkJsonObject_IntOf(json1,'paragraphs[i].words[j].boundingBox.vertices[k].x'); y := CkJsonObject_IntOf(json1,'paragraphs[i].words[j].boundingBox.vertices[k].y'); k1 := k1 + 1; end; k1 := 0; count_k1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].words[j].symbols'); while k1 < count_k1 do begin CkJsonObject_putK(json1,k1); text := CkJsonObject__stringOf(json1,'paragraphs[i].words[j].symbols[k].text'); propertyDetectedBreakType := CkJsonObject__stringOf(json1,'paragraphs[i].words[j].symbols[k].property.detectedBreak.type'); json2 := CkJsonObject_ObjectOf(json1,'paragraphs[i].words[j].symbols[k]'); i2 := 0; count_i2 := CkJsonObject_SizeOfArray(json2,'property.detectedLanguages'); while i2 < count_i2 do begin CkJsonObject_putI(json2,i2); languageCode := CkJsonObject__stringOf(json2,'property.detectedLanguages[i].languageCode'); i2 := i2 + 1; end; CkJsonObject_Dispose(json2); json2 := CkJsonObject_ObjectOf(json1,'paragraphs[i].words[j].symbols[k]'); i2 := 0; count_i2 := CkJsonObject_SizeOfArray(json2,'boundingBox.vertices'); while i2 < count_i2 do begin CkJsonObject_putI(json2,i2); x := CkJsonObject_IntOf(json2,'boundingBox.vertices[i].x'); y := CkJsonObject_IntOf(json2,'boundingBox.vertices[i].y'); i2 := i2 + 1; end; CkJsonObject_Dispose(json2); k1 := k1 + 1; end; j1 := j1 + 1; end; i1 := i1 + 1; end; CkJsonObject_Dispose(json1); k := k + 1; end; j := j + 1; end; i := i + 1; end; CkHttpResponse_Dispose(resp); Memo1.Lines.Add('Success.'); CkBinData_Dispose(imageData); CkJsonObject_Dispose(json); CkHttp_Dispose(http); CkStringBuilder_Dispose(sb); CkStringBuilder_Dispose(sbResponseBody); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.