Sample code for 30+ languages & platforms
SQL Server

Amazon Rekognition - Detect Faces in an Image

See more Amazon Rekognition Examples

Detects faces within an image that is provided as input. 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/kid_blue_coat.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"
    --     }
    --     "Attributes": [
    --         "ALL"
    --     ]
    -- }

    -- Here is the image we used for testing:
    -- (image:https://example-code.com/images/kid_blue_coat.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 @json, 'UpdateString', @success OUT, 'Attributes[0]', 'ALL'

    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.DetectFaces'

    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)

    -- {
    --   "FaceDetails": [
    --     {
    --       "AgeRange": {
    --         "High": 18,
    --         "Low": 8
    --       },
    --       "Beard": {
    --         "Confidence": 98.06282806396484,
    --         "Value": false
    --       },
    --       "BoundingBox": {
    --         "Height": 0.327279269695282,
    --         "Left": 0.5339247584342957,
    --         "Top": 0.23660442233085632,
    --         "Width": 0.35611653327941895
    --       },
    --       "Confidence": 99.99732971191406,
    --       "Emotions": [
    --         {
    --           "Confidence": 99.5849380493164,
    --           "Type": "HAPPY"
    --         },
    --         {
    --           "Confidence": 0.15533843636512756,
    --           "Type": "CALM"
    --         },
    --         {
    --           "Confidence": 0.08864031732082367,
    --           "Type": "SURPRISED"
    --         },
    --         {
    --           "Confidence": 0.05476664379239082,
    --           "Type": "SAD"
    --         },
    --         {
    --           "Confidence": 0.042048510164022446,
    --           "Type": "CONFUSED"
    --         },
    --         {
    --           "Confidence": 0.038942769169807434,
    --           "Type": "DISGUSTED"
    --         },
    --         {
    --           "Confidence": 0.021463459357619286,
    --           "Type": "FEAR"
    --         },
    --         {
    --           "Confidence": 0.013858155347406864,
    --           "Type": "ANGRY"
    --         }
    --       ],
    --       "Eyeglasses": {
    --         "Confidence": 98.5116195678711,
    --         "Value": false
    --       },
    --       "EyesOpen": {
    --         "Confidence": 99.65477752685547,
    --         "Value": true
    --       },
    --       "Gender": {
    --         "Confidence": 97.1164321899414,
    --         "Value": "Female"
    --       },
    --       "Landmarks": [
    --         {
    --           "Type": "eyeLeft",
    --           "X": 0.6554790735244751,
    --           "Y": 0.35153862833976746
    --         },
    --         {
    --           "Type": "eyeRight",
    --           "X": 0.7940073609352112,
    --           "Y": 0.38292214274406433
    --         },
    --         {
    --           "Type": "mouthLeft",
    --           "X": 0.6188991069793701,
    --           "Y": 0.46431097388267517
    --         },
    --         {
    --           "Type": "mouthRight",
    --           "X": 0.7352844476699829,
    --           "Y": 0.490242063999176
    --         },
    --         {
    --           "Type": "nose",
    --           "X": 0.7125006914138794,
    --           "Y": 0.44607019424438477
    --         },
    --         {
    --           "Type": "leftEyeBrowLeft",
    --           "X": 0.6096581220626831,
    --           "Y": 0.3071737587451935
    --         },
    --         {
    --           "Type": "leftEyeBrowRight",
    --           "X": 0.6628581285476685,
    --           "Y": 0.3133310079574585
    --         },
    --         {
    --           "Type": "leftEyeBrowUp",
    --           "X": 0.7027584314346313,
    --           "Y": 0.33200803399086
    --         },
    --         {
    --           "Type": "rightEyeBrowLeft",
    --           "X": 0.7813941240310669,
    --           "Y": 0.35023579001426697
    --         },
    --         {
    --           "Type": "rightEyeBrowRight",
    --           "X": 0.8213478922843933,
    --           "Y": 0.34993964433670044
    --         },
    --         {
    --           "Type": "rightEyeBrowUp",
    --           "X": 0.8495538234710693,
    --           "Y": 0.36189284920692444
    --         },
    --         {
    --           "Type": "leftEyeLeft",
    --           "X": 0.629088282585144,
    --           "Y": 0.34286588430404663
    --         },
    --         {
    --           "Type": "leftEyeRight",
    --           "X": 0.6820939183235168,
    --           "Y": 0.3586524724960327
    --         },
    --         {
    --           "Type": "leftEyeUp",
    --           "X": 0.6580297946929932,
    --           "Y": 0.3468707501888275
    --         },
    --         {
    --           "Type": "leftEyeDown",
    --           "X": 0.6537532210350037,
    --           "Y": 0.35663917660713196
    --         },
    --         {
    --           "Type": "rightEyeLeft",
    --           "X": 0.7655976414680481,
    --           "Y": 0.3776427209377289
    --         },
    --         {
    --           "Type": "rightEyeRight",
    --           "X": 0.8166338801383972,
    --           "Y": 0.38544225692749023
    --         },
    --         {
    --           "Type": "rightEyeUp",
    --           "X": 0.7969376444816589,
    --           "Y": 0.37844377756118774
    --         },
    --         {
    --           "Type": "rightEyeDown",
    --           "X": 0.7909533977508545,
    --           "Y": 0.3877102732658386
    --         },
    --         {
    --           "Type": "noseLeft",
    --           "X": 0.6727234721183777,
    --           "Y": 0.44030481576919556
    --         },
    --         {
    --           "Type": "noseRight",
    --           "X": 0.7237889170646667,
    --           "Y": 0.45200300216674805
    --         },
    --         {
    --           "Type": "mouthUp",
    --           "X": 0.6882695555686951,
    --           "Y": 0.4740942418575287
    --         },
    --         {
    --           "Type": "mouthDown",
    --           "X": 0.6720560789108276,
    --           "Y": 0.5046101808547974
    --         },
    --         {
    --           "Type": "leftPupil",
    --           "X": 0.6554790735244751,
    --           "Y": 0.35153862833976746
    --         },
    --         {
    --           "Type": "rightPupil",
    --           "X": 0.7940073609352112,
    --           "Y": 0.38292214274406433
    --         },
    --         {
    --           "Type": "upperJawlineLeft",
    --           "X": 0.5517005324363708,
    --           "Y": 0.30355724692344666
    --         },
    --         {
    --           "Type": "midJawlineLeft",
    --           "X": 0.5320234894752502,
    --           "Y": 0.43352627754211426
    --         },
    --         {
    --           "Type": "chinBottom",
    --           "X": 0.6419994831085205,
    --           "Y": 0.5531964302062988
    --         },
    --         {
    --           "Type": "midJawlineRight",
    --           "X": 0.7752369046211243,
    --           "Y": 0.48957017064094543
    --         },
    --         {
    --           "Type": "upperJawlineRight",
    --           "X": 0.8515444397926331,
    --           "Y": 0.37258899211883545
    --         }
    --       ],
    --       "MouthOpen": {
    --         "Confidence": 68.26280212402344,
    --         "Value": false
    --       },
    --       "Mustache": {
    --         "Confidence": 99.73213195800781,
    --         "Value": false
    --       },
    --       "Pose": {
    --         "Pitch": -11.299633026123047,
    --         "Roll": 17.6924991607666,
    --         "Yaw": 13.582314491271973
    --       },
    --       "Quality": {
    --         "Brightness": 83.72581481933594,
    --         "Sharpness": 67.22731018066406
    --       },
    --       "Smile": {
    --         "Confidence": 98.4793930053711,
    --         "Value": true
    --       },
    --       "Sunglasses": {
    --         "Confidence": 99.3582992553711,
    --         "Value": false
    --       }
    --     }
    --   ]
    -- }

    -- 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 @AgeRangeHigh int

    DECLARE @AgeRangeLow int

    DECLARE @BeardConfidence nvarchar(4000)

    DECLARE @BeardValue int

    DECLARE @BoundingBoxHeight nvarchar(4000)

    DECLARE @BoundingBoxLeft nvarchar(4000)

    DECLARE @BoundingBoxTop nvarchar(4000)

    DECLARE @BoundingBoxWidth nvarchar(4000)

    DECLARE @Confidence nvarchar(4000)

    DECLARE @EyeglassesConfidence nvarchar(4000)

    DECLARE @EyeglassesValue int

    DECLARE @EyesOpenConfidence nvarchar(4000)

    DECLARE @EyesOpenValue int

    DECLARE @GenderConfidence nvarchar(4000)

    DECLARE @GenderValue nvarchar(4000)

    DECLARE @MouthOpenConfidence nvarchar(4000)

    DECLARE @MouthOpenValue int

    DECLARE @MustacheConfidence nvarchar(4000)

    DECLARE @MustacheValue int

    DECLARE @PosePitch nvarchar(4000)

    DECLARE @PoseRoll nvarchar(4000)

    DECLARE @PoseYaw nvarchar(4000)

    DECLARE @QualityBrightness nvarchar(4000)

    DECLARE @QualitySharpness nvarchar(4000)

    DECLARE @SmileConfidence nvarchar(4000)

    DECLARE @SmileValue int

    DECLARE @SunglassesConfidence nvarchar(4000)

    DECLARE @SunglassesValue int

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @v_Type nvarchar(4000)

    DECLARE @X nvarchar(4000)

    DECLARE @Y nvarchar(4000)

    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'FaceDetails'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'IntOf', @AgeRangeHigh OUT, 'FaceDetails[i].AgeRange.High'
        EXEC sp_OAMethod @jResp, 'IntOf', @AgeRangeLow OUT, 'FaceDetails[i].AgeRange.Low'
        EXEC sp_OAMethod @jResp, 'StringOf', @BeardConfidence OUT, 'FaceDetails[i].Beard.Confidence'
        EXEC sp_OAMethod @jResp, 'BoolOf', @BeardValue OUT, 'FaceDetails[i].Beard.Value'
        EXEC sp_OAMethod @jResp, 'StringOf', @BoundingBoxHeight OUT, 'FaceDetails[i].BoundingBox.Height'
        EXEC sp_OAMethod @jResp, 'StringOf', @BoundingBoxLeft OUT, 'FaceDetails[i].BoundingBox.Left'
        EXEC sp_OAMethod @jResp, 'StringOf', @BoundingBoxTop OUT, 'FaceDetails[i].BoundingBox.Top'
        EXEC sp_OAMethod @jResp, 'StringOf', @BoundingBoxWidth OUT, 'FaceDetails[i].BoundingBox.Width'
        EXEC sp_OAMethod @jResp, 'StringOf', @Confidence OUT, 'FaceDetails[i].Confidence'
        EXEC sp_OAMethod @jResp, 'StringOf', @EyeglassesConfidence OUT, 'FaceDetails[i].Eyeglasses.Confidence'
        EXEC sp_OAMethod @jResp, 'BoolOf', @EyeglassesValue OUT, 'FaceDetails[i].Eyeglasses.Value'
        EXEC sp_OAMethod @jResp, 'StringOf', @EyesOpenConfidence OUT, 'FaceDetails[i].EyesOpen.Confidence'
        EXEC sp_OAMethod @jResp, 'BoolOf', @EyesOpenValue OUT, 'FaceDetails[i].EyesOpen.Value'
        EXEC sp_OAMethod @jResp, 'StringOf', @GenderConfidence OUT, 'FaceDetails[i].Gender.Confidence'
        EXEC sp_OAMethod @jResp, 'StringOf', @GenderValue OUT, 'FaceDetails[i].Gender.Value'
        EXEC sp_OAMethod @jResp, 'StringOf', @MouthOpenConfidence OUT, 'FaceDetails[i].MouthOpen.Confidence'
        EXEC sp_OAMethod @jResp, 'BoolOf', @MouthOpenValue OUT, 'FaceDetails[i].MouthOpen.Value'
        EXEC sp_OAMethod @jResp, 'StringOf', @MustacheConfidence OUT, 'FaceDetails[i].Mustache.Confidence'
        EXEC sp_OAMethod @jResp, 'BoolOf', @MustacheValue OUT, 'FaceDetails[i].Mustache.Value'
        EXEC sp_OAMethod @jResp, 'StringOf', @PosePitch OUT, 'FaceDetails[i].Pose.Pitch'
        EXEC sp_OAMethod @jResp, 'StringOf', @PoseRoll OUT, 'FaceDetails[i].Pose.Roll'
        EXEC sp_OAMethod @jResp, 'StringOf', @PoseYaw OUT, 'FaceDetails[i].Pose.Yaw'
        EXEC sp_OAMethod @jResp, 'StringOf', @QualityBrightness OUT, 'FaceDetails[i].Quality.Brightness'
        EXEC sp_OAMethod @jResp, 'StringOf', @QualitySharpness OUT, 'FaceDetails[i].Quality.Sharpness'
        EXEC sp_OAMethod @jResp, 'StringOf', @SmileConfidence OUT, 'FaceDetails[i].Smile.Confidence'
        EXEC sp_OAMethod @jResp, 'BoolOf', @SmileValue OUT, 'FaceDetails[i].Smile.Value'
        EXEC sp_OAMethod @jResp, 'StringOf', @SunglassesConfidence OUT, 'FaceDetails[i].Sunglasses.Confidence'
        EXEC sp_OAMethod @jResp, 'BoolOf', @SunglassesValue OUT, 'FaceDetails[i].Sunglasses.Value'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'FaceDetails[i].Emotions'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @Confidence OUT, 'FaceDetails[i].Emotions[j].Confidence'
            EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'FaceDetails[i].Emotions[j].Type'
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'FaceDetails[i].Landmarks'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'FaceDetails[i].Landmarks[j].Type'
            EXEC sp_OAMethod @jResp, 'StringOf', @X OUT, 'FaceDetails[i].Landmarks[j].X'
            EXEC sp_OAMethod @jResp, 'StringOf', @Y OUT, 'FaceDetails[i].Landmarks[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