Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
oleobject loo_AuthAws
integer li_BTls
integer li_Port
integer li_BAutoReconnect
oleobject loo_BdJpg
oleobject loo_SbJpg
oleobject loo_Json
oleobject loo_SbRequestBody
oleobject loo_SbResponseBody
integer li_RespStatusCode
oleobject loo_JResp
integer li_AgeRangeHigh
integer li_AgeRangeLow
string ls_BeardConfidence
integer li_BeardValue
string ls_BoundingBoxHeight
string ls_BoundingBoxLeft
string ls_BoundingBoxTop
string ls_BoundingBoxWidth
string ls_Confidence
string ls_EyeglassesConfidence
integer li_EyeglassesValue
string ls_EyesOpenConfidence
integer li_EyesOpenValue
string ls_GenderConfidence
string ls_GenderValue
string ls_MouthOpenConfidence
integer li_MouthOpenValue
string ls_MustacheConfidence
integer li_MustacheValue
string ls_PosePitch
string ls_PoseRoll
string ls_PoseYaw
string ls_QualityBrightness
string ls_QualitySharpness
string ls_SmileConfidence
integer li_SmileValue
string ls_SunglassesConfidence
integer li_SunglassesValue
integer j
integer li_Count_j
string ls_V_Type
string X
string Y
integer i
integer li_Count_i

li_Success = 0

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_AuthAws = create oleobject
li_rc = loo_AuthAws.ConnectToNewObject("Chilkat.AuthAws")

loo_AuthAws.AccessKey = "AWS_ACCESS_KEY"
loo_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.)
loo_AuthAws.Region = "us-west-2"
loo_AuthAws.ServiceName = "rekognition"
// SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
loo_Rest.SetAuthAws(loo_AuthAws)

// URL: https://rekognition.us-west-2.amazonaws.com/
li_BTls = 1
li_Port = 443
li_BAutoReconnect = 1
// Don't forget to change the region domain (us-west-2.amazonaws.com) to your particular region.
li_Success = loo_Rest.Connect("rekognition.us-west-2.amazonaws.com",li_Port,li_BTls,li_BAutoReconnect)
if li_Success <> 1 then
    Write-Debug "ConnectFailReason: " + string(loo_Rest.ConnectFailReason)
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_AuthAws
    return
end if

// 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.
loo_BdJpg = create oleobject
li_rc = loo_BdJpg.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_BdJpg.LoadFile("qa_data/jpg/kid_blue_coat.jpg")
if li_Success <> 1 then
    Write-Debug "Failed to load the input JPG file."
    destroy loo_Rest
    destroy loo_AuthAws
    destroy loo_BdJpg
    return
end if

// 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.
loo_SbJpg = create oleobject
li_rc = loo_SbJpg.ConnectToNewObject("Chilkat.StringBuilder")

loo_BdJpg.GetEncodedSb("base64",loo_SbJpg)

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateSb("Image.Bytes",loo_SbJpg)
loo_Json.UpdateString("Attributes[0]","ALL")

loo_Rest.AddHeader("Content-Type","application/x-amz-json-1.1")
loo_Rest.AddHeader("X-Amz-Target","RekognitionService.DetectFaces")

loo_SbRequestBody = create oleobject
li_rc = loo_SbRequestBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_Json.EmitSb(loo_SbRequestBody)
loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Rest.FullRequestSb("POST","/",loo_SbRequestBody,loo_SbResponseBody)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_AuthAws
    destroy loo_BdJpg
    destroy loo_SbJpg
    destroy loo_Json
    destroy loo_SbRequestBody
    destroy loo_SbResponseBody
    return
end if

li_RespStatusCode = loo_Rest.ResponseStatusCode
Write-Debug "response status code = " + string(li_RespStatusCode)

if li_RespStatusCode >= 400 then
    Write-Debug "Response Status Code = " + string(li_RespStatusCode)
    Write-Debug "Response Header:"
    Write-Debug loo_Rest.ResponseHeader
    Write-Debug "Response Body:"
    Write-Debug loo_SbResponseBody.GetAsString()
    destroy loo_Rest
    destroy loo_AuthAws
    destroy loo_BdJpg
    destroy loo_SbJpg
    destroy loo_Json
    destroy loo_SbRequestBody
    destroy loo_SbResponseBody
    return
end if

loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)

loo_JResp.EmitCompact = 0
Write-Debug loo_JResp.Emit()

// 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

i = 0
li_Count_i = loo_JResp.SizeOfArray("FaceDetails")
do while i < li_Count_i
    loo_JResp.I = i
    li_AgeRangeHigh = loo_JResp.IntOf("FaceDetails[i].AgeRange.High")
    li_AgeRangeLow = loo_JResp.IntOf("FaceDetails[i].AgeRange.Low")
    ls_BeardConfidence = loo_JResp.StringOf("FaceDetails[i].Beard.Confidence")
    li_BeardValue = loo_JResp.BoolOf("FaceDetails[i].Beard.Value")
    ls_BoundingBoxHeight = loo_JResp.StringOf("FaceDetails[i].BoundingBox.Height")
    ls_BoundingBoxLeft = loo_JResp.StringOf("FaceDetails[i].BoundingBox.Left")
    ls_BoundingBoxTop = loo_JResp.StringOf("FaceDetails[i].BoundingBox.Top")
    ls_BoundingBoxWidth = loo_JResp.StringOf("FaceDetails[i].BoundingBox.Width")
    ls_Confidence = loo_JResp.StringOf("FaceDetails[i].Confidence")
    ls_EyeglassesConfidence = loo_JResp.StringOf("FaceDetails[i].Eyeglasses.Confidence")
    li_EyeglassesValue = loo_JResp.BoolOf("FaceDetails[i].Eyeglasses.Value")
    ls_EyesOpenConfidence = loo_JResp.StringOf("FaceDetails[i].EyesOpen.Confidence")
    li_EyesOpenValue = loo_JResp.BoolOf("FaceDetails[i].EyesOpen.Value")
    ls_GenderConfidence = loo_JResp.StringOf("FaceDetails[i].Gender.Confidence")
    ls_GenderValue = loo_JResp.StringOf("FaceDetails[i].Gender.Value")
    ls_MouthOpenConfidence = loo_JResp.StringOf("FaceDetails[i].MouthOpen.Confidence")
    li_MouthOpenValue = loo_JResp.BoolOf("FaceDetails[i].MouthOpen.Value")
    ls_MustacheConfidence = loo_JResp.StringOf("FaceDetails[i].Mustache.Confidence")
    li_MustacheValue = loo_JResp.BoolOf("FaceDetails[i].Mustache.Value")
    ls_PosePitch = loo_JResp.StringOf("FaceDetails[i].Pose.Pitch")
    ls_PoseRoll = loo_JResp.StringOf("FaceDetails[i].Pose.Roll")
    ls_PoseYaw = loo_JResp.StringOf("FaceDetails[i].Pose.Yaw")
    ls_QualityBrightness = loo_JResp.StringOf("FaceDetails[i].Quality.Brightness")
    ls_QualitySharpness = loo_JResp.StringOf("FaceDetails[i].Quality.Sharpness")
    ls_SmileConfidence = loo_JResp.StringOf("FaceDetails[i].Smile.Confidence")
    li_SmileValue = loo_JResp.BoolOf("FaceDetails[i].Smile.Value")
    ls_SunglassesConfidence = loo_JResp.StringOf("FaceDetails[i].Sunglasses.Confidence")
    li_SunglassesValue = loo_JResp.BoolOf("FaceDetails[i].Sunglasses.Value")
    j = 0
    li_Count_j = loo_JResp.SizeOfArray("FaceDetails[i].Emotions")
    do while j < li_Count_j
        loo_JResp.J = j
        ls_Confidence = loo_JResp.StringOf("FaceDetails[i].Emotions[j].Confidence")
        ls_V_Type = loo_JResp.StringOf("FaceDetails[i].Emotions[j].Type")
        j = j + 1
    loop
    j = 0
    li_Count_j = loo_JResp.SizeOfArray("FaceDetails[i].Landmarks")
    do while j < li_Count_j
        loo_JResp.J = j
        ls_V_Type = loo_JResp.StringOf("FaceDetails[i].Landmarks[j].Type")
        X = loo_JResp.StringOf("FaceDetails[i].Landmarks[j].X")
        Y = loo_JResp.StringOf("FaceDetails[i].Landmarks[j].Y")
        j = j + 1
    loop
    i = i + 1
loop


destroy loo_Rest
destroy loo_AuthAws
destroy loo_BdJpg
destroy loo_SbJpg
destroy loo_Json
destroy loo_SbRequestBody
destroy loo_SbResponseBody
destroy loo_JResp