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
(Visual FoxPro) 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
LOCAL loImageData LOCAL lnSuccess LOCAL loJson LOCAL loHttp LOCAL loSb LOCAL lcUrl LOCAL loResp LOCAL loSbResponseBody LOCAL i LOCAL lnCount_i LOCAL lcFullTextAnnotationText LOCAL j LOCAL lnCount_j LOCAL lcLocale LOCAL lcDescription LOCAL k LOCAL lnCount_k LOCAL x LOCAL y LOCAL lnWidth LOCAL lnHeight LOCAL lcLanguageCode LOCAL lcBlockType LOCAL lnI1 LOCAL lnCount_i1 LOCAL loJson1 LOCAL lnJ1 LOCAL lnCount_j1 LOCAL lnK1 LOCAL lnCount_k1 LOCAL lcText LOCAL lcPropertyDetectedBreakType LOCAL lnI2 LOCAL lnCount_i2 LOCAL loJson2 * 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. * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.BinData') loImageData = CreateObject('Chilkat.BinData') * This image file contains some text... lnSuccess = loImageData.LoadFile("qa_data/jpg/text.jpg") IF (lnSuccess <> 1) THEN ? "Failed to load image file." RELEASE loImageData CANCEL ENDIF * Create the above JSON. * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.JsonObject') loJson = CreateObject('Chilkat.JsonObject') 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 * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Http') loHttp = CreateObject('Chilkat.Http') loHttp.SessionLogFilename = "c:/aaworkarea/sessionLog.txt" * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.StringBuilder') loSb = CreateObject('Chilkat.StringBuilder') lcUrl = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY" loResp = loHttp.PostJson3(lcUrl,"application/json",loJson) IF (loHttp.LastMethodSuccess <> 1) THEN ? loHttp.LastErrorText RELEASE loImageData RELEASE loJson RELEASE loHttp RELEASE loSb CANCEL 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 CANCEL ENDIF * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.StringBuilder') loSbResponseBody = CreateObject('Chilkat.StringBuilder') loResp.GetBodySb(loSbResponseBody) loSbResponseBody.WriteFile("qa_output/textDetectResponse.json","utf-8",0) 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.