Build Your Own Dystopian Future on AWS

Become the surveillance state leveraging facial recognition software from AWS Rekognition.

Photo by Matthew Henry on Unsplash
$ pyenv virtualenv 3.7.6 rekognition-demo
$ pyenv activate rekognition-demo
(rekognition-demo) $ pip install chalice boto3 awscli
(rekognition-demo) $ chalice new-project rekognition-demo
(rekognition-demo) $ cd rekognition-demo
(rekognition-demo) $ echo "boto3" >> requirements.txt

Target Acquired

# app.py
import logging
import boto3
from chalice import Chalice
app = Chalice(app_name="rekognition-demo")
app.log.setLevel(logging.DEBUG)
BUCKET = "rekognition-apex-police"COLLECTION = "apex-police"rekognition = boto3.client("rekognition")@app.on_s3_event(bucket=BUCKET, events=["s3:ObjectCreated:*"])
def handle_new_image(event):
response = rekognition.index_faces(
CollectionId=COLLECTION,
Image={"S3Object": {"Bucket": event.bucket, "Name": event.key}},
ExternalImageId=event.key,
MaxFaces=10,
QualityFilter="AUTO",
DetectionAttributes=["ALL"],
)
app.log.debug(f"Indexed {len(response['FaceRecords'])} faces from {event.key}")
# .chalice/policy-dev.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"rekognition:CreateCollection",
"rekognition:DescribeCollection",
"rekognition:IndexFaces",
"rekognition:SearchFacesByImage"
],
"Resource": ["*"]
},
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:ListBucket"],
"Resource": "arn:aws:s3:::rekognition-apex-police/*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}
# .chalice/config.json
{
"version": "2.0",
"app_name": "rekognition-demo",
"stages": {
"dev": {
"api_gateway_stage": "api",
"autogen_policy": false
}
}
}
# Configure your AWS API credentials
(rekognition-demo) $ aws configure
# Create the S3 bucket
(rekognition-demo) $ aws s3api create-bucket --bucket rekognition-apex-police
# Create the Rekognition collection
(rekognition-demo) $ aws rekognition create-collection --collection-id apex-police
# Deploy our app
(rekognition-demo) $ chalice deploy
...
# Sync the local images to S3
(rekognition-demo) $ aws s3 sync data s3://rekognition-apex-police
upload: data/71349130_2620493271306848_3732197409082572800_n.jpg to s3://rekognition-apex-police/71349130_2620493271306848_3732197409082572800_n.jpg
...
upload: data/81421938_2819939351362238_4027235321283346432_o.jpg to s3://rekognition-apex-police/81421938_2819939351362238_4027235321283346432_o.jpg
(rekognition-demo) $ chalice logs --name handle_new_image

Recognizing Users

# app.py continued
# Replacing top level imports
import base64
import logging
import boto3
from botocore.exceptions import ClientError
from chalice import BadRequestError, Chalice
# Previous section would be here ...@app.route("/", methods=["POST"])
def check_image():
request = app.current_request
if "image" not in request.json_body:
raise BadRequestError("No image given")
try:
image = base64.b64decode(request.json_body["image"])
except binascii.Error:
raise BadRequestError("Invalid image given")
try:
response = rekognition.search_faces_by_image(
CollectionId=COLLECTION, Image={"Bytes": image},
)
result = {
"matches": [
match["Face"]["ExternalImageId"] for match in response["FaceMatches"]
],
}
except ClientError:
result = {
"matches": [],
}
return result
(rekognition-demo) $ chalice deploy
Creating deployment package.
Updating policy for IAM role: rekognition-demo-dev-handle_new_image
Updating lambda function: rekognition-demo-dev-handle_new_image
Configuring S3 events in bucket rekognition-apex-police to function rekognition-demo-dev-handle_new_image
Creating IAM role: rekognition-demo-dev-api_handler
Creating lambda function: rekognition-demo-dev
Creating Rest API
Resources deployed:
- Lambda ARN: arn:aws:lambda:us-east-1:759954240342:function:rekognition-demo-dev-handle_new_image
- Lambda ARN: arn:aws:lambda:us-east-1:759954240342:function:rekognition-demo-dev
- Rest API URL: https://xrviuv0vtf.execute-api.us-east-1.amazonaws.com/api/
Apex Police Chief John W. Letteney
$ (echo -n '{"image": "'; base64 Police\ Chief\ John\ Letteney.jpg; echo '"}') |
curl -H "Content-Type: application/json" -d @- https://xrviuv0vtf.execute-api.us-east-1.amazonaws.com/api/
{"matches":["72434429_2645492565473585_3429136798620581888_o.jpg","82393140_2845334582156048_6102571155162398720_n.jpg","82647288_2851484764874363_5294281172166115328_n.jpg","83177039_2881712408518265_7297622762774855680_n.jpg","85026759_2893680170654822_8181472211329089536_o.jpg","71349130_2620493271306848_3732197409082572800_n.jpg"]}
Mark as a Pokemon (not a cop)
$ (echo -n '{"image": "'; base64 pokemark.jpg; echo '"}') |
curl -H "Content-Type: application/json" -d @ https://xrviuv0vtf.execute-api.us-east-1.amazonaws.com/api/
{"matches":[]}

Clean Up

(rekognition-demo) $ chalice delete
Deleting Rest API: xrviuv0vtf
Deleting function: arn:aws:lambda:us-east-1:759954240342:function:rekognition-demo-dev
Deleting IAM role: rekognition-demo-dev-api_handler
Deleting function: arn:aws:lambda:us-east-1:759954240342:function:rekognition-demo-dev-handle_new_image
Deleting IAM role: rekognition-demo-dev-handle_new_image
# Remove the S3 bucket
(rekognition-demo) $aws s3 rm s3://rekognition-apex-police --recursive
(rekognition-demo) $ aws s3api delete-bucket --bucket rekognition-apex-police
# Remove the Rekognition collection
(rekognition-demo) $ aws rekognition delete-collection --collection-id apex-police

Build the Future You Want to See

--

--

Python/web developer with too many random hobbies. I work for NVIDIA but my opinions are my own.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mark Lavin

Python/web developer with too many random hobbies. I work for NVIDIA but my opinions are my own.