Run Python + OpenCV + dlib in Azure Functions

I created a script image processing script in Python (with dlib and OpenCV ) - I was wondering how I can bring this functionality to Azure functions so that the script can be called through the API. Since Python is still in preview for Azure Functions , I would like to know if anyone here has experience connecting modules to Azure Functions and if it is possible to install OpenCV there?

+3
python opencv azure azure-functions
source share
2 answers

You can cast your own modules into your function by loading them into the lib folder located in the same folder as your function.

However, in the context of OpenCV, this is not a supported scenario at the moment. The default version of Python used in the Azure Function environment is Python 2.7. If you try to execute function code using OpenCV for Python 2.7, the error message you get will look like the following:

 2016-11-07T20:47:33.151 Function completed (Failure, Id=42fa9d38-05f1-46d4-a8ce-9fbaa24a870d) 2016-11-07T20:47:33.166 Exception while executing function: Functions.ImageProcessor. Microsoft.Azure.WebJobs.Script: ImportError: numpy.core.multiarray failed to import Traceback (most recent call last): File "D:\home\site\wwwroot\ImageProcessor\run.py", line 17, in <module> import cv2 ImportError: numpy.core.multiarray failed to import 

The fix for this is to upgrade the version of numpy used by Python 2.7 , but you cannot upgrade yourself.

As you noted, Python support for Azure functions is in an experimental phase right now. These issues will be addressed when Python is fully integrated as a first-class language.

+1
source share

so I thought of a dirty hack, it will install the package the first time it starts and give an error, so the function will restart. Follow these steps:

  • Download the package to the function directory (I just added the package to the git project for which the function is synchronized).
  • Do something like (maybe the best way, but I'm really new to python):

     try: import pyodbc except: package = 'pyodbc-3.0.10-cp27-none-win32.whl' pip.main(['install', '--user', package]) raise ImportError('Restarting') 

So the reason is that it doesn’t allow me to install it with administrator rights ... Also, if you included the .txt requirements file in the git repository, the packages will be installed in WebApp, but it looks like the function got its own python environment , so you need to manually install packages.

So, the only real trick is to find the appropriate package of wheels (I strongly believe that the function uses Python 2.7, I could not get it to work with packages for Python 3.4)

0
source share

All Articles