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
(PowerBuilder) 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
integer li_rc oleobject loo_ImageData integer li_Success oleobject loo_Json oleobject loo_Http oleobject loo_Sb string ls_Url oleobject loo_Resp oleobject loo_SbResponseBody integer i integer li_Count_i string ls_FullTextAnnotationText integer j integer li_Count_j string ls_Locale string ls_Description integer k integer li_Count_k integer x integer y integer li_Width integer li_Height string ls_LanguageCode string ls_BlockType integer li_I1 integer li_Count_i1 oleobject loo_Json1 integer li_J1 integer li_Count_j1 integer li_K1 integer li_Count_k1 string ls_Text string ls_PropertyDetectedBreakType integer li_I2 integer li_Count_i2 oleobject loo_Json2 // 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. loo_ImageData = create oleobject // Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 li_rc = loo_ImageData.ConnectToNewObject("Chilkat.BinData") if li_rc < 0 then destroy loo_ImageData MessageBox("Error","Connecting to COM object failed") return end if // This image file contains some text... li_Success = loo_ImageData.LoadFile("qa_data/jpg/text.jpg") if li_Success <> 1 then Write-Debug "Failed to load image file." destroy loo_ImageData return end if // Create the above JSON. loo_Json = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject") loo_Json.UpdateBd("requests[0].image.content","base64",loo_ImageData) loo_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 loo_Http = create oleobject // Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 li_rc = loo_Http.ConnectToNewObject("Chilkat.Http") loo_Http.SessionLogFilename = "c:/aaworkarea/sessionLog.txt" loo_Sb = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder") ls_Url = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY" loo_Resp = loo_Http.PostJson3(ls_Url,"application/json",loo_Json) if loo_Http.LastMethodSuccess <> 1 then Write-Debug loo_Http.LastErrorText destroy loo_ImageData destroy loo_Json destroy loo_Http destroy loo_Sb return end if Write-Debug "status = " + string(loo_Resp.StatusCode) // A 200 response status indicate success. if loo_Resp.StatusCode <> 200 then Write-Debug loo_Resp.BodyStr Write-Debug "Failed." destroy loo_Resp destroy loo_ImageData destroy loo_Json destroy loo_Http destroy loo_Sb return end if loo_SbResponseBody = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder") loo_Resp.GetBodySb(loo_SbResponseBody) loo_SbResponseBody.WriteFile("qa_output/textDetectResponse.json","utf-8",0) loo_Json.LoadSb(loo_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 li_Count_i = loo_Json.SizeOfArray("responses") do while i < li_Count_i loo_Json.I = i ls_FullTextAnnotationText = loo_Json.StringOf("responses[i].fullTextAnnotation.text") j = 0 li_Count_j = loo_Json.SizeOfArray("responses[i].textAnnotations") do while j < li_Count_j loo_Json.J = j ls_Locale = loo_Json.StringOf("responses[i].textAnnotations[j].locale") ls_Description = loo_Json.StringOf("responses[i].textAnnotations[j].description") k = 0 li_Count_k = loo_Json.SizeOfArray("responses[i].textAnnotations[j].boundingPoly.vertices") do while k < li_Count_k loo_Json.K = k x = loo_Json.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].x") y = loo_Json.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].y") k = k + 1 loop j = j + 1 loop j = 0 li_Count_j = loo_Json.SizeOfArray("responses[i].fullTextAnnotation.pages") do while j < li_Count_j loo_Json.J = j li_Width = loo_Json.IntOf("responses[i].fullTextAnnotation.pages[j].width") li_Height = loo_Json.IntOf("responses[i].fullTextAnnotation.pages[j].height") k = 0 li_Count_k = loo_Json.SizeOfArray("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages") do while k < li_Count_k loo_Json.K = k ls_LanguageCode = loo_Json.StringOf("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode") k = k + 1 loop k = 0 li_Count_k = loo_Json.SizeOfArray("responses[i].fullTextAnnotation.pages[j].blocks") do while k < li_Count_k loo_Json.K = k ls_BlockType = loo_Json.StringOf("responses[i].fullTextAnnotation.pages[j].blocks[k].blockType") loo_Json1 = loo_Json.ObjectOf("responses[i].fullTextAnnotation.pages[j].blocks[k]") li_I1 = 0 li_Count_i1 = loo_Json1.SizeOfArray("property.detectedLanguages") do while li_I1 < li_Count_i1 loo_Json1.I = li_I1 ls_LanguageCode = loo_Json1.StringOf("property.detectedLanguages[i].languageCode") li_I1 = li_I1 + 1 loop destroy loo_Json1 loo_Json1 = loo_Json.ObjectOf("responses[i].fullTextAnnotation.pages[j].blocks[k]") li_I1 = 0 li_Count_i1 = loo_Json1.SizeOfArray("boundingBox.vertices") do while li_I1 < li_Count_i1 loo_Json1.I = li_I1 x = loo_Json1.IntOf("boundingBox.vertices[i].x") y = loo_Json1.IntOf("boundingBox.vertices[i].y") li_I1 = li_I1 + 1 loop destroy loo_Json1 loo_Json1 = loo_Json.ObjectOf("responses[i].fullTextAnnotation.pages[j].blocks[k]") li_I1 = 0 li_Count_i1 = loo_Json1.SizeOfArray("paragraphs") do while li_I1 < li_Count_i1 loo_Json1.I = li_I1 li_J1 = 0 li_Count_j1 = loo_Json1.SizeOfArray("paragraphs[i].property.detectedLanguages") do while li_J1 < li_Count_j1 loo_Json1.J = li_J1 ls_LanguageCode = loo_Json1.StringOf("paragraphs[i].property.detectedLanguages[j].languageCode") li_J1 = li_J1 + 1 loop li_J1 = 0 li_Count_j1 = loo_Json1.SizeOfArray("paragraphs[i].boundingBox.vertices") do while li_J1 < li_Count_j1 loo_Json1.J = li_J1 x = loo_Json1.IntOf("paragraphs[i].boundingBox.vertices[j].x") y = loo_Json1.IntOf("paragraphs[i].boundingBox.vertices[j].y") li_J1 = li_J1 + 1 loop li_J1 = 0 li_Count_j1 = loo_Json1.SizeOfArray("paragraphs[i].words") do while li_J1 < li_Count_j1 loo_Json1.J = li_J1 li_K1 = 0 li_Count_k1 = loo_Json1.SizeOfArray("paragraphs[i].words[j].property.detectedLanguages") do while li_K1 < li_Count_k1 loo_Json1.K = li_K1 ls_LanguageCode = loo_Json1.StringOf("paragraphs[i].words[j].property.detectedLanguages[k].languageCode") li_K1 = li_K1 + 1 loop li_K1 = 0 li_Count_k1 = loo_Json1.SizeOfArray("paragraphs[i].words[j].boundingBox.vertices") do while li_K1 < li_Count_k1 loo_Json1.K = li_K1 x = loo_Json1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].x") y = loo_Json1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].y") li_K1 = li_K1 + 1 loop li_K1 = 0 li_Count_k1 = loo_Json1.SizeOfArray("paragraphs[i].words[j].symbols") do while li_K1 < li_Count_k1 loo_Json1.K = li_K1 ls_Text = loo_Json1.StringOf("paragraphs[i].words[j].symbols[k].text") ls_PropertyDetectedBreakType = loo_Json1.StringOf("paragraphs[i].words[j].symbols[k].property.detectedBreak.type") loo_Json2 = loo_Json1.ObjectOf("paragraphs[i].words[j].symbols[k]") li_I2 = 0 li_Count_i2 = loo_Json2.SizeOfArray("property.detectedLanguages") do while li_I2 < li_Count_i2 loo_Json2.I = li_I2 ls_LanguageCode = loo_Json2.StringOf("property.detectedLanguages[i].languageCode") li_I2 = li_I2 + 1 loop destroy loo_Json2 loo_Json2 = loo_Json1.ObjectOf("paragraphs[i].words[j].symbols[k]") li_I2 = 0 li_Count_i2 = loo_Json2.SizeOfArray("boundingBox.vertices") do while li_I2 < li_Count_i2 loo_Json2.I = li_I2 x = loo_Json2.IntOf("boundingBox.vertices[i].x") y = loo_Json2.IntOf("boundingBox.vertices[i].y") li_I2 = li_I2 + 1 loop destroy loo_Json2 li_K1 = li_K1 + 1 loop li_J1 = li_J1 + 1 loop li_I1 = li_I1 + 1 loop destroy loo_Json1 k = k + 1 loop j = j + 1 loop i = i + 1 loop destroy loo_Resp Write-Debug "Success." destroy loo_ImageData destroy loo_Json destroy loo_Http destroy loo_Sb destroy loo_SbResponseBody |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.