1

I want to test if two pictures belong to the same person using Google Cloud Function. The function would recieve the images in base64 format. During runtime, these images would be converted to JPEG format and would be stored in a temporary folder. I want to implement a Facial recognition algorithm to see if these pictures belong to the same person. If any of you guys have suggestions for a framework that can be used on google cloud functions for face identification, kindly let me know. currently, I'm using DeepFace and VGGFace model for image recogniton but the google cloud function doesnt seem to support DeepFace. When I try to deploy the code that imports deepface, it generates the following error

For the sake of debugging, I've deployed the following code, this code would be converted to the desired code when I'm able to find a way around the error.

from flask import jsonify
import base64
from PIL import Image
from io import BytesIO
from deepface import DeepFace


def hello_world(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        Te response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """
    
    request_json = request.get_json()
    data = request.json
    if request.args and 'num1' in request.args:
        base64_string=request.args.get('num1')
    elif request_json and 'num1' in request_json:
        base64_string=request_json['num1']
    else:
        return "Invalid data"

    
    # convert base64 string to bytes
    img_data = base64.b64decode(base64_string)

    # create PIL image object from bytes
    img = Image.open(BytesIO(img_data))

    # save the image as a JPEG file
    img.save('/tmp/output.jpg', "JPEG")
    
    embedding_objs = DeepFace.represent(img_path = "/tmp/output.jpg")
    return embedding_objs[0]["facial_area"]['x']

This code, when deployed on Google Cloud Functions, generates the following error:


Deployment failure:
Function failed on loading user code. This is likely due to a bug in the user code. Error message: Traceback (most recent call last):
File "/layers/google.python.pip/pip/bin/functions-framework", line 8, in <module>
sys.exit(_cli())
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
return self.main(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/functions_framework/_cli.py", line 37, in _cli
app = create_app(target, source, signature_type)
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/functions_framework/__init__.py", line 288, in create_app
spec.loader.exec_module(source_module)
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/workspace/main.py", line 5, in <module>
from deepface import DeepFace
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/deepface/DeepFace.py", line 13, in <module>
import cv2
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/cv2/__init__.py", line 181, in <module>
bootstrap()
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/cv2/__init__.py", line 153, in bootstrap
native_module = importlib.import_module("cv2")
File "/layers/google.python.runtime/python/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You are missing the opengl driver, most likely the nvidia/amd drivers, for nivida it is most likely in the `libgl1-nvidia-glx`package packaged with the driver. or a mesa sofware version: like `libgl1-mesa-glx` – MortenB Mar 31 '23 at 14:26
  • @MortenB can you please guide me on how to install `libgl1-mesa-glx` on Cloud function? Because when I use it in _requirements.txt_ it generates the following error: **ERROR: Could not find a version that satisfies the requirement libgl1-mesa-glx (from versions: none) ERROR: No matching distribution found for libgl1-mesa-glx. For more details see the logs at https://console.cloud.google.com/cloud-build/builds;region=us-central1/69df0c10-9780-4543-8d57-26ee175798a5?project=83020413283.** – AbdullahHabib Mar 31 '23 at 16:15
  • That package is a Linux package (via `apt` or `yum`, etc. depending on the distro) and not avail via `pip` unfortunately – Glen Yu Mar 31 '23 at 18:55
  • Does this answer your question? [ImportError: libGL.so.1: cannot open shared object file: No such file or directory](https://stackoverflow.com/questions/55313610/importerror-libgl-so-1-cannot-open-shared-object-file-no-such-file-or-directo) – Christoph Rackwitz Apr 01 '23 at 10:54
  • @ChristophRackwitz no it does not, as I'm using a cloud function, I cant use apt commands to install new libraries – AbdullahHabib Apr 01 '23 at 14:44

1 Answers1

0

You can try adding the opencv-python-headless package to your requirements to see if that works for you. If not, then you'll need some other libraries like the libgl1-mesa-glx, which I don't think you can install for Cloud Functions. You might want to give Cloud Run a try if you want a serverless option with a bit more flexibility.

Glen Yu
  • 698
  • 1
  • 3
  • 9
  • yes, i tried these solutions before coming to this platform, but since `libgl1-mesa-glx` cant be installed on Cloud Functions, it did not bear any fruitful results. Can you please suggest me an alternative to solve this problem? – AbdullahHabib Mar 31 '23 at 16:21
  • The only way, AFAIK to be able to install those dependencies is to build a container image and run it on Cloud Run, which is also serverless and exposes/serves HTTP(S) traffic. I use it for my ML side projects because I ran into similar issues you did when I tried deploying my python app to App Engine and ran into missing libraries that could not be solved with python requirements alone (my app: https://findingwaldo.app/ is served on Cloud Run) – Glen Yu Mar 31 '23 at 16:48
  • thanks for the advice, i'm currently trying this solution. Now I'm creating an image and it gets stuck everytime it reaches at the following code: **=> => # Collecting tensorflow>=1.9.0 => => # Downloading tensorflow-2.12.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (586.0 MB)** Have you ever come across something like this? – AbdullahHabib Mar 31 '23 at 18:50
  • I'm not sure. Can you post your requriements.txt? – Glen Yu Mar 31 '23 at 18:54
  • The following is my _requirement.txt_: **functions-framework==3.* Pillow deepface** – AbdullahHabib Mar 31 '23 at 19:58