Creating face id service on Google Cloud or AWS Kubernetes in 30 minutes with PyCloud.ai

  • necessary imports, including face_recognition library and pycloud
  • defining const variables, including PyCloud object
  • initialize_service function, which is executed on startup of face-recognition service — that function returns ephemeral data storage which will be available for all endpoints during containers lifetime. We have two lists — face encoding and user names associated with encodings.
  • encode(image) function which will take an image, and return face encodings (note assignment to service “encoder” — we will go back to this later)
  • preprocess(image) function which will make sure we will be working on numpy array type (please note that preprocess function is assigned to “face_recognition” service with annotation @CLOUD.init_service(“face_recognition”), as well as following functions)
  • register(image, name) add new face to database
  • recognize(image) returning the detected names of given image
  • a test code which is checking whether our logic works locally — and what’s equally important — it’s building a deployment graph. PyCloud requires that each annotated function is executed at least once before deployment - otherwise functions won’t be treated as cloud endpoints.
  • Service configuration — we are exposing service ”face_recognition”, adding some system package required to install python dependency, and setting credentials for monitoring console.
virtualenv ~/.venv -p python3
. ~/.venv/bin/activate
chmod a+x pycloud-install-beta*.sh
./pycloud-install-beta*.sh
cmake==3.17.3
face_recognition==1.3.0
pillow==7.1.2
./pycloudcli deploy docker --file face_id_demo.py
./pycloudcli deploy eks --file face_id_demo.py
./pycloudcli deploy gke --file face_id_demo.py
python add_to_database.py a8a29155400dd48faa10f32fcbeef1b5–2038032445.eu-central-1.elb.amazonaws.com 6779 ./obama.jpeg “Barack Obama”
OK
python detect.py a8a29155400dd48faa10f32fcbeef1b5–2038032445.eu-central-1.elb.amazonaws.com 6779 ./obama2.jpeg
[‘Barack Obama’]
curl  a8a29155400dd48faa10f32fcbeef1b5-2038032445.eu-central-1.elb.amazonaws.com:5000 -F "image=@./chuck.jpeg" -F "name=Chuck Norris" -F "endpoint_id=register@face_id_demo"
"OK"
curl  a8a29155400dd48faa10f32fcbeef1b5-2038032445.eu-central-1.elb.amazonaws.com:5000 -F "image=@./two_guys.jpeg" -F "endpoint_id=recognize@face_id_demo"
["Chuck Norris"]
ad6f7b33ac95b434fa1bd101dda68774-1302389727.eu-central-1.elb.amazonaws.com:80
  • there are two services deployed(also marked with different colors)
  • internal communication is over GRPC
  • there were some bad requests because “preprocess” function has thrown some errors
  • most of the request time is spent in “encoder” service, we safely can scale it because it is stateless, for example to 5 replicas if traffic requires it.
kubectl scale deployment encoder --replicas=5
  • authentication
  • data persistence (our face id data will be lost after restarting face-recognition kubernetes pod)

--

--

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