Serverless face recognition API made with Python

This article is the 16th day article of Python-AdventCalandar-2016.

Hello. Do you play Python? Do you recognize your face? Are you serverless?

This article is for those who want to easily set up a face recognition API server in Python. I was thinking of writing an academic story on the Advent calendar, but I chose this subject because the timing was good.

First of all, I will declare it, but this time I will make full use of AWS.

About face recognition

It's very easy to do with OpenCV, but this time we'll make full use of AWS! So, I will use the service "Amazon Rekognition" announced at this year's re: invent.

In a word, it is a great service that can recognize and search objects and faces with high accuracy (miscellaneous).

For more information, please visit https://aws.amazon.com/jp/rekognition/.

The price list is as follows. Please note that the more you use it, the more it will cost you.

スクリーンショット 2016-12-16 13.38.03.png

https://aws.amazon.com/jp/rekognition/pricing/

About serverless architecture

The serverless architecture on AWS is "API Gateway + Lambda".

This time, we will manage them using a library called "Chalice" made by Python.

https://github.com/awslabs/chalice

Try using Chalice

Try "hello world" for the time being using the chalice command.

$ pip install chalice

#Please use an appropriate project name for the freko part.
$ chalice new-project freko && cd freko
$ cat app.py

from chalice import Chalice

app = Chalice(app_name="helloworld")

@app.route("/")
def index():
    return {"hello": "world"}

$ chalice deploy
...
Your application is available at: https://endpoint/dev

$ curl https://endpoint/dev
{"hello": "world"}

Once you've done that, all you have to do is hit the S3 and Rekognition APIs.

Use AWS API

AWS settings

I'm sure many people have already created it, but first use ʻaws-cli` to export the settings.

In this sample, we chose ʻeu-west-1` as the region. I think it can be in any region that Rekognition supports.

$ pip install awscli
$ aws configure

https://github.com/aws/aws-cli

Also install the AWS-SDK made by Python called boto3.

$ pip install boto3

https://github.com/boto/boto3

Upload images to S3

Since it is troublesome to do it with GUI, I will make it all with API.

REGION = 'eu-west-1'

BUCKET = 'freko-default'
S3 = boto3.resource('s3')

#Create a bucket in S3 if the specified bucket name does not exist
def create_s3_bucket_if_not_exists():
    exists = True
    try:
        S3.meta.client.head_bucket(Bucket=BUCKET)
    except botocore.exceptions.ClientError as ex:
        error_code = int(ex.response['Error']['Code'])
        if error_code == 404:
            exists = False
    if exists:
        return
    else:
        try:
            S3.create_bucket(Bucket=BUCKET, CreateBucketConfiguration={
                'LocationConstraint': REGION})
        except Exception as ex:
            raise ChaliceViewError("fail to create bucket s3. error = " + ex.message)
    return

#Upload the file to S3
def upload_file_s3_bucket(obj_name, image_file_name):
    try:
        s3_object = S3.Object(BUCKET, obj_name)
        s3_object.upload_file(image_file_name)
    except Exception as ex:
        raise ChaliceViewError("fail to upload file s3. error = " + ex.message)

Face recognition using ReKognition API


REKOGNITION = boto3.client('rekognition')

#Face recognition by specifying a file in the S3 bucket
def detect_faces(name):
    try:
        response = REKOGNITION.detect_faces(
            Image={
                'S3Object': {
                    'Bucket': BUCKET,
                    'Name': name,
                }
            },
            Attributes=[
                'DEFAULT',
            ]
        )
        return response
    except Exception as ex:
        raise ChaliceViewError("fail to detect faces. error = " + ex.message)

You can get more information by specifying "ALL" for Attributes.

Change policy

Chalice examines the API used in the code at the time of deployment and sets the policy without permission, but since it is still a preview version, it seems that it does not support all APIs. It didn't read ʻupload_file` etc. used when uploading the file to S3. ..

Add S3 and ReKognition to the Statement in policy.json. (Because it is for testing, I make it full)

$ vim .chalice/policy.json

"Statement": [
    {
      "Action": [
        "s3:*"
      ],
      "Resource": "*",
      "Effect": "Allow"
    },
    {
      "Action": [
        "rekognition:*"
      ],
      "Resource": "*",
      "Effect": "Allow"
    }

    ...

]

The command to deploy without automatically generating the policy is: Make an appropriate Makefile and register it.

$ chalice deploy --no-autogen-policy

Set API key

I'm afraid that I can hit the endpoint as much as I want, so I will put in authentication by API key and request restriction for the time being.

For more information, please visit http://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/how-to-api-keys.html.

In Chalice, if you set ʻapi_key_required = True`, you will have an API that requires API key authentication.

@app.route('/face', methods=['POST'], content_types=['application/json'], api_key_required=True)

Try hitting the deployed API

I will use Lena for the test.

lena.jpg

For API parameters, enter name = filename, base64 = base64-encoded string of images.

Please set the API key and URL by yourself.

$ (echo -n '{"name":"lenna.jpg ", "base64": "'; base64 lenna.jpg; echo '"}') | curl -H "x-api-key:your-api-key" -H "Content-Type:application/json" -d @- https://your-place.execute-api.eu-west-1.amazonaws.com/dev/face | jq

If you just want to determine if the image has a face, it's enough to see if the FaceDetails has a value and the && Confidence value is high.

{
  "exists": true,
  "response": {
    "FaceDetails": [
      {
        "BoundingBox": {
          "Width": 0.4585798680782318,
          "Top": 0.3210059106349945,
          "Left": 0.34467455744743347,
          "Height": 0.4585798680782318
        },
        "Landmarks": [
          {
            "Y": 0.501218318939209,
            "X": 0.5236561894416809,
            "Type": "eyeLeft"
          },
          {
            "Y": 0.50351482629776,
            "X": 0.6624458432197571,
            "Type": "eyeRight"
          },
          {
            "Y": 0.5982820391654968,
            "X": 0.6305037140846252,
            "Type": "nose"
          },
          {
            "Y": 0.6746630072593689,
            "X": 0.521257758140564,
            "Type": "mouthLeft"
          },
          {
            "Y": 0.6727028489112854,
            "X": 0.6275562644004822,
            "Type": "mouthRight"
          }
        ],
        "Pose": {
          "Yaw": 30.472450256347656,
          "Roll": -1.429526448249817,
          "Pitch": -5.346992015838623
        },
        "Quality": {
          "Sharpness": 160,
          "Brightness": 36.45581817626953
        },
        "Confidence": 99.94509887695312
      }
    ],
    "ResponseMetadata": {
      ...
    },
    "OrientationCorrection": "ROTATE_0"
  }
}

By the way, if the face is not recognized, the response will be as follows.

{
  "exists": false,
  "response": {
    "FaceDetails": [],
    "ResponseMetadata": {
      ...
    }
  }
}

Finally

It has become an article like how to use AWS-SDK ... Without reflecting on it, I would like to do something cool like calling Rekognition next time when it is put to S3.

The full code is below. I'm glad if you can use it as a reference.

https://github.com/gotokatsuya/freko

Recommended Posts

Serverless face recognition API made with Python
Try face recognition with Python
Try face recognition with python + OpenCV
Face recognition with camera with opencv3 + python2.7
Simple Slack API client made with Python
[python, openCV] base64 Face recognition with images
Face recognition with Edison
[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Easily serverless with Python with chalice
Face recognition with Python's OpenCV
Use Twitter API with Python
I made blackjack with python!
Face recognition with Amazon Rekognition
Web API with Python + Falcon
Play RocketChat with API / Python
Call the API with python3.
Use subsonic API with python3
I made blackjack with Python.
Othello made with python (GUI-like)
I made wordcloud with Python.
Effortlessly with Serverless Python Requirements
I made LINE-bot with Python + Flask + ngrok + LINE Messaging API
A note on touching Microsoft's face recognition API in Python
Face detection with YOLO Face (Windows10, Python3.6)
SNS Python basics made with Flask
Create Awaitable with Python / C API
Get reviews with python googlemap api
Run Rotrics DexArm with python API
Face detection with Lambda (Python) + Rekognition
Numer0n with items made in Python
Quine Post with Qiita API (Python)
I made a fortune with Python.
Hit the Etherpad-lite API with Python
Othello game development made with Python
[python] Read information with Redmine API
python x tensoflow x image face recognition
First Anime Face Recognition with Chainer
Cut out face with Python + OpenCV
I made a daemon with Python
I tried face recognition with OpenCV
A story about adding a REST API to a daemon made with Python
I made a Python wrapper library for docomo image recognition API.
Collecting information from Twitter with Python (Twitter API)
English speech recognition with python [speech to text]
Automatically create Python API documentation with Sphinx
Retrieving food data with Amazon API (Python)
I made a character counter with Python
Easy introduction of speech recognition with Python
Quickly try Microsoft's Face API in Python
[Python] Quickly create an API with Flask
I made a Hex map with Python
[Python] Get Python package information with PyPI API
I made a roguelike game with Python
Touch AWS with Serverless Framework and Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
Othello app (iOS app) made with Python (Kivy)
REST API of model made with Python with Watson Machine Learning (CP4D edition)
Hello World and face detection with OpenCV 4.3 + Python