Skip to content

OpenFaaS

David Salek edited this page Dec 23, 2017 · 10 revisions

This section describes how to build and deploy a microservice for detecting Santa. The solution is based on OpenFaaS, an open-source package for serveless functions as a service. https://www.openfaas.com/

The function used here classifies images based on the Keras model described here: https://github.com/salekd/santa/wiki/Image-classification-with-Keras


Build and deploy a serverless function

Get the OpenFaaS code

git clone https://github.com/openfaas/faas

and deploy the FaaS stack with Docker Swarm.

docker swarm init
cd faas
./deploy_stack.sh

Build the Santa detector serverless function

cd santa/faas-santa-detector
docker build -t faas-santa-detector .

and deploy.

faas-cli deploy --image faas-santa-detector --name faas-santa-detector

The image can be uploaded to Docker Hub.

export DOCKER_ID_USER="salekd"
docker login
docker tag faas-santa-detector $DOCKER_ID_USER/faas-santa-detector
docker push $DOCKER_ID_USER/faas-santa-detector

The image can be downloaded from https://hub.docker.com/r/salekd/faas-santa-detector/


Example usage

Use curl commands for inference. The following two examples

curl localhost:8080/function/faas-santa-detector --data-binary @../data/examples/santa_01.png
curl localhost:8080/function/faas-santa-detector --data-binary @../data/examples/manhattan.png

will return the following results, respectively.

{"detected_objects": [{"score": 1.0000, "class": "santa"}, {"score": 0.0000, "class": "not_santa"}]}
{"detected_objects": [{"score": 0.0053, "class": "santa"}, {"score": 0.9947, "class": "not_santa"}]}

Integratation with a security camera

The new serverless function can be easily integrated with my other project, the Raspberry Pi smart security camera. In this way, you are for sure not going to miss Santa when he comes during Christmas! https://github.com/salekd/rpizero_smart_camera3

The smart security camera uses the Motion software to execute the process_last_photo.sh script when a motion is detected. The following change in this script is needed in order to get a record of timestamps, image file names and detected tags (Santa or not-Santa) in a text file.

@@ -1,7 +1,8 @@
 #!/bin/bash
 export img_dir=/home/pi/motion
 export last_file=`(cd $img_dir; ls -rt *jpg | tail -n 1)`
 curl 192.168.2.2:8080/async-function/faas-mobilenet \
     -d "$(echo -n '{"filename": "'; echo -n $last_file; echo -n '", "image_data": "'; base64 $img_dir/$last_file | tr -d '\n'; echo '"}')" \
     -H "X-Callback-Url: http://192.168.2.2:8080/function/faas-s3-email"
+{ date; echo $last_file; curl 192.168.2.2:8080/function/faas-santa-detector --data-binary @$img_dir/$last_file; } | tee -a /home/pi/santa.txt
 rm -f $img_dir/$last_file