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
(Lianja) 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
// 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. loImageData = createobject("CkBinData") // This image file contains some text... llSuccess = loImageData.LoadFile("qa_data/jpg/text.jpg") if (llSuccess <> .T.) then ? "Failed to load image file." release loImageData return endif // Create the above JSON. loJson = createobject("CkJsonObject") loJson.UpdateBd("requests[0].image.content","base64",loImageData) loJson.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 loHttp = createobject("CkHttp") loSb = createobject("CkStringBuilder") lcUrl = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY" loResp = loHttp.PostJson3(lcUrl,"application/json",loJson) if (loHttp.LastMethodSuccess <> .T.) then ? loHttp.LastErrorText release loImageData release loJson release loHttp release loSb return endif ? "status = " + str(loResp.StatusCode) // A 200 response status indicate success. if (loResp.StatusCode <> 200) then ? loResp.BodyStr ? "Failed." release loResp release loImageData release loJson release loHttp release loSb return endif loSbResponseBody = createobject("CkStringBuilder") loResp.GetBodySb(loSbResponseBody) loSbResponseBody.WriteFile("qa_output/textDetectResponse.json","utf-8",.F.) loJson.LoadSb(loSbResponseBody) // 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 lnCount_i = loJson.SizeOfArray("responses") do while i < lnCount_i loJson.I = i lcFullTextAnnotationText = loJson.StringOf("responses[i].fullTextAnnotation.text") j = 0 lnCount_j = loJson.SizeOfArray("responses[i].textAnnotations") do while j < lnCount_j loJson.J = j lcLocale = loJson.StringOf("responses[i].textAnnotations[j].locale") lcDescription = loJson.StringOf("responses[i].textAnnotations[j].description") k = 0 lnCount_k = loJson.SizeOfArray("responses[i].textAnnotations[j].boundingPoly.vertices") do while k < lnCount_k loJson.K = k x = loJson.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].x") y = loJson.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].y") k = k + 1 enddo j = j + 1 enddo j = 0 lnCount_j = loJson.SizeOfArray("responses[i].fullTextAnnotation.pages") do while j < lnCount_j loJson.J = j lnWidth = loJson.IntOf("responses[i].fullTextAnnotation.pages[j].width") lnHeight = loJson.IntOf("responses[i].fullTextAnnotation.pages[j].height") k = 0 lnCount_k = loJson.SizeOfArray("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages") do while k < lnCount_k loJson.K = k lcLanguageCode = loJson.StringOf("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode") k = k + 1 enddo k = 0 lnCount_k = loJson.SizeOfArray("responses[i].fullTextAnnotation.pages[j].blocks") do while k < lnCount_k loJson.K = k lcBlockType = loJson.StringOf("responses[i].fullTextAnnotation.pages[j].blocks[k].blockType") loJson1 = loJson.ObjectOf("responses[i].fullTextAnnotation.pages[j].blocks[k]") lnI1 = 0 lnCount_i1 = loJson1.SizeOfArray("property.detectedLanguages") do while lnI1 < lnCount_i1 loJson1.I = lnI1 lcLanguageCode = loJson1.StringOf("property.detectedLanguages[i].languageCode") lnI1 = lnI1 + 1 enddo release loJson1 loJson1 = loJson.ObjectOf("responses[i].fullTextAnnotation.pages[j].blocks[k]") lnI1 = 0 lnCount_i1 = loJson1.SizeOfArray("boundingBox.vertices") do while lnI1 < lnCount_i1 loJson1.I = lnI1 x = loJson1.IntOf("boundingBox.vertices[i].x") y = loJson1.IntOf("boundingBox.vertices[i].y") lnI1 = lnI1 + 1 enddo release loJson1 loJson1 = loJson.ObjectOf("responses[i].fullTextAnnotation.pages[j].blocks[k]") lnI1 = 0 lnCount_i1 = loJson1.SizeOfArray("paragraphs") do while lnI1 < lnCount_i1 loJson1.I = lnI1 lnJ1 = 0 lnCount_j1 = loJson1.SizeOfArray("paragraphs[i].property.detectedLanguages") do while lnJ1 < lnCount_j1 loJson1.J = lnJ1 lcLanguageCode = loJson1.StringOf("paragraphs[i].property.detectedLanguages[j].languageCode") lnJ1 = lnJ1 + 1 enddo lnJ1 = 0 lnCount_j1 = loJson1.SizeOfArray("paragraphs[i].boundingBox.vertices") do while lnJ1 < lnCount_j1 loJson1.J = lnJ1 x = loJson1.IntOf("paragraphs[i].boundingBox.vertices[j].x") y = loJson1.IntOf("paragraphs[i].boundingBox.vertices[j].y") lnJ1 = lnJ1 + 1 enddo lnJ1 = 0 lnCount_j1 = loJson1.SizeOfArray("paragraphs[i].words") do while lnJ1 < lnCount_j1 loJson1.J = lnJ1 lnK1 = 0 lnCount_k1 = loJson1.SizeOfArray("paragraphs[i].words[j].property.detectedLanguages") do while lnK1 < lnCount_k1 loJson1.K = lnK1 lcLanguageCode = loJson1.StringOf("paragraphs[i].words[j].property.detectedLanguages[k].languageCode") lnK1 = lnK1 + 1 enddo lnK1 = 0 lnCount_k1 = loJson1.SizeOfArray("paragraphs[i].words[j].boundingBox.vertices") do while lnK1 < lnCount_k1 loJson1.K = lnK1 x = loJson1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].x") y = loJson1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].y") lnK1 = lnK1 + 1 enddo lnK1 = 0 lnCount_k1 = loJson1.SizeOfArray("paragraphs[i].words[j].symbols") do while lnK1 < lnCount_k1 loJson1.K = lnK1 lcText = loJson1.StringOf("paragraphs[i].words[j].symbols[k].text") lcPropertyDetectedBreakType = loJson1.StringOf("paragraphs[i].words[j].symbols[k].property.detectedBreak.type") loJson2 = loJson1.ObjectOf("paragraphs[i].words[j].symbols[k]") lnI2 = 0 lnCount_i2 = loJson2.SizeOfArray("property.detectedLanguages") do while lnI2 < lnCount_i2 loJson2.I = lnI2 lcLanguageCode = loJson2.StringOf("property.detectedLanguages[i].languageCode") lnI2 = lnI2 + 1 enddo release loJson2 loJson2 = loJson1.ObjectOf("paragraphs[i].words[j].symbols[k]") lnI2 = 0 lnCount_i2 = loJson2.SizeOfArray("boundingBox.vertices") do while lnI2 < lnCount_i2 loJson2.I = lnI2 x = loJson2.IntOf("boundingBox.vertices[i].x") y = loJson2.IntOf("boundingBox.vertices[i].y") lnI2 = lnI2 + 1 enddo release loJson2 lnK1 = lnK1 + 1 enddo lnJ1 = lnJ1 + 1 enddo lnI1 = lnI1 + 1 enddo release loJson1 k = k + 1 enddo j = j + 1 enddo i = i + 1 enddo release loResp ? "Success." release loImageData release loJson release loHttp release loSb release loSbResponseBody |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.