|
(DataFlex) Amazon Rekognition - Detect Faces in an Image
Detects faces within an image that is provided as input. This example passes theimage as base64-encoded image bytes.For more information, see https://docs.aws.amazon.com/rekognition/latest/dg/API_DetectFaces.html
Use ChilkatAx-win32.pkg
Procedure Test
Handle hoRest
Boolean iSuccess
Variant vAuthAws
Handle hoAuthAws
Boolean iBTls
Integer iPort
Boolean iBAutoReconnect
Handle hoBdJpg
Variant vSbJpg
Handle hoSbJpg
Handle hoJson
Variant vSbRequestBody
Handle hoSbRequestBody
Variant vSbResponseBody
Handle hoSbResponseBody
Integer iRespStatusCode
Handle hoJResp
Integer iAgeRangeHigh
Integer iAgeRangeLow
String sBeardConfidence
Boolean iBeardValue
String sBoundingBoxHeight
String sBoundingBoxLeft
String sBoundingBoxTop
String sBoundingBoxWidth
String sConfidence
String sEyeglassesConfidence
Boolean iEyeglassesValue
String sEyesOpenConfidence
Boolean iEyesOpenValue
String sGenderConfidence
String sGenderValue
String sMouthOpenConfidence
Boolean iMouthOpenValue
String sMustacheConfidence
Boolean iMustacheValue
String sPosePitch
String sPoseRoll
String sPoseYaw
String sQualityBrightness
String sQualitySharpness
String sSmileConfidence
Boolean iSmileValue
String sSunglassesConfidence
Boolean iSunglassesValue
Integer j
Integer iCount_j
String sV_Type
String X
String Y
Integer i
Integer iCount_i
String sTemp1
Integer iTemp1
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Get Create (RefClass(cComChilkatAuthAws)) To hoAuthAws
If (Not(IsComObjectCreated(hoAuthAws))) Begin
Send CreateComObject of hoAuthAws
End
Set ComAccessKey Of hoAuthAws To "AWS_ACCESS_KEY"
Set ComSecretKey Of hoAuthAws To "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.)
Set ComRegion Of hoAuthAws To "us-west-2"
Set ComServiceName Of hoAuthAws To "rekognition"
// SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
Get pvComObject of hoAuthAws to vAuthAws
Get ComSetAuthAws Of hoRest vAuthAws To iSuccess
// URL: https://rekognition.us-west-2.amazonaws.com/
Move True To iBTls
Move 443 To iPort
Move True To iBAutoReconnect
// Don't forget to change the region domain (us-west-2.amazonaws.com) to your particular region.
Get ComConnect Of hoRest "rekognition.us-west-2.amazonaws.com" iPort iBTls iBAutoReconnect To iSuccess
If (iSuccess <> True) Begin
Get ComConnectFailReason Of hoRest To iTemp1
Showln "ConnectFailReason: " iTemp1
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_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.
Get Create (RefClass(cComChilkatBinData)) To hoBdJpg
If (Not(IsComObjectCreated(hoBdJpg))) Begin
Send CreateComObject of hoBdJpg
End
Get ComLoadFile Of hoBdJpg "qa_data/jpg/kid_blue_coat.jpg" To iSuccess
If (iSuccess <> True) Begin
Showln "Failed to load the input JPG file."
Procedure_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:
// 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.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJpg
If (Not(IsComObjectCreated(hoSbJpg))) Begin
Send CreateComObject of hoSbJpg
End
Get pvComObject of hoSbJpg to vSbJpg
Get ComGetEncodedSb Of hoBdJpg "base64" vSbJpg To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get pvComObject of hoSbJpg to vSbJpg
Get ComUpdateSb Of hoJson "Image.Bytes" vSbJpg To iSuccess
Get ComUpdateString Of hoJson "Attributes[0]" "ALL" To iSuccess
Get ComAddHeader Of hoRest "Content-Type" "application/x-amz-json-1.1" To iSuccess
Get ComAddHeader Of hoRest "X-Amz-Target" "RekognitionService.DetectFaces" To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRequestBody
If (Not(IsComObjectCreated(hoSbRequestBody))) Begin
Send CreateComObject of hoSbRequestBody
End
Get pvComObject of hoSbRequestBody to vSbRequestBody
Get ComEmitSb Of hoJson vSbRequestBody To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
Send CreateComObject of hoSbResponseBody
End
Get pvComObject of hoSbRequestBody to vSbRequestBody
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComFullRequestSb Of hoRest "POST" "/" vSbRequestBody vSbResponseBody To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iRespStatusCode
Showln "response status code = " iRespStatusCode
If (iRespStatusCode >= 400) Begin
Showln "Response Status Code = " iRespStatusCode
Showln "Response Header:"
Get ComResponseHeader Of hoRest To sTemp1
Showln sTemp1
Showln "Response Body:"
Get ComGetAsString Of hoSbResponseBody To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
If (Not(IsComObjectCreated(hoJResp))) Begin
Send CreateComObject of hoJResp
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
Set ComEmitCompact Of hoJResp To False
Get ComEmit Of hoJResp To sTemp1
Showln sTemp1
// 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
Move 0 To i
Get ComSizeOfArray Of hoJResp "FaceDetails" To iCount_i
While (i < iCount_i)
Set ComI Of hoJResp To i
Get ComIntOf Of hoJResp "FaceDetails[i].AgeRange.High" To iAgeRangeHigh
Get ComIntOf Of hoJResp "FaceDetails[i].AgeRange.Low" To iAgeRangeLow
Get ComStringOf Of hoJResp "FaceDetails[i].Beard.Confidence" To sBeardConfidence
Get ComBoolOf Of hoJResp "FaceDetails[i].Beard.Value" To iBeardValue
Get ComStringOf Of hoJResp "FaceDetails[i].BoundingBox.Height" To sBoundingBoxHeight
Get ComStringOf Of hoJResp "FaceDetails[i].BoundingBox.Left" To sBoundingBoxLeft
Get ComStringOf Of hoJResp "FaceDetails[i].BoundingBox.Top" To sBoundingBoxTop
Get ComStringOf Of hoJResp "FaceDetails[i].BoundingBox.Width" To sBoundingBoxWidth
Get ComStringOf Of hoJResp "FaceDetails[i].Confidence" To sConfidence
Get ComStringOf Of hoJResp "FaceDetails[i].Eyeglasses.Confidence" To sEyeglassesConfidence
Get ComBoolOf Of hoJResp "FaceDetails[i].Eyeglasses.Value" To iEyeglassesValue
Get ComStringOf Of hoJResp "FaceDetails[i].EyesOpen.Confidence" To sEyesOpenConfidence
Get ComBoolOf Of hoJResp "FaceDetails[i].EyesOpen.Value" To iEyesOpenValue
Get ComStringOf Of hoJResp "FaceDetails[i].Gender.Confidence" To sGenderConfidence
Get ComStringOf Of hoJResp "FaceDetails[i].Gender.Value" To sGenderValue
Get ComStringOf Of hoJResp "FaceDetails[i].MouthOpen.Confidence" To sMouthOpenConfidence
Get ComBoolOf Of hoJResp "FaceDetails[i].MouthOpen.Value" To iMouthOpenValue
Get ComStringOf Of hoJResp "FaceDetails[i].Mustache.Confidence" To sMustacheConfidence
Get ComBoolOf Of hoJResp "FaceDetails[i].Mustache.Value" To iMustacheValue
Get ComStringOf Of hoJResp "FaceDetails[i].Pose.Pitch" To sPosePitch
Get ComStringOf Of hoJResp "FaceDetails[i].Pose.Roll" To sPoseRoll
Get ComStringOf Of hoJResp "FaceDetails[i].Pose.Yaw" To sPoseYaw
Get ComStringOf Of hoJResp "FaceDetails[i].Quality.Brightness" To sQualityBrightness
Get ComStringOf Of hoJResp "FaceDetails[i].Quality.Sharpness" To sQualitySharpness
Get ComStringOf Of hoJResp "FaceDetails[i].Smile.Confidence" To sSmileConfidence
Get ComBoolOf Of hoJResp "FaceDetails[i].Smile.Value" To iSmileValue
Get ComStringOf Of hoJResp "FaceDetails[i].Sunglasses.Confidence" To sSunglassesConfidence
Get ComBoolOf Of hoJResp "FaceDetails[i].Sunglasses.Value" To iSunglassesValue
Move 0 To j
Get ComSizeOfArray Of hoJResp "FaceDetails[i].Emotions" To iCount_j
While (j < iCount_j)
Set ComJ Of hoJResp To j
Get ComStringOf Of hoJResp "FaceDetails[i].Emotions[j].Confidence" To sConfidence
Get ComStringOf Of hoJResp "FaceDetails[i].Emotions[j].Type" To sV_Type
Move (j + 1) To j
Loop
Move 0 To j
Get ComSizeOfArray Of hoJResp "FaceDetails[i].Landmarks" To iCount_j
While (j < iCount_j)
Set ComJ Of hoJResp To j
Get ComStringOf Of hoJResp "FaceDetails[i].Landmarks[j].Type" To sV_Type
Get ComStringOf Of hoJResp "FaceDetails[i].Landmarks[j].X" To X
Get ComStringOf Of hoJResp "FaceDetails[i].Landmarks[j].Y" To Y
Move (j + 1) To j
Loop
Move (i + 1) To i
Loop
End_Procedure
|