Code Information
There is little documentation on using the HTTP API for Rexognition AWS, but it's pretty simple to use the model that most code uses to use AWS HTTP endpoints.
Important information about the following code:
You must have installed requests
. If you do not have it, you can run the following in your shell (it is recommended to do this in virtualenv
).
pip install requests
The us-east-1
. Rekognition is currently supported in us-east-1
, eu-west-1
and us-west-2
, so you can change the code to support different endpoints in the region as you wish.
He expects two files to exist on the read disk, called source.jpg
and target.jpg
.
Like her in the last movie I saw, I use images of Felicity Jones from Star Wars: The Rogue One as my source and target.
source.jpg
: 
target.jpg
: 
It includes code for signing with AWS Signature Version 4 . There are libraries that will generate signatures for you, but I did not want to rely too much on third-party libraries to demonstrate a complete example.
The AWS credentials you use must have a valid policy for Rekognition .
This was written for Python 2.7 (it shouldn't be terribly difficult to upgrade to Python 3).
The code
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import base64 import datetime import hashlib import hmac import json import requests # Key derivation functions # http:
Code output
If you run the code, it should output something like this:
Response code: 200 Response body: { "FaceMatches": [], "SourceImageFace": { "BoundingBox": { "Height": 0.9448398351669312, "Left": 0.12222222238779068, "Top": -0.017793593928217888, "Width": 0.5899999737739563 }, "Confidence": 99.99041748046875 } }
Really, just use boto3
The simplest thing you can do is use boto3
.
The code will be simplified to something like the following, since all signature generation and JSON operation become unnecessary.
Make sure you configure boto3
with credentials in the environment or through the configuration file, or put your credentials in a line with the code. See boto3
configuration for more information.
This code uses the boto3
Rekognition API .
import pprint import boto3
The above boto3
example should output this:
{u'FaceMatches': [], 'ResponseMetadata': {'HTTPHeaders': {'connection': 'keep-alive', 'content-length': '195', 'content-type': 'application/x-amz-json-1.1', 'date': 'Sat, 31 Dec 2016 23:15:56 GMT', 'x-amzn-requestid': '13edda2d-cfaf-11e6-9999-d3abf4c2feb3'}, 'HTTPStatusCode': 200, 'RequestId': '13edda2d-cfaf-11e6-9999-d3abf4c2feb3', 'RetryAttempts': 0}, u'SourceImageFace': {u'BoundingBox': {u'Height': 0.9448398351669312, u'Left': 0.12222222238779068, u'Top': -0.017793593928217888, u'Width': 0.5899999737739563}, u'Confidence': 99.99041748046875}}