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
(Tcl) 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
load ./chilkat.dll # 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. set imageData [new_CkBinData] # This image file contains some text... set success [CkBinData_LoadFile $imageData "qa_data/jpg/text.jpg"] if {$success != 1} then { puts "Failed to load image file." delete_CkBinData $imageData exit } # Create the above JSON. set json [new_CkJsonObject] 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 set http [new_CkHttp] set sb [new_CkStringBuilder] set url "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY" # resp is a CkHttpResponse set resp [CkHttp_PostJson3 $http $url "application/json" $json] if {[CkHttp_get_LastMethodSuccess $http] != 1} then { puts [CkHttp_lastErrorText $http] delete_CkBinData $imageData delete_CkJsonObject $json delete_CkHttp $http delete_CkStringBuilder $sb exit } puts "status = [CkHttpResponse_get_StatusCode $resp]" # A 200 response status indicate success. if {[CkHttpResponse_get_StatusCode $resp] != 200} then { puts [CkHttpResponse_bodyStr $resp] puts "Failed." delete_CkHttpResponse $resp delete_CkBinData $imageData delete_CkJsonObject $json delete_CkHttp $http delete_CkStringBuilder $sb exit } set sbResponseBody [new_CkStringBuilder] CkHttpResponse_GetBodySb $resp $sbResponseBody CkStringBuilder_WriteFile $sbResponseBody "qa_output/textDetectResponse.json" "utf-8" 0 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: # json1 is a CkJsonObject # json2 is a CkJsonObject set i 0 set count_i [CkJsonObject_SizeOfArray $json "responses"] while {$i < $count_i} { CkJsonObject_put_I $json $i set fullTextAnnotationText [CkJsonObject_stringOf $json "responses[i].fullTextAnnotation.text"] set j 0 set count_j [CkJsonObject_SizeOfArray $json "responses[i].textAnnotations"] while {$j < $count_j} { CkJsonObject_put_J $json $j set locale [CkJsonObject_stringOf $json "responses[i].textAnnotations[j].locale"] set description [CkJsonObject_stringOf $json "responses[i].textAnnotations[j].description"] set k 0 set count_k [CkJsonObject_SizeOfArray $json "responses[i].textAnnotations[j].boundingPoly.vertices"] while {$k < $count_k} { CkJsonObject_put_K $json $k set x [CkJsonObject_IntOf $json "responses[i].textAnnotations[j].boundingPoly.vertices[k].x"] set y [CkJsonObject_IntOf $json "responses[i].textAnnotations[j].boundingPoly.vertices[k].y"] set k [expr $k + 1] } set j [expr $j + 1] } set j 0 set count_j [CkJsonObject_SizeOfArray $json "responses[i].fullTextAnnotation.pages"] while {$j < $count_j} { CkJsonObject_put_J $json $j set width [CkJsonObject_IntOf $json "responses[i].fullTextAnnotation.pages[j].width"] set height [CkJsonObject_IntOf $json "responses[i].fullTextAnnotation.pages[j].height"] set k 0 set count_k [CkJsonObject_SizeOfArray $json "responses[i].fullTextAnnotation.pages[j].property.detectedLanguages"] while {$k < $count_k} { CkJsonObject_put_K $json $k set languageCode [CkJsonObject_stringOf $json "responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode"] set k [expr $k + 1] } set k 0 set count_k [CkJsonObject_SizeOfArray $json "responses[i].fullTextAnnotation.pages[j].blocks"] while {$k < $count_k} { CkJsonObject_put_K $json $k set blockType [CkJsonObject_stringOf $json "responses[i].fullTextAnnotation.pages[j].blocks[k].blockType"] set json1 [CkJsonObject_ObjectOf $json "responses[i].fullTextAnnotation.pages[j].blocks[k]"] set i1 0 set count_i1 [CkJsonObject_SizeOfArray $json1 "property.detectedLanguages"] while {$i1 < $count_i1} { CkJsonObject_put_I $json1 $i1 set languageCode [CkJsonObject_stringOf $json1 "property.detectedLanguages[i].languageCode"] set i1 [expr $i1 + 1] } delete_CkJsonObject $json1 set json1 [CkJsonObject_ObjectOf $json "responses[i].fullTextAnnotation.pages[j].blocks[k]"] set i1 0 set count_i1 [CkJsonObject_SizeOfArray $json1 "boundingBox.vertices"] while {$i1 < $count_i1} { CkJsonObject_put_I $json1 $i1 set x [CkJsonObject_IntOf $json1 "boundingBox.vertices[i].x"] set y [CkJsonObject_IntOf $json1 "boundingBox.vertices[i].y"] set i1 [expr $i1 + 1] } delete_CkJsonObject $json1 set json1 [CkJsonObject_ObjectOf $json "responses[i].fullTextAnnotation.pages[j].blocks[k]"] set i1 0 set count_i1 [CkJsonObject_SizeOfArray $json1 "paragraphs"] while {$i1 < $count_i1} { CkJsonObject_put_I $json1 $i1 set j1 0 set count_j1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].property.detectedLanguages"] while {$j1 < $count_j1} { CkJsonObject_put_J $json1 $j1 set languageCode [CkJsonObject_stringOf $json1 "paragraphs[i].property.detectedLanguages[j].languageCode"] set j1 [expr $j1 + 1] } set j1 0 set count_j1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].boundingBox.vertices"] while {$j1 < $count_j1} { CkJsonObject_put_J $json1 $j1 set x [CkJsonObject_IntOf $json1 "paragraphs[i].boundingBox.vertices[j].x"] set y [CkJsonObject_IntOf $json1 "paragraphs[i].boundingBox.vertices[j].y"] set j1 [expr $j1 + 1] } set j1 0 set count_j1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words"] while {$j1 < $count_j1} { CkJsonObject_put_J $json1 $j1 set k1 0 set count_k1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words[j].property.detectedLanguages"] while {$k1 < $count_k1} { CkJsonObject_put_K $json1 $k1 set languageCode [CkJsonObject_stringOf $json1 "paragraphs[i].words[j].property.detectedLanguages[k].languageCode"] set k1 [expr $k1 + 1] } set k1 0 set count_k1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words[j].boundingBox.vertices"] while {$k1 < $count_k1} { CkJsonObject_put_K $json1 $k1 set x [CkJsonObject_IntOf $json1 "paragraphs[i].words[j].boundingBox.vertices[k].x"] set y [CkJsonObject_IntOf $json1 "paragraphs[i].words[j].boundingBox.vertices[k].y"] set k1 [expr $k1 + 1] } set k1 0 set count_k1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words[j].symbols"] while {$k1 < $count_k1} { CkJsonObject_put_K $json1 $k1 set text [CkJsonObject_stringOf $json1 "paragraphs[i].words[j].symbols[k].text"] set propertyDetectedBreakType [CkJsonObject_stringOf $json1 "paragraphs[i].words[j].symbols[k].property.detectedBreak.type"] set json2 [CkJsonObject_ObjectOf $json1 "paragraphs[i].words[j].symbols[k]"] set i2 0 set count_i2 [CkJsonObject_SizeOfArray $json2 "property.detectedLanguages"] while {$i2 < $count_i2} { CkJsonObject_put_I $json2 $i2 set languageCode [CkJsonObject_stringOf $json2 "property.detectedLanguages[i].languageCode"] set i2 [expr $i2 + 1] } delete_CkJsonObject $json2 set json2 [CkJsonObject_ObjectOf $json1 "paragraphs[i].words[j].symbols[k]"] set i2 0 set count_i2 [CkJsonObject_SizeOfArray $json2 "boundingBox.vertices"] while {$i2 < $count_i2} { CkJsonObject_put_I $json2 $i2 set x [CkJsonObject_IntOf $json2 "boundingBox.vertices[i].x"] set y [CkJsonObject_IntOf $json2 "boundingBox.vertices[i].y"] set i2 [expr $i2 + 1] } delete_CkJsonObject $json2 set k1 [expr $k1 + 1] } set j1 [expr $j1 + 1] } set i1 [expr $i1 + 1] } delete_CkJsonObject $json1 set k [expr $k + 1] } set j [expr $j + 1] } set i [expr $i + 1] } delete_CkHttpResponse $resp puts "Success." delete_CkBinData $imageData delete_CkJsonObject $json delete_CkHttp $http delete_CkStringBuilder $sb delete_CkStringBuilder $sbResponseBody |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.