SQL Server
SQL Server
Google Cloud Vision Text Detection
See more HTTP Misc Examples
Demonstrates 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-textChilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- 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.
DECLARE @imageData int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @imageData OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- This image file contains some text...
EXEC sp_OAMethod @imageData, 'LoadFile', @success OUT, 'qa_data/jpg/text.jpg'
IF @success <> 1
BEGIN
PRINT 'Failed to load image file.'
EXEC @hr = sp_OADestroy @imageData
RETURN
END
-- Create the above JSON.
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateBd', @success OUT, 'requests[0].image.content', 'base64', @imageData
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, '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
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OASetProperty @http, 'SessionLogFilename', 'c:/aaworkarea/sessionLog.txt'
DECLARE @sb int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT
DECLARE @url nvarchar(4000)
SELECT @url = 'https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @url, @json, 'application/json', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imageData
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sb
EXEC @hr = sp_OADestroy @resp
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'status = ' + @iTmp0
-- A 200 response status indicate success.
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @imageData
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sb
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @sbResponseBody int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
EXEC sp_OAMethod @sbResponseBody, 'WriteFile', @success OUT, 'qa_output/textDetectResponse.json', 'utf-8', 0
EXEC sp_OAMethod @json, 'LoadSb', @success OUT, @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:
DECLARE @i int
DECLARE @count_i int
DECLARE @fullTextAnnotationText nvarchar(4000)
DECLARE @j int
DECLARE @count_j int
DECLARE @locale nvarchar(4000)
DECLARE @description nvarchar(4000)
DECLARE @k int
DECLARE @count_k int
DECLARE @x int
DECLARE @y int
DECLARE @width int
DECLARE @height int
DECLARE @languageCode nvarchar(4000)
DECLARE @blockType nvarchar(4000)
DECLARE @i1 int
DECLARE @count_i1 int
DECLARE @j1 int
DECLARE @count_j1 int
DECLARE @k1 int
DECLARE @count_k1 int
DECLARE @text nvarchar(4000)
DECLARE @propertyDetectedBreakType nvarchar(4000)
DECLARE @i2 int
DECLARE @count_i2 int
DECLARE @json1 int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json1 OUT
DECLARE @json2 int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json2 OUT
SELECT @i = 0
EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'responses'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @json, 'I', @i
EXEC sp_OAMethod @json, 'StringOf', @fullTextAnnotationText OUT, 'responses[i].fullTextAnnotation.text'
SELECT @j = 0
EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'responses[i].textAnnotations'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @json, 'J', @j
EXEC sp_OAMethod @json, 'StringOf', @locale OUT, 'responses[i].textAnnotations[j].locale'
EXEC sp_OAMethod @json, 'StringOf', @description OUT, 'responses[i].textAnnotations[j].description'
SELECT @k = 0
EXEC sp_OAMethod @json, 'SizeOfArray', @count_k OUT, 'responses[i].textAnnotations[j].boundingPoly.vertices'
WHILE @k < @count_k
BEGIN
EXEC sp_OASetProperty @json, 'K', @k
EXEC sp_OAMethod @json, 'IntOf', @x OUT, 'responses[i].textAnnotations[j].boundingPoly.vertices[k].x'
EXEC sp_OAMethod @json, 'IntOf', @y OUT, 'responses[i].textAnnotations[j].boundingPoly.vertices[k].y'
SELECT @k = @k + 1
END
SELECT @j = @j + 1
END
SELECT @j = 0
EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'responses[i].fullTextAnnotation.pages'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @json, 'J', @j
EXEC sp_OAMethod @json, 'IntOf', @width OUT, 'responses[i].fullTextAnnotation.pages[j].width'
EXEC sp_OAMethod @json, 'IntOf', @height OUT, 'responses[i].fullTextAnnotation.pages[j].height'
SELECT @k = 0
EXEC sp_OAMethod @json, 'SizeOfArray', @count_k OUT, 'responses[i].fullTextAnnotation.pages[j].property.detectedLanguages'
WHILE @k < @count_k
BEGIN
EXEC sp_OASetProperty @json, 'K', @k
EXEC sp_OAMethod @json, 'StringOf', @languageCode OUT, 'responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode'
SELECT @k = @k + 1
END
SELECT @k = 0
EXEC sp_OAMethod @json, 'SizeOfArray', @count_k OUT, 'responses[i].fullTextAnnotation.pages[j].blocks'
WHILE @k < @count_k
BEGIN
EXEC sp_OASetProperty @json, 'K', @k
EXEC sp_OAMethod @json, 'StringOf', @blockType OUT, 'responses[i].fullTextAnnotation.pages[j].blocks[k].blockType'
EXEC sp_OAMethod @json, 'ObjectOf2', @success OUT, 'responses[i].fullTextAnnotation.pages[j].blocks[k]', @json1
SELECT @i1 = 0
EXEC sp_OAMethod @json1, 'SizeOfArray', @count_i1 OUT, 'property.detectedLanguages'
WHILE @i1 < @count_i1
BEGIN
EXEC sp_OASetProperty @json1, 'I', @i1
EXEC sp_OAMethod @json1, 'StringOf', @languageCode OUT, 'property.detectedLanguages[i].languageCode'
SELECT @i1 = @i1 + 1
END
EXEC sp_OAMethod @json, 'ObjectOf2', @success OUT, 'responses[i].fullTextAnnotation.pages[j].blocks[k]', @json1
SELECT @i1 = 0
EXEC sp_OAMethod @json1, 'SizeOfArray', @count_i1 OUT, 'boundingBox.vertices'
WHILE @i1 < @count_i1
BEGIN
EXEC sp_OASetProperty @json1, 'I', @i1
EXEC sp_OAMethod @json1, 'IntOf', @x OUT, 'boundingBox.vertices[i].x'
EXEC sp_OAMethod @json1, 'IntOf', @y OUT, 'boundingBox.vertices[i].y'
SELECT @i1 = @i1 + 1
END
EXEC sp_OAMethod @json, 'ObjectOf2', @success OUT, 'responses[i].fullTextAnnotation.pages[j].blocks[k]', @json1
SELECT @i1 = 0
EXEC sp_OAMethod @json1, 'SizeOfArray', @count_i1 OUT, 'paragraphs'
WHILE @i1 < @count_i1
BEGIN
EXEC sp_OASetProperty @json1, 'I', @i1
SELECT @j1 = 0
EXEC sp_OAMethod @json1, 'SizeOfArray', @count_j1 OUT, 'paragraphs[i].property.detectedLanguages'
WHILE @j1 < @count_j1
BEGIN
EXEC sp_OASetProperty @json1, 'J', @j1
EXEC sp_OAMethod @json1, 'StringOf', @languageCode OUT, 'paragraphs[i].property.detectedLanguages[j].languageCode'
SELECT @j1 = @j1 + 1
END
SELECT @j1 = 0
EXEC sp_OAMethod @json1, 'SizeOfArray', @count_j1 OUT, 'paragraphs[i].boundingBox.vertices'
WHILE @j1 < @count_j1
BEGIN
EXEC sp_OASetProperty @json1, 'J', @j1
EXEC sp_OAMethod @json1, 'IntOf', @x OUT, 'paragraphs[i].boundingBox.vertices[j].x'
EXEC sp_OAMethod @json1, 'IntOf', @y OUT, 'paragraphs[i].boundingBox.vertices[j].y'
SELECT @j1 = @j1 + 1
END
SELECT @j1 = 0
EXEC sp_OAMethod @json1, 'SizeOfArray', @count_j1 OUT, 'paragraphs[i].words'
WHILE @j1 < @count_j1
BEGIN
EXEC sp_OASetProperty @json1, 'J', @j1
SELECT @k1 = 0
EXEC sp_OAMethod @json1, 'SizeOfArray', @count_k1 OUT, 'paragraphs[i].words[j].property.detectedLanguages'
WHILE @k1 < @count_k1
BEGIN
EXEC sp_OASetProperty @json1, 'K', @k1
EXEC sp_OAMethod @json1, 'StringOf', @languageCode OUT, 'paragraphs[i].words[j].property.detectedLanguages[k].languageCode'
SELECT @k1 = @k1 + 1
END
SELECT @k1 = 0
EXEC sp_OAMethod @json1, 'SizeOfArray', @count_k1 OUT, 'paragraphs[i].words[j].boundingBox.vertices'
WHILE @k1 < @count_k1
BEGIN
EXEC sp_OASetProperty @json1, 'K', @k1
EXEC sp_OAMethod @json1, 'IntOf', @x OUT, 'paragraphs[i].words[j].boundingBox.vertices[k].x'
EXEC sp_OAMethod @json1, 'IntOf', @y OUT, 'paragraphs[i].words[j].boundingBox.vertices[k].y'
SELECT @k1 = @k1 + 1
END
SELECT @k1 = 0
EXEC sp_OAMethod @json1, 'SizeOfArray', @count_k1 OUT, 'paragraphs[i].words[j].symbols'
WHILE @k1 < @count_k1
BEGIN
EXEC sp_OASetProperty @json1, 'K', @k1
EXEC sp_OAMethod @json1, 'StringOf', @text OUT, 'paragraphs[i].words[j].symbols[k].text'
EXEC sp_OAMethod @json1, 'StringOf', @propertyDetectedBreakType OUT, 'paragraphs[i].words[j].symbols[k].property.detectedBreak.type'
EXEC sp_OAMethod @json1, 'ObjectOf2', @success OUT, 'paragraphs[i].words[j].symbols[k]', @json2
SELECT @i2 = 0
EXEC sp_OAMethod @json2, 'SizeOfArray', @count_i2 OUT, 'property.detectedLanguages'
WHILE @i2 < @count_i2
BEGIN
EXEC sp_OASetProperty @json2, 'I', @i2
EXEC sp_OAMethod @json2, 'StringOf', @languageCode OUT, 'property.detectedLanguages[i].languageCode'
SELECT @i2 = @i2 + 1
END
EXEC sp_OAMethod @json1, 'ObjectOf2', @success OUT, 'paragraphs[i].words[j].symbols[k]', @json2
SELECT @i2 = 0
EXEC sp_OAMethod @json2, 'SizeOfArray', @count_i2 OUT, 'boundingBox.vertices'
WHILE @i2 < @count_i2
BEGIN
EXEC sp_OASetProperty @json2, 'I', @i2
EXEC sp_OAMethod @json2, 'IntOf', @x OUT, 'boundingBox.vertices[i].x'
EXEC sp_OAMethod @json2, 'IntOf', @y OUT, 'boundingBox.vertices[i].y'
SELECT @i2 = @i2 + 1
END
SELECT @k1 = @k1 + 1
END
SELECT @j1 = @j1 + 1
END
SELECT @i1 = @i1 + 1
END
SELECT @k = @k + 1
END
SELECT @j = @j + 1
END
SELECT @i = @i + 1
END
PRINT 'Success.'
EXEC @hr = sp_OADestroy @imageData
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sb
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @json1
EXEC @hr = sp_OADestroy @json2
END
GO