Sample code for 30+ languages & platforms
SQL Server

Amazon Rekognition - Detect Text in an Image

See more Amazon Rekognition Examples

Detects text in the input image and converts it into machine-readable text. This example passes theimage as base64-encoded image bytes.

Chilkat SQL Server Downloads

SQL Server
-- 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

    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @authAws int
    EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @authAws OUT

    EXEC sp_OASetProperty @authAws, 'AccessKey', 'AWS_ACCESS_KEY'
    EXEC sp_OASetProperty @authAws, 'SecretKey', 'AWS_SECRET_KEY'
    -- Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
    EXEC sp_OASetProperty @authAws, 'Region', 'us-west-2'
    EXEC sp_OASetProperty @authAws, 'ServiceName', 'rekognition'
    -- SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @authAws

    -- URL: https://rekognition.us-west-2.amazonaws.com/
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    -- Don't forget to change the region domain (us-west-2.amazonaws.com) to your particular region.
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'rekognition.us-west-2.amazonaws.com', @port, @bTls, @bAutoReconnect
    IF @success <> 1
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ConnectFailReason', @iTmp0 OUT
        PRINT 'ConnectFailReason: ' + @iTmp0
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        RETURN
      END

    -- Note: The above code does not need to be repeatedly called for each REST request.
    -- The rest object can be setup once, and then many requests can be sent.  Chilkat will automatically
    -- reconnect within a FullRequest* method as needed.  It is only the very first connection that is explicitly
    -- made via the Connect method.

    -- Load the JPG to be passed as base64 in the JSON.
    DECLARE @bdJpg int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdJpg OUT

    EXEC sp_OAMethod @bdJpg, 'LoadFile', @success OUT, 'qa_data/jpg/monday_keep_smiling.jpg'
    IF @success <> 1
      BEGIN

        PRINT 'Failed to load the input JPG file.'
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @bdJpg
        RETURN
      END

    -- We wish to send the following JSON in the body of our HTTP request:

    -- {
    --   "Image": {
    --       "Bytes": "base64_image_bytes"
    --   }
    -- }

    -- Here is the image we used for testing:
    -- (image:https://example-code.com/images/monday_keep_smiling.jpg/endImage)

    -- Convert binary image bytes to base64.
    -- Note: We are explicitly keeping the data inside Chilkat to avoid having to pass large strings
    -- as arguments to function calls.  This is important for some programming languages.
    DECLARE @sbJpg int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbJpg OUT

    EXEC sp_OAMethod @bdJpg, 'GetEncodedSb', @success OUT, 'base64', @sbJpg

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateSb', @success OUT, 'Image.Bytes', @sbJpg

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/x-amz-json-1.1'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'RekognitionService.DetectText'

    DECLARE @sbRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRequestBody OUT

    EXEC sp_OAMethod @json, 'EmitSb', @success OUT, @sbRequestBody
    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @sbRequestBody, @sbResponseBody
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @bdJpg
        EXEC @hr = sp_OADestroy @sbJpg
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END
    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @respStatusCode OUT

    PRINT 'response status code = ' + @respStatusCode

    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Status Code = ' + @respStatusCode

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Response Body:'
        EXEC sp_OAMethod @sbResponseBody, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @bdJpg
        EXEC @hr = sp_OADestroy @sbJpg
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody

    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "TextDetections": [
    --     {
    --       "Confidence": 95.99308776855469,
    --       "DetectedText": "( MONDAY IT'S",
    --       "Geometry": {
    --         "BoundingBox": {
    --           "Height": 0.6399821043014526,
    --           "Left": 0.219133198261261,
    --           "Top": 0.08677978068590164,
    --           "Width": 0.7433173656463623
    --         },
    --         "Polygon": [
    --           {
    --             "X": 0.219133198261261,
    --             "Y": 0.3588336706161499
    --           },
    --           {
    --             "X": 0.8984103798866272,
    --             "Y": 0.08677978068590164
    --           },
    --           {
    --             "X": 0.9624505639076233,
    --             "Y": 0.4547080099582672
    --           },
    --           {
    --             "X": 0.2831733524799347,
    --             "Y": 0.7267619371414185
    --           }
    --         ]
    --       },
    --       "Id": 0,
    --       "Type": "LINE"
    --     },
    --     {
    --       "Confidence": 99.70352172851562,
    --       "DetectedText": "but keep",
    --       "Geometry": {
    --         "BoundingBox": {
    --           "Height": 0.09703556448221207,
    --           "Left": 0.6335319876670837,
    --           "Top": 0.5153074264526367,
    --           "Width": 0.21070890128612518
    --         },
    --         "Polygon": [
    --           {
    --             "X": 0.6355597376823425,
    --             "Y": 0.5153074264526367
    --           },
    --           {
    --             "X": 0.8442409038543701,
    --             "Y": 0.5266726613044739
    --           },
    --           {
    --             "X": 0.8422132134437561,
    --             "Y": 0.6123430132865906
    --           },
    --           {
    --             "X": 0.6335319876670837,
    --             "Y": 0.6009777784347534
    --           }
    --         ]
    --       },
    --       "Id": 1,
    --       "Type": "LINE"
    --     },
    --     {
    --       "Confidence": 99.92333984375,
    --       "DetectedText": "Smiling",
    --       "Geometry": {
    --         "BoundingBox": {
    --           "Height": 0.31578224897384644,
    --           "Left": 0.5070608258247375,
    --           "Top": 0.6086956262588501,
    --           "Width": 0.4795433282852173
    --         },
    --         "Polygon": [
    --           {
    --             "X": 0.5070608258247375,
    --             "Y": 0.6298336386680603
    --           },
    --           {
    --             "X": 0.9808917045593262,
    --             "Y": 0.6086956262588501
    --           },
    --           {
    --             "X": 0.9866041541099548,
    --             "Y": 0.9033399224281311
    --           },
    --           {
    --             "X": 0.5127732157707214,
    --             "Y": 0.9244779348373413
    --           }
    --         ]
    --       },
    --       "Id": 2,
    --       "Type": "LINE"
    --     },
    --     {
    --       "Confidence": 99.77294158935547,
    --       "DetectedText": "IT'S",
    --       "Geometry": {
    --         "BoundingBox": {
    --           "Height": 0.09903381764888763,
    --           "Left": 0.668789803981781,
    --           "Top": 0.17874395847320557,
    --           "Width": 0.1449044644832611
    --         },
    --         "Polygon": [
    --           {
    --             "X": 0.668789803981781,
    --             "Y": 0.17874395847320557
    --           },
    --           {
    --             "X": 0.8136942386627197,
    --             "Y": 0.17874395847320557
    --           },
    --           {
    --             "X": 0.8136942386627197,
    --             "Y": 0.2777777910232544
    --           },
    --           {
    --             "X": 0.668789803981781,
    --             "Y": 0.2777777910232544
    --           }
    --         ]
    --       },
    --       "Id": 5,
    --       "ParentId": 0,
    --       "Type": "WORD"
    --     },
    --     {
    --       "Confidence": 98.44307708740234,
    --       "DetectedText": "MONDAY",
    --       "Geometry": {
    --         "BoundingBox": {
    --           "Height": 0.11112251877784729,
    --           "Left": 0.5541401505470276,
    --           "Top": 0.3526569902896881,
    --           "Width": 0.39013487100601196
    --         },
    --         "Polygon": [
    --           {
    --             "X": 0.5541401505470276,
    --             "Y": 0.3526569902896881
    --           },
    --           {
    --             "X": 0.9442675113677979,
    --             "Y": 0.3502415418624878
    --           },
    --           {
    --             "X": 0.9458598494529724,
    --             "Y": 0.4613526463508606
    --           },
    --           {
    --             "X": 0.5541401505470276,
    --             "Y": 0.4637681245803833
    --           }
    --         ]
    --       },
    --       "Id": 4,
    --       "ParentId": 0,
    --       "Type": "WORD"
    --     },
    --     {
    --       "Confidence": 99.61898803710938,
    --       "DetectedText": "but",
    --       "Geometry": {
    --         "BoundingBox": {
    --           "Height": 0.06521739065647125,
    --           "Left": 0.6353503465652466,
    --           "Top": 0.5241546034812927,
    --           "Width": 0.0843949019908905
    --         },
    --         "Polygon": [
    --           {
    --             "X": 0.6353503465652466,
    --             "Y": 0.5241546034812927
    --           },
    --           {
    --             "X": 0.7197452187538147,
    --             "Y": 0.5241546034812927
    --           },
    --           {
    --             "X": 0.7197452187538147,
    --             "Y": 0.5893719792366028
    --           },
    --           {
    --             "X": 0.6353503465652466,
    --             "Y": 0.5893719792366028
    --           }
    --         ]
    --       },
    --       "Id": 6,
    --       "ParentId": 1,
    --       "Type": "WORD"
    --     },
    --     {
    --       "Confidence": 99.78804779052734,
    --       "DetectedText": "keep",
    --       "Geometry": {
    --         "BoundingBox": {
    --           "Height": 0.07971014827489853,
    --           "Left": 0.7308917045593262,
    --           "Top": 0.5265700221061707,
    --           "Width": 0.1114649698138237
    --         },
    --         "Polygon": [
    --           {
    --             "X": 0.7308917045593262,
    --             "Y": 0.5265700221061707
    --           },
    --           {
    --             "X": 0.8423566818237305,
    --             "Y": 0.5265700221061707
    --           },
    --           {
    --             "X": 0.8423566818237305,
    --             "Y": 0.6062802076339722
    --           },
    --           {
    --             "X": 0.7308917045593262,
    --             "Y": 0.6062802076339722
    --           }
    --         ]
    --       },
    --       "Id": 7,
    --       "ParentId": 1,
    --       "Type": "WORD"
    --     },
    --     {
    --       "Confidence": 89.76324462890625,
    --       "DetectedText": "(",
    --       "Geometry": {
    --         "BoundingBox": {
    --           "Height": 0.16401274502277374,
    --           "Left": 0.27229300141334534,
    --           "Top": 0.6642512083053589,
    --           "Width": 0.2850286066532135
    --         },
    --         "Polygon": [
    --           {
    --             "X": 0.27229300141334534,
    --             "Y": 0.6642512083053589
    --           },
    --           {
    --             "X": 0.2707006335258484,
    --             "Y": 0.37922704219818115
    --           },
    --           {
    --             "X": 0.43471336364746094,
    --             "Y": 0.37922704219818115
    --           },
    --           {
    --             "X": 0.4363057315349579,
    --             "Y": 0.6642512083053589
    --           }
    --         ]
    --       },
    --       "Id": 3,
    --       "ParentId": 0,
    --       "Type": "WORD"
    --     },
    --     {
    --       "Confidence": 99.92333984375,
    --       "DetectedText": "Smiling",
    --       "Geometry": {
    --         "BoundingBox": {
    --           "Height": 0.294724702835083,
    --           "Left": 0.5079618096351624,
    --           "Top": 0.6304348111152649,
    --           "Width": 0.4734293222427368
    --         },
    --         "Polygon": [
    --           {
    --             "X": 0.5079618096351624,
    --             "Y": 0.6304348111152649
    --           },
    --           {
    --             "X": 0.9808917045593262,
    --             "Y": 0.6086956262588501
    --           },
    --           {
    --             "X": 0.9856687784194946,
    --             "Y": 0.9033816456794739
    --           },
    --           {
    --             "X": 0.512738823890686,
    --             "Y": 0.9227052927017212
    --           }
    --         ]
    --       },
    --       "Id": 8,
    --       "ParentId": 2,
    --       "Type": "WORD"
    --     }
    --   ],
    --   "TextModelVersion": "3.0"
    -- }
    -- 
    -- 

    -- Sample code for parsing the JSON response...
    -- Use the following online tool to generate parsing code from sample JSON:
    -- Generate Parsing Code from JSON

    DECLARE @Confidence nvarchar(4000)

    DECLARE @DetectedText nvarchar(4000)

    DECLARE @GeometryBoundingBoxHeight nvarchar(4000)

    DECLARE @GeometryBoundingBoxLeft nvarchar(4000)

    DECLARE @GeometryBoundingBoxTop nvarchar(4000)

    DECLARE @GeometryBoundingBoxWidth nvarchar(4000)

    DECLARE @Id int

    DECLARE @v_Type nvarchar(4000)

    DECLARE @ParentId int

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @X nvarchar(4000)

    DECLARE @Y nvarchar(4000)

    DECLARE @TextModelVersion nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @TextModelVersion OUT, 'TextModelVersion'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'TextDetections'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @Confidence OUT, 'TextDetections[i].Confidence'
        EXEC sp_OAMethod @jResp, 'StringOf', @DetectedText OUT, 'TextDetections[i].DetectedText'
        EXEC sp_OAMethod @jResp, 'StringOf', @GeometryBoundingBoxHeight OUT, 'TextDetections[i].Geometry.BoundingBox.Height'
        EXEC sp_OAMethod @jResp, 'StringOf', @GeometryBoundingBoxLeft OUT, 'TextDetections[i].Geometry.BoundingBox.Left'
        EXEC sp_OAMethod @jResp, 'StringOf', @GeometryBoundingBoxTop OUT, 'TextDetections[i].Geometry.BoundingBox.Top'
        EXEC sp_OAMethod @jResp, 'StringOf', @GeometryBoundingBoxWidth OUT, 'TextDetections[i].Geometry.BoundingBox.Width'
        EXEC sp_OAMethod @jResp, 'IntOf', @Id OUT, 'TextDetections[i].Id'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'TextDetections[i].Type'
        EXEC sp_OAMethod @jResp, 'IntOf', @ParentId OUT, 'TextDetections[i].ParentId'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'TextDetections[i].Geometry.Polygon'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @X OUT, 'TextDetections[i].Geometry.Polygon[j].X'
            EXEC sp_OAMethod @jResp, 'StringOf', @Y OUT, 'TextDetections[i].Geometry.Polygon[j].Y'
            SELECT @j = @j + 1
          END
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @authAws
    EXEC @hr = sp_OADestroy @bdJpg
    EXEC @hr = sp_OADestroy @sbJpg
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbRequestBody
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO